@manhnguyenv

Welcome to my blog!

Dependency Injection in ASP.NET Core

July 18, 2017 16:58

ASP.NET MVC

https://msdn.microsoft.com/en-us/library/dn178463(v=pandp.30).aspx

Good Article about DI

Dependency Injection in ASP.​NET Core - a quick overview

http://asp.net-hacker.rocks/2016/02/17/dependency-injection-in-aspnetcore.html

+ Dependency Injection (DI)

+ How to register the services (4 ways)

  1. services.AddTransient
  2. services.AddSingleton
  3. services.AddScoped
  4. services.AddInstance

+ How to inject a service globally into all Views using ApplicationInsights.

DI is also working in MiddleWares, TagHelpers and ViewComponents.

 

Implement dependency injection outside of Startup.cs

https://stackoverflow.com/questions/40306928/implement-dependency-injection-outside-of-startup-cs

ASP.NET Core MVC

Dependency injection into controllers

https://docs.microsoft.com/en-us/aspnet/core/mvc/controllers/dependency-injection

 

Using dependency injection in a .Net Core console application

https://andrewlock.net/using-dependency-injection-in-a-net-core-console-application/

 

Essential .NET - Dependency Injection with .NET Core

https://msdn.microsoft.com/en-us/magazine/mt707534.aspx

 

NuGet: Install-Package Microsoft.Extensions.DependencyInjection -Version 1.1.1

 

Dependency Injection (DI) Lifetimes

https://www.hmtmcse.com/language/c/dotnet-core/dependency-injection

 

Code samples:

------------------------------
1) Interface
------------------------------
using System;

namespace ControllerDI.Interfaces
{
    public interface IDateTime
    {
        DateTime Now { get; }
    }
}


------------------------------
2) Services - Repository
------------------------------
using System;
using ControllerDI.Interfaces;

namespace ControllerDI.Services
{
    public class SystemDateTime : IDateTime
    {
        public DateTime Now
        {
            get { return DateTime.Now; }
        }
    }
}


--------------------------------------------------
3) Controllers - Constructor Injection
--------------------------------------------------
using ControllerDI.Interfaces;
using Microsoft.AspNetCore.Mvc;

namespace ControllerDI.Controllers
{
    public class HomeController : Controller
    {
        private readonly IDateTime _dateTime;

        public HomeController(IDateTime dateTime)
        {
            _dateTime = dateTime;
        }

        public IActionResult Index()
        {
            var serverTime = _dateTime.Now;
            if (serverTime.Hour < 12)
            {
                ViewData["Message"] = "It's morning here - Good Morning!";
            }
            else
            {
                ViewData["Message"] = "It's evening here - Good Evening!";
            }
            return View();
        }
    }
}


------------------------------
4) ConfigureServices
------------------------------
public void ConfigureServices(IServiceCollection services)
{
    // Add application services.
    services.AddTransient<IDateTime, SystemDateTime>();
}

Categories