温莎MixIn是单身人士吗? [英] Windsor MixIn is a Singleton?

查看:100
本文介绍了温莎MixIn是单身人士吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要一些状态才能运行的MixIn.

I have a MixIn that requires some state to operate.

我正在注册它..

    container.Register(Component.For(Of ICat) _
                        .ImplementedBy(Of Cat) _
                        .LifeStyle.Transient _
                        .Proxy.MixIns(New MyMixin()))

当我调用ICat的container.Resolve时,我得到了ICat的代理,该代理也实现了IMixin.

When I call container.Resolve(of ICat), I get back a proxy for ICat, which also implements IMixin.

但是,如果我再次调用containerat.Resolve(ICat),则会获得ICat的新代理,但MyMixin是SAME实例. (这很有意义,因为我没有告诉容器创建IMixin的任何方法)

However, if I call container.Resolve(of ICat) again, I get a new proxy for ICat, but MyMixin is the SAME instance. (Which makes sense because I didn't tell the container any way to create IMixin)

因此,即使该组件的生活方式为瞬态,IMixin还是一个Singleton.

So, IMixin is a Singleton, even though the Component's lifestyle is Transient.

如何通过Fluent Interface告诉Windsor为该组件创建MyMixIn的新实例?

How can I tell Windsor, though the Fluent Interface, to create a new Instance of MyMixIn for the component?

推荐答案

我想我已经解决了.

我没有使用 Proxy.Mixins ,而是创建了一个自定义的 Activator ()

Instead of using Proxy.Mixins, I created a custom Activator()

Public Class MixInActivator(Of T)
   Inherits Castle.MicroKernel.ComponentActivator.DefaultComponentActivator

  Public Sub New(ByVal model As Castle.Core.ComponentModel, ByVal kernel As Castle.MicroKernel.IKernel, ByVal OnCreation As Castle.MicroKernel.ComponentInstanceDelegate, ByVal OnDestruction As Castle.MicroKernel.ComponentInstanceDelegate)
    MyBase.New(model, kernel, OnCreation, OnDestruction)
  End Sub

  Protected Overrides Function InternalCreate(ByVal context As Castle.MicroKernel.CreationContext) As Object

    Dim obj As Object = MyBase.InternalCreate(context)
    If GetType(T).IsAssignableFrom(obj.GetType) = False Then
        Dim options As New Castle.DynamicProxy.ProxyGenerationOptions
        Dim gen As New Castle.DynamicProxy.ProxyGenerator
        options.AddMixinInstance(Kernel.Resolve(Of T))
        obj = gen.CreateInterfaceProxyWithTarget(Model.Service, obj, options)
    End If
    Return obj
 End Function
End Class

现在,该组件已按以下方式注册

So now, the component is registered like this

 container.Register(Component.For(Of ICat) _
                     .ImplementedBy(Of Cat) _
                     .LifeStyle.Is(Castle.Core.LifestyleType.Transient) _
                     .Activator(Of MixInActivator(Of IMixin)))

IMixin的注册方式如下

And IMixin is registered as follows

container.Register(Component.For(Of IMixin) _
                       .ImplementedBy(Of MyMixin) _
                       .LifeStyle.Is(Castle.Core.LifestyleType.Transient) _
                       .Named("MyMixin"))

这篇关于温莎MixIn是单身人士吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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