I am implementing a FakeDataSet class by implementing IDbSet interface. As part of implementing this interface, I have to implement Find method. All my entity classes has an Guid type Id column. I am trying to implement Find method for this FakeDbSet class but having hard time to write it in a generic way. Below is my attempts for writing this method.
public class FakeDataSet<T> : IDbSet<T> where T: class, new()
{
// Other methods for implementing IDbSet interface
public T Find(params object[] keyValues)
{
var keyValue = (Guid)keyValues.FirstOrDefault();
return this.SingleOrDefault(m => m.Id == keyValue); // How can I write this
}
}
Since it does not know about Id being of Guid type, I am getting compilation error on m.Id call.
‘T’ does not contain a definition for ‘Id’ and no extension method
‘Id’ accepting a first argument of type ‘T’ could be found (are you
missing a using directive or an assembly reference?)
Any ideas on how this could be accomplished?
Something like this to give you an idea:
Source: Generic Repository: Fake IDbSet implementation update (Find Method & Identity key)