Type 6 IOC, weaving the dependency 4
Everybody’s gone weavin’,
Weavin’ D.N.G. !
Life is a beach, let’s try a new meta aspect : ReplaceConstructorCall, and by the way, discover a new type of IOC, not constructor based, not setter based, everything is done by AspectDNG. Maybe this is not even IOC. Don’t care, it’s cool.
I’ve written this today, it’s some kind of proof of concept. It is in CVS now, but maybe it will evolve or change. IOC is one buzz word of today. In one of his (famous) article, in French, and in English, Sami Jaber describes 3 types of commonly used IOC, interface based, getter based, and constructor based. I have named this blog entry after this article on tss. Mainly because I’m not really serious. However, I think that AOP may “eclipse” IOC.
An example is coming.
The beginning, a very simple set of base classes :
<span class="rem">// base.cs</span>
<span class="kwrd">namespace</span> AspectDNG.Sample {
<span class="kwrd">public</span> <span class="kwrd">interface</span> IComplexSystem {
<span class="kwrd">void</span> DoTheWholeThing(<span class="kwrd">string</span> a);
}
<span class="kwrd">public</span> <span class="kwrd">class</span> MockSystem : IComplexSystem {
<span class="kwrd">public</span> MockSystem() {}
<span class="kwrd">public</span> <span class="kwrd">void</span> DoTheWholeThing(<span class="kwrd">string</span> a) {}
}
<span class="kwrd">public</span> <span class="kwrd">class</span> Pono {
<span class="kwrd">private</span> <span class="kwrd">string</span> m_a;
<span class="kwrd">private</span> IComplexSystem m_system = <span class="kwrd">new</span> MockSystem();
<span class="kwrd">public</span> Pono(<span class="kwrd">string</span> a) {
m_a = a;
}
<span class="kwrd">public</span> <span class="kwrd">void</span> TryTheThing() {
m_system.DoTheWholeThing(m_a);
}
}
<span class="kwrd">public</span> <span class="kwrd">class</span> EntryPoint {
<span class="kwrd">public</span> <span class="kwrd">static</span> <span class="kwrd">void</span> Main(<span class="kwrd">string</span>[] args) {
Pono p = <span class="kwrd">new</span> Pono(<span class="str">"HELLO IOC6 !"</span>);
p.TryTheThing();
}
}
}
What can we see there ? An interface, describing a complex system we want to use, then a Mock object, that may be avoided, but that is there for type safety, and for bookmarking. After this you find a plain old .net object, that depends on our complex system. The entry point creates the Pono object, and ask him to call its dependency. If you compile that and call it, nothing will happens. Let’s weave the dependencies ! Here is my aspect :
<span class="rem">// aspect.cs</span>
<span class="kwrd">namespace</span> AspectDNG.Sample {
<span class="kwrd">using</span> AspectDNG;
[Insert(<span class="str">"module:"</span>)]
<span class="kwrd">public</span> <span class="kwrd">class</span> RealComplexSystem : IComplexSystem {
[ReplaceConstructorCall(<span class="str">"* *.MockSystem::.ctor()"</span>)]
<span class="kwrd">public</span> RealComplexSystem() {}
<span class="kwrd">public</span> <span class="kwrd">void</span> DoTheWholeThing(<span class="kwrd">string</span> a) {
System.Console.WriteLine(a.ToLower());
}
}
}
This is a concrete implementation of my complex service. I use custom attributes to describe that I want RealComplexSystem to be
inserted in the main module of the target assembly, and I use my new ReplaceConstructorCall. It’s explicit, every call to the constructor
of MockSystem will be replaced by the constructor of RealComplexSystem. It rocks no ?If I execute this, my complex system will do its job :
D:/temporary/adng>AspectDNG -dw base.exe aspect.dll D:/temporary/adng>base hello ioc6 ! D:/temporary/adng>
So, what do you think about this ?
Trackbacks
Use the following link to trackback from your own site:
http://evain.net/blog/articles/trackback/4

Excellent!
Hi,
Good initiative. Good vibe. But, maybe so intrusive on the target code (custom attributes).
Another french blogger (Leo) has posted at this subject : http://hydrate.typepad.com/leo/.
The solution that is proposed is a mix between aspect programming, injection of dependance and dynamic proxy. The vibe is similar but more exciting (I’m agree with the fact that I’m not really objective) and promising (.NET Framework 2.0)
Permalink : “IoC like or Dynamic Proxy ?”
http://hydrate.typepad.com/leo/2004/12/what_is_this_.html
Yeah,
You’re right. The custom attributes are used from the aspect and not the target code.
It’s important to notice that AspectDNG as Indigo supports two ways to declare “behaviour” : custom attributes and xml. It’s a significant feature in order to cover (custom attributes?) development and (xml ?) integration requirements.
Why do I have an objection about using custom attributes in aspect ?
The aspect can be provided by a third party. So, it’s not judicious to choose a solution so intrusive. The third party must only “know” the interface of complex system (IComplexSystem).
Does (the use of xml with) AspectDNG require that the assembly that contains aspect is not strong named (i.e signed) ?
AOP : done
IOC : done
Dynamic Proxy : starting
Very interesting. I like Leo’s approach too as it uses more “conventional” generics.
It seems like lots of exciting new ideas are emerging around IoC lately.