2012-08-14 52 views
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>(); 
} 

这可能吗?如果是这样,我错过了什么?

回答

0

我的一个同事向我指出在工厂方法支持温莎

刚刚添加该到我的安装程序得到它的工作...

container 
    .Register(
     Component 
      .For<DataCacheFactory>() 
      .ImplementedBy<DataCacheFactory>() 
      .LifeStyle.Singleton 
     , 
     Component 
      .For<IXxxCacheFactory>() 
      .ImplementedBy<XxxCacheFactory>() 
      .DependsOn(new {cacheName}) 
      .LifeStyle.Transient 
     , 
     Component 
      .For<IXxxCache>() 
      .UsingFactoryMethod(kernel => kernel.Resolve<IXxxCacheFactory>().Create()) 
     , 
     Component 
      .For<IXxxCache>() 
      .ImplementedBy<AppFabricXxxCache>() 
      .Named("AppFabricXxxCache") 
      .LifeStyle.FromContext() 
     , 
     Component 
      .For<IXxxCache>() 
      .ImplementedBy<DatabaseXxxCache>() 
      .Named("DatabaseXxxCache") 
      .LifeStyle.FromContext() 
    ); 

我也用的容器创建DataCacheFactory,因为这显然是一个昂贵的操作。所以现在的用法是......

[Test] 
public void database_xxx_cache_returned_when_cache_does_not_exist() 
{ 
    // ARRANGE 
    ConfigurationManager.AppSettings["CacheName"] = "this_cache_does_not_exist"; 
    var container = Castle.WindsorContainerBootStrap.BootStrapContainerAndRunInstallers<SingletonLifestyleManager>(); 

    // ACT 
    var cache = container.Resolve<IXxxCache>(); 

    // ASSERT 
    Assert.That(cache, Is.InstanceOf<DatabaseXxxCache>()); 

    // TIDYUP 
    container.Dispose(); 
}