c# - 在通用存储库使用者类中创建通用方法
问题描述
我想在通用存储库使用者类中创建通用方法。
这是我在通用存储库类中的通用方法:
public class CosmosDBRepository<T> : ICosmosDBRepository<T> where T : class
{
public async Task<IEnumerable<T>> GetItemsAsync(Expression<Func<T, bool>> predicate, Expression<Func<T, object>> orderByDesc, int takeCount = -1)
{
var criteria = _container.GetItemLinqQueryable<T>(true)
.Where(predicate)
.OrderByDescending(orderByDesc)
.ToFeedIterator();
var query = criteria;
var results = new List<T>();
while (query.HasMoreResults)
{
if (takeCount > -1 && results.Count >= takeCount) break;
results.AddRange(await query.ReadNextAsync());
}
return results;
}
}
通用存储库消费者类:
public class SubscriptionRepository : CosmosDBRepository<Subscription>, ISubscriptionRepository
{
public SubscriptionRepository(
ICosmosDBClient client
) : base(client)
{
}
public async Task<List<T>> GetSubscriptions<T, TE>(
TE eventItem,
params SubscriptionAction[] subscriptionAction)
where T : Subscription
where TE : Event
{
Expression<Func<T, bool>> predicate = (x) => x.EventType == eventItem.EventType
&& x.IsActive;
predicate = predicate.And(x => subscriptionAction.Contains(x.Action));
if (!string.IsNullOrEmpty(eventItem.PayerNumber))
{
predicate = predicate.And(x => x.PayerNumber == eventItem.PayerNumber);
}
else if (!string.IsNullOrEmpty(eventItem.AccountNumber))
{
predicate = predicate.And(x => x.AccountNumber == eventItem.AccountNumber);
}
var result = await GetItemsAsync(predicate, o => o.PayerNumber);
return result.ToList();
}
}
现在我想GetSubscriptions
在SubscriptionRepository
类中创建泛型方法。
您能否建议我如何实现这一目标?
目前我收到以下编译时错误:
无法从 'System.Linq.Expression<System.Func<T,bool>>' 转换为 'System.Linq.Expression.Expression<System.Func<FleetHub.Notifications.Domain.CosmosDB.Containers.Subscription,bool>>'
解决方案
这里至少有两个问题,性质相同——你试图用一个通用的方法来包装一个具体的CosmosDBRepository<Subscription>.GetItemsAsync
函数。第一个可以解决将谓词更改为:
Expression<Func<Subscription, bool>> predicate = x => x.EventType == eventItem.EventType && x.IsActive;
但它不会解决CosmosDBRepository<Subscription>.GetItemsAsync
将返回集合Subscription
而不是任意Subscription
后代集合的问题。如果您GetSubscriptions
仍然需要通用,则需要过滤和转换结果GetItemsAsync
:
return result.OfType<T>().ToList();
或者为方法提供一个映射器函数:
public async Task<List<T>> GetSubscriptions<T, TE>(
TE eventItem,
Func<Subscription, T> mapper,
params SubscriptionAction[] subscriptionAction)
where T : Subscription
where TE : Event
{
.....
return result.Select(mapper).ToList();
}
推荐阅读
- twitter-bootstrap - 没有空间嵌套的引导按钮
- c# - 使用 UTF-8 编码解码 Base64 字符串
- c# - 如何在流文档中设置页宽 (A4)
- typescript - 有没有办法区分 Javascript/TypeScript 中不同接口类型的对象?
- python - Python - 用最接近的非零颜色填充图像的颜色
- python - 如何根据列值/字符串将数据框中的多行替换为另一个数据框中的行?
- azure-powershell - 如何使用“az storage entity insert”命令将特殊字符插入 Azure 表存储?
- visual-studio - Visual Studio:如何在连续执行中重用相同的控制台
- mongoose - 猫鼬将项目添加到数组
- javascript - 一个函数,它接受两个参数,一个数组和一个元素,它返回给定元素的索引