In this session of ASP.NET Core tutorial, we are going to talk about Dependency Injection in detail. This feature is a built-in capacity in ASP.NET Core that, enable us to improve our code testability, maintainability and empower our implementation.
How to inject dependency on Controller?
- Step1: Create an Interface and determined required method
1 2 3 4 5 6 7 8 |
public interface ICostRepository { Cost GetCostByID(int id); } |
- Step2: Create Implementation Class for created Interface
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
public class StaticCostRepository : ICostRepository { private List<Cost> costs = new List<Cost> { new Cost {ID=1, Amount=100, Date=DateTime.Now, Category="Bill", Comment="Payment for Mobile bill"}, new Cost{ID=2, Category="Grocery", Date=DateTime.Now, Amount=40, Comment="Payment for buying milk and vegetables"}, new Cost{ID=3, Category="Grocery", Date=DateTime.Now, Amount=40, Comment="Payment for buying meat abd chicken"}, }; public Cost GetCostByID(int id) { return costs.FirstOrDefault(c => c.ID == id); } } |
- Step3: Add Interface and Implementation within ConfigureServices of Startup Class
1 2 3 4 5 6 7 8 9 10 |
public void ConfigureServices(IServiceCollection services) { services.AddControllersWithViews(); services.AddSingleton<ICostRepository, StaticCostRepository>(); } |
- Step4: Inject Interface to Controller via Constructor Injection
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
public class CostController : Controller { private readonly ICostRepository costRepository; public CostController(ICostRepository _costRepository) { costRepository = _costRepository; } ... } |
Dependency Injection and Controller
As you can see in the next image, CostController has dependency on ICostRepository that is injected to the Controller through Constructor Injection. Thus, ASP.NET Core create an instance from implementation of ICostRepository. But, ASP.NET Core cannot realize which implementation is required. Hence, we should register this service to ConfigureServices of Startup Class.
1 2 3 4 5 6 7 8 9 10 |
public void ConfigureServices(IServiceCollection services) { services.AddControllersWithViews(); services.AddSingleton<ICostRepository, StaticCostRepository>(); } |
Differences Between Add Service Methods
There are three different methods to add services to Web App. AddSingleton, AddTransient and AddScoped that their instance lifespans are different from each other.
AddSingleton
This method creates an instance of the service once as per the request. Created instance is available in whole life of Web App and is the same in all the requests.
AddTransient
This method creates an instance of the service each time they are requested. Thus, the instance is always new in the different request. This method is suitable option for lightweight and stateless service.
AddScoped
This Method creates an instance of the service per each request. The instance is valid and same for whole the request but it is different in another request.
If you need more details, watch this session video. Also, for being updated about our coming sessions, follow us on Instagram, Facebook, Telegram or YouTube and you can have access to list of all sessions HERE.
You can download this Session Slides form HERE