I've been working on creating my own IoC container for learning purposes. After asking a couple questions about them, I was shown that creating a factory to "resolve" the objects was the best solution (see third solution here). User Krzysztof Koźmic showed that Castle Windsor actually can implement this for you.
I've been reading the source of CW all morning. I know when Resolve is called, it "returns the interface". How does this interface "intercept" calls (since there is no implementation behind) and call it's own methods?
I know there's obviously some reflection trickery going on here and it's quite amazing. I'm just not at all user how the "interception" is done. I tried venturing down the rabbit hole myself on git, but I've gotten lost. If anyone could point me in the right direction it'd be much appreciated.
Also - Wouldn't creating a typed factory have the dependency on the container inside the calling code? In ASP.NET MVC terms, that's what it seems to me.
EDIT: Found Reflection.Emit... could this be what's used?
EDIT2: The more and more I look into this, the more complicated it sounds to automatically create factories. I might end up just sticking with the repetitive code.
MVC.net form post/anti forgery
1:MVC - Changing or adding a class for either 'Html.ValidationMessage' or 'Html.TextBox'
- Dependency injection merely instantiates an existing class this implements the interface. Jquery problem:validation code not working For example, you might have a MyServices class this implements IMyServices. How to make selectedItem in a SelectList be dynamic IoC frameworks commit you various ways to specify this when you ask for an IMyServices, it will resolve to an instance of MyServices. Master ASP.NET MVC There might be any IL Emit magic going on to set up the factory or helper methods, although the actual instances are simply classes you've defined.. How do I convert DateTime format to ShortDateString in Jquery?
- Mocking allows you to instantiate a class this implements an interface, without actually having to code this class. Why is VS2010 in ASP.NET MVC2 giving me the wrong JQuery intellisense? This does usually make use of Reflection and IL Emit, as you thought. Typically the emitted IL code is fairly simple, delegating the bulk of the job to methods written in C#. Most of the complexity of mocking has to did with specifying the behavior of the method itself, as the frameworks often allow you to specify behavior with a fluent syntax. Some, like Moles, simply let you specify a delegate to implement the method, though Moles must did other, crazier things like redirecting calls to static methods..
ImpromptuFactorythis provides the starting point for creating factories with a dynamic implementation based on the interface..