【问题标题】:How to filter with Contains a vlaue object type in DDD repository?如何使用包含 DDD 存储库中的值对象类型进行过滤?
【发布时间】:2023-01-20 20:31:20
【问题描述】:

我用 DDD 方法开发了一个项目来管理假期。我有 2 个值对象,名称分别为 HolidayTitle 和 HolidayDate。 现在我想在查询存储库中使用 Contains 进行过滤,但我不能。

我在存储库中的代码如下:

public async Task<ListResponse<IList<GetHolidaysByDatesDto>>> GetHolidays(GetHolidaysQuery param)
        {
            var query = _repository.AsQueryable();

            if (param.Date.HasValue)
                query = query.Where(x => x.Date == param.Date);

            if (!string.IsNullOrEmpty(param.Title))
                query = query.Where(x => x.Title.Contains(param.Title));

            var pagingData = await query.GetPagingData(param);

            query = query.SetPaging(param);
            var holidays = await query.ToListAsync();

            var mappedResult = _mapper.Map<IList<GetHolidaysByDatesDto>>(holidays);

            var finalResult = new ListResponse<IList<GetHolidaysByDatesDto>>()
            {
                PageCount = pagingData.PageCount,
                PageNumber = pagingData.PageNumber,
                RowCount = pagingData.RowCount,
                Result = mappedResult
            };

            return finalResult;
        }

而HolidayTitle的代码如下

public class HolidayTitle : BaseValueObject<HolidayTitle>
    {
        public static readonly int minLength = 5;
        public static readonly int maxLength = 300;

        public string Value { get; private set; }

        private HolidayTitle(string value)
        {
            if (value is null)
                throw new NullOrEmptyArgumentException(HolidayErrors.HolidayTitleIsNull);

            value = value.Trim();

            if (value == string.Empty)
                throw new NullOrEmptyArgumentException(HolidayErrors.HolidayTitleIsEmpty);

            if (value.Length > maxLength || value.Length < minLength)
                throw new RangeLengthArgumentException(HolidayErrors.HolidayTitleLengthIsNotInRangeLength, minLength.ToString(), maxLength.ToString());

            Value = value;
        }

        public bool Contains(string str)
        {
            return Value.Contains(str);
        }

        public override bool IsEqual(HolidayTitle otherObject)
        {
            return Value == otherObject.Value;
        }

        public override int ObjectGetHashCode()
        {
            return Value.GetHashCode();
        }

        public static implicit operator string(HolidayTitle value)
        {
            return value.Value;
        }

        public static HolidayTitle GetInstance(string value)
        {
            return new(value);
        }
    }

我收到这个错误

System.InvalidOperationException: 'The LINQ expression 'DbSet<Holiday>()
.Where(h => !(h.IsDeleted))
.Where(h => h.Title.Contains(__param_Title_0))' could not be translated. Additional information: Translation of method 'Core.Domain.Holidays.ValueObjects.HolidayTitle.Contains' failed. If this method can be mapped to your custom function, see https://go.microsoft.com/fwlink/?linkid=2132413 for more information. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'. See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.'

【问题讨论】:

  • 试试query.AsEnumerable().Where......
  • 我应该添加我的评论作为答案吗?
  • 我已经添加了答案,请采纳

标签: c# domain-driven-design contains iqueryable value-objects


【解决方案1】:

您必须使用以下内容,因为 queryIQuerable query.AsEnumerable().Where......

【讨论】:

    【解决方案2】:

    你不能写

         query = query.Where(x => x.Title.Contains(param.Title));
    

    因为 EF 不知道您的 HolidayTitle.Contains 方法。

    另一方面,EF 知道如何翻译方法string.Contains

         query = query.Where(x => x.Title.Value.Contains(param.Title));
    

    【讨论】:

    • 我同意你声明的第一部分,但我不同意你写的代码。因为“值”在此部分中不可用。
    • 抱歉,试试x.Title.Value.Contains(param.Title)(我修好了)
    • 我再次遇到同样的错误。
    猜你喜欢
    • 2017-03-17
    • 1970-01-01
    • 1970-01-01
    • 2021-06-14
    • 1970-01-01
    • 2019-02-07
    • 2011-03-21
    • 1970-01-01
    • 2015-09-21
    相关资源
    最近更新 更多