0

I just started learning Web api. I came across this concept and I find all the information regarding this is so complex. Is there any source where DI is explained in simple terms for a beginner like me.

1
  • Read the book Dependency Injection in .NET. It is the best way you can make sense of the (often misleading) information on the Internet. Commented Jan 31, 2018 at 14:36

2 Answers 2

0

The wikipedia article on Dependency Injection was very helpful to me when first learning the concepts. Also this question and this post help explain the concepts well.

It might be helpful to think of a dependency as any code that an object calls or runs that is not contained in that object. Imagine the following pseudo code API Controller

public class MyController: ApiController {
    public IHttpActionResult Get(string id) {
         var databaseConnection = new DatabaseConnection("example connection string");
         databaseConnection.Open();
         var myRecords = databaseConnection.RunAQuery("some SQL");
         var mapper = new RecordToDataModelMapper();
         return mapper.map(myRecords);
    }
}

What if later we need to change the database connection string? Or what if I want to make changes to classes DatabaseConnection or RecordToDataModelMapper? This becomes increasingly messy if we have multiple methods in this controller or other controllers that call the same code. So rather than using the new keyword and constructors to create new objects whose methods/code our controller depends on we could just require that an instance of an object that implements an interface be injected into the constructor for the controller

public class MyController: ApiController {
    private IDatabaseConnection _databaseConnection;
    private IRecordToDataModelMapper _mapper;
    public ApiController(IDatabaseConnection databaseConnection,
                         IRecordToDataModelMapper mapper) {
          _databaseConnection = databaseConnection;
          _mapper = mapper;
    }
    public IHttpActionResult Get(string id) {
         databaseConnection.Open();
         var myRecords = _databaseConnection.RunAQuery("some SQL");
         return mapper.map(myRecords);
    }
}

Changes can now be made to the dependent classes much more easily. All the controller cares is that it receives objects the implement the interfaces in the constructor. So how do those objects get instantiated and what code is responsible for passing them to the controller constructor? The answer is magic. Not really, the real answer is some kind of container object. You could try to write a container yourself, but typically it makes more sense to use a Dependency Injection library like autofac

Sign up to request clarification or add additional context in comments.

Comments

0

I can understand how confusing it might be to start dealing with Dependency Injection in the beginning. While some may recommend you to read a book or show a short example, I think a complete sample code would help much better.

You may have a look at this simple Web API service, that uses Dependency Injection with Autofac container:

https://github.com/borismod/ReSharperTnT

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.