AspectDNG ã使ã£ã¦ã¤ã³ã¿ã¼ãã§ã¼ã¹ã追å å®è£ ãã
.NET ç¨ã® AOP ãã¼ã«ã§ãã AspectDNG ã使ã£ã¦ãæå®ã®ã¯ã©ã¹ã«ã¤ã³ã¿ã¼ãã§ã¼ã¹ã追å å®è£ ããã
æé ã¯ä»¥ä¸ã®éã
- ã¯ã©ã¹ã¨ã¤ã³ã¿ã¼ãã§ã¼ã¹ã®ä½æ
- ã¢ã¹ãã¯ãã®å®ç¾©ã¨é©ç¨
- ãã¹ãã¯ã©ã¹ã®ä½æã¨å®è¡
ã¯ã©ã¹ã¨ã¤ã³ã¿ã¼ãã§ã¼ã¹ã®ä½æ
ã¢ã¹ãã¯ãã®é©ç¨å¯¾è±¡ã¨ããã¯ã©ã¹ã¨ã¢ã¹ãã¯ãã§è¿½å ããããã®ã¤ã³ã¿ã¼ãã§ã¼ã¹ãä½æãã
public class Data {}
public interface IChecker { void Check(int num, string msg); }
ããããã«ããã¦ã¢ã»ã³ããªãä½æãã¦ãã
>csc /t:library /out:commonlib.dll Data.cs IChecker.cs
ã¢ã¹ãã¯ãã®å®ç¾©ã¨é©ç¨
AspectDNG ã§ã¯ã«ã¹ã¿ã ã¢ããªãã¥ã¼ãã§ã¢ã¹ãã¯ããå®ç¾©ã§ããã¤ã³ã¿ã¼ãã§ã¼ã¹ã追å å®è£ ããã«ã¯ä»¥ä¸ã®ã«ã¹ã¿ã ã¢ããªãã¥ã¼ãã使ç¨ããäºã«ãªãã
- ImplementInterface ã§ã¤ã³ã¿ã¼ãã§ã¼ã¹ã追å
- Insert ã§ã¡ã½ããã®å®è£ ã追å
using System; using DotNetGuru.AspectDNG.Joinpoints; //Data ã¯ã©ã¹ã¸ã¤ã³ã¿ã¼ãã§ã¼ã¹ã追å ããå®ç¾© [ImplementInterface("Data")] public interface ICheckerAspect : IChecker { } //ã¢ã¹ãã¯ãã®å®ç¾©ã¯ã©ã¹ã //Check ã¡ã½ããã®ã·ã°ããã£ãåãã ãã§ã¯ //ã¦ã£ã¼ãå¾ã«ã¤ã³ã¿ã¼ãã§ã¼ã¹ã®å®è£ ã¨ãã¦èªèãããªããããªã®ã§ //IChecker ã¤ã³ã¿ã¼ãã§ã¼ã¹ãå®è£ ããããã«ãã¦ãã public class SimpleAspect : IChecker { //Data ã¯ã©ã¹ã¸ Check ã¡ã½ããã®å®è£ ã追å ããå®ç¾© [Insert("Data")] public void Check(int num, string msg) { Console.WriteLine("--- Check : {0}", this); } }
ããã§ãSimpleAspect ã¯ã©ã¹ã® IChecker ã¤ã³ã¿ã¼ãã§ã¼ã¹å®è£ 宣è¨ãå¤ãã¨ãå®è¡æã« System.TypeLoadExceptionï¼'Data' ã«ããã¡ã½ãã 'Check' ã«å®è£ ãå«ã¾ãã¦ãã¾ããï¼ãçºçãããã注æãå¿ è¦ã
ã¢ã¹ãã¯ãå®ç¾©ã¯ã©ã¹ããã«ããã¦ãäºåã«ä½æãã¦ãããã¢ã»ã³ããªã«ã¢ã¹ãã¯ããã¦ã£ã¼ãããã
>csc /t:library /out:aspects.dll /r:aspectdng.exe;commonlib.dll SimpleAspect.cs >aspectdng.exe commonlib.dll aspects.dll
aspectdng.exe ã®å®è¡ã§ commonlib.dll ãã¢ã¹ãã¯ãé©ç¨å¾ã®ã¢ã»ã³ããªã§ç½®æãããã
ãã¹ãã¯ã©ã¹ã®ä½æã¨å®è¡
Data ã¯ã©ã¹ã¸ IChecker ã¤ã³ã¿ã¼ãã§ã¼ã¹ã®å®è£ ã追å ããã¦ããäºã確èªããããã®ãã¹ãã¯ã©ã¹ä½æ
public class Tester { public static void Main(string[] args) { //Data ã¯ã©ã¹ã¸ IChecker ã¤ã³ã¿ã¼ãã§ã¼ã¹ã®å®è£ ã //追å ããã¦ããäºãç¢ºèª IChecker checker = new Data() as IChecker; checker.Check(100, "test"); } }
å®è¡çµæã¯ä»¥ä¸ã®ããã«ãªããã¤ã³ã¿ã¼ãã§ã¼ã¹ã®å®è£ ã追å ããã¦ããäºã確èªã§ããã
>csc /r:commonlib.dll;aspects.dll Tester.cs >Tester.exe --- Check : Data?