我正在设计我的第一个分层应用程序,它由数据、业务和表示层组成。
我的业务组件(例如,Business.Components.UserComponent)目前有以下方法:
public void Create(User entity)
{
using (DataContext ctx = new DataContext())
{
ctx.Users.AddObject(entity);
ctx.SaveChanges();
}
}
我喜欢这个设计。但是,我在网上遇到了一些建议以下实现的示例:
public void Create(User entity)
{
// Instanciate the User Data Access Component
UserDAC dac = new UserDAC();
dac.InsertUser(entity);
}
这将导致为所有实体创建一个数据访问组件,每个实体都包含基本方法(创建、编辑、删除...等)。
这似乎是双重工作,因为我必须创建具有基本方法的数据访问组件以及仅调用数据访问组件中的方法的业务组件。
在分层应用程序中正确实现基本 CRUD 功能的最佳实践是什么?它们应该在业务组件或数据访问组件中“编码”吗?