Before we see what, is Template-Based Code generation first of all we start withwhat is template?
Template means one file which provides a starting point for a new document. When you open the template, it has pre-programming with basic details.
Table of Content
- 1. What is template-based code generation?
- 2. Advantages of TBCG
- 3. Disadvantages of TBCG
- 4. TBCG with razor and Roslyn
- 5. Conclusion
The template is also available readymade and it is also created manually by user. Now so many programs support template so it is the best solution for preventing to write similar code, again and again, you can save one of them and use it whenever you want so you can't need to format document each and every time or make new just reference the template and start from there. Using a template, you can save time to format your design002E
What is template-based code generation?
Template-based code generation is named as ‘TBCG’. It is a way that generates code from the high-level specification which is named as template. In model-driven engineering, template-based code generation is the popular technique it gives both emphasis and automation and the TBCG technique is a good guide for developers in their choice and ideas.
The structure of code generators has two inputs: first is template files second is data source for filling out the templates.
Active Server Pages (ASP) or Java Server Pages (JSP) are examples of code generator templates. That is the combination of two programs - the template code that provides the skeleton or structure of the generated code, and the embedded code that controls how the template is expanded. Template base generated code is like built-in dynamic code
The data source provides information to generate a customized code. This could be unspecified, depending on the embedded code, or it could be a structure that is populated from database metadata (table and procedure definitions).
The difficulty of writing large software systems has responsible to an increased interest in generative programming where one program creates another program.The generator generates code using the abstract data (model) and Template-based code generation (TBCG) uses templates to describe the structure of the target text.
Read More: What Is Razor View Engine In Asp.net Core?
Advantages of TBCG
Separation of model and template. The template is strongly separated from the template control logic that is referred by the code generator in a consistent and predictable manner. Again, and again using the templates is like as the meta-data models are as easy as associating the relevant templates and meta-data sources.
Clarity and Conciseness. The TBCG templates are clear and readable as they don’t contain any template-control code or data access logic. They merely represent an abstract description of their target code and thus don't require the template developer to understand any additional language
Standard Control Code Inference. Since the control code is inferred from the template block structure it's innately reusable among templates and template blocks similar to it is usually sure to be the same for similar blocks.
Format flexibility. The template is flexible with the text and it can generate as there are no formatting limitations imposed by a control language. Generating a function call with parameters at the same line is as easy as generating each parameter on a different line simply by changing the separator associated with the block.
Change flexibility. The template is very straight forward to change since there is no need to change the control code logic or rearrange the template blocks among different control blocks.
Disadvantages of TBCG
Lack of imperative control code. The lack of imperative control code is preventing the transformation from changing the model; however, this disadvantage is introduced by design as in our view a transformation should not change its source model.
Change Impact. Changing the model or the template may have a great effect on the generated code-base. Unless managed correctly, model changes can become an impediment to the development process. That is, however, a developing issue with any generative technology and the projective templates approach makes it possible to assess the impact of model changes by comparing before/after projections.
Over the generation. Having a power generation technology may encourage the utilization of code-generation in place of normal coding techniques. On the other side, it sometimes makes sense to generate explicit code rather than use a generic framework as code can be easy to understand and analyze.
TBCG with razor and Roslyn.
Razor is a language that is called templating language and technology it is part of ASP.NET. Razor is a mix of C# and XML code to generate HTML page. It works well because HTML and XAML both are XML-based languages. Both are mostly the same so it is work excellent. This same technic is also used for non-XML text with the result of variable quality basic of the razor is described below.
It is responsible for producing an HTML response when called by the Controller Action method.
The Controller Action methods can return different types of responses which are collectively HTML Response called Action Results. The View-result is the Action Result which produces the.
The ViewResults are produced by the View Engine. When the Controller Action method call the view () or PartialView () it calls the View Engine, which generates the HTML Response.
View Engine is liable for rendering the view into HTML form to the browser.
Roslyn is a .NET Compiler Platform, Roslyn is its nickname. It is a set of open-source compilers and code analysis APIs For C# and also for visual basic .Net language is from Microsoft and Roslyn is the project notably which includes self-hosting versions of the C# and .NET compilers in short is written in languages automatically. Roslyn is a compiler which previews the new language for code generation, analysis, refactoring and c
Steps of working Razor and Roslyn code generation
The first step is to use the razor C#/XAML template which is compiled into C# code of a code generator class.
The second step is to compile the Roslyn for C# generator to an assembly
The third step is to use the LoadContext to temporarily load the generator assembly and instantiate the generator object.
The fourth step is to bind the template to XML data and create a XAML file and it is done using a generator.
The fifth step is to create a picture file from XAML and it is done using WPF.
This blog is useful for you to understand about the template and code generation with Razor and Roslyn. This both are useful for generating the regular HTML template automatically. You don’t need to write code again and again. Razor is scripting language engine which is use to write C# Code in HTML. Roslyn is Compiler which is generated from code automatically which is provided by Microsoft.