Generic Repository, UnitOfWork and IOC container I am stuck to define a generic repository with AutoFac IOC container. I am keeping thing very simple and only showing relevant information. 

BaseEntity

public abstract class BaseEntity
{
    public int Id { get; set; }
}


IRepository

public interface IRepository&lt;TEntity&gt; where TEntity : BaseEntity
{
    List&lt;TEntity&gt; GetAll();
    void Insert(TEntity entity);
    void Update(TEntity entity);
    void Delete(TEntity entity);
}


IUnitOfWork 

public interface IUnitOfWork : IDisposable
{
    int SaveChanges();
    void Dispose(bool disposing);
    IRepository&lt;TEntity&gt; Repository&lt;TEntity&gt;() where TEntity : BaseEntity;
}


IService

public interface IService
{
    IUnitOfWork UnitOfWork { get; }
}


IService&lt;TEntity&gt; 

public interface IService&lt;TEntity&gt; : IService where TEntity : BaseEntity
{
    List&lt;TEntity&gt; GetAll();
    TEntity GetById(int id);
    void Add(TEntity entity);
    void Update(TEntity entity);
    void Delete(TEntity entity);
}


UnitOfWork 

public class UnitOfWork : IUnitOfWork
{

    private readonly IEntitiesContext _context;
    private bool _disposed;
    private ObjectContext _objectContext;
    private Hashtable _repositories;
    private DbTransaction _transaction;


    public UnitOfWork(IEntitiesContext context)
    {
        _context = context;
    }

    public int SaveChanges()
    {
        return _context.SaveChanges();
    }

    public IRepository&lt;TEntity&gt; Repository&lt;TEntity&gt;() where TEntity : BaseEntity
    {
        if (_repositories == null)
        {
            _repositories = new Hashtable();
        }
        var type = typeof(TEntity).Name;
        if (_repositories.ContainsKey(type))
        {
            return (IRepository&lt;TEntity&gt;)_repositories[type];
        }
        var repositoryType = typeof(EntityRepository&lt;&gt;);
        _repositories.Add(type, Activator.CreateInstance(repositoryType.MakeGenericType(typeof(TEntity)), _context));
        return (IRepository&lt;TEntity&gt;)_repositories[type];
    }
}


Service&lt;TEntity&gt;

public class Service&lt;TEntity&gt; : IService&lt;TEntity&gt; where TEntity : BaseEntity
{
    public IUnitOfWork UnitOfWork { get; private set; }

    public Service(IUnitOfWork unitOfWork)
    {
        UnitOfWork = unitOfWork;
    }

    public List&lt;TEntity&gt; GetAll()
    {
        return UnitOfWork.Repository&lt;TEntity&gt;().GetAll();
    }
}


EntityRepository&lt;TEntity&gt; 

public class EntityRepository&lt;TEntity&gt; : IRepository&lt;TEntity&gt; where TEntity : BaseEntity
{
    private readonly IEntitiesContext _context;
    private readonly IDbSet&lt;TEntity&gt; _dbEntitySet;

    public EntityRepository(IEntitiesContext context)
    {
        _context = context;
        _dbEntitySet = _context.Set&lt;TEntity&gt;();
    }

    public List&lt;TEntity&gt; GetAll()
    {
        return _dbEntitySet.ToList();
    }
}


Now these are generic repository, services and unit-of-work. In my presentation I am using IService&lt;T&gt; which will be injected through AutoFac. Now I have 2 concerns:


I am directly using EntityRepository inside UnitOfWork var repositoryType = typeof(EntityRepository&lt;&gt;), which is a problem for me because I am injecting dependencies through IOC.
If I need to use a custom Repository (by inheriting EntityRepository) in future, how can I inject it in my UnitOfWork?
First, I would caution against using the generic repository + unit of work pattern that was super trendy a few years ago because it has problems not unlike the problems you're facing (along with problems stemming from the fact that EF crosses traditional DAL/Domain/BL boundaries). Simply put, I would not use repositories with EF unless absolutely necessary, and if I had to, I would not use the generic repository pattern.

If you're hell-bent on using this pattern, then you will need to understand the origins of the problem you're facing. Because you have a generic type being returned by your UnitOfWork "factory," you aren't able to instantiate it during your bootstrapping, you are being forced to to instantiate it on-demand at runtime, resolving using the type you're using in your UnitOfWork method call. 

If you are okay with using the same repository instance for all of your resolutions, then just make your objects require Repository&lt;SomeType&gt; in your object constructors or properties with public setters, then use RegisterGeneric(). You can see how that is used here. This bypasses the entire "at runtime" requirement. 

If you want to maintain the pattern of fetching a repository using a method, then you need to treat your UnitOfWork class as a factory and use dynamic instantation.