views:

73

answers:

2

I am using nHibernate. My classes are POCO, that map 1:1 with my database tables. I created a IGenericDAO -> GenericDAO that does all my basic CRUD. (repository) Each table has a DAO class, so:

public class UserDAO : GenericDAO

Any table specific queries will go in the tableDAO class.

I then have a factory, IDAOFactory.

public class NHibernateDAOFactory : IDAOFactory
{
   public static UserDAO GetUserDAO()
   {
        return new UserDAO();
   }
}

Possible improvement: 1. Just so my web application isn't littered with:

IDAOFactory dbFactory = new NHibernateDAOFactory();

I was thinking of creating:

public class DAOFactoryFactory
{
      public static IDAOFactory Load()
      {
         return new NHibernateDAOFactory();
      }
}

This way I have a single point of change in case I need to swap database layers. (i.e. switch between linq2sql and nhibernate, but not sure that is possible in reality)

Any suggestions/pointers on how to improve this, or is this pretty much solid?

+1  A: 

You might consider using a DI container instead of hoping that having only one place where you new the factory protects you from further changes. Also, calling a factory method Load is a bit confusing - why not call it CreateDAO instead?

Dmitri Nesteruk
A: 

Chances that you are going to be able to swap ORM implementations are slim to none. I would use IoC container (StructureMap). Define interfaces for your Dao's and then inject them in your services via ioc container.

epitka