ユニティ依存性注入膨張容器のAOP

EntLib \ PIABユニティダイナミックエージェントを使用します

1  使用してシステムを。
2  使用してUnityの。
3  使用Unity.Interceptionと、
4  使用Unity.Interception.Interceptors.InstanceInterceptors.InterfaceInterceptionと、
5  使用Unity.Interception.PolicyInjection.Pipelineと、
6  使用Unity.Interception.PolicyInjection.Policiesと、
7  名前空間FrameworkConsole
 8  {
 9つの     パブリック クラスUnityAOP
 10      {
 11          公共 静的 ボイドショー()
 12          {
 13             コンテナ= IUnityContainer 新しい新 UnityContainer(); // 容器宣言
14              container.RegisterType <iBusiness、ビジネスを>(); // 登録iBusiness 
15              iBusinessバス= container.Resolve <iBusiness>(); // オブジェクトiBusinessゲット
16台              のバスを。 doSomethingの(); // 呼び出し
。17  
18は、              Console.WriteLineを(" ************************************************************ " );
 19              container.AddNewExtension <傍受> ();
 20である              コンテナ。 RegisterType <iBusiness、事業>()// 登録iBusiness
21は                  .configure <傍受>()// 構成切片
22は                  .SetInterceptorFor <iBusiness>(新しい新しい InterfaceInterceptor()); // 設定インターセプター
23は              バス= container.Resolve <iBusiness>(); // 検索オブジェクトiBusiness 
24台の              バス().DoSomething; //のに呼び出し
25              Console.Read();
 26である         }
 27          
28      }
 29 }
コードの表示

ビジネスレイヤ

1      #region业务
 2      [ExceptionHandlerAttribute(オーダー= 3 )]
 3      [LogHandlerAttribute(オーダー= 2 )]
 4      [AfterLogHandlerAttribute(注文= 5 )]
 5      パブリック インターフェースIBusiness
 6      {
 7          ボイドのdoSomething();
8      }
 9  
10      パブリック クラスビジネス:IBusiness
 11      {
 12          公共 ボイドのdoSomething()
 13          {
 14              Console.WriteLineを(doSomethingの);
 15          }
 16      }
 17      #endregionのサービス
 18である     * / 
19      
20である     InterfaceInterceptor:インターフェースのメソッドをマーキング、このインタフェースの継承されたメソッドは、このインタフェースのメソッドを実装することができるであろうように、傍受さ
 21      * /を
コードの表示

ユニティ拡張プロパティ

1     #region特性
 2  
3      パブリック クラスLogHandlerAttribute:HandlerAttribute
 4      {
 5          公共 オーバーライドICallHandlerのcreateHandler(IUnityContainer容器)
 6          {
 7              戻り 新しい LogHandler(){注文= この.ORDER}。
8          }
 9      }
 10  
11      パブリック クラスExceptionHandlerAttribute:HandlerAttribute
 12      {
 13          公共 オーバーライドICallHandlerのcreateHandler(IUnityContainer容器)
14          {
 15              リターン 新しい exceptionHandlerの(){注文= この.ORDER}。
16          }
 17      }
 18  
19      パブリック クラスAfterLogHandlerAttribute:HandlerAttribute
 20      {
 21          公共 オーバーライドICallHandlerのcreateHandler(IUnityContainer容器)
 22          {
 23              リターン 新しい AfterLogHandler(){注文= この.ORDER}。
24          }
 25      }
 26      #endregion特性
コードの表示

動作に対応する特性

1      #region対応する特徴的な挙動
 2  
3。     パブリック クラスLogHandler:ICallHandler
 4。     {
 5。         公共 INT注文{ GETSET ;}
 6。         ///  <まとめ> 
7。         ///  
8。         ///  </要約> 
9。         ///  < PARAM名= "入力"> メソッド呼び出しのパラメータのリスト</ PARAM> 
10          ///  <PARAM NAME = "getNextを"> </ PARAM> 
11          ///  <> </戻り>返し
12がある         公共IMethodReturnがinvoke(入力IMethodInvocation 、
GetNextHandlerDelegate getNextを) 13         {
 14              Console.WriteLineを(" 記録されたログ、メッセージ:{0}、CTIME:{} 1 "" LOGINFO " 、DateTime.Now);
 15              リターン getNextを()(INPUT、getNextを); // 最初のログイン後、次のステップ
16          }
 17      }
 18である 
19。 
20は、     パブリック クラスexceptionHandlerの:ICallHandler
 21は     、{
 22は         公衆 INT注文{ GETSETを;}
 23が         公衆起動IMethodReturn(IMethodInvocation入力、GetNextHandlerDelegate getNextを)
24          {
 25              IMethodReturn methodReturn = getNextを()(INPUT、getNextを); // 実行結果を確認するために、再度次のステップには、異常である
26である             IF(methodReturn.Exception == NULL 27              {
 28                  Console.WriteLineを(" 異常" );
 29              }
 30              他の
31はありません             {
 32                  Console.WriteLineを($ " 例外:methodReturn.Exception.Message {} " );
 33である             }
 34れる             戻りmethodReturnを、
 35         }
 36      }
 37  
38は、     パブリック クラスAfterLogHandler:ICallHandler
 39      {
 40          公共 のint注文{ GETSET ;}
 41が         公衆IMethodReturn呼び出し(IMethodInvocation入力、GetNextHandlerDelegate getNextを)
 42は         、{
 43で              IMethodReturn methodReturn = getNextを()(INPUT、getNextを); // 次のステップ、次いでバックログレコード完了するために、前の
44である              (Console.WriteLineを" :{0}、ctimeの完全なログ、メッセージ :{1}、 演算結果2} { "" AfterLog "、DateTime.Now、methodReturn.ReturnValue);
 45              リターンmethodReturn;
 46れる         }
 47      }
 48      #endregionに対応する特徴的な挙動
コードの表示

プログラムの実行を追跡するプログラムシーケンスを見つける:=>ビジネスメソッド=> =完全なログをログ>異常検出

プログラムの実行パイプラインのロシア人形パターンのMVCモデルに似ここで典型的なシーケンス

マイクロソフトのドキュメント:

System.Configuration  https://docs.microsoft.com/zh-cn/dotnet/api/system.configuration?view=netframework-4.8

IUnityContainer  https://docs.microsoft.com/zh-cn/previous-versions/msp-np/ee649880%28v%3dpandp.10%29

おすすめ

転載: www.cnblogs.com/Dewumu/p/11772733.html