Код: Выделить всё
private readonly IRepository _itemRepository;
var item = await _itemRepository.GetAll()
.IncludeCheckingSecurity(i => i.Documents)
.SingleAsync();
Код: Выделить всё
IObjectWithSecurityКод: Выделить всё
IEnumerableКод: Выделить всё
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);
}
}
Например, этот вызов репозитория:
Код: Выделить всё
private readonly IRepository _itemRepository;
var item = await _itemRepository.GetAll()
.IncludeCheckingSecurity(i => i.Documents)
.SingleAsync();
Код: Выделить всё
private readonly IRepository _itemRepository;
var item = await _itemRepository.GetAll()
.IncludeCheckingSecurity(i => i.Documents.Where(a => a.AllowAccess))
.SingleAsync();
Код: Выделить всё
public interface IObjectWithSecurity
{
public bool AllowAccess { get; set; }
}
Код: Выделить всё
Expression whereToAppend = e => ((IObjectWithSecurity)e).AllowAccess;
var newIncludeWithWhere = Expression.Lambda(Expression.AndAlso(propertySelector, whereToAppend), propertySelector.Parameters);
return source.Include(newIncludeWithWhere);
Подробнее здесь: https://stackoverflow.com/questions/791 ... ore-includ
Мобильная версия