通用實體框架核心中的條件

c# entity-framework entity-framework-core generic-collections

我正在構建一個使用工作單元設計模式的Web API應用程序。我創建了一個帶搜索功能的存儲庫:

public IQueryable<User> Search(SearchUserViewModel parameter)
{
    // Find all user from database
    var users = GetAll();

    // Id has been defined.
    if (parameter.Id != null)
        users = users.Where(x => x.Id == parameter.Id.Value);

    // Email has been defined.
    if (parameter.Email != null && !string.IsNullOrWhiteSpace(parameter.Email.Text))
    {
        var emailSearch = parameter.Email;
        switch (emailSearch.Mode)
        {
            case TextSearchMode.ContainIgnoreCase:
                // TODO: Recheck.
                users = users.Where(x => x.Email.Contains(emailSearch.Text));
                break;
            case TextSearchMode.Equal:
                users = users.Where(x => x.Email.Equals(emailSearch.Text));
                break;
            case TextSearchMode.EqualIgnoreCase:
                users = users.Where(x => x.Email.Equals(emailSearch.Text, StringComparison.OrdinalIgnoreCase));
                break;
            default:
                users = users.Where(x => x.Email.Contains(emailSearch.Text));
                break;
        }
    }

    if (parameter.Password != null && !string.IsNullOrWhiteSpace(parameter.Password.Text))
    {
        var passwordSearch = parameter.Password;
        switch (passwordSearch.Mode)
        {
            case TextSearchMode.ContainIgnoreCase:
                // TODO: Recheck.
                users = users.Where(x => x.Email.Contains(passwordSearch.Text));
                break;
            case TextSearchMode.Equal:
                users = users.Where(x => x.Email.Equals(passwordSearch.Text));
                break;
            case TextSearchMode.EqualIgnoreCase:
                users = users.Where(x => x.Email.Equals(passwordSearch.Text, StringComparison.OrdinalIgnoreCase));
                break;
            default:
                users = users.Where(x => x.Email.Contains(passwordSearch.Text));
                break;
        }
    }

    return users;
}

使用枚舉:

public class TextSearch
{
    /// <summary>
    /// Comparision mode.
    /// </summary>
    public TextSearchMode Mode { get; set; }

    /// <summary>
    /// Text which needs searching.
    /// </summary>
    public string Text { get; set; }
}

搜索功能現在運行正常。但我想減少我的代碼。我想知道是否有辦法創建像這樣的函數:

private IQueryable<T> SearchPropertyText(IQueryable<T> records, string propertyName, TextSearchMode mode)
{
    switch (mode)
    {
        case Equal:
            // TODO:
        case EqualIgnoreCase:
            // TODO:
        default:
            // TODO
    }

    return records;
}

然後我可以使用這樣的函數:

users = SearchPropertyText(users, user.Email, emailSearch.Mode)

正如您在上面看到我的代碼,我在TextSearchMode枚舉中列出了一些文本搜索模式,基於模式,我將使用Equal,EqualIgnoreCase,Contains,...搜索屬性...

我正在考慮通用功能,但我現在還不知道如何創建它。

有人可以幫我嗎?

一般承認的答案

您需要傳遞一個Func來定義如何從任何用戶獲取所需的屬性,而不是傳遞user.Email。另外,傳遞TextSearchMode應該傳遞TextSearch對象,因為您還需要Text屬性來執行搜索。這是一個代碼示例:

private IQueryable<T> SearchPropertyText(IQueryable<T> records, Func<T, string> property, TextSearch search)
{
    switch (search.Mode)
    {
        case TextSearchMode.ContainIgnoreCase:
            records = records.Where(x => property(x).Contains(search.Text));
            break;
        case TextSearchMode.Equal:
            records = records.Where(x => property(x).Equals(search.Text));
            break;
        case TextSearchMode.EqualIgnoreCase:
            records = records.Where(x => property(x).Equals(search.Text, StringComparison.OrdinalIgnoreCase));
            break;
        default:
            records = records.Where(x => property(x).Contains(search.Text));
            break;
    }
    return records;
}

然後像這樣使用它:

users = SearchPropertyText(users, user => user.Email, emailSearch);


Related

許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow