2009-03-24 8 views
0

私は、動作するためには何らかの状態が必要なMixInを持っています。ように私はそれを登録していますWindsor MixInはシングルトンですか?

は...

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

私は(ICATの)container.Resolveを呼び出す

は、私は戻ってもIMixinを実装ICATのプロキシを取得します。

しかし、container.Resolve(ICatの)をもう一度呼び出すと、ICatの新しいプロキシが取得されますが、MyMixinは同じインスタンスです。 (コンテナにIMixinを作成する方法を教えてくれなかったので意味があります)

IMixinは、コンポーネントのライフスタイルがTransientであっても、シングルトンです。

Fluent Interfaceでは、WindsorにコンポーネントのMyMixInの新しいインスタンスを作成する方法を教えてください。

答えて

0

私はこれを解決したと思います。

代わりProxy.Mixinsを使用して、私はカスタムアクティベーター()

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

私はWindsorにどのように泡立つか分かりませんが、DynamicProxyレベルでは、プロキシタイプごとにmixinインスタンスがあります。だからあなたがミックスインインスタンスを作成している場合は、新しいプロキシタイプを生成するたびにいるかもしれません。これを回避するには、混合型のEqualsとGetHashCodeをオーバーライドします。

しかし私は正しいとは限りませんので、最初に確認してください。

関連する問題