Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inversion of Control & Dependency Injection in the .NET Framework

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!

like image 931
Shankar R10N Avatar asked Jul 20 '10 19:07

Shankar R10N


People also ask

What is meant by Inversion of Control?

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.

Why it is called Inversion of Control?

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.

What is Inversion of Control in Spring simple words?

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.

What is IoC example?

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.


2 Answers

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

  • System.IO.StreamWriter
  • System.IO.StreamReader

Property Injection

  • System.ComponentModel.IComponent.Site

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

  • System.ComponentModel.Design.IDesigner.Initialize
  • System.ComponentModel.TypeConverter (many methods take ITypeDescriptorContext)
  • System.Web.Mvc.IModelBinder.BindModel (from ASP.NET MVC)

Ambient Context

  • System.Threading.Thread.CurrentPrincipal
  • System.Threading.Thread.CurrentCulture
  • System.Threading.Thread.CurrentUICulture
  • System.Diagnostics.Trace.Listeners

FWIW, I drew these examples from my upcoming book.

like image 122
Mark Seemann Avatar answered Sep 21 '22 11:09

Mark Seemann


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.

like image 36
Justin Niessner Avatar answered Sep 25 '22 11:09

Justin Niessner