What I'm trying to achieve is AOP through attributes using Castle Windsor interceptors. I had some success, but I had problems with aspects at the class level and method level.
- If I use only class attributes, all methods will be intercepted.
- If I use only method level attributes, these methods will be intercepted.
- If I add a class level attribute and a method level attribute, both intercepts will be performed on methods that were attributed, but those that were not will not be intercepted.
So this component:
public interface IMyComponent
{
void ShouldBeInterceptedByStopWatch_AND_ExceptionLogger();
void ShouldBeInterceptedByExceptionLogger();
}
[LogExceptionAspect]
public class MyComponent : IMyComponent
{
[StopwatchAspect]
public void ShouldBeInterceptedByStopWatch_AND_ExceptionLogger()
{
for (var i = 0; i < 2; i++) Thread.Sleep(1000);
}
public void ShouldBeInterceptedByExceptionLogger()
{
throw new NotImplementedException();
}
}
, ShouldBeInterceptedByExceptionLogger(), , ShouldBeInterceptedByExceptionLogger(). ShouldBeInterceptedByStopWatch_AND_ExceptionLogger() .
- https://bitbucket.org/jayala/juggernet-aop
, , , IContributeComponentModelConstruction, , , +, .
:
var container = new WindsorContainer()
.AddFacility<LogExceptionAspectFacility>()
.AddFacility<StopwatchAspectFacility>()
.Register(Component
.For<IMyComponent>()
.ImplementedBy<MyComponent>()
.LifeStyle.Transient);
, ,
public abstract class BaseAspectFacility<TAspectAttribute, TAspectInterceptor> : AbstractFacility
where TAspectAttribute : Attribute
where TAspectInterceptor : IInterceptor
{
protected override void Init()
{
var interceptorName = Guid.NewGuid().ToString();
Kernel.Register(Component.For<IInterceptor>()
.Named(interceptorName)
.ImplementedBy<TAspectInterceptor>()
.LifeStyle.Singleton);
var contributor = new ContributeAspectToModel<TAspectAttribute>(interceptorName);
Kernel.ComponentModelBuilder.AddContributor(contributor);
}
}