Click here to Skip to main content
12,511,920 members (52,317 online)
Click here to Skip to main content
Add your own
alternative version


114 bookmarked

Creating DAL Components Using Custom ASP.NET Build Providers And Compiler Techniques

, 24 Oct 2006 CPOL
Rate this:
Please Sign up or sign in to vote.
This article describes how to create Data Access Layer Components (DALC) using ASP.NET build providers and a self-defined description language in C#, including an easy scanner, parser, and CodeDOM generator.


There are many articles on the internet that are dealing with the creation and usage of a Data Access Layer (DAL) and the comprising components, also known as DALC (DAL Components). There is nothing new in the process of creation of a DAL. You can actually use Typed DataSets, Microsoft's Enterprise Library (DAAB), or you may use one of the many third party tools to implement a comprehensive DAL system.

The main objectives of this composition are to show how to create and use ASP.NET build providers and also to explain how easy you can analyze a small self-defined description language to declare DALCs or anything else. To implement a full-blown DAL killer application which you can use for the rest of your programming life is not the objective of this article. Normally, you would not define your own description language to declare DALCs, you would instead use an XML-based description of the components and analyze them using the feature-rich XML classes that come with the .NET framework.

I just wanted to implement a lexical analyzer (a.k.a. scanner or tokenizer), some parsing techniques, and dynamic code generation using the .NET CodeDOM. There are a lot of situations in daily work where it would be handy to develop some kind of parser (even a very small and simple one) to come up with an acceptable and elegant solution. In fact, for one of my customers, I defined a description language to automate the extension of a web application.

To show an example of the final result of a dynamically generated DAL using the DALComp application, let's assume we have a database table called Articles. For this table, the DAL (see section "DALC Description Language" below), respectively, the build provider will automatically create a class called Article, containing private member fields and public properties that correspond to the table column names. Nullable types are created for value types.

In addition, the system generates static methods (also defined within a .dal file) to select the requested data. The data are returned as a generic list of type Article (in C# List


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Jürgen Bäurle
Software Developer (Senior)
Germany Germany
I’m a software developer based in Germany.


You may also be interested in...


Comments and Discussions

GeneralBugz Pin
dojohansen29-Oct-07 4:11
memberdojohansen29-Oct-07 4:11 

first of all thanks for an interesting and well written article.

There is a little error in the generated code for Nullable<> wrapped members. First of all, since the type is constructed from Nullable<> these properties should not have a set accessor; one sets instead the HasValue or Value properties of the Nullable<> struct. Secondly, and obvious given the above, the helper method to create instances from a query should not assign null to such a property (indeed this will not build as Nullable<> is a value-type and thus not nullable!) but rather get the wrapper and set HasValue to false.

That is, it should NOT be (this isn't exact, but codeproject stupidly hides the article in "post comment" mode so bear with me...)

if (record["id"] is DBNull) obj.ID = null;

but rather

if (record["id"] is DBNull) obj.ID.HasValue = false;

I would also like to suggest a simple improvement: Only use the Nullable<> wrapper when the database column is in fact nullable.

I understand that the article is meant to give us a starting point from which we can make our own stuff, but figured it was worth mentioning anyway. Thanks again, and happy coding!

Dag Øystein

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160929.1 | Last Updated 25 Oct 2006
Article Copyright 2006 by Jürgen Bäurle
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid