Source Code Generation using Razor Template Engine for Both Client Side and Server Side

Automating Source code generation can be surprisingly easy and will reap major benefits. It will help you develop 90% of the API’s for any project in just a button click. The primary benefits of automating source code generation are shown below in the form of a Bar chart.

Razor Template Engine

Explanation

Creating dynamic source code (Controller, facade, and Dao including interfaces) using code templates and razor template engine.

Steps to be followed to generate controller code are as follows

1. Create a controller template and keep only the common operations. Rename the file with extension as .txt.

2. Inside the file, where ever the Entity name is there replace it with razor template code (@Model.Entity). Assume Entity is your “table name” which will be given as  input parameter in reality.

3. Create a model object of dynamic type (refer email sending template in our solution)

4. Then bind data in the template like

var result = Utilities.BindDataInTemplate(template, “reg-email”, input.User as Object); Here BindDataInTemplate functions which is written in utilities file, binds the data in the template.

5. Save the result string as “.CS” Controller.cs.

The above similar steps are followed to generate other source code (facade and Dao including interfaces).

Details of API to generate server side dynamic source code 

API URL : http://localhost:53154/api/controller/create

Method Type : POST

Request :

{

"Entity": "Table name",

"Author": "User name";

}

 

Details of API to generate client side dynamic source code 

(simple html,route.js,module.js,controller.js,service.js)

API URL : http://localhost:53154/api/controller/create/js

Method Type : POST

Request :

{

"Entity": "Table name",

"Author": "User name",

"ControllerDescription" : "Description for Controller.js"

"ServiceDescription"    : "Description for Service.js"

"FilePath"              : "FilePath (Example: "E: Angulartemplate")";

}

  

Reference : Please refer trick#2 in the below link for using code templates and razor template engine

http://odetocode.com/blogs/scott/archive/2013/01/09/ten-tricks-for-razor-views.aspx.

Conclusion

Reliability, availability, productivity, performance, and cost reduction are powerful arguments for adopting automation solution. It helps you to create over 90% of the API’s that are required for an application. However, the tool generates code for API’s with common operations i.e. Save/Update, GetById, GetAll, Search, Search with Pagination and Delete. The API’s which are specific to the application should be taken care.

 

FacebookTwitterGoogle+Share
About Vijetha M

Vijetha is a Software Developer by profession and adventurer by passion. Trekking is her fascination and exploring places is her obsession. Being a volunteer of Bhumi, she fulfills her dream of educating orphan children and helping the society.



Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>