インターフェイス (または実装) で傍受呼び出しハンドラーとハンドラー属性を使用しようとしています。インターフェイスに DoSomething() と DoSomethingElse() の 2 つのメソッドがあり、DoSomethingElse(); のインターセプターしかないとします。メイン インターフェイスを解決すると、DoSomethingElse() を呼び出さなくても、Call Handler のコンストラクターが呼び出されます。
Lazy(of IMainInterface) に解決してこれを試しましたが、関数 DoSomething() を呼び出した瞬間に、Call Handler がまだ不必要に作成されています。コードまたは構成によってこれが発生しないようにする方法はありますか。ここに私のサンプル実装があります
ハンドラ属性とコール ハンドラ
<AttributeUsage(AttributeTargets.Method)>
Public Class NormalSampleAttribute
Inherits HandlerAttribute
Public Sub New()
Console.WriteLine("Constructor of Normal Sample Attribute")
End Sub
Public Overrides Function CreateHandler(container As Microsoft.Practices.Unity.IUnityContainer) As Microsoft.Practices.Unity.InterceptionExtension.ICallHandler
Console.WriteLine("Create Handler")
Return New SampleCallHandler
End Function
End Class
Public Class SampleCallHandler
Implements ICallHandler
Public Sub New()
Console.WriteLine("Constructor of Sample Call handler")
End Sub
Public Function Invoke(input As IMethodInvocation, getNext As GetNextHandlerDelegate) As IMethodReturn Implements ICallHandler.Invoke
Console.WriteLine("Invoke of Sample Call handler - " & input.MethodBase.Name)
Return getNext.Invoke(input, getNext)
End Function
Public Property Order As Integer Implements ICallHandler.Order
End Class
インターフェイスと実装
Public Interface IMainInterface
Sub DoSomething()
<NormalSample()>
Sub DoSomethingElse()
End Interface
Public Class MainClass
Implements IMainInterface
Public Sub New()
Console.WriteLine("main class - Constructor")
End Sub
Public Sub DoSomething() Implements IMainInterface.DoSomething
Console.WriteLine("main class do something...")
End Sub
Public Sub DoSomethingElse() Implements IMainInterface.DoSomethingElse
Console.WriteLine("main class do something else...")
End Sub
End Class
メソッドを登録して実行するメインモジュール
Module Module1
Public container As IUnityContainer
Sub Main()
container = New UnityContainer
DoRegistrations()
Console.WriteLine("Before lazy Resolve")
Dim lmc As Lazy(Of IMainInterface) = container.Resolve(Of Lazy(Of IMainInterface))()
Console.WriteLine("Before making lazy function call")
lmc.Value.DoSomething()
Console.ReadLine()
End Sub
Sub DoRegistrations()
container.AddNewExtension(Of InterceptionExtension.Interception)()
container.RegisterType(Of IMainInterface, MainClass)()
container.Configure(Of Interception).SetDefaultInterceptorFor(Of IMainInterface)(New InterfaceInterceptor)
End Sub
End Module
次の出力が生成されます。
遅延解決
前 遅延関数呼び出しを行う前
メイン クラス - コンストラク
ター 通常のサンプル属性のコンストラクター
ハンドラーの作成サンプルのコンストラクター
ハンドラーの
メイン クラスが何かを行う...
DoSomethingElse() が呼び出されることはありませんが、ハンドラー作成のコストがすべてのフローに追加されます。これを回避する方法はありますか?どんな助けでも大歓迎です。
前もって感謝します!SV