How can I configure a Factory with the possible providers?

Posted by Jonathas Costa on Stack Overflow See other posts from Stack Overflow or by Jonathas Costa
Published on 2010-04-14T14:36:05Z Indexed on 2010/04/14 16:33 UTC
Read the original article Hit count: 312

Filed under:
|
|

I have three assemblies: "Framework.DataAccess", "Framework.DataAccess.NHibernateProvider" and "Company.DataAccess". Inside the assembly "Framework.DataAccess", I have my factory (with the wrong implementation of discovery):

public class DaoFactory 
{
    private static readonly object locker = new object();
    private static IWindsorContainer _daoContainer;

    protected static IWindsorContainer DaoContainer
    {
        get
        {
            if (_daoContainer == null)
            {
                lock (locker)
                {
                    if (_daoContainer != null)
                        return _daoContainer;

                    _daoContainer = new WindsorContainer(new XmlInterpreter());

                    // THIS IS WRONG! THIS ASSEMBLY CANNOT KNOW ABOUT SPECIALIZATIONS!
                    _daoContainer.Register(
                        AllTypes.FromAssemblyNamed("Company.DataAccess")
                            .BasedOn(typeof(IReadDao<>)).WithService.FromInterface(),
                        AllTypes.FromAssemblyNamed("Framework.DataAccess.NHibernateProvider")
                            .BasedOn(typeof(IReadDao<>)).WithService.Base());                        
                }
            }

            return _daoContainer;
        }
    }

    public static T Create<T>()
        where T : IDao
    {
        return DaoContainer.Resolve<T>();
    }
}

This assembly also defines the base interface for data access IReadDao:

public interface IReadDao<T>
{
    IEnumerable<T> GetAll();
}

I want to keep this assembly generic and with no references. This is my base data access assembly.

Then I have the NHibernate provider's assembly, which implements the above IReadDao using NHibernate's approach. This assembly references the "Framework.DataAccess" assembly.

public class NHibernateDao<T> : IReadDao<T>
{
    public NHibernateDao()
    {
    }

    public virtual IEnumerable<T> GetAll()
    {
        throw new NotImplementedException();
    }
}

At last, I have the "Company.DataAccess" assembly, which can override the default implementation of NHibernate provider and references both previously seen assemblies.

public interface IProductDao : IReadDao<Product> 
{
    Product GetByName(string name);
}

public class ProductDao : NHibernateDao<Product>, IProductDao
{
    public override IEnumerable<Product> GetAll()
    {
        throw new NotImplementedException("new one!");
    }

    public Product GetByName(string name)
    {
        throw new NotImplementedException();
    }
}

I want to be able to write...

IRead<Product> dao = DaoFactory.Create<IRead<Product>>();

... and then get the ProductDao implementation. But I can't hold inside my base data access any reference to specific assemblies! My initial idea was to read that from a xml config file.

So, my question is: How can I externally configure this factory to use a specific provider as my default implementation and my client implementation?

© Stack Overflow or respective owner

Related posts about castle-windsor

Related posts about ioc