mvc

[Introduction] to Dependency Injection in C#

Dependency Injection in C#,ASP.NET MVC,dependencies,IRepository ,constructor,how to do dependency injection in ASP.NET MVC
Share it:
how to do dependency injection in ASP.NET MVC

how to do dependency injection in ASP.NET MVC

When you first begin using ASP.NET MVC you’ll hear a lot about dependency injection. But what is dependency injection? How do you use it? Why should you use it?



Dependencies are created when one method uses another. Let’s use a common example of a service class that needs to retrieve data using a repository class. You could write something like this:

public class MyRepository
{
    public List GetValuesFromRepository()
    {
        // Do something
    }
}
 
public class MyService
{
    public List GetValuesFromService()
    {
        MyRepository repo = new MyRepository();
        return repo.GetValuesFromRepository();
    }
}


Now the GetValuesFromService() method in MyService depends on the GetValuesFromRepository() method in MyRepository. This creates two problems.


  1. Your service class is now tied to a specific repository implementation. At first, this might not seem like a problem but when you need to change the repository implementation (say SQL Server to NoSQL) or you need to support multiple repositories then you find yourself needing to make changes to the service class.
  2. You cannot unit test GetValuesFromService() without also testing GetValuesFromRepository(). This also might not seem like a problem but remember that GetValuesFromRepository() has its own dependencies including the database server, the database structure, its connection string, and the data in the database. Now your unit tests can fail because you cannot connect to the database or the data in it has changed. This means you will waste a lot of time maintaining tests.
Let’s make two changes to this code. First, we’ll create an interface for our repository and make sure our implementation uses it.

public interface IRepository
{
    List GetValuesFromRepository()
}
 
public class MyRepository : IRepository
{
    public List GetValuesFromRepository()
    {
        // Do something
    }
}

Now we’ll change our service class so that it has a constructor that accepts an implementation of IRepository and we’ll use that in our methods.

public class MyService
{
    IRepository _repo;
 
    public MyService(IRepository repo)
    {
        _repo = repo;
    }
 
    public List GetValuesFromService()
    {
        return _repo.GetValuesFromRepository();
    }
}


Our service class still depends on a repository but not on a specific implementation of it. Because you always provide MyService with the repository you want to use you can easily change the implementation. This allows you to switch from SQL Server to MySQL or NoSQL by simply passing a different repository implementation into the constructor when you create an instance of MyService. 

You can now unit test just GetValuesFromService() by providing an implementation of IRepository that returns a guaranteed. This removes the dependency on GetValueFromRepository() and its dependencies.

It also allows you to reliably test what happens under hard to simulate error conditions like the database throwing an exception during a query. So far I’ve covered what dependency injection is and why you should use it. 

I’ve also given a fairly simple example of how to do dependency injection. In part 2 I’ll cover a simple example of using dependency injection with ASP.NET MVC.
Share it:

mvc

Post A Comment:

0 comments: