Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I design a general search criteria?

Suppose we have an application (on a server) that has a set of resources: these resources are indexed by name through a dictionary, that is Dictionary<string, Resource> resources.

The client sends to the server (using WCF) the names of one or more resources (for example, it sends a List<string>). On the server there may be only a subset of the resources requested by the client, so when a server receives this list, it sends back a list containing only the names of found resources.

I would like to generalize the search criteria that the client sends to the server, so that in future it is easy to extend the application (both client-side and server-side) with a more complex search criteria. To achieve this goal, I thought to create the ISearchCriteria interface, so the client should send an object of a class which implements this interface.

public interface ISearchCriteria
{
    // the names of the resources which match with the search criteria
    ICollection<string> GetCompliantResources();
}

However it seems to me that this solution is not very correct, because the GetComplianceResources method should interact with the dictionary on the server, but the client should not know anything about this dictionary... I might use the strategy pattern, associating each concrete strategy to the specific search criteria. In this way could separate the control logic from the data (ie. the search criteria).

UPDATE (strategy pattern and DTO)

// Both on the server and on the client
public interface ISearchCriteria
{
    // empty
}

// Both on the server and on the client
public class DefaultSearchCriteriaDTO : ISearchCriteria
{
    List<string> ResourceNames { get; set; }
    List<int> OtherCriteria { get; set; }
}

// Both on the server and on the client
public class MySearchCriteriaDTO : ISearchCriteria
{
    string SearchString { get; set; }
}

// On the server.
public interface IStrategy<T> : where T : ISearchCriteria
{
    public List<string> Match(T criteria);
}

// On the server
public class DefaultStrategy : IStrategy<T> where T : DefaultSearchCriteriaDTO
{
    public List<string> Match(T criteria)
    {
        // search for resources and returns those found
    }
}
  • Is there some design pattern for this purpose?
  • Are there better alternatives?
like image 253
enzom83 Avatar asked Dec 22 '22 01:12

enzom83


1 Answers

Honestly, I wouldn't implement it until there's a need for it.

As it it right now, you're going to be programming around situations that may never exist. How will you know when you're "done?"

Remeber: YAGNI.

like image 87
Matt Grande Avatar answered Dec 23 '22 16:12

Matt Grande