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

IDbContext

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.

Controllers

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
   Microsoft.Web.Infrastructure.DynamicModuleHelper.DynamicModuleUtility.RegisterModule(typeof(UnityPerRequestHttpModule));

// 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.

Advertisements

Unit of Work and Repository Pattern with Unity Dependency Injection

May 20, 2017 — My new preferred implementation.

Here’s my implementation of a Unit of Work and Repository pattern for ASP.NET MVC with Unity Dependency Injection.

The following example touches on several debatable issues. It is not the intent of this post to contribute to the debate, rather, as an academic or mental exercise to learn more about the patterns and issues. It should not be taken as a recommendation for or against the patterns presented below.

Package and Framework Versions in this article:
  1. MVC4
  2. Entity Framework 5
  3. Unity 3
  4. Unity.MVC3

Unit of Work

public interface IUnitOfWork : IDisposable
{
    MovieContext DbContext { get; }
    int Save();
}

public class UnitOfWork : IUnitOfWork
{
    protected string ConnectionString;
    private MovieContext context;

    public UnitOfWork(string connectionString)
    {
        this.ConnectionString = connectionString;
    }

    public MovieContext DbContext
    {
        get
        {
            if (context == null)
            {
                context = new MovieContext(ConnectionString);
            }
            return context;
        }
    }

    public int Save()
    {
        return context.SaveChanges();
    }

    public void Dispose(bool disposing)
    {
        if (disposing)
        {
            if (context != null)
            {
                context.Dispose();
                context = null;
            }
        }
    }

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }
}

Repository

Generic repositories are presented here but often I’ll extend GenericRepository to provide a more specific or restricted API.

public interface IGenericRepository<TEntity> where TEntity : class
{
    IQueryable<TEntity> GetAll();
    IQueryable<TEntity> FindBy(Expression<Func<TEntity, bool>> predicate);
    TEntity Add(TEntity entity);
    void Delete(TEntity entity);
    void Update(TEntity entity);
}

public class GenericRepository<TEntity> :
    IGenericRepository<TEntity> where TEntity : class
{
    protected MovieContext DbContext;

    public GenericRepository(IUnitOfWork unitOfWork)
    {
        this.DbContext = unitOfWork.DbContext;
    }

    public IQueryable<TEntity> GetAll()
    {
        IQueryable<TEntity> query = this.DbContext.Set<TEntity>();
        return query;
    }

    public IQueryable<TEntity> FindBy(Expression<Func<TEntity, bool>> predicate)
    {
        IQueryable<TEntity> query = this.DbContext.Set<TEntity>().Where(predicate);
        return query;
    }
    ...
}

Service

The service layer keeps much of the business logic out of the controllers and provides a convenient place to put the Entity → model projections.

public interface IMovieService, IDisposable
{
    IEnumerable<MovieDTO> GetMovies(MovieSearchCriteria searchCriteria);
    IEnumerable<MovieDTO> GetRecommendedMovies(MovieSelectionCriteria criteria);
    ...
}

public class MovieService : IMovieService
{
    private IUnitOfWork unitOfWork;
    private IGenericRepository<Movie> movieRepository;
    private IGenericRepository<Genre> genreRepository;

    public MovieService(IUnitOfWork unitOfWork,
                        IGenericRepository<Movie> movieRepository,
                        IGenericRepository<Genre> genreRepository)
    {
        this.unitOfWork = unitOfWork;
        this.movieRepository = movieRepository;
        this.genreRepository = genreRepository;
    }

    public IEnumerable<MovieDTO> GetMovies(MovieSearchCriteria searchCriteria)
    {
        var movies = movieRepository.FindBy(m => m.Year == searchCriteria.Year)
            .Select(p => new MovieDTO { Title = p.Title, Year = p.Year, ... }).ToList();
        return movies;
    }

    ...

    public void Dispose()
    {
        this.unitOfWork.Dispose();
    }
}

Unity Config

Register everything with Unity. A single line of code registers GenericRepository for all Entity types. Specific repositories, if defined, will also get registered here. Unity.MVC3 takes care of the MVC controllers so they are not registered here.

public static IUnityContainer BuildUnityContainer(string connectionString)
{
    var container = new UnityContainer();
    container.RegisterType<IUnitOfWork, UnitOfWork>(new HierarchicalLifetimeManager(),
        new InjectionConstructor(connectionString));
    container.RegisterType(typeof(IGenericRepository<>), typeof(GenericRepository<>));
    container.RegisterType<IMovieService, MovieService>();
    return container;
}

The Microsoft.Practices.Unity.HierarchialLifetimeManager provides:

  1. A Dispose() call after each request
  2. The same DbContext instance per request

Controller

public class MovieController
{
    private IMovieService movieService;

    public MovieController(IMovieService movieService)
    {
        this.movieService = movieService;
    }
    ...
}
And that’s it.