可以通过Resolve <T>中的T类型暗示Castle Windsor Factory吗?</t>

时间:2012-08-14 18:38:31

标签: .net castle-windsor factory-pattern

我有以下城堡windsor流利的配置代码......

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>();
}

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

1 个答案:

答案 0 :(得分:0)

我的一位同事向我指出了Windsor的工厂方法支持

只需将此添加到我的安装程序即可使用...

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();
}