问题描述
我正在使用 洋葱架构设计一个 ASP.NET MVC 应用程序 由 Jeffrey Palermo 描述.
I am designing an ASP.NET MVC application using the Onion Architecture described by Jeffrey Palermo.
这是一个 ASP.NET MVC 2.0 项目,我要求所有视图都使用专用视图模型进行强类型化——我们不会将域模型传递给我们的视图.我们使用 AutoMapper 来做翻译——AutoMapper 在基础设施中是孤立的,Web 不知道也不关心 AutoMapper 正在被使用.
It is an ASP.NET MVC 2.0 project, where I am requiring that all views be strongly typed using dedicated View Models -- we will not be passing domain models to our views. We are using AutoMapper to do the translation -- AutoMapper is isolated in the infrastructure, Web does not know or care that AutoMapper is being used.
目前,我正在 Web 项目中定义 IViewModelMapping 接口——仅仅是因为该服务将由控制器使用,并且它可以直接访问自己的视图模型.通过这种方式,接口可以访问域模型(在 Core 中)和视图模型(在 Web 中).
Currently, I am defining the IViewModelMapping interfaces in the Web project -- simply because this service will be used by the Controllers and it has direct access to its own View Models. This way the interface can access both the Domain Models (in Core) and the View Models (in Web).
为了提供 IViewModelMapping 接口的实际实现,我在 Infrastructure 项目中创建了一个 ObjectMapping 命名空间,它将实际映射实现隔离到洋葱的 Intrastructure.在这样做时,这将要求基础设施依赖于核心和 Web.
In order to provide the actual implementation of the IViewModelMapping interfaces, I created an ObjectMapping namespace in the Infrastructure project, which will isolate the actual mapping implementation to the Intrastructure of the onion. In doing so, this will require Infrastructure to have a dependency on BOTH Core AND Web.
我的问题是:由于这两个项目在技术上都位于洋葱的外围(在同一层中)——是否允许一个项目依赖该层中的另一个项目?有没有人注意到这种设计的任何潜在缺陷?
My question is: since both of these projects are technically on the outskirts of the onion (in the same layer) -- is one project allowed to have a dependency on another project in that layer? Does anyone notice any potential pitfalls with this design?
另一种设计是将 IViewMapper 接口移到 Core 中——但这是不可能的,因为 Core 无法访问 ViewModel 类.我也可以将视图模型移到 Core 中,但我觉得它们不属于那里,因为它们特定于 UI 层.
An alternative design would be moving the IViewMapper interfaces into Core -- but this would be impossible because Core does not have access to the ViewModel classes. I could also move the view models into Core, but I feel like they would not belong there, since they are specific to the UI layer.
提议的架构如下——注意基础设施依赖于核心和网络.Web 保持隔离,只能访问核心业务逻辑.
The proposed architecture is as follows -- notice that Infrastructure has a dependency on Core AND Web. Web remains isolated and only has access to the Core business logic.
http://www.matthidinger.com/images/onion-arch.png
推荐答案
您不希望基础设施依赖 UI(Web) 是对的,但我有时会打破这条规则.
You are correct that you don't want Infrastructure to depend on UI(Web), but I break that rule sometimes.
我认为不是 IViewModelMapping,而是使用 Map() 方法创建 IMapper.然后,接口可以具有可能与视图模型映射有关的实现,或者可能只是常规映射.无论哪种方式,该接口都可以在 Core 中,因为它在语义上不绑定到任何类型的模型.
I would think instead of IViewModelMapping, create IMapper with method Map(). Then, the interface can have implementations that might have to do with view model mapping, or maybe just regular mapping. Either way, that interface can be in Core because it is not semantically bound to any type of model.
很棒的图形.我希望我回答了你的问题.Onion Architecture 的总体理念是将您的业务逻辑和模型保持在应用程序的中间(核心),并尽可能将您的依赖项向外推.
Great graphic. I hope I answered the meat of your question. The overall philosophy of the Onion Architecture is to keep your business logic and model in the middle (Core) of your application and push your dependencies as far outward as possible.
这篇关于同一层中的洋葱架构依赖:基础设施和 Web 通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!