Chad Myers asked:
How many, if any, of these kinds of statements should you see in your code:
ISomeService svc = ObjectFactory.GetObject<ISomeService>();
0? 1? Less than 12?
Later in the thread:
(Greg Young channelled Smashing Pumpkins when he said...)
> 0
So how do you kick the container off, like what Ayende said in the
Program.Main() call?
Quite simply I am working on a way to cheat. I am currently working on and I hope to have done + released in early January a mechanism for getting rid of constructors. I have some very basic tests passing now. Here is one, it might make things more clear.
A a = new A();
Assert.AreEqual(typeof(AProxy), a.GetType());
There are lots of reasons this can be good. Think about your IOC setups or for AOP if you use a dynamic proxy! My goal in taking on this project is not to make this a production system frankly a post build step is a bad (read: fragile) way of doing this. My hope in this is to show the power that comes from this and to then lobby the CLR or C# team to build this for me natively.
I have a lot of difficult problems to solve still (especially with parameters) but here are the basics so people can comment on it and keep me from making bad decisions.
I run as a post build step. I find all places in code where a constructor is called and map them to my own generic mega-factory including the parameters associated with the constructor call. This factory dispatches the call to a delegate handler. As of now this handler is strongly typed ...
Factory.Hook(new ConstructorOf<Foo>.WithSignature<string, int>.To( yourdelegate ))
The delegate signature would be Foo xxx(string, int)
If you have hooked a delegate to the constructor I will call your delegate anytime that the constructor is called as opposed to calling the constructor. You can in your delegate return a derived type etc (all without using factories).
If there is no handler registered I will generate a delegate dynamically that will just pass through and call the original constructor.
There are some really tough problems here, in particular because I am trying to make things completely transparent. As an example the following condition can happen.
private static void FooFactory(string _Param) {
if(_Param == "greg") return new FooProxy();
return new Foo();
}
In this case I want the new FooProxy() to actually call back into the factory but I can't let the new Foo() call come back to the factory (would produce infinite recursion). Since this is runtime configurable I also can't do this at compile time (not that I could anyways because you could always generate code etc that made my assumptions invalid). So what I am left with is needing to have my generic factory look at the call stack (or keep a call stack) in order to figure out if you are already in a call of the same signature in your current construction operation. I think I will write this part quickly tomorrow and throw it up on my blog and give some performance analysis.
Thoughts?