Onion Architecture : Can UI depend on Domain

泪湿孤枕 提交于 2019-12-31 03:37:23

问题


I am making an ASP.NET MVC website with an Onion architecture. I have implemented a repository pattern and am now creating a Controller method in my project. This is my architecture:

  1. Domain : Entities / Domain Interfaces
  2. Repository : Generic repository (for now) using Entity Framework Code First
  3. Service : Generic Service that calls the Repository
  4. MVC

Now in my MVC project I need to access repository but according to Jeffrey Palermo, MVC is dependant from Service and Domain, but nothing is said of Repository.

Can i make my UI dependant of Repository?

I cannot do anything without the dependency right now, because Service creation depends on Repository. I have tried modifying Startup.cs to not directly use Repository objects in my Controllers, but it still means I need to add a reference to Repository (see http://dotnetliberty.com/index.php/2015/10/15/asp-net-5-mvc6-dependency-injection-in-6-steps/ )


回答1:


At this point MVC (UI) is the composition root, which would need to know all the layers in order to register them with the DI container.

The start up would either refer to the implementations directly when registering them or indirectly via extension methods exposed from that layer. Either way the composition root needs to reference all lower layers if they are to be added to DI container.

The controllers should also be dependent on abstractions from lower layers and not concretions.

If injecting repositories directly into controllers then I suggest reviewing the design as that may indicate mixing of responsibilities.



来源:https://stackoverflow.com/questions/44656592/onion-architecture-can-ui-depend-on-domain

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!