0

I'm building a Web API application which use Unit of work design pattern. I've created a repository with search function:

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;
}

With an enumeration:

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

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

Search function runs fine for now. But I want to reduce my code. I wonder if there is a way to create a function like this one:

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

    return records;
}

Then I can use the function like this:

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

As you've seen my code above, I have some mode of text searching listed in the TextSearchMode enumeration, base on mode, I'll search property with Equal, EqualIgnoreCase, Contains, ...

I'm thinking about generic function, but I don't know how to create it for now.

Can anyone help me please ?

1
  • It's possible with generic function and expression composition, but it (as well as your current implementation) might not do what you think - for instance some of the conditions will be translated to SQL and will depend on database string comparison settings (case sensitive or not). While other will do what you think, but will be performed in memory, hence hurting the query performance. Commented Mar 31, 2017 at 9:07

1 Answer 1

1

Rather than passing user.Email, you need to pass a Func that defines how to get the desired property from any user. Also, rather that passing a TextSearchMode, you should pass a TextSearch object since you also need the Text property to perform the search. Here is a code example:

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;
}

Then use it like so:

users = SearchPropertyText(users, user => user.Email, emailSearch);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.