创建数据访问组件和业务组件是否需要双重工作?

发布于 2024-09-18 05:52:35 字数 680 浏览 4 评论 0原文

我正在设计我的第一个分层应用程序,它由数据层、业务层和表示层组成。

我的业务组件(例如,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 功能的最佳实践是什么?它们应该在业务组件中还是在数据访问组件中“编码”?

I am designing my first Layered Application which consists of a Data, Business, and a Presentation layer.

My business components (e.g, Business.Components.UserComponent) currently has the following method:

public void Create(User entity)
{
    using (DataContext ctx = new DataContext())
    {
        ctx.Users.AddObject(entity);
        ctx.SaveChanges();
    }
}

I like this design. However, I've encountered some examples online that would recommend the following implementation:

public void Create(User entity)
{
    // Instanciate the User Data Access Component
    UserDAC dac = new UserDAC();
    dac.InsertUser(entity);
}

This would result in creating a Data Access Component for all Entities, each containing the basic methods (Create, Edit, Delete...etc).

This seems like double work since I would have to create the Data Access Components with the basic methods as well as the Business Components that just simply calls the methods in the Data Access Component.

What would be considered best practice for properly implementing the basic CRUD functionalities in a Layered Application? Should they be 'coded' in the Business Component or in a Data Access Component?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

悲念泪 2024-09-25 05:52:36

这取决于。如果您希望业务层只执行 CRUD 操作,那么您可以遵循最初的方法。如果您计划使用一些大型业务逻辑,其中业务组件将与许多实体一起使用,则第二种方法更好。

人们喜欢使用第二种方法的原因是测试和持久性的无知。如果您使用第一种方法,您的业务组件将与实体框架紧密耦合。模拟 ObjectContext 并不是一件容易的事,因此测试很困难。如果您使用第二种方法,您的业务层将独立于持久层。您可以轻松测试它,甚至可以根据需要更改持久层。但这些是您目前可能不需要的更高级的概念。您的代码需要一些额外的改进才能进行测试。

DAC 也可以作为存储库来实现。有很多方法可以创建通用存储库,以便您只有一个类,并在实例化存储库时定义实体类型:

var repository = new GenericRepository<User>();

另请注意,使用单独的数据访问层会带来新的复杂性,因为有时在多个存储库之间共享单个上下文是合理的存储库。这与所谓的工作单元模式一起出现。

互联网上有很多关于实现存储库和工作单元模式的文章。我将其中一些作为最爱保存在家里,因此如果您有兴趣,我可以将它们包含在稍后的答案中。

It depends. If you expect that your business layer simply do CRUD operations than you can follow your initial approach. If you plan to use some big business logic where business component will work with many entities second approach is better.

Reason why people like to use second approach is testing and persistance ignorance. If you use first approach your business component is tightly coupled with Entity framework. Mocking ObjectContext is not very easy task so testing is hard. If you use second approach your business layer is independent on persistance layer. You can easily test it and you can even change persistance layer if you need to. But those are more advanced concepts which you are probably not looking for at the moment. Your code would need some additional improvement to be testable.

DAC can be also implemented as Repository. There are plenty of ways how to create generic repository so that you have only one class and you define the entity type when instancing repository:

var repository = new GenericRepository<User>();

Also be aware that using separate data access layer introduces new complexity because sometimes it is reasonable to share single context among multiple repositories. This comes together with something known as Unit of work pattern.

There are plenty of articles about implementing Repository and Unit of work patterns on the Internet. I have some of them stored as favorites at home so If you are interested I can include them to my answer later on.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文