views:

234

answers:

1

I am using Castle Windosr container. I want to be able to specify some constructor dependencies at runtime which you can obviously do by using a Resolve overload that takes a dictionary, all good and fine. However if I want to specify runtime dependency for a dependency of the root then I'm lost, at the moment I've worked around by explicitly creating each and bedding it in. Essentially its a decorator scenario and I want to get an instance of the decarator whilst providing a dependency at runtime for the object under decoration. Any ideas? I'd rather not have to do what I'm doing below and I'd rather not have the decarator constructor populate the object underneath since there will be times when the dependencies are not the same.

   public static IActivity GetActivityFromIoC(string key, Message message, Audit audit)
        {

            IActivity activity = IoC.Resolve<IActivity>(key, new Dictionary<
                                                                              string, object>(){
                { "message", message }
                });

            IActivity auditingActivity = IoC.Resolve<IActivity>("auditing.activity", new Dictionary<
                                                                              string, object>(){
            { "activity", activity },     
            { "message", message },
            { "audit", audit }

                });

            return auditingActivity;

        }
+1  A: 

You probably could handle this by writing your own ISubDependencyResolver implementation that would do that for you. The container alone does not allow that, and most likely it never will. Why? The short answer is - by doing that you make assumptions about you component's dependency dependencies, which is a no-no and container is all about removing this kind of knowledge from the caller.

Krzysztof Koźmic
I starred this at work so I could answer it at home, but you beat me to it :)
Mauricio Scheffer