Whenever we create new ASP.NET MVC 3 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 very hard to maintain all components in one project.
In such scenario, 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. Apart from model, and controller, it is also advisable to encapsulate business logic in separate library project rather than placing it in controller. By this way we can reuse business logic to expose additional services for e.g. it may happen application also provide API for third party integration. In this case we can reuse business logic to build API services because generally business logic remain same regardless of consumer (here web portal or API services).
So here we will see how we can divide model, controller, and view in separate project. Once we done with dividing MVC component in different project, it is quite easier to encapsulate business logic in separate project.
To get started, create blank solution in Visual Studio and add one library project to it. Let give it name ModelLib. We need to add reference to Entity Framework as ModelLib is responsible to deal with data source. To add reference of Entity Framework via NuGet type following in Package Manager Console in Visual Studio.
Now we can add model classes in this project. So far we have done successfully but it may require that we need to add more references to the project as and when it is needed. For e.g. reference to System.ComponentModel.DataAnnotations to use Data Annotations Attribute to validate property or field of model.
Now add one more class library project named ControllerLib and add following references to the project.
- Entity Framework
- ASP.NET MVC 3 assembly which is by default installed at C:\Program Files\Microsoft ASP.NET\ASP.NET MVC 3\Assemblies
- ModelLib project
Now we can create new controller within this project as we were creating it with default project template by inheriting class from System.Web.Mvc.Controller and use the same naming convention i.e. NameController.cs where name is replaced with controller name.
NOTE: While adding new model or controller classes, verify that its access modifier is public. Because here model and controller classes are placed in different assembly and might be in different namespace also.
So far we are ready with controller and model classes, now its turn to build views. To build view, add empty MVC 3 project template (or we can even use Phil Haack’s Really Empty MVC 3 template) and add references to the ControllerLib project. Now we are just one step away to finish! Now we can add view as we were adding it in default project template within sub directory of view directory.
To run application set MVC 3 project, which we added in last, as a startup project and run by pressing F5.
One great thing is that in this pattern also Add View dialog populate with model classes from ModelLib project as displayed in below image so we can even create strongly typed view based on model!!
Sample project can be downloaded from here.
Hope this would be helpful. You can follow me @NandipMakwana to get latest update via twitter.