12
I want to make an implementation of the standard Repository where I will be using the Entityframework and have the following:
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 not to need to be replicated in all other classes Repository. Implementation of IDisposable
:
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 be able to implement my default classes Repository:
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’re not allowed to do it that way.
Follow error message:
The type 'CS.domain.Representative' cannot be used as type Parameter’T' in the Generic type or method 'CS.Repository.Customrepository'. There is no implicit Reference Conversion from 'CS.domain.Representative' to 'System.Idisposable'.
So I added the inheritance of IDisposable
in my class Domain.Representante
.
However I am still prevented. I get the following error message:
The type 'CS.domain.Representative' cannot be used as type Parameter’T' in the Generic type or method 'CS.Repository.Customrepository'. There is no implicit Version from 'CS.domain.Representative' to 'CS.Repository.Irepository'.
Turkeys:
- I’m modeling the pattern wrong?
- There is a way to solve the first mistake without having to go rogue and without having to make my domain classes inherit from
IDisposable
? - How to solve the second error?
Vlw @Crood, is one of the layers that I use in my systems, has several but, this is very practical.
– user6026
+1. I liked the scheme of having
Expressions
in the selection methods. I will incorporate these prototypes into my code, including.– Leonel Sanches da Silva
In this case, New() is a constraint where it requires a constructor without a parameter. Microsoft Website Explanation: The Type argument must have a constructor without public parameters. When used in conjunction with other restrictions, the new() restriction must be last specified. @Crood
– user6026
Okay, @Gypsy Gypsy!
– user6026
Congratulations @Harrypotter, very interesting!
– user3628
Not at all @Tiagopsilva...
– user6026