I would like to implement a simple (testing purposes only) in memory repository as below. The interfaces it implements are generic. One of the methods “Delete” is used in the sample code below.
The cast of predicate throws an exception. How can I implement the deleted method correctly?
public class InMemoryReportingRepository : IReportingRepository
{
private readonly List<IDto> m_dtos;
public InMemoryReportingRepository()
{
m_dtos = new List<IDto>();
}
// ommitted stuff
public void Delete<TEntity>(Expression<Func<TEntity, bool>> predicate) where TEntity : class, IDto
{
var delete = m_dtos.FirstOrDefault((Func<IDto, bool>) predicate.Compile());
m_dtos.Remove(delete);
}
}
Try the following:
Func<TEntity, bool>andFunc<IDto, bool>are completely different types, which is why your cast failed.However, I would recommend using class level generics for your in memory data store: