AOP and Spring.NET
Today I wrote a nice code using AOP and Spring.NET.I used IOC facilities of Spring.NET and it is really fantastic. Implementing some interfaces and a little bit of code you can have great and flexible code.
This is an example of interface that you must implement:
using System;
using System.Reflection;
using Actvalue.Data.Interfaces;
using My.JobProfiler.Entities;
using Spring.Aop;
namespace My.AOP
{
public class CVLoggingAfterAdvice : IAfterReturningAdvice
{
public void AfterReturning(
object returnValue, MethodInfo method, object[] args, object target)
{
EasyCV.Entities.CVStatus st;
if (args.Length > 0)
st = (EasyCV.Entities.CVStatus)args[0];
else return;
IUnitOfWork UoW = DataAccessProviderFactory.GetConversationUnitOfWork();
CVHistory obj = new CVHistory();
obj.CV = UoW.GetByKey<Actvalue.JobProfiler.Entities.CVCore>(st.cvst_cvcr_id);
obj.Modified = DateTime.Now;
obj.Status_Id = st.cvst_cvtypologicalId_State;
obj.Type_Id = st.cvst_cvtypologicalId_Type;
obj.User = "rew";
UoW.RegisterNew(obj);
}
}
}
After running a method I logged everything there without change class (class that I call) that works. After in web.config I used IOC Spring facilities:
<spring>
<context>
<resource uri="config://spring/objects" />
</context>
<objects xmlns="http://www.springframework.net">
<description></description>
<object id="afterAdvice" type="My.AOP.CVLoggingAfterAdvice, Actvalue.AOP" />
<object id="throwsAdvice" type="My.AOP.CVLoggingThrowsAdvice, Actvalue.AOP" />
<object id="CVStatusChanged" type="Spring.Aop.Framework.ProxyFactoryObject">
<property name="Target">
<object type="My.DataAccessLayer.CVStatusDAL, My.DataAccessLayer" />
</property>
<property name="InterceptorNames">
<list>
<value>afterAdvice</value>
<value>throwsAdvice</value>
</list>
</property>
</object>
</objects>
</spring>
This is a little methos test for see if it works:
[NUnit.Framework.Test()]
public void CVHistoryTest1()
{
// Create AOP proxy using Spring.NET IoC container.
IApplicationContext ctx = ContextRegistry.GetContext();
ICVStatus command = (ICVStatus)ctx["CVStatusChanged"];
My.Entities.CVStatus obj=new CVStatus();
obj.cvst_cvcr_id = 23;
obj.cvst_cvtypologicalId_State = 588;
obj.cvst_cvtypologicalId_Type = 592;
obj.cvst_dateinserted = DateTime.Now;
obj.cvst_dateupdated = DateTime.Now;
obj.cvst_profcatassigned = 12111;
command.Update(obj);
}
I used more times AOP with Spring.NET and it is a great framework for AOP too (for IOC maybe is the best). Maybe in future I will write an article about AOP in Spring.NET...
Bye
Antonio