【问题标题】:Windsor MixIn is a Singleton?Windsor MixIn 是单身人士?
【发布时间】:2010-10-15 06:07:56
【问题描述】:

我有一个 MixIn,它需要一些状态才能运行。

我是这样注册的..

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

当我调用 container.Resolve(of ICat) 时,我得到了一个 ICat 代理,它也实现了 IMixin。

但是,如果我再次调用 container.Resolve(of ICat),我将获得 ICat 的新代理,但 MyMixin 是相同的实例。 (这是有道理的,因为我没有告诉容器任何创建 IMixin 的方法)

所以,IMixin 是一个单例,即使组件的生活方式是瞬态的。

如何通过 Fluent 接口告诉 Windsor 为组件创建一个新的 MyMixIn 实例?

【问题讨论】:

    标签: castle-windsor aop mixins castle-dynamicproxy


    【解决方案1】:

    我想我解决了这个问题。

    我没有使用 Proxy.Mixins,而是创建了一个自定义 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
    

    所以现在,组件是这样注册的

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

    而IMixin注册如下

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

    【讨论】:

      【解决方案2】:

      我不确定它是如何冒泡到 Windsor 的,但在 DynamicProxy 级别,每个代理类型都有一个 mixin 实例。因此,如果您正在创建自己的 mixin 实例,您也可能每次都在生成新的代理类型。为了避免这种情况,请在您的混合类型中覆盖 Equals 和 GetHashCode。

      但我可能不正确,所以您可能需要先确认一下。

      【讨论】:

        【解决方案3】:

        如果你用transcient Lifestyle注册mixin,它会为每个组件创建一个新的实例:

        container.Register(
            Component.For<ICat>().ImplementedBy<Cat>()
                .LifestyleTransient()
                .Proxy.MixIns(m => m.Component("mixin")),
            Component.For<MyMixin>().LifestyleTransient().Named("mixin")
        );
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-03-30
          • 1970-01-01
          • 2011-01-01
          • 1970-01-01
          相关资源
          最近更新 更多