Implement Interface Without Creating Implementation (Dynamic Proxies?)


Implement Interface Without Creating Implementation (Dynamic Proxies?)



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.


Visual Studio 2010 RC + ASP.NET MVC 2 RTM won't re-target from .NET Framework 4 to 3.5

1:

MVC2 and Session Start Event
There are two separate concepts here:. Test Function in MVC Project
  1. Dependency injection merely instantiates an existing class this implements the interface. WebHost4Life host migrated my .NET MVC site and now membership functionality does not work For example, you might have a MyServices class this implements IMyServices. SSI-like feature in ASP.NET / ASP.NET MVC IoC frameworks commit you various ways to specify this when you ask for an IMyServices, it will resolve to an instance of MyServices. how do i load thousands of rows in my asp.net mvc project from database into slickgrid? 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.. HTML.DropDownList values from multiple sources?
  2. Mocking allows you to instantiate a class this implements an interface, without actually having to code this class. Authentication for IIS content in virtual directory under ASP.NET MVC website 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..
To elaborate a bit further, you don't actually need to use IL to implement IoC functionality, although this is often valuable to avoid the overhead of repeated Reflection calls, since Reflection is relatively expensive. Here is any information on what Castle Windsor is doing.. To answer your question, the most helpful place I found to start was the OpCodes class. This is a good summary of the available functionality in IL and how the OpCodes function. It's essentially a stack-based assembly language (no registers to worry around ), although strongly-typed and with first-class access to object symbols and concepts, like types, fields, and methods. Here is a good Code Project article introducing the basics of IL. If you're interested, I must also send you any helper classes I've created over the last few years this I use for my own Emit code..

2:

The typed factory is implemented using Castle DynamicProxy library. It generates a type on the fly this implements the interface and forwards all calls to this type, you make via the interface, to the interceptor.. It imposes no dependency in your code. The interface is created in your assembly, this you control, where you don't reference Windsor. In another assembly (entry point to the app) you tell Windsor around this interface and tell it to make it a factory and Windsor learns around your interface and does stuff with it. That's Inversion of Control in its glory.. It's actually not this complicated :).

3:

ImpromptuInterface creates DLR dynamic proxies based on interfaces. It allows you to have a dynamic implementation with a static interface. In fact it even has a baseclass ImpromptuFactory this provides the starting point for creating factories with a dynamic implementation based on the interface..


76 out of 100 based on 76 user ratings 826 reviews

@