c# - How do I inject into base class with Castle Windsor? -


i have series of core services want configure castle windsor, things logging, caching, email config, etc. making these services configurable app.config change great boon (e.g. development/testing it's great able tell app route email traffic through other mechanism actual mail server).

two questions:

  1. many of classes need access these services inherit abstract base class (contains core logic used subclasses) seem ideal inject core services base class somehow children inherit references services. note these subclasses implement interface may better path go down?
  2. i have scenario unrelated objects in other assemblies need able tap core services. these objects not instantiated me other libraries (i'm implementing interface of 3rd party library uses implementation in framework). if need access email or logging or other core service in code, how reference?

i hope makes sense, thank you.

regarding first point, use property injection.

you have 2 choices injecting dependencies; via constructor or via properties. since don't want pass dependencies down constructor chain, other way via property injection. has advantage if base class need add/remove/change dependency, doesn't affect inherits it.

some folks (myself included) shy away property injection because makes dependencies non-obvious , can imply optional. can make unit testing (you're doing that, right?) difficult because have inspect class see dependencies needed. if in constructor, it'd obvious.

however, if can make sane null-object implementations of services are optional, or unit-testing implications don't phase you, route go down.


as second question, if can't control how class gets created, can't expect windsor supply of dependencies. @ best, can resolve dependencies individually (i.e. call container.resolve<iyourdependency>()) , assign them properties of implementation.


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -