如何在Ardalis中定义选择器。规范库?

发布于 2025-01-23 11:56:17 字数 1395 浏览 2 评论 0原文

我正在尝试利用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

滥情稳全场 2025-01-30 11:56:18
public class ProductByIdsSpec : Specification<Product, ProductMenuItem>
{
    public ClientRecordByIdsSpec(IEnumerable<int> ids)
    {
        ...
    }

    public override Expression<Func<Product, ProductMenuItem>> Selector { get; }
        = (product) => new ProductMenuItem();
}

您可以在规范类中覆盖选择器属性并在此处实现

https://github.com/ardalis/specification/blob/main/specification/src/ardalis.specification/specification/specification.cs#l29

public class ProductByIdsSpec : Specification<Product, ProductMenuItem>
{
    public ClientRecordByIdsSpec(IEnumerable<int> ids)
    {
        ...
    }

    public override Expression<Func<Product, ProductMenuItem>> Selector { get; }
        = (product) => new ProductMenuItem();
}

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

太傻旳人生 2025-01-30 11:56:18

截至今天,这对我有用。在LIB的当前实现中,选择器属性是通过ispecificationbuilder设置的,这一点一点都不明显。我宁愿检查单位测试和代码比文档。

public class ProductByIdsSpec : Specification<Product, ProductMenuItem>
{
    public ClientRecordByIdsSpec(IEnumerable<int> ids)
    {
        if (ids == null || !ids.Any())
        {
            return;
        }

        Query
            .Select(x => new ProductMenuItem() { Name = x.Name, x.Id = x.Id });
            .Where(x => ids.Contains(x.Id));
    }

}

As of today, this works for me. In the current implementation of the lib the Selector property is set via ISpecificationBuilder which is not at all obvious. I'd rather check unit tests and code than docs.

public class ProductByIdsSpec : Specification<Product, ProductMenuItem>
{
    public ClientRecordByIdsSpec(IEnumerable<int> ids)
    {
        if (ids == null || !ids.Any())
        {
            return;
        }

        Query
            .Select(x => new ProductMenuItem() { Name = x.Name, x.Id = x.Id });
            .Where(x => ids.Contains(x.Id));
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文