I'm trying to make a get by property generic method, so far i'm thinking something like this.
public async Task<TAggregate> GetByKeyAsync<TKey, TProperty>(Expression<Func<TAggregate, TProperty>> expression, TKey key, CancellationToken cancellationToken)
{
TAggregate aggregate = default(TAggregate);
MemberExpression member = expression.Body as MemberExpression;
if (member == null)
{
throw new ArgumentException($"Expression '{expression.ToString()}' refers to a method, not a property.");
}
PropertyInfo propertyInfo = member.Member as PropertyInfo;
if (propertyInfo == null)
{
throw new ArgumentException($"Expression '{expression.ToString()}' refers to a field, not a property.");
}
IQueryable<TAggregate> dbSet = (from entry in Session.Query<TAggregate>()
select entry);
/// stuck here.
/// TODO : handle find by a string property, ignore case .
if(typeof(TKey) == typeof(String))
{
aggregate = await dbSet.FirstOrDefaultAsync(item => key., cancellationToken);
}
/// TODO : search by any other typeof property '=='
return aggregate;
}
I want to search for that specific property (using the expression to access TAggregate property) in the dbset, use the key
argument which is the value used to compare, and return first row that matches.
Aucun commentaire:
Enregistrer un commentaire