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.


