У нас есть собственный статический класс, содержащий собственный метод .Include, который будет использоваться при использовании Entity Framework для вызова репозитория. Мы хотели бы, чтобы включение использовало любой предоставленный селектор, например i.Documents ниже:
private readonly IRepository _itemRepository;
var item = await _itemRepository.GetAll()
.IncludeCheckingSecurity(i => i.Documents)
.SingleAsync();
И если репозиторий имеет определенный тип (IObjectWithSecurity) и селектор имеет определенный тип (IEnumerable из IObjectWithSecurity), добавить дополнительный where для фильтрация селектора по свойству, существующему в IObjectWithSecurity:
public static IIncludableQueryable IncludeCheckingSecurity(
this IQueryable source,
Expression propertySelector)
where TRepositoryType : class
{
var previousType = typeof(TRepositoryType);
var thisInclude = typeof(TThisInclude);
if (typeof(IObjectWithSecurity).IsAssignableFrom(previousType) && typeof(IEnumerable).IsAssignableFrom(thisInclude))
{
// Want to add a where to 'propertySelector' which filters it on a property within IObjectWithSecurity (which we have proved it is above)
return source.Include(propertySelector);
}
}
Подробнее здесь: https://stackoverflow.com/questions/791 ... rk-include