Simple Example – AOP – Policy Injection with Unity

This is just a simple example explaining the processes/patterns to implement them using Unity 2.0, Dependency Inject, Inversion of Control (Ioc), and Aspect Oriented Programming (AoP). One of my primary goals is to not over complicate these examples. The examples should show how to do one thing and one thing only, but could possibly show how to implement these process/patterns in multiple way.

In this entry I will create a very simple application that shows how to do dependency injection, Interception and Policy Injection using Unity 2.0.

I’m using Unity 2.0 which is part of Enterprise Framework 5.0, and have created a very simple example application to test implementing AOP through Unity Interception. I’ve read a few articles on the web of how to use Unity with Interception and Policy Injection, and believe the articles from Dino Espisto were very benefical in help me figure this stuff out.
MSDN – Aspect-Oriented Programming, Interception and Unity 2.0
MSDN – Interceptors in Unity
MSDN – Policy Injection in Unity

If you are not aware of AOP, one of the primis is that Cross Cutting functionality, such as Loggin, should not be included in your domain classes.  My example contains a very simple console application that inlcudes using Interception that is configured through the Fluent API and a Configuration file.  The example also shows how to do Policy Injection and also configured using Fluent API and Configuration file.

For a contrived rule, emails should not be sent where the email address ends with “@Test.com”.  I have a class called EmailSerivce.  The EmailService is a fake, it doesn’t do anything other then sleep and print a message to the console.   I do not want to add any code to the EmailSerivce class that validates email; this should be handel by Unity Policy Injection.
Continue reading “Simple Example – AOP – Policy Injection with Unity”