AOP و پردازش فراخوانیهای تو در تو
نویسنده: مهتدی حسن پور
تاریخ: ۱۳۹۲/۰۸/۲۸ ۱۳:۱۵
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
public interface IMyService
{
void foo();
void bar();
}
public class MyService : IMyService
{
public void foo()
{
Console.Write("foo");
bar();
}
public void bar()
{
Console.Write("bar");
}
} //using Castle.DynamicProxy;
public class Interceptor : IInterceptor
{
public void Intercept(IInvocation invocation)
{
Console.WriteLine("Intercepted: " + invocation.Method.Name);
invocation.Proceed();
}
} //using System;
//using Castle.DynamicProxy;
//using StructureMap;
class Program
{
static void Main(string[] args)
{
ObjectFactory.Initialize(x =>
{
var dynamicProxy = new ProxyGenerator();
x.For<IMyService>()
.EnrichAllWith(myTypeInterface =>
dynamicProxy.CreateInterfaceProxyWithTarget(myTypeInterface, new Intercept()))
.Use<MyService>();
});
var myService = ObjectFactory.GetInstance<IMyService>();
myService.foo();
}
} Intercepted foo foo Intercepted bar bar
Intercepted foo foo bar
public class MyService : IMyService
{
public virtual void foo()
{
Console.Write("foo");
bar();
}
public virtual void bar()
{
Console.Write("foo");
bar();
}
} // جایگزین روش پیشین در متد Main
x.For<IMyService>()
.EnrichAllWith(myTypeInterface => dynamicProxy.CreateClassProxy<MyService>(new Intercept()))
.Use<MyService>();