簡體   English   中英

如何在 DDD 存儲庫中過濾包含 vlaue object 類型?

[英]How to filter with Contains a vlaue object type in DDD repository?

我用 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 = query.Where(x => x.Title.Contains(param.Title));

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

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

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

您必須使用以下內容,因為queryIQuerable query.AsEnumerable().Where......

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM