Entity Framework DbContext with Unity IoC

In the past, I have attempted a Unit of Work + Repository architecture. There are arguments that this is duplicate work since DbContext is a UnitOfWork and Repository. I have also found I needed to import the EntityFramework references into my application layer projects (which defeated some of my motivation for abstracting the DbContext).

In my most recent projects I have given up on forcing DbContext into my own Unit of Work and Repository. Now, I extract an IDbContext interface so I can test against mocks more easily.

Package and Framework versions in this article

  1. MVC5
  2. WebApi 2
  3. Entity Framework 6
  4. Unity 4
  5. Unity.Mvc
  6. Unity.AspNet.WebApi
  7. Moq 4


I abstract my own interface so I can mock and test without an actual database.

public class MovieDbContext : DbContext, IMovieDbContext
    public MovieDbContext(string connectionString)
        : base(connectionString)
        // configuration settings

    public virtual IDbSet<Movie> Movies { get; set; }
    public virtual IDbSet<Genre> Genres { get; set; }

    public override void OnModelCreating(DbModelBuilder modelBuilder) { ... }
public interface IMovieDbContext : IDisposable
    IDbSet<Movie> Movies { get; set; }
    IDbSet<Genre> Genres { get; set; }

    int SaveChanges();

Since I’m extending DbContext, IDisposable and SaveChanges() is implemented for me. I add to the interface if my application requires more of the DbContext’s methods.

I Moq this up on my test class

    protected virtual IMovieDbContext GetMockContext(IList<Movie> movies, IList<Genre> genres)
        var dbSetMovies = GetMockDbSet<Movie>(movies);
        var dbSetGenres = GetMockDbSet<Genre>(genres);

        var mockContext = new Mock<IMovieDbContext>();
        mockContext.Setup(m => m.Movies).Returns(dbSetMovies);
        mockContext.Setup(m => m.Genres).Returns(dbSetGenres);

        return mockContext.Object;

    protected virtual IDbSet<T> GetMockDbSet<T>(IList<T> data) where T : class
        var queryable = data.AsQueryable();
        var mockSet = new Mock<IDbSet<T>>();
        mockSet.As<IQueryable<T>>().Setup(m => m.Provider).Returns(queryable.Provider);
        mockSet.As<IQueryable<T>>().Setup(m => m.Expression).Returns(queryable.Expression);
        mockSet.As<IQueryable<T>>().Setup(m => m.ElementType).Returns(queryable.ElementType);
        mockSet.As<IQueryable<T>>().Setup(m => m.GetEnumerator()).Returns(queryable.GetEnumerator());
        mockSet.Setup(d => d.Add(It.IsAny<T>())).Callback<T>((s) => data.Add(s));

        return mockSet.Object;

Service Layer

I now can segregate the query code into a service layer. I usually use AutoMapper, but for this example have manual projections.

public class MovieService : IMovieService
    private IMovieDbContext db;

    public MovieService(IMovieDbContext db)
        this.db = db;

    public MovieDto GetMovie(string title)
        var query = db.Movies.Where(m => m.Title == title)
                             .Select(m => new MovieDto { Id = m.Id, Title = m.Title, Year = m.Year });

        var movie = query.FirstOrDefault();

        return movie;

The service easily takes a DbContext mock to test the query logic without connecting to a database.


The controller doesn’t care about the DbContext nor its query logic. The service is injected with the IoC container (Unity).

public class MovieController : Controller
    private IMovieService service;

    public MovieController(IMovieService service)
        this.service = service;

    public ActionResult GetMovie(string title)
        var movie = this.service.GetMovie(title);
        return View("MovieDetail", movie);

Unity IoC

I include the Unity.Mvc and Unity.AspNet.WebApi bootstrappers which provide the UnityMvcActivator.cs and UnityWebApiActivator.cs files. I uncomment the lines to get the per request lifetime object management.

// UnityMvcActivator.cs
   // TODO: Uncomment if you want to use PerRequestLifetimeManager

// UnityWebApiActivator.cs
   // Use UnityHierarchicalDependencyResolver if you want to use a new child container for each IHttpController resolution.
   var resolver = new UnityHierarchicalDependencyResolver(UnityConfig.GetConfiguredContainer());

And finally, register your types with the container in UnityConfig.cs.

public static RegisterTypes(IUnityContainer container)
    container.RegisterType<IMovieDbContext, MovieDbContext>(new PerRequestLifetimeManager(),
        new InjectionConstructor("name=MovieDbConnection");
    container.RegisterType<IMovieService, MovieService>(new PerRequestLifetimeManager());

Now I have a testable application without a hard dependency on the database. I still need the EntityFramework reference in the Web project but my controllers don’t really know that.

MVC Bootstrap Input Group Button Space

Recently, I’ve fired-up a new MVC5 website with the default template. I’m looking to use the Bootstrap input button group but instead of the controls flush against each other I see a large gap between the two elements.

Here is what I’m expecting:


Here is what I get instead:


Here is the HTML markup:

<div class="row">
    <div class="col-md-4">
        <h3>Bootstrap Input Group</h3>
        <div class="input-group">
            <input type="text" class="form-control" />
            <span class="input-group-btn">
                <button class="btn btn-primary">OK</button>

The Problem

It turns out the Visual Studio’s MVC template Site.css stylesheet is causing problems here.

/* Set width on the form input elements since they're 100% wide by default */
textarea {
    max-width: 280px;

The Solution

Remove the max-width and the space between the elements disappear.

Getting The Solution

It would be helpful to share how I solved this issue — For experienced developers this is probably common knowledge.

In this case I used the browser’s debugger tools to inspect the page elements: On the rendered page in the browser, hover the mouse pointer over the input box then Right-ClickInspect Element.


Chrome’s Developer Tools gives you a screen like the one above. Using this you can examine which CSS rules are applied to a specific element and it also tells you which file the rule comes from (site.css, line 20).

The process on Firefox and Internet Explorer is almost exactly the same as with Chrome.

Frameworks and libraries in this article:
  1. ASP.NET MVC 5.1.2
  2. Twitter Bootstrap 3.1.1