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 theHomeController
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 dayIf you give someone a program, you will frustrate them for a day; if you teachthem how to program, you will frustrate them for a lifetime. =)
No comments:
Post a Comment