Tuesday, October 25, 2016

C# - Dependency Injection and Controllers in .NET 4 (MVC)


Dependency injection is a technique that follows the Dependency Inversion Principle, allowing for applications to be composed of loosely coupled modules. ASP.NET Core has built-in support for dependency injection, which makes applications easier to test and maintain.

ASP.NET Core’s built-in support for constructor-based dependency injection extends to MVC controllers. By simply adding a service type to your controller as a constructor parameter, ASP.NET Core will attempt to resolve that type using its built in service container. Services are typically, but not always, defined using interfaces. For example, if your application has business logic that depends on the current time, you can inject a service that retrieves the time (rather than hard-coding it), which would allow your tests to pass in implementations that use a set time.

For this exercise we are going to need Unity package installed in our Visual Studio.
Go to Tools -> NuGet Package Manager and browse for Unity and click "install"

Create a folder and name it "Interfaces", then create interface "IDateTime":
using System;

namespace ControllerDI.Interfaces
{
    public interface IDateTime
    {
        DateTime Now { get; }
    }
}
Create another folder and name it "Services".Add a class and name it "SystemDateTime"
using System;
using ControllerDI.Interfaces;

namespace ControllerDI.Services
{
    public class SystemDateTime : IDateTime
    {
        public DateTime Now
        {
            get { return DateTime.Now; }
        }
    }
}
With this in place, we can use the service in our controller. In this case, we have added some logic to the HomeController Index method to display a greeting to the user based on the time of day.
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 if (serverTime.Hour < 17)
            {
                ViewData["Message"] = "It's afternoon here - Good Afternoon!";
            }
            else
            {
                ViewData["Message"] = "It's evening here - Good Evening!";
            }
            return View();
        }
    } 
}
When we use dependency injection is important to remember that we need to "tell" the compiler that we need our object ready when we call our class. At the moment we installed Unity plugin, some files are created, find UnityConfig.cs and open it.
In "public static void RegisterTypes" method add: container.RegisterType<IDateTime, SystemDateTime>(); Save and compile your project. If you see any problem related to some UserManager initialization, you might need to the next lines under the code you just addedabove:
container.RegisterType<DbContext, ApplicationDbContext>(new HierarchicalLifetimeManager());
container.RegisterType<UserManager<ApplicationUser>>(new HierarchicalLifetimeManager());
container.RegisterType<IUserStore<ApplicationUser>, UserStore<ApplicationUser>>(new HierarchicalLifetimeManager());
container.RegisterType<AccountController>(new InjectionConstructor());
Now, save and compile again. 





Programming Thought of the day

If you give someone a program, you will frustrate them for a day; if you teach
them how to program, you will frustrate them for a lifetime. =)


No comments:

Post a Comment