I want to make an implementation of the Repository pattern where I'll be using EntityFramework and have the following: p>
Interface IRepository
:
public interface IRepository<T> where T : class
{
T GetById(int id);
IQueryable<T> GetAll(Expression<Func<T, bool>> filter);
bool Save(T entity);
bool Delete(int id);
bool Delete(T entity);
}
Standard class already having some implementations so it does not need to be replicated across all other Repository classes . IDisposable
implementation:
public abstract class CustomRepository<T> where T : class, IRepository<T> , IDisposable
{
protected readonly DataContext context;
public CustomRepository(DataContext dataContext) {
this.context = dataContext;
}
// Implements of IRepository<T>
public abstract T GetById(int id);
public abstract IQueryable<T> GetAll(Expression<Func<T, bool>> filter);
public abstract bool Save(T entity);
public abstract bool Delete(int id);
public abstract bool Delete(T entity);
// implements of IDisposable
public void Dispose() {
if (context != null)
context.Dispose();
GC.SuppressFinalize(this);
}
}
And then I can implement my classes from the Repository pattern:
public class RepresentanteRepository : CustomRepository<Domain.Representante>
{
public override Domain.Representante GetById(int id) { ... }
public override IQueryable<Domain.Representante>
GetAll(Expression<Func<Domain.Representante, bool>> filter) { ... }
public override bool Save(Domain.Representante entity) { ... }
public override bool Delete(int id) { ... }
public override bool Delete(Domain.Representante entity) { ... }
}
But you are not being allowed to do this. Here is the error message:
The type 'CS.Domain.Representant' can not be used as type parameter 'T' in the generic type or method 'CS.Repository.CustomRepository'. There is no implicit reference conversion from 'CS.Domain.Representant' to 'System.IDisposable'.
Then I added the inheritance of IDisposable
in my class Domain.Representante
.
However I am still hindered. I get the following error message:
The type 'CS.Domain.Representant' can not be used as type parameter 'T' in the generic type or method 'CS.Repository.CustomRepository'. There is no implicit reference conversion from 'CS.Domain.Representante' to 'CS.Repository.IRepository'.
Peruntas:
IDisposable
?