Windsor MixIn is a Singleton?

前端 未结 3 563
庸人自扰
庸人自扰 2021-01-26 06:25

I have a MixIn that requires some state to operate.

I am registering it as so..

    container.Register(Component.For(Of ICat) _
                        .         


        
相关标签:
3条回答
  • 2021-01-26 07:07

    I think I resolved this.

    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)))
    

    And IMixin is registered as follows

    container.Register(Component.For(Of IMixin) _
                           .ImplementedBy(Of MyMixin) _
                           .LifeStyle.Is(Castle.Core.LifestyleType.Transient) _
                           .Named("MyMixin"))
    
    0 讨论(0)
  • 2021-01-26 07:09

    If you register the mixin with transcient lifestyle, it will create a new instance for each component:

    container.Register(
        Component.For<ICat>().ImplementedBy<Cat>()
            .LifestyleTransient()
            .Proxy.MixIns(m => m.Component("mixin")),
        Component.For<MyMixin>().LifestyleTransient().Named("mixin")
    );
    
    0 讨论(0)
  • 2021-01-26 07:11

    I'm not sure how it bubbles up to Windsor, but at DynamicProxy level, there's mixin Instance per proxy type. So if you're creating yourself mixin instances, you may be also each time be generating a new proxy type. To circumvent this, override Equals and GetHashCode in your mixed in type.

    I may however not be right, so you may want to make sure first.

    0 讨论(0)
提交回复
热议问题