locked
Example of Dependecny Injection in ASP.Net RRS feed

  • Question

  • User1884398186 posted

    I was recently asked in an interview to give an example of dependency injection in  ASP.Net.

    Is there some inbuilt 'dependency injection' happening in ASP.Net without the developer noticing it?

    Monday, October 13, 2014 9:56 AM

Answers

  • User281315223 posted

    I was recently asked in an interview to give an example of dependency injection in  ASP.Net.

    About Dependency Injection

    As quoted from an incredible Stack Overflow discussion on this topic  : 

    Dependency Injection means giving an object its instance variables

    Dependency Injection is a design pattern (or methodology) that involves passing in the necessary objects that a new object requires to be created (or its dependencies) rather than making the object have to build them itself. You'll often see it most commonly used and implemented when dealing with Services.

    A example would be if you created a class for a Car that had specific properties that were created within the Car itself : 

    class YourCarClass
    {
        // The car creates the specific properties of that car
        private Engine CarEngine = new SpecificEngineType();
        private Tires CarTires = new SpecificCarTires();
        private Battery CarBattery = new SpecificCarBatteryType();
    
        // Additional properties here
    }

    This means that if you wanted to change any of those properties for the car, which are things that the car depends upon, you would have to manually change it after creating this car, or you would need to create a new Class or Constructor that created a car with your new specificiations.

    Dependency Injection allows you to pass in all of the dependencies (in this case types of Engine, Tires and Battery) to create the Car : 

    // Creating a car by passing in the necessary dependencies
    Car c1 = new Car(new YourSpecificEngineType(), new YourSpecificTires(), new YourSpecificBattery());

    This allows you to create specific cars at run time and dramatically improves the ability to test your classes by passing in specific parameters as opposed to manually changing predefined classes.

    You can check out the following discussions, which could provide more examples and explanations than I could cover : 

    Is there some inbuilt 'dependency injection' happening in ASP.Net without the developer noticing it?

    Generally, most Dependency Injection that you'll see within ASP.NET is done through an external library specifically to handle DI. Unity, Castle Windsor, AutoFac, StructureMapper and many others. The latest upcoming edition of ASP.NET, vNext, is slated to have built-in dependency injection built-in by default (and very easily configurable if you want to branch out into one of these other common libraries).

    You'll often see it used within ASP.NET MVC and Web API when you need to access a service within one of your Controllers, so you'll inject it into them. You can see an example of this here. Additionally, if you use the commonly encountered Repository Pattern with something like Entity Framework, you'll commonly see DI in use there as well.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 13, 2014 10:31 AM