I have this:
public void AssertReadWorks<T>(
IRepository<T> repository,
T entity,
Expression<Func<T, T, bool>> keyComparer) where T : class
{
entity = repository.GetAll().Single(x => x.Id == entity.Id);
}
[TestMethod]
public void ReadTest_DataFieldGroup()
{
AssertReadWorks(
_unitOfWork.DataFieldSetRepository,
new DataFieldSet { Label = "test", Title = "test" },
(a, b) => a.Id == b.Id);
}
This does not compile since it is not known that T has an Id property. Note that the keyComparer
parameter is not used at the moment. I want to use the keyComparer parameter (or another appropriate parameter) to dynamically generate the predicate for Single()
:
Expression<Func<T, bool>> keyComparingPredicate =
x => a predicate that compares the key of x with the key of `entity`;
entity = repository.GetAll().Single(keyComparingPredicate);
The point is that not all Ts will have Id properties, some will have different names, some will have composite keys. The original AssertReadWorks()
works fine if it is not generic. The problem is just building the predicate dynamically in the generic case. If it can be done with something different from the keyComparer paramter, fine with me.
Any ideas? :)