Nutan Jayavant
Nutan Jayavant

Reputation: 31

Separating out Model and Views and Controllers in different projects

I want to have separate projects for models, controllers and views. Each will go in different project. the model can go in class library and added as dll.

what type of project is best suitable for controllers? Should it be class library or web application?

and if views are in separate project how controller and view will interact to each other?

Upvotes: 2

Views: 3152

Answers (2)

Md. Nazrul Islam
Md. Nazrul Islam

Reputation: 3017

Whenever we create new ASP.NET MVC 3/4/5 project, it follow pre-defined directory convention and adds few files and directory to the project. However it is not compulsory to follow this default directory structure, but it help us to keep application clean & maintainable by grouping application component based on functionality i.e. controller, model, view etc. But when it comes to large project, it is advisable to divide different components in different library projects. i.e. all model classes goes in separate library project, all controller goes in another library project and so on.

To separate controller model and view do the following

First of all take an empty MVC 3/4/5 web application named it SeparatingControllerModelsView. Then add a controller and named it HomeController. Now add a model class and named it TestModel. Add a view right clicking on Index method and select the TestModel to create a strongly type view.

Now for separating Modles and Controllers add two library projects and named it ModelLibrary & ControllerLibrary. Delete the default class from both Library projects. Right click on the ModelLibrary project and click the Properties. Project properties will open and change the default namespace with the namespace (SeparatingControllerModelsView.Models) when create the model class TestModel. Now right click the ModelLibrary Project and click add existing item and add the TestModel class from the SeparatingControllerModelsView web application.

In this way also change the controller library namespace with the namespace of HomeController class. Now add reference System.Web, System.Web.Mvc. Add the HomeController class from SeparatingControllerModelsView web application right clicking the ControllerLibrary and then add existing item.

Now Delete the Controllers and Models directory from the SeparatingControllerModelsView. Add reference the two library project and run the project it works as like default MVC project.

If any area is added to the project then need to add another Library project according to area. Change the default namespace of the library (both controller and model) accordingly namespace of area’s controller and models.

As namespaces are same of the separated library so at runtime using the namespace they interact with each other easily.

also can use the following link

Creating an ASP.NET MVC Areas Application Using Multiple Projects

The final project will look like this....

enter image description here

Upvotes: 2

Zasz
Zasz

Reputation: 12528

ASP.NET MVC works on the principle of convention over configuration. If you opt for a project structure as you have described, then you have decided to not to follow the usual CONVENTIONS.

Therefore, most of the advantages that MVC gives you, (like tying up view and controller actions, model binding, url routing) gets thrown outside the window.

However nothing prevents Models from existing in separate class library, as ASP.NET MVC does not impose any convention on the nature of your model classes - any C# POCO can be your model class including your library types.

It is not impossible to do it the way you pictured, but then you will have to configure a lot of settings (where convention used to be enough) in the framework now. Also when MVC3 upgrades to MVCX (where x > 3) there's no telling whether your custom setup will continue to work, making upgrades more expensive.

However you might do this : Keeping the controllers simple and small, you can pull out most of your Services and Repositories and Models and ClassMappings and integration with DI, ORM etc out into separate or combined DLLs. That way the overall project structure is aligned to MVC conventions while your business logic is out in other projects.

Upvotes: 3

Related Questions