Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Opinion on ASP.NET MVC Onion-based architecture [closed]

Tags:

What is your opinion on the following 'generic' code-first Onion-inspired ASP.NET MVC architecture: screenshot of the solution explorer

The layers, explained:

Core - contain the Domain model. e.g. that's the business objects and their relationship. I am using Entity Framework to visually design the entities and the relations between them. It lets me generate a script for a database. I am getting automatically-generated POCO-like models, which I can freely refer to in the next layer (Persistence), since they are simple (i.e. they are not database-specific).

Persistence - Repository interface and implementations. Basically CRUD operations on the Domain model.

BusinessServices - A business layer around the repository. All the business logic should be here (e.g. GetLargestTeam(), etc). Uses CRUD operations to compose return objects or get/filter/store data. Should contain all business rules and validations.

Web (or any other UI) - In this particular case it's an MVC application, but the idea behind this project is to provide UI, driven by what the Business services offer. The UI project consumes the Business layer and has no direct access to the Repository. The MVC project has its own View models, which are specific to each View situation. I am not trying to force-feed it Domain Models.

So the references go like this: UI -> Business Services -> Repository -> Core objects

What I like about it:

  • I can design my objects, rather than code them manually. I am getting code-generated Model objects.
  • UI is driven/enforced by the Business layer. Different UI applications can be coded against the same Business model.

Mixed feelings about:

  • Fine, we have a pluggable repository implementation, but how often do you really have different implementations of the same persistence interface?
  • Same goes for the UI - we have the technical ability to implement different UI apps against the same business rules, but why would we do that, when we can simply render different views (mobile, desktop, etc)?
  • I am not sure if the UI should only communicate with the Business Layer via View models, or should I use Domain Models to transfer data, as I do now. For display, I am using view models, but for data transfer I am using Domain models. Wrong?

What I don't like:

  • The Core project is now referenced in every other project - because I want/have to access the Domain models. In classic Onion architecture, the core is referenced only by the next layer.
  • The DbContext is implemented in the .Core project, because it is being generated by the Entity Framework, in the same place where the .edmx is. I actually want to use the .EDMX for the visual model design, but I feel like the DbContext belongs to the Persistence layer, somewhere within the database-specific repository implementation.

As a final question - what is a good architecture which is not over-engineered (such as a full-blown Onion, where we have injections, service locators, etc) but at the same time provides some reasonable flexibility, in places where you would realistically need it?

Thanks

like image 326
hyankov Avatar asked Apr 12 '13 21:04

hyankov


People also ask

What are the benefits of onion architecture?

Benefits of Onion Architecture It's a good fit for microservices, where it's not only a database that can act as a data access layer, but also for example an http client, if you need to get data from another microservice, or even from an external system.

Is MVC onion architecture?

In essence, MVC solves the separation of concern issue but the tight coupling issue still remains. On the other hand, Onion Architecture addresses both the separation of concern and tight coupling issues.

What is difference between clean architecture and onion architecture?

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.

What is onion architecture?

While writing his thoughts, Palermo also defined the key tenets of Onion Architecture: The application is built around an independent object model. Inner layers define interfaces. Outer layers implement interfaces. The direction of coupling is toward the center.


1 Answers

Wow, there’s a lot to say here! ;-)

First of all, let’s talk about the overall architecture.

What I can see here is that it’s not really an Onion architecture. You forgot the outermost layer, the “Dependency Resolution” layer. In an Onion architecture, it’s up to this layer to wires up Core interfaces to Infrastructure implementations (where your Persistence project should reside).

Here’s a brief description of what you should find in an Onion application. What goes in the Core layer is everything unique to the business: Domain model, business workflows... This layer defines all technical implementation needs as interfaces (i.e.: repositories’ interfaces, logging interfaces, session’s interfaces …). The Core layer cannot reference any external libraries and has no technology specific code. The second layer is the Infrastructure layer. This layer provides implementations for non-business Core interfaces. This is where you call your DB, your web services … You can reference any external libraries you need to provide implementations, deploy as many nugget packages as you want :-). The third layer is your UI, well you know what to put in there ;-) And the latest layer, it’s the Dependency Resolution I talked about above.

Direction of dependency between layers is toward the center.

Here’s how it could looks like:

Onion App Structure

The question now is: how to fit what you’ve already coded in an Onion architecture.

Core: contain the Domain model

Yes, this is the right place!

Persistence - Repository interface and implementations

Well, you’ll need to separate interfaces with implementations. Interfaces need to be moved into Core and implementations need to be moved into Infrastructure folder (you can call this project Persistence).

BusinessServices - A business layer around the repository. All the business logic should be here

This needs to be moved in Core, but you shouldn’t use repositories implementations here, just manipulate interfaces!

Web (or any other UI) - In this particular case it's an MVC application

cool :-)

You will need to add a “Bootstrapper“ project, just have a look here to see how to proceed.

About your mixed feelings:

I won’t discuss about the need of having repositories or not, you’ll find plenty of answers on stackoverflow.

In my ViewModel project I have a folder called “Builder”. It’s up to my Builders to discuss with my Business services interfaces in order to get data. The builder will receive lists of Core.Domain objects and will map them into the right ViewModel.

About what you don’t like:

In classic Onion architecture, the core is referenced only by the next layer.

False ! :-) Every layer needs the Core to have access to all the interfaces defined in there.

The DbContext is implemented in the .Core project, because it is being generated by the Entity Framework, in the same place where the .edmx is

Once again, it’s not a problem as soon as it’s really easy to edit the T4 template associated with your EDMX. You just need to change the path of the generated files and you can have the EDMX in the Infrastructure layer and the POCO’s in your Core.Domain project.

Hope this helps!

like image 59
MaxSC Avatar answered Oct 12 '22 00:10

MaxSC