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<TEntity> where TEntity : BaseEntity
{
List<TEntity> 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<TEntity> Repository<TEntity>() where TEntity : BaseEntity;
}
IService
public interface IService
{
IUnitOfWork UnitOfWork { get; }
}
IService<TEntity>
public interface IService<TEntity> : IService where TEntity : BaseEntity
{
List<TEntity> 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<TEntity> Repository<TEntity>() where TEntity : BaseEntity
{
if (_repositories == null)
{
_repositories = new Hashtable();
}
var type = typeof(TEntity).Name;
if (_repositories.ContainsKey(type))
{
return (IRepository<TEntity>)_repositories[type];
}
var repositoryType = typeof(EntityRepository<>);
_repositories.Add(type, Activator.CreateInstance(repositoryType.MakeGenericType(typeof(TEntity)), _context));
return (IRepository<TEntity>)_repositories[type];
}
}
Service<TEntity>
public class Service<TEntity> : IService<TEntity> where TEntity : BaseEntity
{
public IUnitOfWork UnitOfWork { get; private set; }
public Service(IUnitOfWork unitOfWork)
{
UnitOfWork = unitOfWork;
}
public List<TEntity> GetAll()
{
return UnitOfWork.Repository<TEntity>().GetAll();
}
}
EntityRepository<TEntity>
public class EntityRepository<TEntity> : IRepository<TEntity> where TEntity : BaseEntity
{
private readonly IEntitiesContext _context;
private readonly IDbSet<TEntity> _dbEntitySet;
public EntityRepository(IEntitiesContext context)
{
_context = context;
_dbEntitySet = _context.Set<TEntity>();
}
public List<TEntity> GetAll()
{
return _dbEntitySet.ToList();
}
}
Now these are generic repository, services and unit-of-work. In my presentation I am using IService<T>
which will be injected through AutoFac. Now I have 2 concerns:
I am directly using
EntityRepository
insideUnitOfWork var repositoryType = typeof(EntityRepository<>)
, 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 myUnitOfWork
?