Hi Guys,
I'm relatively new to dependency injection, so I'm probably butchering the concept to some extent. However, I'm trying to achieve something like the following, but am unsure as to whether or not it's feasible:
Lets say I have two containers, each containing different instances of the same type of dependency. For example, each contains a different single instance of MyApplicationContext
and MyRequestContext
.
Now lets say I have several classes which depend on one (or both) of these instances. They shouldn't be concerned with which of the two containers they use; they simply require an instance of the dependency to get the job done.
In an ideal world, each of these dependable classes makes a static call in its constructor, which in turn reflectively injects the dependencies from an appropriate container...
public class MyDependableClass{
protected MyApplicationContext Application {get; set;}
protected MyRequestContext Request {get; set;}
public MyDependableClass() {
Dependencies.Inject(this);
}
}
However, AFAIK there's no practical way of determining an appropriate container. I've considered registering each object against a particular container (e.g. container.Register(obj);
), but this would be laborious and wouldn't work if dependencies were required in the constructor. Alternatively you could analyze the call stack to infer the container from a registered top-level object... wouldn't work for asynchronous calls, etc
Any ideas?
Example: I may have several classes which can depend on an instance of a proxy; let's call it ILogicProxy
. This proxy may forward calls to either local logic or remote logic on another machine. Furthermore, the application may establish connections with several remote machines. So... we have potentially multiple ILogicProxy
instances which need to be injected into several classes... but which one goes where? A solution like this could just use simple 'setter property injection', however this doesn't scale when more dependencies are required, since it will cause the 'wiring up' process to become messy/verbose.