Quickly build a data access layer within 15 minutes
Download link
-
- Download codesmith 30Daily free trial Professional Edition
-
- Download the latest . NettiersTemplate
- Viewing bookArticleRelated video demonstration
Introduction
Through this article, you can learn how to use the Microsoft Enterprise Library,Codesmith,. NettiersThe number of templates is less15Quickly build a data access layer within minutes.
From now on, we set the data access layer (Data access layer)Dal, It isProgramThe layer that interacts with the database. HandwritingDalLayerCodeIt is a tedious and time-consuming repetitive activity, and many vulnerabilities may occur during program compilation.
Make sure that you have installedSQL ServerOfNorthwindDatabase, but this example can also run on any other database. Of course, after reading the article, you will not waste any time to write for your application.DalThe layer code is faster and simpler.
After reading this article, you will1Use best practices to create a data access layer within minutes (Other14Minutes are used to download the required software for the first time ).
Code Generation Overview
Code Generation, or code generation using tools and software, is not a new concept. In fact, code generation is quite common. This article usesCodesmithTo generateDalCode andT-SQLScript.
CodesmithIs a common tool for developers. It can use templates to output the desired format code. You can use a template to generate any code you want. This article uses. NettiersTemplate.
Step 1 install software
First, downloadCodesmithAnd. Nettiers, Where the former is30Daily trial version (of course, there are cracking tools on the Internet. The latest version is3.2, YesFor. NET 2.0, We download3.1Or3.0Version)
Download and install Codesmith
Http://www.codesmithtools.com/
CodesmithMany templates have been built in, and we will include them into what we need in a moment.. NettiersTemplate, installedCodesmith, We download. NettiersTemplate Library.
Is Codesmith Download . Nettiers Template Library
Http://www.nettiers.com
Download the latest. NettiersTemplate Library Installation File.
The last step is to ensure that you have installedSQL ServerDatabase and prepare the connection string to connect to the database.
Step 2 -Codesmith
OpenCodesmithFirst, let's get familiar with its usage.
Previously introducedCodesmithIs a template-driven tool. InCodesmithThere is a template browser window on the right, which allows you to quickly use the template you have installed, see.
Let's first learn how to use the template and double-clickHashtable. CSTTemplate.
Hashtable. CSTIs used to generate a strongly typed setHashtableType template. Let's take a quick look.CodesmithIn the Properties window.
The Properties window allows you to set template attributes. Hashtable. CST Template, we need to set Classname ,Itemtype And Keytype Attribute. For example, we want to create Person Object, and useInteger Type key to access Personcollection Set. You need Classname Set Personcollection ,Itemtype Set Person , Keytype Set Int . Click Run Button to generate the source code of the strongly typed collection you need.
Take a lookHashtable. CSTYou will find it a bit likeASP. NETIn factCodesmithThe template is similarASP. NETTo generate the code you want, justASP. NETIs used to generateHtmlCode, andCodesmithIs used to generate what you needSource code.
One thing you need to remember isCodesmithIt does not automatically generate the code you need, but you can customize the template to generate all the code you want.
Step 3-Generate data access layer
We have basically understoodCodesmithTo demonstrate how to use. NettiersTemplate.
Remember,CodesmithIt allows you to quickly generate code and rarely produce errors caused by handwritten code.. NettiersThe code generated by the template is also the best practice for data access recommended by Microsoft.
First we need to add the. nettiers templates to the codesmith template Explorer:
First, we need. NettiersAdd a template to the template browser window.
-
- In the template browser, click the Open Directory icon and browse the directory containing the template.
- Select. NettiersThe installation directory of. My installation directory isC: \ Program Files \ serialcoder \ nettiers 0.9.2-caribert \ Templates \
-
- After selection, a folder will be added in the template browser window, expand it and selectNettiers. CSTTemplate
-
- Now let's configure the data connection string to openNettiers. CSTTemplate, click in the Properties windowSourcedatabaseThe dialog box next to the property.
-
- In this window, you can create a new data source, such.
-
- Test whether the connection is successful. ClickOKClose the window and select the name of the data source you just created.
- Finally, this isEntiredatabaseThe property isTrue,NamespaceThe property isDemo, outputdirectoryAttribute of the directory you want to output. Of course, you need to create a directory to generate code in advance.
Now you have completedNettiers. CSTSet all attributes of the template. It looks like this.
Of course we didn't tell you all about. NettiersBut forDalThe layer code is enough. Click the toolbar.RunButton (Small arrow) to generate code.
Note: You must set the primary key for the data table you created. If the primary key is not created, no response code will be generated for this table.RunA report is displayed. You can see the table code generated.
Step 4-View generated code
UseVisual Studio. NETOpen the code output directory you just set (which isC: \ nettiers \ northwind_demodirectory)Demo. slnSolution.
Let's take a quick look at the projects and files included in the solution.
You shoshould find 3 projects within the solution:
the solution contains 3 projects
- Demo -This project contains the main classes you want to interact with the database.
- Demo. dataaccesslayer - This type of library contains the actual database operation Execution Code
- Demo. dataaccesslayer. sqlclient -Such libraries include MicrosoftRecommended database access design patterns and best practices(Microsoft patterns & Practices classes).
Step 5 - Compilation and completion
Next, we useVisual StudioTo compile the project.
Congratulations, you now have a database access layer that uses the best practices for accessing databases recommended by Microsoft.
Now you can use yourDalFor example, you need to obtain, update, and delete an employee's record.
Using demo. dataaccesslayer. sqlclient;
// Select by primary key
Employee Employee = sqldatarepository. employeeprovider. getbyemployeeid (13 );
Employee. firstname = "John ";
Employee. lastname = "doe ";
// Save changes
Sqldatarepository. employeeprovider. Save (employee );
// Delete record
Sqldatarepository. employeeprovider. Delete (employee );
For more information, seeDalFor more information, see. Nettiers.
Http://cstemplates.sourceforge.net/nettiers-manual-v1.html
Remember, your data access layer code is generated through templates. If you modify the database structure, you only need to generate a new one using the template.DalThe code at the layer is enough. (Of course, If you modify the generated code before re-generating the code and re-generate the new achievements to overwrite it for you, what should you do, you can write a subclass that inherits the automatically generated class, where you can override the related method, or. NET 2.0In addition, you can expand your template based on your ideas, including annotations, naming conventions, patterns, and so on.
Section
CodesmithYou can generate any code you want based on your template. This article shows you how to use. NettiersTemplate to create a database access layer that uses the best practices for accessing databases in your application.
You can view the video demonstration here:
Http://community.codesmithtools.com/r.ashx? Id = 1
Code Smith 3.1A new feature of is the ability to compute code lines. It supports conservative computingCodesmithGenerated code.
Here we can see. NettiersGenerated91,559Line of code. Suppose a very good programmer can write in an hour.60Line of code.1,800Hours. To join a powerful programmer, you have to pay it every hour.60USD,CodesmithAnd. NettiersSaving your company1,800Hours and109,000USD. (It seems a little exaggerated .)
ObtainCodesmithAnd exert its power.
Codesmith And . Nettiers Related Resources
If youCodesmithYou can accessCommunityTo obtain the solution: Http://community.codesmithtools.com/. codesmithThere are templates written by many people in the community and they are shared to everyone for download. You can check whether you need them.
If you want to buyCodesmithTo access its website.Http://www.codesmithtools.com /.You can get an authorization to exempt30Day Trial restrictions. If you have any questions, you can send them to their after-sales support email address.Email sales@codesmithtools.com.
If you. NettiersIf the template has a problem, visit the following Forum.:
Http://community.codesmithtools.com/forums/16/ShowForum.aspx
English link:
Build a data access layer in less than 15 minutes
Http://community.codesmithtools.com/blogs/tutorials/archive/2006/02/13/nettiers.aspx
In addition, we can use this post to investigate what your persistent layer uses, nhib.pdf, ibatis.net , daab, SPL , sqlhelper, CMP is another one, in the future, I want to use the daab . I don't know which ones are suitable for enterprise development.