PDA

View Full Version : Weaving aspect into inherted class


khurram
03-28-2006, 01:44 PM
I have a class which inherits from the base class; the instance of my class is returned from the factory class; the user interface layer is bound to the base class.

public class HelpdeskFunctionality {
public virtual List<HelpdeskCase> GetOpenedCases() { return null; }
//..Few more
}

internal class HelpdeskImplementation : HelpdeskFunctionality {
public override List<HelpdeskCase> GetOpenedCases() {
//..Implementation
}
//..Few more
}

I am trying to weave few aspects; using this

public class HelpdeskFactory {
public HelpdeskFunctionality GetHelpdeskFunctionality() {
Spring.Aop.Framework.ProxyFactory pf = new ProxyFactory(new HelpdeskImplementation());
pf.AddAdvice(new Aspects.LoggingAspect());
pf.AddAdvice(new Aspects.ExceptionAspect());
return (HelpdeskFunctionality)pf.GetProxy(); //Not casting
}
}

Problem is; the base class gets changed; and the user interface is not binding/working as expected; that is with the above code; I am breaking the things. Need suggestions/guidelines.

Bruno Baia
03-28-2006, 04:37 PM
hi,

Right now, AOP Proxy needs interface implemented objects to work.
So your pb is not due to the inherited class, but to the fact that your class 'HelpdeskFunctionality' doesn't implement any interfaces.

What you want to do will be supported with based inheritance proxy (Aleks can say more about it). The only thing you need to know is your target method will have to be virtual.


To make it work with the current implementation, you need to create an interface :

public interface IHelpdesk
{
List<HelpdeskCase> GetOpenedCases();

//..Few more
}

public class HelpdeskFunctionality : IHelpdesk { ...
]


Use the interface instead of the implementation :

public IHelpdesk GetHelpdeskFunctionality()
{
Spring.Aop.Framework.ProxyFactory pf = new ProxyFactory(new HelpdeskImplementation());
pf.AddAdvice(new Aspects.LoggingAspect());
pf.AddAdvice(new Aspects.ExceptionAspect());
return (IHelpdesk) pf.GetProxy(); // Cast should work
}


-Bruno

khurram
03-28-2006, 06:40 PM
The problem is; this is VS2005 project and in UI layer Object Data Source controls are used. The VS2005 designer doesnt likes interfaces; the base class is there to support the designer. An instance is retreived from the factory in ObjectDataSource_Creating event.