1

I have a series of core services that I want to configure with Castle Windsor, things like Logging, Caching, Email config, etc. Making these services easily configurable by an app.config change would be a great boon (e.g. even just for development/testing it's great to be able to tell the app to route all the email traffic through some other mechanism than the actual mail server).

Two questions:

  1. Many of the classes that need access to these services all inherit from an abstract base class (contains core logic used by all subclasses) so it would seem ideal to inject the core services into this base class somehow so that all the children would inherit the references to the services. Note these subclasses also all implement an Interface so that may be the better path to go down?
  2. I also have a scenario where unrelated objects in other assemblies also need to be able to tap into the core services. These objects are not instantiated by me but by other libraries (I'm implementing the interface of some 3rd party library that then uses my implementation in its framework). If I need access to email or logging or some other core service in this code, how do I get a reference?

I hope that makes sense, thank you.

Grudge
  • 35
  • 4
  • Create [another question](http://meta.stackexchange.com/questions/39223/one-post-with-multiple-questions-or-multiple-posts) for the second bullet, and add some more details. Do your implementations of the interfaces need the core services, or do the objects in the other assemblies need them? Some example code would likely help. – Patrick Quirk May 12 '15 at 13:22
  • @Patrick Quirk - I kept the 2 questions together because I suspect they might be solved similarly. To clarify the question though, it is the implementation of the interface that requires the core services. Basically my implementation is registered as a listener that gets used in a 3rd party assembly so I have no control over how it gets invoked, etc. Will the property injection method work here as well? In other words can I tell Windsor to always inject properties x, y and z into any instance of my type? Thanks – Grudge May 12 '15 at 13:49
  • I added a bit more to my answer based on your comment – Patrick Quirk May 12 '15 at 18:14
  • Please state your questions more clear and please read this [article](http://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/). – Ognyan Dimitrov May 13 '15 at 08:36

1 Answers1

2

Regarding your first point, use property injection.

You have two choices for injecting dependencies; via the constructor or via properties. Since you don't want to pass dependencies down the constructor chain, the only other way is via property injection. This has the advantage that if a base class need to add/remove/change a dependency, it doesn't affect everything that inherits from it.

Some folks (myself included) shy away from property injection because it makes dependencies non-obvious and can imply that they are optional. This can make unit testing (you're doing that, right?) difficult because you have to inspect the class to see what dependencies are needed. If they were in the constructor, it'd be obvious.

However, if you can make sane null-object implementations of your services so that they are optional, or the unit-testing implications don't phase you, then this is a good route to go down.


As to your second question, if you can't control how the class gets created, you can't expect Windsor to supply any of its dependencies. At best, you can resolve the dependencies individually (i.e. call container.Resolve<IYourDependency>()) and assign them to the properties of your implementation.

Patrick Quirk
  • 23,334
  • 2
  • 57
  • 88
  • Thanks for the reply, what exactly do you mean by "At best, you can resolve the dependencies individually and assign them to the properties of your implementation." As I understand it, you're not supposed to ever reference the container other than at initial creation/config. Is there someway to instruct the container at this step that it should inject my dependencies into an object of given type? I'm sorry, I'm confused – Grudge May 13 '15 at 14:03
  • You're right, in general you shouldn't directly use the container. However if this 3rd party library creates your object instead of the container creating it, you don't have much of a choice. [Windsor can't inject properties into an already created object](http://stackoverflow.com/questions/851940/windsor-castle-injecting-properties-of-constructed-object), so you have to do it yourself. – Patrick Quirk May 13 '15 at 14:21