0
我有以下的温莎城堡流利的配置代码...城堡温莎工厂在解决T型暗示<T>?
container
.AddFacility<TypedFactoryFacility>()
.Register(
Component
.For<IXxxCache>()
.ImplementedBy<AppFabricXxxCache>()
.Named("AppFabricXxxCache")
.LifeStyle.FromContext(),
Component
.For<IXxxCache>()
.ImplementedBy<DatabaseXxxCache>()
.Named("DatabaseXxxCache")
.LifeStyle.FromContext(),
Component
.For<IXxxCacheFactory>()
.ImplementedBy<XxxCacheFactory>()
.DependsOn(new {cacheName})
);
的XxxCacheFactory如下:...
public class XxxCacheFactory : IXxxCacheFactory
{
private readonly IWindsorContainer _container;
private readonly string _cacheName;
public XxxCacheFactory(IWindsorContainer container, string cacheName)
{
_container = container;
_cacheName = cacheName;
}
public IXxxCache Create()
{
try
{
var cache = new DataCacheFactory().GetCache(_cacheName);
return _container.Resolve<IXxxCache>("AppFabricXxxCache", new {cache});
}
catch (DataCacheException)
{
return _container.Resolve<IXxxCache>("DatabaseXxxCache");
}
}
public void Release(IXxxCache component)
{
_container.Release(component);
}
}
我可以用下面的代码这方面的工作。 ...
[Test]
public void database_xxx_cache_returned_when_cache_does_not_exist()
{
ConfigurationManager.AppSettings["CacheName"] = "this_cache_does_not_exist";
var container = Castle.WindsorContainerBootStrap.BootStrapContainerAndRunInstallers<SingletonLifestyleManager>();
var factory = container.Resolve<IXxxCacheFactory>();
var cache = factory.Create();
}
理想我想削减了工厂创造的一部分,只是有容器得到使用工厂类我有正确执行,像这样......
[Test]
public void database_xxx_cache_returned_when_cache_does_not_exist()
{
ConfigurationManager.AppSettings["CacheName"] = "this_cache_does_not_exist";
var container = Castle.WindsorContainerBootStrap.BootStrapContainerAndRunInstallers<SingletonLifestyleManager>();
var cache = container.Resolve<IXxxCache>();
}
这可能吗?如果是这样,我错过了什么?