问题描述
我有三个组件:Framework.DataAccess,Framework.DataAccess.NHibernateProvider和Company.DataAccess。里面的组件Framework.DataAccess,我有我的厂(用错误执行发现的):
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>();
}
}
该组件还定义了基本接口进行数据访问IReadDao:
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.
然后,我有NHibernate的供应商的组件,它实现了使用NHibernate的做法上述IReadDao。该组件引用Framework.DataAccess组件。
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();
}
}
最后,我有Company.DataAccess组件,它可以覆盖NHibernate的供应商,同时引用previously看到组件的默认实现。
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>>();
...然后拿到ProductDao的实施。但我不能在我的基础数据获得持有任何提及具体的组件!我最初的想法是读取从XML配置文件。
... 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?
推荐答案
明白了。
我使用IWindsorInstaller实行包:
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());
}
}
而在Framework.DataAccess,我现在容器:
And at 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;
}
}
这篇关于如何配置与可能的供应商一个厂?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!