Castle Windsor组分活化剂的预期LifeStyle是什么?

时间:2013-02-21 15:34:31

标签: proxy castle-windsor castle activator

我正在使用Castle Windsor和DynamicProxy从头开始实现持久性延迟加载(我知道NHibernate可能是一个选项等)。我已经实现了一个自定义组件激活器来始终将我的业务类实例化为代理。我发现当从类本身内部调用类方法时,没有使用使用拦截器时自动创建的默认mixin代理,这是一个问题。所以我继承了DefaultComponentActivator并重写了CreateInstance()我正在调用CreateClassProxy()来获取从业务类继承的代理,在这方面工作正常。

现在我期待我的这个'ProxyComponentActivator'激活器只被Castle实例化一次,但是为每个类类型创建了一个新实例。这是对的吗?

目前的注册是这样的:

public void Install(
   IWindsorContainer container,
   Castle.MicroKernel.SubSystems.Configuration.IConfigurationStore store) {
    container.Register( 
        Classes
            .FromAssemblyContaining(typeof(OneOfMyBusinessClasses))
            .InNamespace(typeof(OneOfMyBusinessClasses).Namespace)
            .WithService.DefaultInterfaces()
            .Configure(reg => reg.Activator<ProxyComponentActivator>())
            .LifestyleTransient(),
        etc.
    );
);

激活器实现如下:

public class ProxyComponentActivator : DefaultComponentActivator
{
    protected Castle.DynamicProxy.ProxyGenerator ProxyGenerator { get; set; }
    protected PersistenceInterceptor PersistenceInterceptor { get; set; }


    public ProxyComponentActivator(ComponentModel model, Castle.MicroKernel.IKernelInternal kernel, ComponentInstanceDelegate onCreation, ComponentInstanceDelegate onDestruction)
        : base(model, kernel, onCreation, onDestruction)
    {
        this.ProxyGenerator = kernel.Resolve<Castle.DynamicProxy.ProxyGenerator>();
        this.PersistenceInterceptor = kernel.Resolve<PersistenceInterceptor>();
    }


    protected override object CreateInstance(CreationContext context, ConstructorCandidate constructor, object[] arguments) //, Type[] signature)
    {
        object instance;

        Type implType = this.Model.Implementation;

        ProxyGenerationOptions p = new ProxyGenerationOptions();

        IPersistent ip = new Persistent();
        p.AddMixinInstance(ip);

        try
        {          
            instance = this.ProxyGenerator.CreateClassProxy(implType, null, p, arguments, this.PersistenceInterceptor);                
        }
        catch
        {
            throw new ComponentActivatorException("ComponentActivator: could not proxy " + implType.FullName, Model);
        }

        return instance;
    }
}

我也曾试图像这样注册激活剂,但无济于事......

Component.For<ProxyComponentActivator>()
.ImplementedBy<ProxyComponentActivator>()
.LifestyleSingleton()

提前感谢您的帮助, 路易斯

1 个答案:

答案 0 :(得分:1)

Windsor中的每个组件都将获得自己的激活器实例