caSTLe-windsor - 温莎的通用类型工厂

标签 castle-windsor typed-factory-facility

也许从 Windsor 开始抽象类型工厂并不容易(2.5.3,如果重要的话),但无论如何我都必须这样做。 我正在尝试建立一个工厂,根据消息类型返回处理器。到目前为止,我已经从不同的地方清除了以下代码:

public class Complicato
{
    public static void Do(string[] args)
    {
        IKernel kernel = new DefaultKernel();
        IWindsorContainer container = new WindsorContainer();

        kernel.AddFacility<TypedFactoryFacility>();

        container.Install();
        container.Register(

            Component.For<HandlerSelector, ITypedFactoryComponentSelector>(),

            AllTypes.FromThisAssembly().BasedOn(typeof(ITrier<>))
                .WithService.Base().Configure(conf => conf.LifeStyle.Is(LifestyleType.Transient)),
            Component.For<Factor>(),
            Component.For<ITryFactory>().AsFactory(c => c.SelectedWith<HandlerSelector>()).LifeStyle.Singleton);

        var factor = container.Resolve<Factor>();
        var factory = container.Resolve<ITryFactory>();
    }
}

public class HandlerSelector : DefaultTypedFactoryComponentSelector
{
    protected override Type GetComponentType(MethodInfo method, object[] arguments)
    {
        return typeof(ITrier<>).MakeGenericType(arguments[0].GetType());
    }
}

public class Factor
{
    private ITryFactory factory;

    public void Try(IWhat onto)
    {
        factory.GetTrier(onto).Try(onto);
    }
}


public interface ITryFactory
{
    ITrier<IWhat> GetTrier(IWhat onto);
    void Release(object elem);
}


public interface IWhat { }


public interface ITrier<in TWhat> where TWhat : IWhat
{
    void Try(TWhat input);
}


public class TrierYes : ITrier<WhatYes>
{
    public void Try(WhatYes input) { Console.WriteLine("Yes? " + input.Aye()); }
}

public class TrierNo : ITrier<WhatNot>
{
    public void Try(WhatNot input) { Console.WriteLine("No? " + input.Naa()); }
}

public class WhatYes : IWhat
{
    public bool Aye() { return true; }
}

public class WhatNot : IWhat
{
    public bool Naa() { return false; }
}

这里的主要问题是 id 不起作用。首先我得到带有 null 工厂的 Factor,然后尝试显式解析工厂给我 ComponentActivator: Could not proxy Factories.Complex.ITryFactory 并带有拦截器 CaSTLe.TypedFactory.Interceptor 的内部消息无法解析和“Keys (components使用特定键) - CaSTLe.TypedFactory.Interceptor 未在容器中注册”。我什至不知道处理程序选择器是否有效,到目前为止还没有问题。

如果我使 ITrier 不再通用 - 它会突然开始工作,但这绝对不是我想要实现的目标。

那么我在 Windsor 配置中是否会犯一些愚蠢的初学者错误或误解类型工厂的想法?

为了完整起见,以下是异常消息:

Castle.MicroKernel.ComponentActivator.ComponentActivatorException was unhandled
  Message=ComponentActivator: could not proxy Factories.Complex.ITryFactory
  Source=Castle.Windsor
  StackTrace:
       at Castle.MicroKernel.ComponentActivator.DefaultComponentActivator.CreateInstance(CreationContext context, Object[] arguments, Type[] signature) in e:\OSS.Code\Castle.Windsor\src\Castle.Windsor\MicroKernel\ComponentActivator\DefaultComponentActivator.cs:line 166
  InnerException: Castle.MicroKernel.Resolvers.DependencyResolverException
       Message=The interceptor Castle.TypedFactory.Interceptor could not be resolved
       Source=Castle.Windsor
       StackTrace:
            at Castle.Core.InterceptorReference.Castle.MicroKernel.IReference<Castle.DynamicProxy.IInterceptor>.Resolve(IKernel kernel, CreationContext context) in e:\OSS.Code\Castle.Windsor\src\Castle.Windsor\Core\InterceptorReference.cs:line 142

最佳答案

获胜者是

container.AddFacility<TypedFactoryFacility>(); // good code

而不是

kernel.AddFacility<TypedFactoryFacility>(); // bad code

现在我只有未注入(inject)工厂和不正确的HandlerSelector的问题。

通过向 Factor 引入显式初始化构造函数来解决 NullReference。我不知道为什么我认为没有它就可以工作。

处理程序接口(interface)的最终版本如下:

public interface ITrier<out TWhat> where TWhat: IWhat
{
    void Try(IWhat input);
}

允许协变。不太优雅,因为需要不必要的 Actor 阵容,并且处理程序会放松其类型。但这就是残酷的现实。你要么是同变体,要么是逆变体。

关于caSTLe-windsor - 温莎的通用类型工厂,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13082695/

相关文章:

c# - CaSTLe DynamicProxy 拦截器可以更改参数值吗?

c# - 在不创建实现的情况下实现接口(interface)(动态代理?)

caSTLe-windsor - 温莎城堡 : Can I get all instances of a type?

caSTLe-windsor - 如何配置CaSTLe Windsor以根据提供给Resolve()的参数(“名称”除外)动态选择提供程序

caSTLe-windsor - 温莎城堡 : Using convention registration along with specific implementations

.net - 如何将拦截器添加到 CaSTLe Windsor 中的 Typed Factory Facility 工厂方法

c# - 温莎城堡 : Can a Typed Factory Facility pass a parameter down to one of the type's dependencies?

c# - 深层对象图中的动态依赖关系——我做错了什么?

caSTLe-windsor - CaSTLe Windsor 3 Interceptor 不发布由类型化工厂创建的组件,但 2.5.4 发布了。为什么?

caSTLe-windsor - 温莎城堡 : Set component dependencies on existing object