Tuesday, 13 March 2007

Factory pattern extended with generics

When I first encountered the Factory design pattern, I was intriged. I started reading about the abstract en method versions of the pattern. I immediatly disliked the method version because of the switch case statement.

This was obvious a violation of the open closed principle. The abstract version was beter.

But then Generics came to the scene and I was thinking of using them to build my own GenericDaoFactory to loose couple the client code from the instantiation of my data access objects

This is how I did it:

I started by creating an abstract DaoBaseClass with some standard mutual CRUD methods:

public abstract class DaoBaseClass <DTOTypeObj>

public abstract DTOTypeObj SelectRecord(DTOTypeObj obj);
public abstract void InsertRecord(DTOTypeObj obj);
public abstract void UpdateRecord(DTOTypeObj obj);
abstract void DeleteRecord(DTOTypeObj obj);


We pass in a Data Transfer Object. These objects just contain some state. Here is an example for the Customer DTO

public struct DTOCustomer

// Add all customer transfer properties
private int _customerNumber;
private string _customerAccount;
private int _id;

public int CustomerNumber

get { return _customerNumber; }
{_customerNumber = value; }


public string CustomerAccount

get { return _customerAccount; }
set { _customerAccount = value;



The DTO for the employee looks simular. Now let's use the DaoBaseClass and create a DaoCustomer class:

public class DaoCustomer : DaoBaseClass<DTOCustomer>

public override DTOCustomer SelectRecord(DTOCustomer obj) {

int customerId = obj.CustomerNumber;

return CustomerService.GetCustomer(obj.CustomerNumber);


public override void InsertRecord(DTOCustomer obj)




The other methods are left out to save some space.

What's next? Let's create the generic factory

public class GenericDaoFactory<T, DTO> where T:DaoBaseClass<DTO>, new()
private GenericDaoFactory(){}

public static T CreateDaoInstance()

T instance = new T();
return instance;



The factory is quit simple. We pass in the type of daoObject we want and the factory will create an instance for us. So we are working typesafe. We also have to pass in a DTO type with which the dao object will work.

The only thing we have to do now, is use the factory to create some Dao objects. Like this:

public void DoSomeStuffWithCustomersAndEmployees()

DaoCustomer daoCustomer =

GenericDaoFactory<DaoCustomer, DTOCustomer>.CreateDaoInstance();

DTOCustomer dtoCustomer = new DTOCustomer();
dtoCustomer.CustomerNumber = 10;
// Fill in some other data ...

DaoEmployee daoEmployee =
GenericDaoFactory<DaoEmployee, DTOEmployee>.CreateDaoInstance();

DTOEmployee dtoEmployee = new DTOEmployee();
dtoEmployee.EmployeeNumber = 5;


As we can see, we ask the factory to give us a DaoCustomer. It returns us the object and we can start using it. The same happens for the DaoEmployee object.

That's it

Comments are always welcome

Labels: , ,


Post a Comment

Subscribe to Post Comments [Atom]

<< Home