Castle Windsor组件活化剂的预期生活方式是什么? [英] What is the expected LifeStyle of a Castle Windsor component activator?

查看:126
本文介绍了Castle Windsor组件活化剂的预期生活方式是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using Castle Windsor and DynamicProxy to implement persistence Lazy Loading from scratch (I know NHibernate could be an option etc.) I have implemented a custom component activator to always instantiate my business classes as proxies. I found that the default mixin proxies automatically created when using interceptors were not being used when class methods are called from inside the class itself, which was a problem. So I inherited DefaultComponentActivator and overriding CreateInstance() I'm calling CreateClassProxy() to get a proxy that inherits from the business class, which in that respect works fine.

现在我期望我的这个 ProxyComponentActivator激活器仅由Castle实例化一次,但是将为每种类类型创建一个新实例。

Now I was expecting this 'ProxyComponentActivator' activator of mine to be instantiated by Castle only once, but a new instance is being created for each class type. Is that correct?

当前注册是这样的:

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

我也尝试过注册激活器,毫无用...

I have also tried to register the activator like this, to no avail...

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

在此先感谢您的帮助,
Luis

Thanks in advance for any help, Luis

推荐答案

温莎中的每个组件都会获得自己的激活器实例

Every component in Windsor will get its own activator instance

这篇关于Castle Windsor组件活化剂的预期生活方式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆