torsdag den 4. december 2014

Dependency Injection in a Nutshell

Issue

How to explain in a nutshell how dependency injection makes our code more robust, simple and more easy to maintain.

Case

I have a class that returns a list of user objects loaded from an XML-file.

 public class OpusRepository : IOpusRespository  
 {  
   public virtual List<OpusUser> Users()  
   {  
     // Read file from static path  
     // Get users from file  
     // return list of users  
   }  
 }  

The code that uses the IOpusRepository gets an instance of the OpusRepository class by calling a custom class that creates an instance of the OpusRepository.
It looks like this