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

前端 未结 4 1796
没有蜡笔的小新
没有蜡笔的小新 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

    Setting up Unity for ASP.NET MVC and WebAPI is quite easy if you install and add the Unity.Mvc* and Unity.WebAPI* Nuget packages to your project. (The * is a version number, like 3 or 4 or 5. Look for the appropriate versions for your project. Here are for example the links to the Unity.Mvc 5 package and to the Untity.WebAPI 5 package.)

    The usage of these packages is explained in this blog post.

    The building blocks are roughly like so:

    You build a unity container and register all your dependencies there, especially the EF context:

    private static IUnityContainer BuildContainer()
    {
        var container = new UnityContainer();
    
        container.RegisterType<MyContext>(new HierarchicalLifetimeManager());
    
        container.RegisterType<IOrderService, OrderService>();
        container.RegisterType<ICustomerService, CustomerService>();
        container.RegisterType<IEmailMessenger, EmailMessenger>();
        // etc., etc.
    
        return container;
    }
    

    MyContext is your derived DbContext class. Registering the context with the HierarchicalLifetimeManager is very important because it will ensure that a new context per web request will be instantiated and disposed by the container at the end of each request.

    If you don't have interfaces for your services but just concrete classes you can remove the lines above that register the interfaces. If a service needs to be injected into a controller Unity will just create an instance of your concrete service class.

    Once you have built the container you can register it as dependency resolver for MVC and WebAPI in Application_Start in global.asax:

    protected void Application_Start()
    {
        var container = ...BuildContainer();
    
        // MVC
        DependencyResolver.SetResolver(
            new Unity.MvcX.UnityDependencyResolver(container));
    
        // WebAPI
        GlobalConfiguration.Configuration.DependencyResolver =
            new Unity.WebApiX.UnityDependencyResolver(container);
    }
    

    Once the DependencyResolvers are set the framework is able to instantiate controllers that take parameters in their constructor if the parameters can be resolved with the registered types. For example, you can create a CustomerController now that gets a CustomerService and an EmailMessenger injected:

    public class CustomerController : Controller
    {
        private readonly ICustomerService _customerService;
        private readonly IEmailMessenger _emailMessenger;
    
        public CustomerController(
            ICustomerService customerService,
            IEmailMessenger emailMessenger)
        {
            _customerService = customerService;
            _emailMessenger = emailMessenger;
        }
    
        // now you can interact with _customerService and _emailMessenger
        // in your controller actions
    }
    

    The same applies to derived ApiControllers for WebAPI.

    The services can take a dependency on the context instance to interact with Entity Framework, like so:

    public class CustomerService // : ICustomerService
    {
        private readonly MyContext _myContext;
    
        public CustomerService(MyContext myContext)
        {
            _myContext = myContext;
        }
    
        // now you can interact with _myContext in your service methods
    }
    

    When the MVC/WebAPI framework instantiates a controller it will inject the registered service instances and resolve their own dependencies as well, i.e. inject the registered context into the service constructor. All services you will inject into the controllers will receive the same context instance during a single request.

    With this setup you usually don't need a context = new MyContext() nor a context.Dispose() as the IOC container will manage the context lifetime.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-02-09 00:35

    Entity Framework IS already a Unit of Work pattern implementation as well as a generic repository implementation (DbContext is the UoW and DbSet is the Generic Repository). And I agree that it's way overkill in most apps to engineer another UoW or Generic Repository on top of them (besides, GenericRepsitory is considered to be an anti-pattern by some).

    A Service layer can act as a concrete repository, which has a lot of benefits of encapsulating data logic that is specific to your business needs. If using this, then there is little need to build a repository on top of it (unless you want to be able to change your backend service technology, say from WCF to WebApi or whatever..)

    I would put all your data access in your service layer. Don't do data access in your controller. That's leaking your data layer into your UI layer, and that's just poor design. It violates many of the core SOLID concepts.

    But you do NOT need an additional UnitOfWork, or other layers beyond that in most cases, unless your apps are very complex and intended to work in multiple environments...

    0 讨论(0)
  • 2021-02-09 00:40

    If you aren't using a repository then I assume you would have some place to write your logic/processing that your service operation would use. I would create a new instance of the Context in that logic/process class method and use its methods directly. Finally, dispose it off right after its use probably under a "using".

    The processing method would eventually transform the returned/processed data into a data/message contract which the service returns to the controller.

    Keep the data logic completely separate from Controller. Also keep the view model separate from data contract.

    0 讨论(0)
提交回复
热议问题