Wednesday, October 22, 2008

Using Codesmith and .netTiers to build a Data Access Layer

Using Codesmith and .netTiers to build a Data Access Layer
Tutorial about how to connect database with asp.net and vb.net using Codesmith and .netTiers tools

Introduction

Some time ago, I have involved in a National Energy company information system (CIS) development. It is actually the project developed by my company and I am a junior of the developer team. We start it by explaining and discussing about all the detail and requirement of database because this is the main part and most important part of this project. My data layer syntax in asp.net is using codesmith and .nettiers as tools to simplified and shortened my programming.

Main

Codesmith is a client application that uses templates to generate code. It saves time. It reduces repetitive coding. .NetTiers is an open source tools that provides templates for codesmith to generate a n-tiered architecture. Combine these 2 client applications can quickly create the datalayer for the project. It does simply provide a Business Object like Employees, Companies, and others. This is done by creating collections. You can insert, update, select from databases in a different way besides ado.net method as usual. Maybe you can view the details of what these two are from their sites.

If you want to try, first you need to download codesmith and .nettiers and then install them, mine is using codesmith 4.0. Then open codesmith explorer. Click open folder from top corner of codesmith explorer then browse to your nettiers installation folder. After that execute Nettier.cst as shown below.






So there are minimum three attribute now you must config in the next opened window. It is select database source you want to use in the project development. In this case mine is CISdata which have been designed before using Power Designer 11. Second set the output directory. And third is set the namespace. Namespace is used for your database collection access later. For example my namespace is CISLib. Later I can write Imports CISLib, Imports CISLib.DAL, Imports CISLib.DAL.DataRepository, Imports CISLib.DAL.SqlClient in my asp.net vb coding. Last click generate.







Now we can wait for quite some time depends on your computer specification. It takes about 1-2 minutes to complete. If all goes well, you’ll have a new solution in the folder you selected as output directory. Open the solution. You’ll see that it has three projects. All three set to produce DLLs. Next most important step is to copy all .DLL files you generated from the output bin directory to your project bin directory.






Last thing is to config Enterprise Library Configuration from nettiers. Navigate to Start Menu, nettiers, Enterprise Library Configuration. Minimize it. Open Your visual studio solution. Add another project to the solution. Add a new item to the project. Select Application Configuration File as the type. It’s called App.conf by default. Leave it as it is. Open Enterprise Library Configuration window. Click on Open File and select the App.Conf you just created.

Three important part to config is nettiers application block, SQL data provider instance, SQL connection string->database, integrated security, and server. After all save it. This generates some *.conf files in your project directory. Go there and copy all of the *.conf files to your bin directory. This is the end of codesmith and nettiers installation and configuration part. Next step is how to use it.



I’ll include part of my asp.net vb coding to let us understand how to use this as a collection to connect to the database which we already generated before.


Imported library.

The method to insert to the database.

Key syntax here are 'Dim objCompany as New COMPANIES'. Table COMPANIES in database now can be inserted with values from input textbox on asp.net form with easier method, example insert value to COMPANY_ID, STATUS_ID filed in COMPANIES table is just as simple as shown below, instead of declare connection, command, and executenonquery as usual.


The method to select/retrieve data from the database.

Key Syntax is also in declaration of COMPANIESCollection object. We can notice here object declare as COMPANIESCollection for database retrieve instead of only as COMPANIES for database insert. One more important syntax is COMPANIESProvider.getpaged. It is the syntax used in codesmith and nettiers implementation. There are a lot more of syntax we can use for this data layer connection. For more expertise explanation about it, maybe we can look from their original help and tutorial.

Region "Company"

Private Sub showCompanyData()

Dim objCompanyCol As New COMPANIESCollection

Try

objCompanyCol = COMPANIESProvider.GetPaged("COMPANY_ID='2'", "", 0, 999, 999)

If objCompanyCol.Count > 0 Then

txtCompanyID.Text = objCompanyCol(0).COMPANY_ID

End If

Catch ex As Exception

Response.Write(ex.ToString())

End Try

End Sub

Private Sub getCompanyIDMaxnum()

Dim objCompanyCol As New COMPANIESCollection

Try

objCompanyCol = COMPANIESProvider.GetPaged("", "CAST(COMPANY_ID AS INT) DESC", 0, 999, 999)

If objCompanyCol.Count > 0 Then

txtCompanyID.Text = CStr(CInt(objCompanyCol(0).COMPANY_ID) + 1)

Else

txtCompanyID.Text = "1"

End If

Catch ex As Exception

Response.Write(ex.Message)

End Try

End Sub

#End Region

Conclusion

The possibilities for CodeSmith templates are limited only by your own creativity. As I've demonstrated in this article, CodeSmith is an exceptionally powerful developer productivity tool. Coupled with the .NetTiers templates you can write a best practices Data Access Layer for your application in a matter of minutes.

References

All the useful links or references that can help you learn about this tutorial

  1. http://www.codesmithtools.com/
  2. http://www.nettier.com

No comments: