如何在Ardalis中定义选择器。规范库?
我正在尝试利用 ardalis.specification 库库在我的asp.net 6项目中应用规范模式。
安装库后,我创建了以下规范,
public class ProductByIdsSpec : Specification<Product, ProductMenuItem>
{
public ClientRecordByIdsSpec(IEnumerable<int> ids)
{
if (ids == null || !ids.Any())
{
return;
}
Query.Where(x => ids.Contains(x.Id));
// some how I need to map Product to ProductMenuItem so only the needed columns are pulled from the database.
}
}
而不是从数据库中删除product
中的每个值,我只想通过将数据投影到product Menuitem
中来提取所需的数据。以上规范是返回以下错误
selectornotfoundexception ardalis.specification.selectornotfoundexception:规范必须具有定义的选择器
我可以如何定义Entity(IE,product
)和结果对象(即,code> product> product menuitem
)之间的映射?
我尝试添加select()
定义,但正在给我相同的错误
public class ProductByIdsSpec : Specification<Product, ProductMenuItem>
{
public ClientRecordByIdsSpec(IEnumerable<int> ids)
{
if (ids == null || !ids.Any())
{
return;
}
Query.Where(x => ids.Contains(x.Id));
Query.Select(x => new ProductMenuItem() { Name = x.Name, x.Id = x.Id });
}
}
I am trying to utilize the Ardalis.Specification library to apply the specification pattern in my asp.net 6 project.
After installing the library, I created the following specification
public class ProductByIdsSpec : Specification<Product, ProductMenuItem>
{
public ClientRecordByIdsSpec(IEnumerable<int> ids)
{
if (ids == null || !ids.Any())
{
return;
}
Query.Where(x => ids.Contains(x.Id));
// some how I need to map Product to ProductMenuItem so only the needed columns are pulled from the database.
}
}
Instead of pulling every value in Product
from the database, I want to only pull the needed data by projecting the data to ProductMenuItem
. The above specification is returning the following error
SelectorNotFoundException Ardalis.Specification.SelectorNotFoundException: The specification must have Selector defined
How can I define the map between entity (i.e., Product
) and and the result object (i.e., ProductMenuItem
)?
I tried to add Select()
definition but is giving me the same error
public class ProductByIdsSpec : Specification<Product, ProductMenuItem>
{
public ClientRecordByIdsSpec(IEnumerable<int> ids)
{
if (ids == null || !ids.Any())
{
return;
}
Query.Where(x => ids.Contains(x.Id));
Query.Select(x => new ProductMenuItem() { Name = x.Name, x.Id = x.Id });
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以在规范类中覆盖选择器属性并在此处实现
https://github.com/ardalis/specification/blob/main/specification/src/ardalis.specification/specification/specification.cs#l29
You can override the Selector property in your Specification class and implement your projection there
https://github.com/ardalis/Specification/blob/main/Specification/src/Ardalis.Specification/Specification.cs#L29
截至今天,这对我有用。在LIB的当前实现中,
选择器
属性是通过ispecificationbuilder
设置的,这一点一点都不明显。我宁愿检查单位测试和代码比文档。As of today, this works for me. In the current implementation of the lib the
Selector
property is set viaISpecificationBuilder
which is not at all obvious. I'd rather check unit tests and code than docs.