Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7860381
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T22:15:54+00:00 2026-06-02T22:15:54+00:00

Heres the problem: Im working on a solution that uses generic repositories, the repositories

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T22:15:56+00:00Added an answer on June 2, 2026 at 10:15 pm

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is my code, everything is working fine the only problem is with the
So we have this problem that we are trying to figure out. Heres what
I'm working on a .NET CF 2.0 application that uses the camera, showing the
I have a solution that uses webforms for front end & mvc for admin
I'm currently working on a site that uses ajax to load the content into
I am currently working on a menu that uses superfish. It is completely customizable
I am working on a data driven web application that uses a SQL 2005
I'm working on a site that uses setTimeout() to do kind of a 'slideshow'
I have a working solution that reports progress & text to a progress bar
I am working with a solution that contains an ASP.NET website and class library

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.