Skip to main content

Posts

Showing posts with the label T4 Template

T4, Generating interface automatically based on provided classes

With new techniques and patterns interface plays a key role in application architecture. Interface makes application extendable like defining file upload interface and implementing based on file system, Azure Blob storage, Amazon S3. At starting we might be implementing based on Azure Blob but later we might move to Windows based file system and so on. Ideally we create interface based on need and start implementing actual default implementation class. Many a times at starting of implementation there is one to one mapping between Interface and Class. Like from above example File upload interface and the initial or default class implementation that we design and with time it will get extended. In this article, we will try to create interface based on default class implementation. This is not at all recommended in Test Driven Design (TDD) where we test the application before actual code implementation but I feel sometimes and in some situations it is okay do that and test straig...

Advance enum generation for lookup table through T4

Sometime back I had written simple T4 to generate enum for look up tables http://vikutech.blogspot.in/2014/01/enumeration-generation-for-lookup-table.html . Later on I had posted same on Nu-Get https://www.nuget.org/packages/t4.lookup . This time I am going to extend it to support multiple enums, enum Id, configuration through class and different settings for enum description, columns. Here is the entire code for generating code <#@ template debug="true" hostSpecific="true" #> <#@ output extension=".cs" #> <#@ Assembly Name="System.Data" #> <#@ import namespace="System" #> <#@ import namespace="System.Collections.Generic" #> <#@ include file="EF.Utility.CS.ttinclude"#> <#@ import namespace="System.Data.SqlClient" #> <# // TODO: Look for alternatives to remove connection string var connectString = "data source=.;initial...

Strongly typed configuration through database

We usually save most of application setting under app.config or web.config with key and value pair under appSetting element. The big problem with those are whenever you need value, typecasting need to be done and also need to be called through key. The other problem I find is with hierarchy of settings like download related setting present should under download element for easy accessing. To solve strongly typed configuration and hierarchy, ConfigurationManager ( http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager(v=vs.110).aspx ) class comes for rescue. You might have seen under web.config where we have sectionGroup and related configuration under elements. To implement it, lot of codes need to written to achieve simplification. To reduce numbers of codes for implementing XML configuration a very good and free tool is available called "Configuration Section Designer" available on http://csd.codeplex.com/ . There we do not have to wri...

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()#> { ...