Adam Miller's Weblog
dotnetcraftsman
c# development, design, and other thoughts
Wednesday, July 07, 2004
 
Dynamic SQL Engine
I finally got started on my initial Dynamic SQL Engine. I'm going to describe how I went about creating it. I'd like to hear any suggestions for improvements. I started with my basic data layer that uses a factory creation pattern. These factories generate all of the ADO.Net objects (for example the SqlProviderFactory can generate SqlCommand, SqlConnection, etc...).
IDataProviderFactory: The interface for all other factory providers.
OleDbProviderFactory: OleDb Support
SqlProviderFactory: SqlClient Support
With that setup in place I started my DynamicSqlEngine which so far only includes 4 classes:
DataTableAttribute: This attribute defines what table a specific class may represent.
DataColumnAttribute: This attribute defines a custom attribute that will let me label properties or methods with a column name (I've only added support for properties so far).
IDynamicSqlObject: This is an interface that an object must implement to use the DynamicSqlEngine. It is just an empty interface. (I know I could have simply used "object" but I believe this method will make it more clear for error checking as well as to the users of the engine)
DynamicSqlEngine: This is where all the dynamic SQL is created for any object that implements the IDynamicSqlObject interface and has properties (or soon to come methods) with the DataColumnAttribute.

To show an example of how this would be used from the client side I'll create a sample class called person:

[DataTableAttribute("People")]
class Person : IDynamicSqlObject
{
  private string _firstName;
  private string _lastName;

  public Person(string firstName, string lastName)
  {
    _firstName = firstName;
    _lastName = lastName;
  }

  [DataColumnAttribute("FirstName")]
  public string FirstName
  {
    get { return (_firstName); }
    set { _firstName = value; }
  }

  [DataColumnAttribute("LastName")]
  public string LastName
  {
    get { return (_lastName); }
    set { _lastName = value; }
  }
}

After the user had created a class they could simply use my
data access layer to insert a new person like this:

  Person person = new Person("John", "Doe");
  SqlProviderFactory factory = new SqlProviderFactory();
  DynamicSqlEngine.Insert(factory, "MyConnectionString", person);

This would simply insert John Doe into the People table in your database. I will post more of my DynamicSqlEngine as it's being created if I feel like it (or if anyone is interested in seeing it).

Comments:
Wow! That's really cool. I have no idea how you did this.
 
hey, I just got a free $500.00 Gift Card. you can redeem yours at Abercrombie & Fitch All you have to do to get yours is Click Here to get a $500 free gift card for your backtoschool wardrobe
 
hey, I just got a free $500.00 Gift Card. you can redeem yours at Abercrombie & Fitch All you have to do to get yours is Click Here to get a $500 free gift card for your backtoschool wardrobe
 
Post a Comment

<< Home

Powered by Blogger