Avoid ‘Tall’ DAO Factories

software-engineering
Author

Humberto C Marchezi

Published

August 21, 2009

A “tall” DAO factory can be defined as a big class that contains too much methods for each business class that compounds your domain model.

public class DAOFactory
{
  IClass1DAO GetClass1DAO() { ... }
  IClass2DAO GetClass2DAO() { ... }
  IClass3DAO GetClass3DAO() { ... }
  IClass4DAO GetClass4DAO() { ... }
  IClass5DAO GetClass5DAO() { ... }
  IClass6DAO GetClass6DAO() { ... }
  IClass7DAO GetClass7DAO() { ... }
  IClass8DAO GetClass8DAO() { ... }
  IClass9DAO GetClass9DAO() { ... }
  IClass10DAO GetClass10DAO() { ... }
  : : : :
}

Besides big, these kind of class should be modified every time a new domain class is added to your system.
In order to avoid that to happen, one good option is to use a generic method for all DAO interfaces.

public class DAOFactory
{
  ICommonDAO GetDAO < I > ( ) where I : ICommonDAO { ... }
}

The action of searching the corresponding DAO interface implementation can be easily achieved by using .NET reflection support for Assemblies and Types.