My background in strongly typed modern (at the time) C++ had always enforced reference (pointers) access via an interface (or pure virtual class) in conjunction with the pattern identified by Kevlin Henney called PFA (Parameterize From Above), complemented by my own personal hatred of the global singleton "don't know how to solve a problem so I'll just cheat" anti-pattern had made me subconsciously an advocate of DI. Like many of the GoF (Gang of Four) patterns named in the book Design Patterns : Elements of Reusable Object-Orientated Software I'd been using them for years, I just didn't know they had a name (remember all this book really did/does is allow us to talk about a pattern using the same language, the patterns are just a result of good design and are pretty obvious). Now I understood, now I could talk to my other developers using the same language.
Fast forward and now my main development language of choice is C# and safe in the knowledge that DI is nothing new, I continue reading to find out what specifically I didn't know. The gap in my own implementations can be termed the IoC Container, the mechanism for resolving the dependencies. Now strictly speaking you don't need to use a 3rd party IoC Container as you can do what I had been doing for years and just create your own for what you need and wire it all together yourself. Its not that difficult and sometimes that's all you need.
Note: Do not confuse the IoC Container with a service locator, the IoC Container is much better and the service locator goes against my opinions on a global top level singleton - The service locator is that singleton to bridge all singletons.
IoC Containers as supported by numerous frameworks provide at least 2 or 3 of the following features:
- Automatic Dependency resolution
- Lifetime management
- (optionally) File based configuration.
No comments:
Post a Comment