Getting Started with Codesmith (for example: building a data access layer within 15 minutes-dal-nettiers)

Source: Internet
Author: User

[C #] Establish a data access layer translation within 15 minutes

Applicable to ASP.net 1.1 and 2.0 (select template.)

Use tool:. Nettiers templates

Codesmith

In this article, you will learn how to build a data access layer based on Microsoft's Enterprise Library through the Codesmith and. Nettiers Template Libraries

The data access layer, which uses the DAL, encapsulates all of the functionality in the application that interacts with the database. This typical code, if it needs to be written manually, does not require. NET's expertise, and requires knowledge of data access. Writing a DAL layer for an application is a tedious, time-consuming, repetitive and potentially error-ridden piece of code.

In this article we use the Northwind sample database in SQL Server, and this database is ubiquitous. Of course this allows you to write programs of the DAL layer is not monotonous and time-consuming, become fast, simple, easy.

If you follow this article, in the end you will be able to build the Dal in a minute (the other 14 minutes for the first time is used to download the software) [foreigner is also humorous]

Automatic code Generation overview

Code generation is not a new concept, it is the use of a software tool to generate code. In fact, the concept of automatic code generation is a long-standing one, and in this article I will use the Codesmith tool to generate the code and SQL statement scripts for the DAL.

Codesmith is a tool used by developers that allows developers to generate the desired code using the template format. This codesmith the advantage that software developers still retain control of the code generated through the template. Templates provide an opportunity for people to generate new code, code reuse, and build libraries, such as the nettiers template mentioned in this article.

preparatory work

is to download the required tools and templates, Codesmith is a commercial developer tool, but it has a 30-day trial version that we can use in this version. Codesmith has a number of built-in templates, and the next service pack will contain nettiers templates

http://www.codesmithtools.com/

Download. Nettiers

Http://www.nettiers.com

Click the "Latest Download" link on the left column of the. nettiers site to download the latest MSI-formatted installation package on the page that is being transferred.

The final step is to verify that you have the SQL database installed and that the database is properly available for the connection string

Step 2

After downloading and installing Codesmith, you can open Codesmith from Codesmith Studio in the Start menu. You should familiarize yourself with Codesmith before you use the nettiers template.

As mentioned in the introduction, Codesmith is a template-driven code generation tool, a template Explorer on the right side of the program window that provides a quick way to access a template that has been installed or written.

To explain the other functional operations of Codesmith, we need a template that can be opened hashtable.cst template by double-clicking Hashtable.cst through the template Explorer.

HASHTABLE.CST is an important template in Codesmith that produces a strongly typed set of basic data types for a hash table, before we introduce the template, let's take a quick look at the Codesmith another property window.

This property window allows you to set the properties in the template. When you use the HASHTABLE.CST template, we need to set some common elements such as class name, Itemtype,keytype, for example, if you want to create a strongly typed set of this object of person, And through the shaping class should ask a class named Personcollection, then you should define the class name as Personcollection,itemtype for Person,keytype Int. You can generate this strong type set by clicking the Run button on the Codesmith (which can be found on the toolbar)

If you take a moment to read the hashtable.cst file, you might say, "It looks like asp.net." You're right, Codesmith's template is a copy of the ASP.net page, based on its similar ideas. However, ASP.net is used to produce HTML pages that Codesmith used to produce source files or other text files.

The more important thing to remember is that Codesmith does not limit your creativity, because you are driving the generation of code by controlling the template.

Step 3

Well, we have a basic understanding of codesmith, let's take a look at the nettiers template.

Remember, the purpose of codesmith is to make you write code faster and less incorrectly. The Nettiers template lets you create a DAL layer one step at a speed.

First we need to add nettiers to the Codesmith template Explorer.

1. Click the browse icon in the template Explorer to select the directory that contains the template

2. To the Nettiers installation directory, in My computer is C:/Program files/serialcoder/nettiers 0.9.2/

3. When this directory creates a shortcut in Template Explorer Manager, expand it, select Nettiers.cst, and double-click it.

4. Now that the Nettiers template has been installed, you need to configure the data source, open the Properties window, and configure the data source by clicking on the button with an ellipsis.

5. It will open the database picker, we will create a new data source, so click the ellipsis button to open the data source Manager. The following illustration shows a sample data source manager.

6. Test the connection to ensure it is valid. After clicking OK, close the dialog box and make sure the new data source is selected.

Finally, set the Entiredatabase property value to True,

Set the NameSpace property value to Demo,

Set the OutputDirectory property value to the stored directory of the output source code, and if the directory does not exist, create a directory manually.

When you have completed the above steps, the NETTIERS.CST Properties window looks like this:

For Nettiers and codesmith Other features are not said, these actions enough to let us create a data access layer, click on the Codesmith toolbar Run button, you can produce the DAL layer of code.

Step 4, view the generated code

Open visual Studio. NET, open the Demo.sln file under the directory you just set up

You will find that there are three projects in the solution,

Demo-Master library, containing classes used during application interaction with the database

Demo.dataaccesslayer-Contains data access implementation code class library

Demo.DataAccessLayer.SqlClient-including Microsoft Database design Paradigm Class Library

Step 5, compile, code complete

Congratulations, you already have a data access layer that implements the best design paradigm that Microsoft recommends.

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.