What exactly does that mean?
At the heart of the matter IoC is all about loosely coupled software components.Class1 has a member of type Class2 which is specified at design time.
Class1 has a member of type Interface1, and Class2 is an implementation of Interface1.
What is the difference between these two approaches as presented above?
The second example clearly involves more code? One might think then that in fact the first approach is better, or even that it is "cleaner"; it is quicker to write, it is simple to understand, and it results in fewer source files.
The following Group class is an implementation of the first example.
public class Boy { public string GivenName { get; set; } } public class Girl { public string GivenName { get; set; } } public class Group { public Dictionary<string, Boy> Boys { get; private set; } public Dictionary<string, Girl> Girls { get; private set; } public void AddBoy(Boy boy) { /* ... */ } public void AddGirl(Girl girl) { /* ... */ } }
...and this Group class is an implementation of the second example.
public interface IPerson { string GivenName { get; set; } } public class Boy : IPerson { public string GivenName { get; set; } } public class Girl : IPerson { public string GivenName { get; set; } } public class Group { public Dictionary<string, IPerson> People { get; private set; } public void AddPerson(IPerson person) { /* ... */ } }
As you can see there is not actually any more code required to implement the second approach, however this (second) approach does enable us to create a new type to be included in our Group object without ever needing to modify that Group definition -which would not be possible if we were to take the first approach; indeed, taking the first approach would mean a complete re-compile. As an aside; how would we enumerate the Group from the first implementation? Compare this with how straight forward it is to achieve with the second implementation.
public class Man { public string GivenName { get; set; } }
This is basic OO right? It demonstrates the taxonomic relationships between, and the separation of the concepts of, a person and the individual types of persons; and is not really anything to do with IoC.
The thing is however, if we had not modeled our domain in such a manner (second approach) it would be more difficult to apply the IoC concept (hmmm? IoC is really a concept rather than a pattern -Dependency Injection and Service Locator are IoC patterns)
Now back to our discussion of IoC: Inversion of Control is often mistakenly believed to be the ability to change the run-time implementation of some loosely coupled components. I have stated "mistakenly believed" here because this is really a side-effect of implementing our software whilst adhering to the patterns utilized in applying Inversion of Control.
Eh? Let me explain ... as you can see from the example code; because the Girl, Boy and Man objects are implementations of the IPerson interface they are completely interchangeable, that is; wherever I have cause to use an IPerson I could in actual fact be using any of the concrete implementations thereof.
This author does not mean to imply that one must use interfaces in order to apply Inversion of Control, for example we could have provided
an abstract Person class to encapsulate the functionality of Name, Age, DateOfBirth, etc.; however it must be stated that passing objects around
by interface is considered to be the preferred pattern.
We should also never consider concepts or patterns in isolation because they are always the constituent parts of a (sometimes much) bigger picture, and therefore we also need to consider other maxims: KISS, YAGNI, SOLID. Any decent engineer will be able to immediately tell you that we are of course primarily concerned here with the I and D of our SOLID principles; though in applying Inversion of Control we completely adhere to SOLID.
With the application of Inversion of Control the decision of what concrete type is used at run-time is taken
away from the design time implementer. And this is the crux of IoC: the
client does not care where the object comes from and how it is created; all that the client knows is that there is
an object which is required in order to complete a series of instructions and something else is responsible for the lifetime of that object; the client simply
needs an instance. It could be said therefore that it is the control of the lifetime of an object upon which a client is
dependent that has been inverted, which is whythis concept is also known as Dependency Inversion.
We should also never consider concepts or patterns in isolation because they are always the constituent parts of a (sometimes much) bigger picture, and therefore we also need to consider other maxims: KISS, YAGNI, SOLID. Any decent engineer will be able to immediately tell you that we are of course primarily concerned here with the I and D of our SOLID principles; though in applying Inversion of Control we completely adhere to SOLID.
This means that at design time we specify in our client that we will be working with a concrete implementation of some interface, and it can be determined at run-time which concrete object(s) we actually use, but this is not a requirement of the concept.
Okay so, how do we achieve this
As was alluded to at the beginning of this article, there is usually more than one approach to solving a "problem", and in subsequent blogs we will take a look at the Dependency Injection pattern and the Service Locator anti-pattern, and we will discuss their correlation to the concept of Inversion of Control.
Hey Bill, any idea on when you might be able to finish this post? This is great stuff! Thanks!
ReplyDeletextrmcheeze .... crikey, I had no idea that I'd been away from my blog for so long!
DeleteThere are no excuses, only reasons, and far too many to be listed here.
I'm not intending to finish this post off at the moment, mainly because I have too much to say about too many other things. I'm planning on keeping things going on my blog and making plenty more postings in the future.
If you're still interested in hearing the remainder of what I have to say on this subject, drop me a line and I'll get around to finishing it off sooner rather than later. :o)