Quickly build a data access layer within 15 minutes

Source: Internet
Author: User

Download link

  • Download CodeSmith 30-day free trial Professional Edition
  • Download the latest. NetTiers Template
  • Watch the video demonstration of this Article

 

Introduction

Through this article, you can learn how to use the Microsoft Enterprise Library, CodeSmith, And. NetTiers templates to quickly build a data access layer within less than 15 minutes.

From now on, we have abbreviated the Data Access Layer as DAL, which is the Layer in which the program interacts with the database. The handwritten DAL-Layer Code is boring, time-consuming, and repetitive activities. Many vulnerabilities may occur during program compilation.

Make sure that the SQL Server's Northwind database is installed, but this example can also run on any other database. Of course, after reading the article, you will not waste any time to write the DAL layer code for your application, but it will be faster and simpler.

After reading this article, you will use best practices within one minute to create a data access layer (the other 14 minutes 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. In this article, we use CodeSmith to generate code and T-SQL scripts for the DAL layer.

CodeSmith is 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 a. NetTiers template.

 

Step 1 install software

First, download CodeSmith and. netTiers, the former is a 30-day trial version (of course, there are also cracking tools on the Internet, the latest version is 3.2, is. net 2.0, we can download version 3.1 or 3.0)

Download and install CodeSmith

Http://www.codesmithtools.com/

CodeSmith has many built-in templates, and we will include them in the. NetTiers template we need later. We have installed CodeSmith and downloaded the. NetTiers template library.

Download the. NetTiers template library for CodeSmith

Http://cstemplates.sourceforge.net

Download the latest. NetTiers template library installation file from the address above.

The last step is to ensure that you have installed the SQL Server database and are ready to connect to the database.

Step 2-CodeSmith

Open CodeSmith. Let's get familiar with its usage quickly.

As mentioned above, CodeSmith is a template-driven tool. There is a template browser window on the right side of CodeSmith, which allows you to quickly use the template you have installed. See.

 

Let's take a look at the template and double-click the Hashtable. cst template.

Hashtable. cst is a hashtable template used to generate a strongly typed set. Let's take a quick look at another CodeSmith window, the attribute window.

 

The Properties window allows you to set the attributes of the template. If you use the Hashtable. cst template, we need to setClassName,ItemTypeAndKeyTypeAttribute. For example, we want to createPersonObject, and useIntegerType key to accessPersonCollectionSet. You needClassNameSetPersonCollection,ItemTypeSetPerson,KeyTypeSetInt. Click the run button in the toolbar to generate the source code of the strongly typed set you need.

Take a look at Hashtable. cst, you will find it a bit like ASP. NET syntax, in fact, CodeSmith template is similar to ASP. NET form to generate the code you want, just ASP. NET is used to generate HTML code, while CodeSmith is used to generate the source code you need.

Remember that CodeSmith does not automatically generate the code you need, but you can define a template to generate all the code you want.

Step 3: generate a data access layer

We have basically understood CodeSmith's usage. Now we will demonstrate how to use the. NetTiers template.

Remember, CodeSmith allows you to quickly generate code and rarely produce errors caused by handwritten code .. The code generated by the NetTiers 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, add the. Nettiers template to the template browser window.

  1. In the template browser, click the Open Directory icon and browse the directory containing the template.
  2. Select the. NetTiers installation directory. The installation directory is C: \ Program Files \ SerialCoder \ NetTiers. 0.9.2 -Caribert \ Templates \
  3. After selection, a folder will be added to the template browser window, expand it, and select the NetTiers. cst Template
  4. Now let's configure the data connection string, open the NetTiers. cst template, and click the dialog box after the SourceDataBase attribute in the Properties window.
  5. In this window, you can create a new data source, such.
  6. Test whether the connection is successful. Click OK to close the window and select the name of the data source you just created.
  7. Finally, the EntireDatabase attribute is True, the NameSpace attribute is Demo, and the OutputDirectory attribute is the directory you want to output. Of course, you must create a directory to generate code in advance.

So far, you have completed all the attribute settings of the NetTiers. cst template, which should look like this.

Of course we didn't tell you all about it. netTiers attribute settings, but it is enough for the DAL-Layer Code to generate the flat. Click the Run button (Small arrow) in the toolbar to generate the code.

Note: You must set the primary key for the data table you created. If no primary key is created, no response code will be generated for this table. After you click the Run button, a report will pop up, you can see the table code generated.

Step 4-view the generated code

Use Visual Studio. NET to open the Demo. sln solution in the code output directory you just set (I set c: \ NetTiers \ Northwind_Demo \ directory.

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 three projects.

  • Demo-This project contains the main classes you want to interact with the database.
  • Demo. dataaccesslayer-These libraries contain actual database operation Execution Code.
  • Demo. dataaccesslayer. sqlclient-This type of library contains the code for Microsoft recommended database access design Patterns and best Practices (Microsoft Patterns & Practices classes.
Step 5-compile and complete

Next, we use Visual Studio to 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 your dal layer in other projects. For 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 about generating the Dal layer, refer to the. nettiers link below.

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 just need to generate the Dal Layer Code through the template. (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 in.. NET 2.0 uses multiple file classes.) In addition, you can expand your template based on your ideas, including annotations, naming conventions, and patterns.

Section

 

Codesmith can generate any code you want based on your template. This article shows you how to use it in just a few minutes. the nettiers template is used 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

A new feature of codesmith 3.1 is that code lines can be computed. It can calculate the code generated by codesmith conservatively.

 

 


Here we can see that. nettiers generates 91,559 lines of code. Suppose a very powerful programmer can write 60 lines of code in an hour, and it takes 1,800 hours to write the code. To join a powerful programmer, you have to pay it nearly $60 an hour. codesmith and. nettiers help your company save 1,800 hours and $109,000. (It seems a little exaggerated .)

Get a CodeSmith and exert its power.

CodeSmith and. NetTiers related resources

If you have some issues with CodeSmith, you can access its community to get a solution: http://community.codesmithtools.com. there are many templates written by many people in the CodeSmith community and they are shared to everyone for download. You can check whether you need them.

If you think about buying CodeSmith, you can access its website http://www.codesmithtools.com. You can get an authorization to exempt from 30-day trial restrictions. If you still have questions, you can send e-mail sales@codesmithtools.com to their after-sales support mailbox.

If you have any questions about the. NetTiers template, 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, I would like to take this post to investigate what your persistent layer uses, including nhib.pdf, ibatis.net, DAAB, SPL, SQLHelper, and CMP, I think DAAB in the Enterprise Library will be used in the project in the future. I don't know if it is suitable for enterprise development. What are the disadvantages of DAAB.

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.