Search code examples
c#asp.netinversion-of-controlautofac

Entity Framework DB Context null when using Autofac IOC


I'm trying to move the query logic out of my controllers. My problem is context is null and is throwing an exception in my concrete class PetRepository when I try and get a list of pets.

In the interface:

    public interface IPetRepository
    {
    List<Pet> GetAllPets();   
    PetStoreContext context { get; set; }
    }

In the concrete implementation:

public class PetRepository : IPetRepository
{
    public PetStoreContext context { get; set; }

    public List<Pet> GetAllPets()
    {
        return context.Pet.ToList(); //this line throws a null exception
    }
 }

In my controller I'm using constructor injection:

public class PetsController : BaseController
{
    private IPetRepository _petRepository;

    public PetsController(IPetRepository petRepository)
    {
        _petRepository = petRepository;
    }
 }

Then my action result

public ActionResult Index()
    {            
        var model = new PetListing()
        {
            Pets = _petRepository.GetAllPets()
         }
       return View(model);
     }

And finally I am just doing a simple mapping with autofac.

  private void RegisterAutoFac()
    {
        var builder = new ContainerBuilder();
        builder.RegisterControllers(Assembly.GetExecutingAssembly());
        builder.RegisterSource(new ViewRegistrationSource());

        builder.RegisterType<PetRepository>().As<IPetRepository>();

        var container = builder.Build();

        DependencyResolver.SetResolver(new AutofacDependencyResolver(container));

    }

If I use the same lines of code in my controller directly then I get a list of pets back from the DB e.g

public ActionResult Index()
{
 public PetStoreContext context = new PetStoreContext();    
return View(context.Pet.ToList());
}

Solution

  • context is a property not being set, instead let IoC instantiate it for you by injecting it into your repo:

    public class PetRepository : IPetRepository
    {
        private readonly PetStoreContext _context;
    
        public PetRepository(PetStoreContext context)
        {
            _context = context;
        }
    
        public List<Pet> GetAllPets()
        {
            return _context.Pet.ToList();
        }
     }