Combining DIY Dependency Injection with Aspect Oriented Programming

For an upcoming project, I plan on using both Dependency Injection and Aspect Oriented Programming. I'll be implementing this on my own by following the DIY dependency injection guide and using LOOM.Net for the AOP part.

A common pattern for creating an interlaced boolean class and aspect class is

AspectClass aspect = new AspectClass();
LogicClass logic = Weaver.Create<LogicClass>(aspect);

      

My intuition would be doing the weave in the glue code, for example. for classes ConcreteLogicA

implementing ILogicA

which depends on ILogicB

implementedConcreteLogicB

class MyInjector
{
    ...
    public ILogicA GetLogicA(AspectClass aspectToInterweave)
    {
        return Weaver.Create<ConcreteLogicA>(aspectToInterweave, GetLogicB(aspectToInterweave));
    }

    public ILogicB GetLogicB(AspectClass aspectToInterweave)
    {
        return Weaver.Create<ConcreteLogicB>(aspectToInterweave);
    }
    ...
}

      

Will this be a viable solution or not. The advantage is that I don't have to mix my logic with my aspects (which is admittedly kind of a key to AOP), but this way I add a little logic to the glue code.

+3


source to share


2 answers


As you start a new project, I urge you to take a closer look at the principles of SOLID and try to apply them to your new project. Once you've developed your application using the right abstractions and consistent with SOLID principles, it is unlikely that you ever need to use code weaving tools like LOOM and PostSharp. These tools are especially valuable if you are already in the unfortunate position that you have an outdated codebase that you cannot easily change.

Instead of using code weaving, let the design of your application guide you. When you are developing your application using the correct abstractions, it would be easy to add crosscutting concerns using decorators. You can find examples of systems that are built using the correct abstractions here and here .

These articles describe how you identify cross-cutting issues using decorators. When you design your system in this way, you can test your end-to-end implementation separately from the rest of your code. Something much more difficult when using weaving tools. It will be easy with the right abstractions.



In the last couple of years, I have consulted with several companies where I have taught developers how to properly apply a design pattern with greater flexibility and maintainability in mind. Their legacy codebases have reached a pretty daunting goal, but you seem to be in a good position to start a new project.

Use this as an opportunity to improve your design skills and allow your application to remain serviceable for years to come.

+3


source


I think your problem can be solved with a dependency injection container that supports dynamic interception (most of them are). This repository demonstrates the process of refactoring existing code towards aspects. It uses the Castle Windsor DI container. The advantage of implementing aspects with dynamic interception instead of IL Weaving is that you can easily inject dependencies into your aspects and you don't have to rely on any surrounding context / service locator.



0


source







All Articles