Click here to Skip to main content
15,891,033 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
GeneralRe: Dependency Injection/IoC Pin
TNCaver11-Oct-18 4:02
TNCaver11-Oct-18 4:02 
GeneralRe: Dependency Injection/IoC Pin
kdmote11-Oct-18 9:21
kdmote11-Oct-18 9:21 
GeneralRe: Dependency Injection/IoC Pin
RickZeeland10-Oct-18 6:29
mveRickZeeland10-Oct-18 6:29 
GeneralRe: Dependency Injection/IoC Pin
Slacker00710-Oct-18 8:05
professionalSlacker00710-Oct-18 8:05 
GeneralRe: Dependency Injection/IoC Pin
Marco Bertschi10-Oct-18 8:37
protectorMarco Bertschi10-Oct-18 8:37 
GeneralRe: Dependency Injection/IoC Pin
Slacker00710-Oct-18 8:57
professionalSlacker00710-Oct-18 8:57 
GeneralRe: Dependency Injection/IoC Pin
TNCaver11-Oct-18 4:04
TNCaver11-Oct-18 4:04 
GeneralRe: Dependency Injection/IoC Pin
Sander Rossel10-Oct-18 8:37
professionalSander Rossel10-Oct-18 8:37 
An example says more than a thousand words...
Let's say you have some class that connects to a database.
This class is used by another class that does some business logic with data from a database and uses your database class.
C#
public class DatabaseClass
{
   public List<Customer> GetCustomers()
   {
      // Get customers...
   }
}

public class BusinessLogicClass
{
   public int GetCustomerCount()
   {
      var db = new DatabaseClass();
      var customers = db.GetCustomers();
      // Use customers...
   }
}
Now there is no way to unit test GetCustomerCount because you always need your database because there is a tight coupling with DatabaseClass which directly goes to the database.
So instead of using new DatabaseClass we can "inject" an interface instead.
C#
public interface IDatabaseClass
{
   List<Customer> GetCustomers();
}

public class DatabaseClass : IDatabaseClass
{
   public List<Customer> GetCustomers()
   {
      // Get customers...
   }
}

public class BusinessLogicClass
{
   private readonly IDatabaseClass db;

   public BusinessLogicClass(IDatabaseClass db)
   {
      this.db = db;
   }

   public int GetCustomerCount()
   {
      var customers = db.GetCustomers();
      // Use customers...
   }
}
Now, we can use the BusinessLogicClass with the DatabaseClass or with some mock object for testing purposes.
C#
// Production code.
var bl = new BusinessLogicClass(new DatabaseClass());
var count = bl.GetCustomerCount();
// Do something with count.

// Testing code.
var bl = new BusinessLogicClass(new MyMockObject()); // Use a mocking framework instead!
var count = bl.GetCustomerCount();
// We know our mock object returns 5, but with an actual database we couldn't say.
Assert.Equals(5, count);
Now that's a lot better, our business logic does not depend on the DatabaseClass, and by extension the actual database, anymore.
However, using new DatabaseClass() everywhere in your code isn't ideal either.
You may want to switch database systems at some point or whatever.
So that's where a DI framework comes in.
It let's your register your concrete types for any interface and that type gets injected.
To switch implementations you'll only need to change your registration.
How registration is done depends on the framework you use.
For example, Unity can do registration by convention (DatabaseClass is injected for IDatabaseClass) or it just injects the first (or only?) class it finds that implements IDatabaseClass.
Other frameworks (and Unity too) make you explicitly register types for interfaces.
In .NET MVC, for example, the frameworks creates your Controller classes and can inject any registered implementations in your Controllers constructor (in the example above IDatabaseClass would be injected with DatabaseClass).
You could also have an IBusinessLogicClass of which the implementation requires an IDatabaseClass etc.

In any case, the bottom line is that your code becomes testable because it depends on abstractions (interfaces) and not on implementations.
It's a tough subject, so good luck.
I hope this helps Smile | :)

GeneralRe: Dependency Injection/IoC Pin
Marc Clifton10-Oct-18 9:10
mvaMarc Clifton10-Oct-18 9:10 
GeneralRe: Dependency Injection/IoC Pin
Slacker00710-Oct-18 9:42
professionalSlacker00710-Oct-18 9:42 
GeneralRe: Dependency Injection/IoC Pin
Marc Clifton11-Oct-18 4:01
mvaMarc Clifton11-Oct-18 4:01 
GeneralRe: Dependency Injection/IoC Pin
TNCaver11-Oct-18 4:07
TNCaver11-Oct-18 4:07 
GeneralRe: Dependency Injection/IoC Pin
TNCaver11-Oct-18 4:09
TNCaver11-Oct-18 4:09 
GeneralRe: Dependency Injection/IoC Pin
Slacker00711-Oct-18 5:44
professionalSlacker00711-Oct-18 5:44 
GeneralRe: Dependency Injection/IoC Pin
Jon McKee10-Oct-18 10:26
professionalJon McKee10-Oct-18 10:26 
GeneralRe: Dependency Injection/IoC Pin
Matthew Dennis10-Oct-18 12:30
sysadminMatthew Dennis10-Oct-18 12:30 
GeneralRe: Dependency Injection/IoC Pin
GuyThiebaut10-Oct-18 20:35
professionalGuyThiebaut10-Oct-18 20:35 
GeneralRe: Dependency Injection/IoC Pin
englebart11-Oct-18 4:33
professionalenglebart11-Oct-18 4:33 
GeneralRe: Dependency Injection/IoC Pin
Steve Naidamast11-Oct-18 5:46
professionalSteve Naidamast11-Oct-18 5:46 
GeneralRe: Dependency Injection/IoC Pin
RandyBuchholz12-Oct-18 12:34
RandyBuchholz12-Oct-18 12:34 
GeneralThought of the Day Pin
OriginalGriff10-Oct-18 5:00
mveOriginalGriff10-Oct-18 5:00 
GeneralRe: Thought of the Day Pin
Johnny J.10-Oct-18 5:05
professionalJohnny J.10-Oct-18 5:05 
GeneralRe: Thought of the Day Pin
W Balboos, GHB10-Oct-18 5:21
W Balboos, GHB10-Oct-18 5:21 
GeneralRe: Thought of the Day Pin
megaadam10-Oct-18 5:32
professionalmegaadam10-Oct-18 5:32 
GeneralRe: Thought of the Day Pin
PIEBALDconsult10-Oct-18 5:35
mvePIEBALDconsult10-Oct-18 5:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.