msgbartop
Around Software Development and Innovation
msgbarbottom

27 Feb 11 AOP e Spring.NET

Riporto un pò di codice che ho scritto usando AOP (Aspect Oriented Programming) e Spring.NET.

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);

}
}
}

Dopo aver eseguito un metodo il mio compito è quello di loggare tutto ciò che avviene senza modificare i sorgenti delle classi coinvolte ne logging. Ecco l’aspetto del web.config che usa delle classi per AOP e Spring.NET:

<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>

Infine il solito unit test ;)

[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);

}

Bye

Antonio