How to configure architecture for mvc project

I am trying to get MVC. I am from ASP.Net background.

After creating a new mvc 3 application, I got the Controller, Models and Views under the same webapp project. In ASP.Net, we usually create separate projects for models and controllers (which, I believe, are the same as the Business Layer). I also created a separate project for DAL, where I will use EF.

I am confused, how is the ideal solution structure? Should I create separate projects for each layer? Since I created DAL as a separate project, I had to specify a link to WebApp, because I wanted to return the model from DAL, and because of this, now I can not add a link to DAL to my WebApp.

Can someone please throw some light on what I'm missing here? Am I not doing it right?

+6
source share
2 answers

MVC really leaves the M part to the developer.

Even in your official examples, you will see options. Your question reveals one of the most common misconceptions about MVC. You should NOT bind your domain or data models directly to views, and you should not use their controller methods as parameters. See this post for more or less published .

Ideally, your controllers will call DAL, and some mechanism will display these data or domain models to view the models. It is those View models that are models that exist specifically to facilitate the user interface that must exist in the WebApp Models folder.

So, you are definitely on the right track by creating a new assembly to contain the DAL. One of the “simple” mechanisms for mapping to a ViewModel is a simple method for each ViewModel:

public class MyWidgetFormModel() { public string Name { get; set; } public string Price { get; set; } public MapFromDAL(DAL.Widget widget) { this.Name = widget.Name; this.Price = widget.Price; } } 

Update : based on your comments, here is a great answer about one layout of a custom project.

+6
source

When I started with MVC, I followed the architecture of the bow of Jeffrey Palermo. You can read about it:

here: http://jeffreypalermo.com/blog/the-onion-architecture-part-1/

here: http://jeffreypalermo.com/blog/the-onion-architecture-part-2/

and here: http://jeffreypalermo.com/blog/the-onion-architecture-part-3/

It uses IoC support for decoupling. I think you should consider using IoC containers because the MVC architecture was looked around for patterns using IoC to separate services (layers).

You can also download a working sample from http://codecampserver.codeplex.com/ using the onion architecture.

This is not the only architecture you can use with MVC, but it is a very good place to start and learn about IoC and decoupling in MVC applications.

+3
source

Source: https://habr.com/ru/post/890794/


All Articles