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
}
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With