Another solution if you can't implement an interface and don't want to use the reflection, could be to pass how to get the date to the GenericService:
By a property:
public class GenericService<T>: IGenericService<T> where T : class
{
public Func<T, DateTime> DateGetter { get; set; }
readonly IGenericRepository<T> _genericRepository;
public IEnumerable<T> GetRecordList(DateTime date)
{
var query=_genericRepository.FindBy(r => DateGetter(r) == date);
}
}
By the constructor:
public class GenericService<T>: IGenericService<T> where T : class
{
Func<T, DateTime> _dateGetter;
public GenericService(..., Func<T, DateTime> dateGetter)
{
_dateGetter = dateGetter
...
}
readonly IGenericRepository<T> _genericRepository;
public IEnumerable<T> GetRecordList(DateTime date)
{
var query=_genericRepository.FindBy(r => _dateGetter(r) == date);
}
}
By the method itself:
public class GenericService<T>: IGenericService<T> where T : class
{
readonly IGenericRepository<T> _genericRepository;
public IEnumerable<T> GetRecordList(DateTime date, Func<T, DateTime> dateGetter)
{
var query=_genericRepository.FindBy(r => dateGetter(r) == date);
}
}
By type:
The Adam Ralph' solution inspired me this one (not the best I guess, but it still work)
public class GenericService<T>: IGenericService<T> where T : class
{
public Dictionary<Type, Func<object, DateTime>> _dateGetter = new Dictionary<Type, Func<object, DateTime>>()
{
{ typeof(TypeWithDate), x => ((TypeWithDate)x).Date },
{ typeof(TypeWithDate2), x => ((TypeWithDate2)x).Date }
};
readonly IGenericRepository<T> _genericRepository;
public IEnumerable<T> GetRecordList(DateTime date)
{
var query=_genericRepository.FindBy(r => _dateGetter[typeof(T)](r) = date);
}
}
Dateproperty on a common interface? If not, is that something you can add?