AOPish stuff with Unity 2.0? Possible or not?

Dec 24, 2010 at 3:39 PM
Edited Dec 24, 2010 at 3:42 PM

Hello everyone and happy holidays. I'm wanting to add to our system two separate AOP type implmentations that I've implemented using PostSharp but would rather use Unity for a number of reasons. I'm wondering if this is currently possible in Unity 2.0 or not.

First I would simply like the developer to be able to create a class and by deriving it from a certain interface be able to specify certain logging attributes.


public class ClassA : ILoggingInterface


        [Log(EntryLevel = LogSeverity.Debug, ExitLevel = LogSeverity.Debug, ExceptionLevel = LogSeverity.Debug)]
public DoSomethingSpecial(string Parm1)
              // Does some stuff needed by the Developer

 This would log entry into the method, exit from the method and log any exception that occurred whilst in the method.

Secondly I'd like to do something like this to allow easy validation by the developer

public class ClassB : IValidationInterface
        [Required] [StringLength(50)]
        public string Name { get;  set }

This would ensure that the property when used was not null and that it didn't exceed 50 characters. (Note: I'd also like to be able to do this for parameters of methods as well.

If someone could let me know if chasing down this track is worth it or not and a brief few pointers that would help immensely.

Thanks in advance,

Dec 30, 2010 at 7:31 PM


Have you seen the Unity interception stuff and the policy injection block? This is triggered based on container configuration rather than deriving from an interface, but we have attributes that look a lot like what you're describing. You'll need to grab Enterprise Library 5.0 to get the full package. Unity provides the underlying interception mechanism, but Entlib provides the services (like logging and validation) that can be wired up via interception.

Also, note based on my terminology that Unity's "AOP"ish support is based on runtime interception rather than compile-time code weaving if that's a concern for you.

My suggestion would be to grab the Enterprise Library Hands on labs. They include a set of interception exercises which will walk you through the capabilities and show you what you can do. There's also a set of Unity Hands on labs included in the Entlib package which give more examples of the capabilities of the container.