洋葱架构:UI是否可以依赖于域 [英] Onion Architecture : Can UI depend on Domain

查看:125
本文介绍了洋葱架构:UI是否可以依赖于域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个具有洋葱体系结构的ASP.NET MVC网站.我已经实现了存储库模式,现在正在我的项目中创建Controller方法.这是我的架构:

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. 域:实体/域接口
  2. 存储库:(首先)使用Entity Framework Code First的通用存储库
  3. Service:调用存储库的通用服务
  4. MVC

现在在我的MVC项目中,我需要访问存储库,但是根据Jeffrey Palermo的说法,MVC依赖于Service和Domain,但是对存储库一无所知.

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.

我可以使UI依赖于存储库吗?

Can i make my UI dependant of Repository?

现在没有依赖项我什么也做不了,因为服务的创建取决于存储库.我尝试修改Startup.cs以使其不直接在Controller中使用存储库对象,但这仍然意味着我需要添加对存储库的引用(请参阅

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/ )

推荐答案

在这一点上,MVC(UI)是合成根,需要知道所有层才能在DI容器中注册它们.

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.

启动将在注册实现时直接引用实现,或者通过从该层公开的扩展方法间接引用实现.无论哪种方式,如果要将合成根添加到DI容器,都需要引用所有较低的层.

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.

这篇关于洋葱架构:UI是否可以依赖于域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆