Using Unity in ASP.NET MVC 5 in Less than 5 Minutes

Unity is a dependency injection framework built by the Pattern and Practices team at Microsoft. In this post, I will give you a quick overview of implementing Unity into an ASP.NET MVC project very quickly.

What is Unity?

Unity is a very nicely integrated library from Microsoft that has both configuration-based and code-based dependency injection options. In this post, I will focus on the code-based configuration but may circle back later to do config-based. It takes almost no effort to get Unity up and running. You can contrast this with the amount of effort to get Ninject up and running in an MVC application. Neither requires a herculean effort but Unity is slightly easier, I think. You will notice they are remarkably similar, though.

Unity and ASP.NET MVC 5

Unity has a Nuget package available specific for MVC.

1. Right click the MVC project in Solution Explorer and click ‘Manage Nuget Packages…’.
2. In the Nuget Package Manager window, select the Browse tab.
3. Search for Unity.
4. Select the Unity.MVC package. I am using version 5.0.13 so if you select a different version, it may work for you just as it does now and it may not.
5. Click the Install button.

Okay, Unity is installed. How do we use it?

1. Add a folder to the project named code.
2. Add a class named MessageService.cs to the code folder.
3. Add an interface named IMessageService.cs to the code folder.
4. Open the MessageService.cs file and replace the class declaration with the code below:

public class MessageService : IMessageService
{
    public string GetMessage()
    {
        return "Hello, World!";
    }
}

5. Open the IMessageService.cs file and replace the interface declaration with the following code:

public interface IMessageService
{
    string GetMessage();
}

6. Open the HomeController.cs file and  replace the class declaration with the following code:

public class HomeController : Controller
{
    private IMessageService _messageService = null;

    public HomeController(IMessageService messageService)
    {
        _messageService = messageService;
    }

    public ActionResult Index()
    {
        ViewBag.Message = _messageService.GetMessage();
        return View();
    }
}

7. Finally, right click the Index() action and select ‘Go To View’. Replace the HTML in the view with the following code:

<div class="jumbotron">
    <h1>@ViewBag.Message</h1>
    <p class="lead">ASP.NET is a free web framework for building great Web sites and Web applications using HTML, CSS and JavaScript.</p>
    <p><a href="https://asp.net" class="btn btn-primary btn-lg">Learn more &raquo;</a></p>
</div>

8. One last thing. In the App_Start\UnityConfig.cs file, add the following line to the RegisterTypes method:

container.RegisterType<IMessageService, MessageService>();

That is all there is to it. If you set a break point in the Home Controller constructor, you will see that Unity is injecting the MessageService into that constructor in place of the IMessageService interface because of the change in bullet point 8. You should see the ‘Hello, World!’ message on the home page when it launches.

If you want to skip all the copy/pasting you can download the code from here.

Leave a Reply

Your email address will not be published.