Welcome to AspAdvice Sign in | Join | Help

AzamSharp

Some day I will know everything I hope that day never comes

Syndication

Tags

Navigation

Using Build Providers to Dynamically Create Entity Classes

There are lot of good articles about Build Providers. Bilal Haider in his article Creating Custom BuildProvider in ASP.NET 2.0 explains how to create a simple build provider which create an entity class using an xml file. Fritz Onion also provides an excellent blog post in which he explains the purpose of build providers. Check out his cool post using the following link.

http://pluralsight.com/blogs/fritz/archive/2004/09/06/2188.aspx

Inspired by the great work by these individuals I decided to create a simple application that will create entity classes dynamically using the information stored in an XML file.

The first step is to create a simple example file which will contain the information you need to create the entity classes.

<mappings>

<mapping connectionString="Server=localhost;Database=Northwind;Trusted_Connection=true"

selectCommand="SELECT * FROM Products" className="Product" namespace="EntityClass.BLL"/>

<mapping connectionString="Server=localhost;Database=Northwind;Trusted_Connection=true"

selectCommand ="SELECT * FROM Categories" className="Category" namespace="EntityClass.BLL" />

</mappings>

The mapping file contains the information on how the entity class will be created, using which database and what table, the name of the entity class and the namespace.

Next, drop the .map XML file inside the App_Code folder. Now, everything inside the App_Code folder is compiled and build using a provider. The extension (.map) is a new extension and hence we have to register it using our own custom build provider. This is performed in the web.config file.

<compilation debug="true">

<buildProviders>

<add extension=".map" type="EntityClassProvider.EntityBuilder,EntityClassProvider"/>

</buildProviders>

</compilation>

As, you can see the .map extension is mapped to the EntityClassProvider.EntityBuilder class. This is our custom class let's check it out.

public class EntityBuilder : BuildProvider

{

public override void GenerateCode(AssemblyBuilder assemblyBuilder)

{

string virtualPath = base.VirtualPath;

List<Entity> entities = PopulateEntityClass(virtualPath);

CodeCompileUnit code = CodeGenerator.GenerateCode(entities);

assemblyBuilder.AddCodeCompileUnit(this, code);

base.GenerateCode(assemblyBuilder);

}

Above, is just the part of the class. The base.VirtualPath property returns the virtual path of the .map file which is inside the App_Code folder. The class CodeGenerator (custom class) is used to generate the code. The coe generation is performed by using the CodeDom classes.

Anyway, at the end the class is mapped to the database and created automatically. Check out the effect below:

BuildProviderImage1

 

And here is another image which shows all the properties of the Category class.

BuildProviderImage2

 

I will be writing an article on BuildProviders so check out GridViewGuy regularly :).

References:

Programming Microsoft ASP.NET 2.0 Applications: Advanced Topics (Paperback)
by Dino Esposito

ISBN-10: 0735621772

http://www.amazon.com/Programming-Microsoft-ASP-NET-2-0-Applications/dp/0735621772/ref=pd_bxgy_b_text_b/104-2292989-5255967

Published Wednesday, January 24, 2007 5:46 PM by azamsharp

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

No Comments

Leave a Comment

(required) 
required 
(required) 
Enter the code you see below