I am designing an ASP.NET MVC application using the Onion Architecture described by Jeffrey Palermo.
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.
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).
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?
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.
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
The Infrastructure Layer is the outermost layer of the Onion Architecture. It's responsible for implementing all the IO operations that are required for the software. This layer is also allowed to know about everything contained in the inner layers, being able to import entities from the Application and Domain layers.
Onion Architecture Layers These layers are towards to center. The center part is the Domain entities that represent the business and behavior objects. These layers can vary but the domain entities layer is always part of the center. The other layer defines more behavior of an object.
Clean Architecture was introduced by Robert “Uncle Bob” Martin in 2012 in this post. It builds on the concepts of Onion Architecture but with somewhat different details of the layers. Instead of “Domain Model”, it refers to the core as “Entities”, but still representing enterprise-wide business rules.
According to Jeffrey Palermo: "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.
You are correct that you don't want Infrastructure to depend on UI(Web), but I break that rule sometimes.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With