AOP With Unity 2.0 - 3

by Daniel Lins Leite 3. May 2010 01:24

In the first post i showed a framework for Aspect-Oriented Programming ( AOP ) using only a proxy generator and a Unity 2.0. In this post i want to show the advances of the framework.

part 1 - http://www.machinaaurum.com.br/blog/post/AOP-With-Unity-20.aspx
part 2 - http://www.machinaaurum.com.br/blog/post/AOP-With-Unity-20-2.aspx

Before and After Properties

The framework now can intercept the get and the set method of a property, either before and after the execution and have some information of the original called property.

    interface IMoveOperation
    {
        int Version { get; set; }
        void Move(Account sourceAccount, Account destinationAccount, float quantity);
    }

To inject a aspect in this property, the Log aspect ( see part2 ), must implement the IPropertyInjection interface.

    class LogMethodInjection : IMethodInjection, IPropertyInjection
    {
        ILog Log { get; set; }
        string Prefix { get; set; }

        public LogMethodInjection(ILog log, string prefix)
        {
            Log = log;
            Prefix = prefix;
        }
       
        #region IMethodInjection Members

        void IMethodInjection.Before(MethodInfo methodInfo, object[] parameters)
        {
            Log.Source.TraceInformation(Prefix + " Before " + methodInfo.Name);
        }

        void IMethodInjection.After(MethodInfo methodInfo, object[] parameters)
        {
            Log.Source.TraceInformation(Prefix + " After " + methodInfo.Name);
        }

        #endregion

        #region IPropertyInjection Members

        void IPropertyInjection.BeforeGet(PropertyInfo propertyInfo)
        {
            Log.Source.TraceInformation(Prefix + " Before Get Property " + propertyInfo.Name);
        }

        void IPropertyInjection.AfterGet(PropertyInfo propertyInfo)
        {
            Log.Source.TraceInformation(Prefix + " After Get Property " + propertyInfo.Name);
        }

        void IPropertyInjection.BeforeSet(PropertyInfo propertyInfo)
        {
            Log.Source.TraceInformation(Prefix + " Before Set Property " + propertyInfo.Name);
        }

        void IPropertyInjection.AfterSet(PropertyInfo propertyInfo)
        {
            Log.Source.TraceInformation(Prefix + " After Set Property " + propertyInfo.Name);
        }

        #endregion
    }

I have to decorate the property in the concrete class.

    [Log("Class", Order = 1)]
    class MoveOperation : IMoveOperation
    {
        #region IAccountManager Members

        [Log("Method", Order = 2)]
        public void Move(Account sourceAccount, Account destinationAccount, float quantity)
        {
            sourceAccount.Quantity -= quantity;
            destinationAccount.Quantity += quantity;
        }

        [Log("Property", Order = 2)]
        public int Version { get; set; }

        #endregion
    }

And using the interface...

            {
                IMoveOperation operation = container.Resolve<IMoveOperation>();

                operation.Version = 1;

                operation.Move(a, b, 10.0f);
                operation.Move(a, b, 10.0f);
                operation.Move(a, b, 10.0f);

                Console.WriteLine(operation.Version);
            }

running this code i get the following result in the Outpu Window.

 

 

kick it on DotNetKicks.com

Tags: , , ,

Development

Comments (2) -

Christoph Austria
5/5/2010 11:44:58 PM #

Looks cool, unfortunately your Unity extension is built against the beta (?) and is thus not compatible with the final version of Unity, which ships with the final release of Enterprise Library 5.

Daniel Lins Leite Brazil
5/6/2010 2:55:43 AM #

True. I will change that.

Pingbacks and trackbacks (5)+


RecentComments

Comment RSS