Using Custom Conventions In .NET
Conventions Over Configuration approach used in several places by .NET designers to avoid excessive configuration. While the default conventions are helpful in getting up and running quickly, it can be useful sometimes to change the default behavior. We look at a few examples.
There are normally ways to work around the conventions for one-off cases by explicitly setting up the configuration, but what if you want to change the conventions themselves? These are some of the things possible -
- Dino Esposito shows how the convention around ASP.NET MVC Controller names work, and how they can be modified
- The convention for View locations in ASP.NET MVC can be changed if you use a Custom View Engine
- Several conventions are used in Entity Framework CodeFirst, such as property that maps to the ID column and which class maps to which table automatically. These can be changed.
We have already covered Extensibility points in ASP.NET MVC earlier which provide several other opportunities to introduce your own desired conventions.
The approach can be used with several third-party libraries as well. For example -
- FluentNHibernate Conventions allow NHibernate auto-mapping conventions to be changed
- IoC containers such as Castle-Windsor allow for auto-registration. You can always modify or add to the conventions used in auto-registration.
Identifying where to stick with the default convention, and when not, is a matter of judgement. Using custom conventions in appropriate places can definitely make your code more maintainable.