Template studio, an excellent template-based code generation tool

Source: Internet
Author: User

Llbl Gen as an excellent ORMCodeGenerate tools to quickly generate applications based on templatesProgramCode. You may also recommend template generators such as T4 and code Smith, but don't worry. Let's take a look at this article first.ArticleAs an entry-level tutorial, compare your strengths and weaknesses.

Take the third-party template provide common SQL functions for Microsoft SQL Server as an example to illustrate the process of template generation.

Design Template

Open template studio, select templatebindings viewer, and click the new button to add a new templatebinding template file.

This setting is convenient for llbl Gen work. There are several key items
For database is a database type supported by templates. You can select only one database type. If you select platform, the generated code is supported.. NET Framework version, which will affect the SLN and CS/VB. the version of the proj file. Select C # For target language to generate C # code. logic language indicates that the language used to write the template is tdl syntax.
Bind the template ID and the corresponding template file name in templateid-filename bindings.

Templatebinding is an xml configuration file with the following content:


description =" template Bindings for the templates which are specific based databases (7/2000/2005/CE ). for. NET 2.0 based platforms "
databasedriverid =" 2d18d138-1dd2-467e-86cc-4838250611ae "precedencelevel =" 10 ">



<Language name = "C #">
<! -- DB specific template bindings -->
<Templatebinding templateid = "WB. sqlsystemfunctionstemplate" filename = "sqlserverspecific \ net2.x \ C # \ WB. sqlsystemfunctions. template"/>
</Language>
</Templatebindings>

This is the idea of code batch generation. For example, if I have five templates, add a configuration file to these five templates, reference these five templates, and then

You only need to start the configuration file. When the code is generated, parameters are automatically passed to these five templates to generate code in batches.

Select WB. sqlsystemfunctionstemplate and press Ctrl + e to edit the template file WB. sqlsystemfunctions. template.

The logictdl syntax is similar to ASP. NET, but <[and]> is used to indicate the variables used in the template.

In the template sets-template Definition Language (tdl) Section of llblgenpro2.6sdkdocumentation, the meaning of <[rootnamespace]> is mentioned, the original Article is will be replaced by the specified root namespace in the Generator configuration screen, which is understood as the namespace variable entered when the code is generated.

Start llbl Gen and configure the generation Parameters

F7 calls up the code generation interface, switches to the task queue to execute, add tasks to add a new task, and select SD as the type. tasks. base. consumetdltemplate, which is used to compile the template written in Section 1 and generate C # code.

Modify filenameformat = sqlfunctioncils. [extension], templateid = WB. sqlsystemfunctionstemplate

This is actually the preset file generated by the configuration code. The file content is as follows:

<? XML version = "1.0" encoding = "UTF-8"?>
<Preset name = "SQL functions" lastmodifiedon = "2011-08-30t22: 40: 50.3165235 + 08: 00" createdby = "northwind" issealed = "false" xmlns = "http: // SD/llblgen/Pro/presetelementdefinitions. XSD ">
<Supportedplatforms>
<Platform name = ". NET 2.0"/>
</Supportedplatforms>
<Supportedtemplategroups>
<Templategroup name = "adapter"/>
</Supportedtemplategroups>
<Taskpresets>
<Taskpreset name = "SD. Tasks. Base. consumetdltemplate">
<Parameters>
<Parameter name = "destinationfolder" value = ""/>
<Parameter name = "filenameformat" value = "sqlfunctioncils. [extension]"/>
<Parameter name = "templateid" value = "WB. sqlsystemfunctionstemplate"/>
</Parameters>
</Taskpreset>
</Taskpresets>
</Preset>

At this point, the configuration has been completed. Click start generator to generate the code.

 

Analysis of template files

Let's take a look at the role of the template. The role of this template is to convert the date and string functions in the built-in SQL Server System into C # code.
If you want to call the year function in SQL Server, you may need to write this method.

Private int getsqlserveryear ()
{
Resultsetfields fields = new resultsetfields (1 );
Fields. definefield (New entityfield ("afld", new dbfunctioncall ("year", new object [] {datetime. Today}), 0 );
Typedlistdao Dao = new typedlistdao ();
Return (INT) Dao. getscalar (fields, null );
}

If you use this template, the following code has been generated for you and can be used directly.

Private int getsqlserveryear ()
{
Resultsetfields fields = new resultsetfields (1 );
Fields. definefield (New entityfield ("aday", sqlfunctionfactory. Year (datetime. Today), 0 );
Typedlistdao Dao = new typedlistdao ();
Return (INT) Dao. getscalar (fields, null );
}

That is to say, it is white and used to simplify the method for calling SQL server functions.

However, the above Code only serves the built-in functions of SQL Server, and does not generate C # code for user-defined functions. If you have a custom function somefunctiontogetasetting, you need to add a partial sqlfunctionfactory class. The Code is as follows:

Public partial class sqlfunctionfactory
{

Public static iexpression somefunctiontogetasetting (INT someparameter)
{< br> return New dbfunctioncall ("DBO", "somefunctiontogetasetting", new object [] {someparameter });
}< BR >}< br> Add a code layer for the client to conveniently call

Public partial class sqlfunctioncils
{
Private sqlfunctioncballs ()
{
}
Public static bool somefunctiontogetasetting (INT someparameter)
{
Return (bool) sqlfunctionfactory. getscalar (sqlfunctionfactory. somefunctiontogetasetting (someparameter ));
}
}

The final call code is as follows:

Bool somesetting = sqlfunctioncils. somefunctiontogetasetting (12 );

conclusion: the template builder template studio of llbl GEN has low popularity due to its commercial nature. It is not quick to get started with code Smith/T4 and has few documents. In most cases, you do not need to use a custom template. The installation package of llbl GEN has a complete adapter/selfservicing template. However, it is necessary to thoroughly grasp the ORM tool llbl Gen and understand and learn its template technology.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.