0

私は次の城のウィンザー流暢な設定コードを持っています...

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

これは可能ですか?もしそうなら、私は何が欠けていますか?

4

1 に答える 1

0

同僚の1人が、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();
}
于 2012-08-15T12:06:46.213 に答える