EF6 (code first), MVC, Unity, and a service layer without a repository

前端 未结 4 1804
没有蜡笔的小新
没有蜡笔的小新 2021-02-09 00:11

My application is using SQL Server 2012, EF6, MVC and Web API.

It\'s also using a repository and assorted files such as:

DatabaseFactory.cs
Disposable.c         


        
4条回答
  •  不思量自难忘°
    2021-02-09 00:23

    If you move ahead with this architecture, you are going to be tightly coupling the Entity Framework with either your service or your controller. The repository abstraction gives you a couple things:

    1) You are able to easily swap out data access technologies in the future

    2) You are able to mock out your data store, allowing you to easily unit test your data access code

    You are wondering where to put your EF context. One of the benefits of using the Entity Framework is that all operations on it are enrolled into a transaction. You need to ensure that any data access code uses the same context to enjoy this benefit.

    The design pattern that solves that problem is the Unit of Work pattern, which by the looks of things, you are already using. I strongly recommend continuing to use it. Otherwise, you will need to initialize your context in your controller, pass it to your service, which will need to pass it to any other service it interacts with.

    Looking at the objects you have listed, it appears to be a considerate attempt to build this app with enterprise architectural best practices. While abstractions do introduce complexity, there is no doubting the benefit they provide.

提交回复
热议问题