Speed up building software

To speed up software building .the most common way is reuse something ready.
there are ways to create reusable stuff .make high cohesion: create function, class ,use patterns , components … what happens is, (say want class reusable) sometimes the use case is not exactly same as last time when class been created or lets say the class is has not been created perfectly but can not change it now, and 85% codes are same ,only some thing (say project name, model name etc) are different .

lets go with code generation .yes , generate the common portion of code, and manual code the difference .Microsoft has a nice code generate engine — T4. the syntax is something like asp.net web form engine :
complete code can be found here https://github.com/iorilan/NetCodeGenerator/blob/master/NetGenerator/BusinessLayer/BusinessService.tt

#@ template debug="false" hostspecific="false" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ assembly name="$(SolutionDir)\DataModels\bin\Debug\DataModels.dll" #>
<#@ import namespace="DataModels" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ output extension=".cs" #>
var prjName = "YourProject";
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Validation;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using log4net;
using <#=prjName#>.BusinessLogic.Helpers;
using <#=prjName#>.BusinessLogic.Models;
using <#=prjName#>.DataAccess;
using <#=prjName#>.DataAccess.Models;
using System.Data.Entity.Migrations;

/// <summary>
/// This file is generated from NetGenerator
/// providing basic features of business domain object
/// </summary>
namespace <#=prjName#>.BusinessLogic.Services
var assembly = typeof(TableSample).Assembly;
var types = assembly.GetTypes();
foreach(var t in types){

var tableName = t.Name;

var modelName = tableName+"Obj";
var contextClassName = prjName+"Context";
var contextInterfaceName = "I"+prjName;
var repoName = tableName+"s";

above showing the a code sniped of how to use T4 template to generate the code . not only c#, you could also use it for generating java, swift, cshtml … as long as ,it helps you keep typing in or copy the same portion of code again and again !

Author: lanliang


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s