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