我有一个 MixIn 需要某种状态才能运行。
我正在这样注册..
container.Register(Component.For(Of ICat) _
.ImplementedBy(Of Cat) _
.LifeStyle.Transient _
.Proxy.MixIns(New MyMixin()))
当我调用container.Resolve(ICat)时,我得到ICat的代理,它也实现了IMixin。
但是,如果我再次调用 container.Resolve(of ICat),我会获得 ICat 的新代理,但 MyMixin 是相同的实例。 (这是有道理的,因为我没有告诉容器任何创建 IMixin 的方法)
因此,IMixin 是一个单例,尽管组件的生活方式是瞬态的。
我如何通过 Fluent Interface 告诉 Windsor 为组件创建一个新的 MyMixIn 实例?
我想我解决了这个问题。
而不是使用代理.Mixin,我创建了一个自定义活化剂()
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"))
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)