You will find lots of articles regarding Dependency Injection and why you should implement this design pattern. While this post is not about the “why” but the “how”, my two cents to “why do it” is below:
- Code becomes easily testable
- Components/layers are decoupled
- Overall promotes maintainability and good software practice
This post will guide you on how to enable dependency injection using Unity from Microsoft.Practices on your application, whether it be ASP.NET MVC, ASP.NET Web API or ASP.NET Console application.
ASP.NET MVC with Unity
- Install the nuget package “Unity.Mvc”
- Make sure that the following classes have been added to your App_Start folder:
- UnityConfig.cs
- UnityMvcActivator.cs
- Navigate to the UnityConfig class, method RegisterTypes(IUnityContainer container) and start mapping your Interface/Class dependencies. For example, if you have an Email Service that some parts of your application will need to depend on, you will need to create an Interface IEmailService.cs with an implementation class EmailService.cs
public static void RegisterTypes(IUnityContainer container) { // TODO: Register your type's mappings here. container.RegisterType<IEmailService, EmailService>(); }
- Now that you have registered your mappings, you can then begin injecting these dependencies wherever they are needed. Examples below:
public class HomeController : Controller { public readonly IEmailService _emailService; public HomeController(IEmailService emailService) { _emailService = emailService; } public ActionResult SendEmail(string title, string email, string message) { _emailService.Send(title, email, message); return null; } }
Maybe on a separate controller too?
public class FormsController : Controller { public readonly IEmailService _emailService; public FormsController(IEmailService emailService) { _emailService = emailService; } public ActionResult SendEmail(string title, string email, string message) { _emailService.Send(title, email, message); return null; } }
- OPTIONAL: If you have a Unit Test project, then you can pass on a different mapping to your interface like the below:
container.RegisterType<IEmailService, TestEmailService>();
ASP.NET Web API with Unity
- Install the nuget package “Unity.WebApi”
- Make sure that the following classes have been added to your App_Start folder:
- UnityConfig.cs
- UnityWebApiActivator.cs
- Navigate to the UnityConfig class, method RegisterTypes(IUnityContainer container) and start mapping your Interface/Class dependencies. For example, if you have an Email Service that some parts of your application will need to depend on, you will need to create an Interface IEmailService.cs with an implementation class EmailService.cs
public static void RegisterTypes(IUnityContainer container) { // TODO: Register your type's mappings here. container.RegisterType<IEmailService, EmailService>(); }
- Now that you have registered your mappings, you can then begin injecting these dependencies wherever they are needed. Examples below:
public class SampleApiController : ApiController { public readonly IEmailService _emailService; public SampleApiController(IEmailService emailService) { _emailService = emailService; } public void Post([FromBody]string title, [FromBody]string email, [FromBody]string message) { _emailService.Send(title, email, message); } }
ASP.NET Console app with Unity
- Install the nuget package “Unity”
- Make sure that the following classes have been added to your App_Start folder:
- UnityConfig.cs
- If not yet configured, navigate to the UnityConfig class, method RegisterTypes(IUnityContainer container) and start mapping your Interface/Class dependencies. For example, if you have an Email Service that some parts of your application will need to depend on, you will need to create an Interface IEmailService.cs with an implementation class EmailService.cs
public static void RegisterTypes(IUnityContainer container) { // TODO: Register your type's mappings here. container.RegisterType<IEmailService, EmailService>(); }
- In your Program.cs, add the following code:
class Program { static void Main(string[] args) { var unity = UnityConfig.GetConfiguredContainer(); // Explicitly resolve the dependencies / mappings var emailService = unity.Resolve(); emailService.Send(/*add params here*/); } }