Skip to main content


Showing posts from January, 2014

Interface based model binding to populate properties value automatically

Interface is great way to make consistency and re-useability of codes. In this tutorial, I am going to show the power of interface to populate view/domain models automatically and interchange values between models. Technologies or techniques used in this approach: - MVC. - Interface for models. - MVC model binder for associated model with interface. Through this model binder we will populate values. - T4 template to generate code to register model binder with all models associated with interface. - Generic extension method to interchange values. It might sounding bit complex to achieve small thing. Just hold on with me and see unfolding. This will result in easier maintenance and less repetitive code. First let's start with creation of Interface that need to be populate automatically. This interface would be implemented on models and values will be auto-populated on binder. /// <summary> /// Interface for storing entry related values /// </summar

Enum generation for lookup table through T4

Sometime there is need of mapping some values from database to code level, basically in look up tables. I am going to generate enum based on database values. In this example, I am going for T4 template to generate up enum by using SqlDataReader. We can have any SQL query to generate enum. In my case, Privilege code, name and description from table would generate up enum. <#@ template debug="true" hostSpecific="true" #> <#@ output extension=".cs" #> <#@ Assembly Name="System.Data" #> <#@ include file="EF.Utility.CS.ttinclude"#> <#@ import namespace="System.Data.SqlClient" #> <# var code = new CodeGenerationTools(this); var connectString = "Set connection string"; var queryString = "select PrivilegeCode, PrivilegeName, PrivilegeDescription from Privilege"; #> namespace <#= code.VsNamespaceSuggestion()#> {