Table of contents
Aspect Engine
The AspectEngine is the entry point to add AOP capabilities to your classes. Usually there is no point in just constructing an AspectEngine and start using it. You need one of the Builders to proper construct an AspectEngine with a configuration that depicts your intentions. For more information about builder, check the Builder Reference.
This is document is a WORK IN PROGRESS
The AspectEngine.Wrap method
Once you have your proper AspectEngine instance, use the Wrap method with a object instance to start all the magic. From now on let's call this instance the invocation target.
using System.Collections; AspectEngine engine = // ... IList list = engine.Wrap( new ArrayList() ) as IList;
Here is what happens in a nutshell:
- The engine will extract the inner type of instance
- This inner type is compared with the aspect definitions in the configuration
- If no match is found, the very same instance is returned
- If a match is found, the instance is wrapped with a proxy and the proxy is returned
You must note that the instance you've passed in the Wrap's argument is used by the proxy, so now you have a totally different instance to work with. See the caveats section to see it can be a problem for some cases.
The returned proxy is guaranteed to:
- Extend your type
- Override all virtual methods in your type a dispatch to the invocation target
- Implement all mixins interfaces if any
- Create all mixins instances that your aspect declare
- Dispatch invocations to the mixins to the proper mixin instance
Please note that for performance reasons we don't check for collisions in the types implemented. So, if your type exposes a Name property and one of the mixin have this same property on its interface you'll get a really nasty error.
Your mixins must have a default constructor or the engine won't be able to create the mixins instances.
The AspectEngine.WrapInterface method
This method, available since Alpha 2.0.0.4, allows you to specify the target type to be matched and the invocation target instance.
using System.Collections; AspectEngine engine = // ... IList list = engine.WrapInterface( typeof( IList ), new ArrayList() ) as IList;
Despite the fact that you may have the same result, Aspect# will chose a different path when generating the proxy for you. In this case you don't have to be afraid of non-virtual methods not being overrided, thus this is a safer strategy if you're dealing with type that you don't have control.
Also, if you have an aspect definition for a interface, instead of a concrete type, you may be forced to use the WrapInterface instead.