Wednesday 24 September 2008

Using Code Generator to Create Sql Mapper Class



Good to see you again!!

This sample code used CodeDOM(System.CodeDOM) to create a code generator. The purpose is dual. It not only demonstrates how to use CodeDOM but also the way to automate a Sql input mapper generator.

Internally it uses SqlDMO COM dll to access a Sql Server database. It then fetches the required Stored Procedure, and exposes the input parameters.

The downloadable code contains the code fit for the purpose. Its as generalised as it should be. Feel free to use it.

The following is the class diagram for the CodeDOMDemo classes:


The following is class diagram for the SqlCodeDOM classes:


The following fixture gives you some of the usage scenarios.



using NUnit.Framework;
using Sanjeet.Demos.CodeDOMDemo;

namespace Sanjeet.Demos
{
[TestFixture]
public class GenerateStoredProcFixture
{
private const string serverName = "serverName";
private const string username = @"username";
private const string password = "password";
private const string databaseName = "databaseName";

[Test]
public void GenerateCounterParty()
{
const string spName = "GetCounterParties";
const string filename = @"c:\CounterParty.cs";
GenerateStoredProcedure sp =
new GenerateStoredProcedure(serverName, username, password, databaseName, spName, filename,
"Sanjeet.Data");
sp.Generate();
}

[Test]
public void GenerateDealsEnteredbyAllCounterParty()
{
const string spName = "GetDealsEntered";
const string filename = @"c:\DealsEnteredbyAllCounterParty.cs";
GenerateStoredProcedure sp =
new GenerateStoredProcedure(serverName, username, password, databaseName, spName, filename,
"Sanjeet.Data");
sp.Generate();
}

[Test]
public void GenerateDealsEnteredbyCounterParty()
{
const string spName = "DealsExport";
const string filename = @"c:\DealsEnteredbyCounterParty.cs";
GenerateStoredProcedure sp =
new GenerateStoredProcedure(serverName, username, password, databaseName, spName, filename,
"Sanjeet.Data");
sp.Generate();
}
}
}


Happy Coding!

No comments: