DCSIMG
עמוד הבית| חבילות השירות שלנו| חומר חופשי| צור קשר
Unity and AOP - בלוג היועצים של מיקרוסופט ישראל

בלוג היועצים של מיקרוסופט ישראל

Unity and AOP

Unity and AOP:  InterfaceInterceptor Example.

One of the solution architecture design key decisions is the cross-cutting concerns handling.
When we look at many of these cross-cutting concerns, we can recognize a pattern. Many of them happen only at either the start or the end of a method:

  • Log when a method is called.
  • Checking user inputs for validity.
  • Handling exceptions.

This leads to a different approach to implementing cross-cutting concerns. We can put some special handling before or after method calls , get the code out of the methods  and enhance its maintainability.

The Unity 1.2 Interception extension lets us do just that.

Interception is a design pattern that is designed for cross-cutting concerns. Unity 1.2 provides support for interception through the Interception container.
The Unity Interception extension ships with two instance interceptors and one type interceptor:

  • TransparentProxyInterceptor
  • InterfaceInterceptor
  • VirtualMethodInterceptor

Selection of a specific interceptor depends on your specific needs, because each one has various tradeoffs.

Below is an example of using the InterfaceInterceptor which works very similar to the VirtualMethodInterceptor,but, in current case the Logger Class implements an ILogger Interface which makes the use of the InterfaceInterceptor possible for AOP.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using Microsoft.Practices.Unity;

using Microsoft.Practices.Unity.InterceptionExtension;

namespace UnityAOPDemo

{

class Program

{

static void Main(string[] args)

{

IUnityContainer container = new UnityContainer();

container.AddNewExtension<Interception>();

container.RegisterType<ILogger, Logger>(); container.AddNewExtension<Interception>();

container.Configure<Interception>().SetInterceptorFor<ILogger>(new InterfaceInterceptor());

var logger = container.Resolve<ILogger>();

logger.Write("Hello World...");

Console.ReadKey();

}

}

public interface ILogger

{

[Log]

void Write(string message);

}

public class Logger : ILogger

{

public void Write(string message)

{

Console.WriteLine(message);

}

}

public class LogAttribute : HandlerAttribute

{

public override ICallHandler CreateHandler(IUnityContainer container)

{

return new LogHandler();

}

}

public class LogHandler : ICallHandler

{

public int Order { get; set; }

public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)

{

Console.WriteLine("Log before..., ");

IMethodReturn msg = getNext()(input, getNext);

Console.WriteLine("Log after... ");

return msg;

}

}

}

תוכן התגובה

alikl כתב/ה:

Georgsa!

Great example - easy to bootstrap. Thanks for sharing.

One question though...

In order to make it all happen, do I need to initialize IUnityContainer for each method i need to wrap? If the answer yes, then I have hard to understand why it is AOP... I could easily write another line of code like this in the beginning of the method and at the end. To give you my understanding of AOP I use HttpModule for ASP.NET where no code intervention is needed at all - it is all configuration based making the call interception possible at runtime.

I am sure I am missing something here... what is it?

# November 8, 2008 3:22 AM

georgsa כתב/ה:

Hi Alikl

You are using unity container in a class level, in most cases, to implement IoC pattern. Interceptors are the natural extensions for support AOP

The concept in this example, is to build infrastructure to enable developers do not think about cross-cutting routines. When we using unity to resolve interface to its current implementation (in this example) you are getting this routines implemented depending on attributes in the interface. And, of course, this technique is not suitable every application.

# November 8, 2008 6:04 AM

Niraj כתב/ה:

Any idea how can this be done using Configuration without attributes. Also what happens in case Logger class throws an exception & I want to execute different code depending on whether exception is thrown or not.

# December 19, 2008 8:12 AM

nick_acelal כתב/ה:

# June 30, 2009 12:01 PM
שלח תגובה

(שדה חובה)  

(שדה חובה)  

(אופציונלי)

(שדה חובה) 

Please add 1 and 2 and type the answer here:


Enter the numbers above: