How can I set up a Factory with possible suppliers?

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

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 a base interface for accessing IReadDao data:

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

      

I want this assembly to be generic and unreferenced. This is my basic data assembly.

Then I have a NHibernate provider assembly that implements the above IReadDao using NHibernate approach. This assembly references the "Framework.DataAccess" assembly.

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

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

      

Finally, I have an assembly "Company.DataAccess" that can override the default NHibernate implementation and references the assemblies seen previously.

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 write ...

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

      

... and then get the ProductDao implementation. But I can't keep in my underlying data access to any assembly-specific reference! My initial idea was to read this from the 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?

0


a source to share


1 answer


Got it.

I am using IWindsorInstaller to implement packages:

public class TestDaoInstaller : IWindsorInstaller
{
    public void Install(IWindsorContainer container, IConfigurationStore store)
    {
        container.Register(
            AllTypes.FromAssemblyNamed("Company.DataAccess")
                .BasedOn(typeof(IReadDao<>)).WithService.FromInterface(),
            AllTypes.FromAssemblyNamed("Framework.DataAccess.NHibernate")
                .BasedOn(typeof(IReadDao<>)).WithService.Base());
    }
}

      



And in Framework.DataAccess, my container is now:

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

                _daoContainer = new WindsorContainer(new XmlInterpreter());
                IWindsorInstaller[] daoInstallers = _daoContainer.ResolveAll<IWindsorInstaller>();
                foreach (IWindsorInstaller daoInstaller in daoInstallers)
                    _daoContainer.Install(daoInstaller); 

            }
        }

        return _daoContainer;
    }
}

      

0


a source







All Articles