Is there any specific example/instance of DI being applied as an architectural principle or design pattern in the .NET Framework itself? Do any (or many) of the types in the framework/BCL conform to IoC?
The type names and a brief illustration/explanation based in C# would be great!
This would compund the need for DI infused design principle as a best-practice...as it is gleaned from the base framework itself.
I reiterate, I am not looking for IoC/DI Frameworks BUT for IoC/DI IN the framework.
EDIT: Just wanted to get more instances/examples ... hence the bounty!
Inversion of Control (IoC) is a design principle that allows classes to be loosely coupled and, therefore, easier to test and maintain. IoC refers to transferring the control of objects and their dependencies from the main program to a container or framework.
Dependency Injection was originally called Inversion of Control (IoC) because the normal control sequence would be the object finds the objects it depends on by itself and then calls them. Here, this is reversed: The dependencies are handed to the object when it's created.
Inversion of control- It means giving the control of creating and instantiating the spring beans to the Spring IOC container and the only work the developer does is configuring the beans in the spring xml file.
Examples of an IOC include unusual network traffic, unusual privileged user account activity, login anomalies, increases in database read volume, suspicious registry or system file changes, unusual DNS requests and Web traffic showing non-human behavior.
In general there aren't a lot of examples of DI in the BCL - perhaps because the BCL is a rather self-contained framework, and DI is more of an application architecture concern... However, here are some examples I've been able to find so far.
Constructor Injection
There are not many examples of Constructor Injection in the BCL. The best candidates are
Property Injection
We also see a variation in Workflow Foundation's WorkflowRuntime.AddService
and related methods, although you might argue that this is closer to Method Injection.
Method Injection
Ambient Context
FWIW, I drew these examples from my upcoming book.
Both the StreamReader and StreamWriter could be seen as examples of IoC/DI.
Each allow you to inject a different Stream object (or one of its derivatives) for reading/writing respectively.
FileInfo fi = new FileInfo(@"C:\MyFile.dat");
StreamWriter sw = new StreamWriter(fi.Open());
Or:
MemoryStream ms = new MemoryStream();
StreamWriter sw = new StreamWriter(ms);
Would both allow:
sw.Write("Hello world!");
The same way, no matter what kind of Stream you injected in the call to the constructor.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With