вторник, 28 августа 2012 г.

ASP.NET MVC: Putting Your Controllers In A Separate Assembly

Having been a part of many large enterprise ASP.NET MVC application implementations using test driven development I learned early on that separating your controller classes into a their own project significantly reduces the noise in your web project.  It wasn’t until a recent talk I gave to user group in my region that I realized that this isn’t a widely adopted practice.  For large applications with a lot of developers and a complex architecture I highly recommend it. 
Putting your controllers in a separate assembly is very straight forward.  First create a controllers project in your solution and then you just need to update your route registrations to tell them where to look for the controllers.
  1. routes.MapRoute(name"Default"url"{controller}/{action}/{id}",
  2.                 namespacesnew[] {"[Namespace of the Project that contains your controllers]"},
  3.                 defaultsnew {controller = "Home"action = "Index"id = UrlParameter.Optional});
In order to tell ASP.NET MVC where to look for your controllers when registering your routes you use the ‘namespaces’ parameter of the MapRoute method as illustrated above.
I know that the concept of putting controllers in a separate assembly is a bit controversial and everyone seems to have a strong opinion either for or against, so let me know what side of the fence you fall on.

Комментариев нет: