Charles
Charles

Reputation: 716

MVC 3 Generic Repositories Injection with Autofac

Heres the problem: Im working on a solution that uses generic repositories, the repositories work fine and so on. The mather is that im trying to refactor the code in order to enable inject dependencies over the Controllers constructors. Why i want to achieve this?, the solution uses TDD, we want to easy the way the application is tested. I dont want to create fakes as we are doing actualy, rather than i actually want to take advantage of the benefits of the EF repositories and resolve at compile time if im using a FakeRepository(mades changes at level entity) and the real repository that makes changes to the database.

Im using EF as the persistence technology.

These lines represents the repository

public class Repository<T> : IRepository<T> where T
                                : class, IEntity
{
    private readonly DbSet<T> dbset;
    private readonly DbContext _context;

    public Repository(DbContext context)
    {
        _context = context;
        dbset = context.Set<T>();
    }

    public void Add(T entity)
    {
        //Some logic...
    }

    public void Update(T entity)
    {
        //Some logic...
    }

    //more methods...

}

These lines represents the Fake Repository

    public class FakeRepository<T> : IRepository<T> where T
                                : class, IEntity
{
    private readonly DbSet<T> dbset;
    private readonly DbContext _context;

    public FakeRepository(DbContext context)
    {
        _context = context;
        dbset = context.Set<T>();
    }

    public void Add(T entity)
    {
        //Some logic...
    }

    public void Update(T entity)
    {
        //Some logic...
    }

    public void Remove(T entity)
    {
        //Some logic...
    }

    //more methods...
}

These lines represents the Interface contract of the repository.

public interface IRepository<T>
                    where T : class, IEntity
{
    void Add(T entity);
    void Remove(T entity)
    void Remove(T entity);
    //more methods...
}

This is a controller example that its constructor expects a Generic of the previos types.

public class DemoController : Controller
{
    private readonly IRepository<IEntity> _repository;
    public DemoController(IRepository<IEntity> repository)
    {
        _repository = repository;
    }

    public ViewResult Index()
    {
        return View(_repository.FindAll());
    }

}

So, the problem is... How do i register the types on the Autofac container. I see many forums about how to achieve this but didnt find an approach that solves this need.

Itried this in the global.asax:

    protected void Application_Start()
    {
        ConfigureIoC();
        AreaRegistration.RegisterAllAreas();

        RegisterGlobalFilters(GlobalFilters.Filters);
        RegisterRoutes(RouteTable.Routes);
    }

    private static void ConfigureIoC()
    {
        var builder = new ContainerBuilder();
        builder.RegisterControllers(typeof(Global).Assembly);

        //At this section the dependencies are added in order to resolve Types.
        builder.RegisterType<MyController>().InstancePerHttpRequest();

        builder.RegisterType<MyContext>().As<DbContext>();
        builder.RegisterType<Repository<Entity>>().As<IRepository<IEntity>>();

        var container = builder.Build();
        DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
    }

I also tried registering individual types but the autofac simply does not know how to resolve the controller constructor.

Thanks in advance!.

Upvotes: 2

Views: 6032

Answers (2)

Charles
Charles

Reputation: 716

Thanks for your advice Steven. Seeing the documentation and some other blogs i found a solution that i share. Also i will try your alternative to check wich is faster than the other.

The code for resolving contructor injection based upon generics focus is as follows:

    protected void Application_Start()
    {
        ConfigureIoC();
        AreaRegistration.RegisterAllAreas();

        RegisterGlobalFilters(GlobalFilters.Filters);
        RegisterRoutes(RouteTable.Routes);
    }

    private static void ConfigureIoC()
    {
        var builder = new ContainerBuilder();
        builder.RegisterControllers(typeof(Global).Assembly);

        //First we register the types for DemoController.
        builder.RegisterType<Repository<Entity>>().As<IRepository<IEntity>>();
        //Then we register the controller itself resolving/indicating the target type to use on the constructor.
        builder.Register(c => new DemoController(c.Resolve<IRepository<IEntity>>()));

        //We build the container.
        var container = builder.Build();
        DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
    }

Theres another approach using another method of autofac, but since the previous method resolves automatically the dependences its enough. I'll just put the method just for reference

            builder.RegisterType(typeof(DemoController)).UsingConstructor(typeof(IRepository<IEntity>));

Also i share a link where you can download a sample project and shows very clear how Autofac works in this mather.

Autofac T

Upvotes: 1

Steven
Steven

Reputation: 172776

You are injecting an IRepository<IEntity> into the DemoController, which is quite odd, since IEntity is not a concrete entity and DemoController probably needs a concrete entity (such as Customer). Since you want to map IRepository<T> to the concrete Repository<T>, you'll need the following registration:

builder.RegisterGeneric(typeof(Repository<>))
    .As(typeof(IRepository<>));

You also probably need to register the DbContext as per web request, or in a lifetime scope. And it seems strange to register the MyController as PerHttpRequest.

Upvotes: 4

Related Questions