My project structure is like this. I have an interface IUnitOfWork
which has inside it
void Commit();
MyContext Context { get; }
void Register(BaseRepository<IEntity> repository);
This is my UnitOfWork class
public class UnitOfWork : IUnitOfWork
{
private readonly Dictionary<string, BaseRepository<IEntity>> repositories;
public MyContext Context { get; }
public UnitOfWork()
{
repositories = new Dictionary<string, BaseRepository<IEntity>>();
this.Context = new MyContext();
}
public void Commit()
{
repositories.ToList().ForEach(x => x.Value.Submit());
}
void IUnitOfWork.Register(BaseRepository<IEntity> repository)
{
repositories.Add(repository.GetType().Name, repository);
}
}
My IBaseRepository interface
public interface IBaseRepository<T> where T : class, IEntity
{
List<T> GetAll(Func<T, bool> filter = null);
void Save(T item);
void Create(T item);
void Update(T item, Func<T, bool> findByIDPredecate);
}
My IEntity interface
public interface IEntity
{
int Id { get; set; }
}
public abstract class BaseRepository<TEntity> : IBaseRepository<TEntity>
where TEntity : class, IEntity
{
public BaseRepository(IUnitOfWork unitOfWork)
{
unitOfWork.Register(this);
But I am getting this compiler error
Error CS1503 Argument 1: cannot convert from '
Repositories.BaseRepository<TEntity>
' to 'Repositories.BaseRepository<DataAccess.Interfaces.IEntity>
'
However I am explicitly specifying that the TEntity will be of type IEntity (or it will inherit of it). Why is this happening?