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!

Tuesday 23 September 2008

CSS Parser



Presenting to you a simple piece of code that parses CSS. It uses Regular Expressions to achieve the functionality.



The sample code below gives you an idea how to use it.



/*
*
* Date Created: 02-Sep-2008
* Author: SAHAY, Sanjeet, IDC
* Filename: CssParserFixture.cs
* Assembly: Sanjeet.Demos
* Project: Demos



* Purpose: Add purpose
*
*/
using System.Collections.Generic;
using NUnit.Framework;
using Sanjeet.Demos.CSSParser;

namespace Sanjeet.Demos
{
[TestFixture]
public class CssParserFixture
{
private IDictionary<string, string> properties;

private IContainer GetContainer()
{
const string cssFilepath = @"..\..\..\CssParser\StyleSheet.css";
return new DefaultContainer(cssFilepath);
}

[Test]
public void GetButtonsClip()
{
IContainer container = this.GetContainer();
this.properties = container.GetProperties("Buttons");
Assert.IsTrue(this.properties["clip"].Equals("rect(auto auto auto auto)"));
}

[Test]
public void GetButtonsPropertiesCount()
{
IContainer container = this.GetContainer();
this.properties = container.GetProperties("Buttons");
Assert.IsTrue(this.properties.Count == 16);
}

[Test]
public void GetH1Color()
{
IContainer container = this.GetContainer();
this.properties = container.GetProperties("H1");
Assert.IsTrue(this.properties["color"].Equals("blue"));
}

[Test]
public void GetH1FontStyle()
{
IContainer container = this.GetContainer();
this.properties = container.GetProperties("H1");
Assert.IsTrue(this.properties["font-style"].Equals("italic"));
}

[Test]
public void GetH1PropertiesCount()
{
IContainer container = this.GetContainer();
this.properties = container.GetProperties("H1");
Assert.IsTrue(this.properties.Count == 2);
}

[Test]
public void GetMasterTablePropertiesCount()
{
IContainer container = this.GetContainer();
this.properties = container.GetProperties("MasterTable");
Assert.IsTrue(this.properties.Count == 8);
}

[Test]
public void GetNameLabelPropertiesCount()
{
IContainer container = this.GetContainer();
this.properties = container.GetProperties("NameLabel");
Assert.IsTrue(this.properties.Count == 4);
}

[Test]
public void GetUL()
{
IContainer container = this.GetContainer();
this.properties = container.GetProperties("UL");
Assert.IsTrue(this.properties.Count == 4);
}

[Test]
public void GetULFontWeight()
{
IContainer container = this.GetContainer();
this.properties = container.GetProperties("UL");
Assert.IsTrue(this.properties["font-weight"].Equals("bold"));
}
}
}

Abstract Factory Pattern



Purpose:
The Abstract Factory Pattern involves the following OO priciple: Depend upon abstractions, do not depend upon concerete classes.

Abstraction doesn't necessarily mean an Interface. It points to a super type. It could easily be an Abstract class.

Encapsulating Object Creation. But Why?
We all have been "newing" for a long time without worries. Now we say that its better to encapsulate object creation. How is it going to help us? We are "newing" anyways!

The problem is certainly not with "new". Its the change that brings all the change. We need a way to encapsulate all the related "newing" at a single place so that its not spread across the code. In this example, a NuclearOrdananceFactory(Factory) is entrusted with creating Bullets, Guns, and Bombs(Products). Whenever Russia or China(Client) needs to create Nuclear bombs it goes to the Factory. It simply creates relevant weapons. No hassles, no worries, no code duplication, and no maintenance nightmares.

Abstract. Why Abstract?
The Factory classes like NuclearOrdnanceFactory, and BiologicalOrdnanaceFactory implement an Abstract interface. This allows the Client to create a set of related products.

I am presenting to you the most common design pattern that is also widely mis-understood! I am trying to simplify things by giving you a code sample and a test fixture that explains the usage.

It involves three distinct participants: The Factory, The Product, and The Client. The following class diagram will help ease down things.



The following fixture explains the usage of the factory.



/*
*
* Date Created: 16-Sep-2008
* Author: SAHAY, Sanjeet, IDC
* Filename: AbstractFactoryFixture.cs
* Assembly: Sanjeet.Demos
* Project: Demos
* Purpose: The Abstract Factory Pattern involves the following OO priciple:
* Depend upon abstractions, do not depend upon concerete classes
*
*/

using NUnit.Framework;
using Sanjeet.Demos.Patterns.AbstractFactory.Client;
using Sanjeet.Demos.Patterns.AbstractFactory.Factory;

namespace Sanjeet.Demos
{
[TestFixture]
public class AbstractFactoryFixture
{
[Test]
public void CreateABiologicalCountry()
{
IOrdnanceFactory factory = new BiologicalOrdnanceFactory();
ICountry country = new China();
country.MakeWeapon(factory);
}

[Test]
public void CreateAChemicalCountry()
{
IOrdnanceFactory factory = new ChemicalOrdnanceFactory();
ICountry country = new UnitedStates();
country.MakeWeapon(factory);
}

[Test]
public void CreateANuclearCountry()
{
IOrdnanceFactory factory = new NuclearOrdnanceFactory();
ICountry country = new India();
country.MakeWeapon(factory);
}

[Test]
public void CreateAStrongCountry()
{
ICountry country = new India();
country.MakeWeapon(new NuclearOrdnanceFactory());
country.MakeWeapon(new ChemicalOrdnanceFactory());
country.MakeWeapon(new BiologicalOrdnanceFactory());
}
}
}


Cheers!

Tuesday 16 September 2008

Welcome

Welcome to my new blog.

As the name suggests its going to be technical. I have been keeping a lot busy these days. But I hope to spare some time now, and share my technical findings with you all.

My area of expertise is .Net and you may call me a OO purist. All these years I have tried my best to convince people around me that there is nothing like R.A.D.(Rapid Application Development). Its a myth. It ends up being S.C.A.D.(Slow and Cumbersome Application Development). I will explain you as we go along.

My earlier technical outbursts can be seen here.