enz*_*m83 4 .net c# design-patterns
假设我们有一个具有一组资源的应用程序(在服务器上):这些资源通过字典按名称索引,即Dictionary<string, Resource> resources.
客户端向服务器(使用WCF)发送一个或多个资源的名称(例如,它发送一个List<string>).在服务器上,可能只有客户端请求的资源的子集,因此当服务器收到此列表时,它会发回一个仅包含已找到资源名称的列表.
我想概括客户端发送到服务器的搜索条件,以便将来很容易使用更复杂的搜索条件扩展应用程序(客户端和服务器端).为了实现这个目标,我想创建ISearchCriteria接口,所以客户端应该发送一个实现这个接口的类的对象.
public interface ISearchCriteria
{
// the names of the resources which match with the search criteria
ICollection<string> GetCompliantResources();
}
Run Code Online (Sandbox Code Playgroud)
然而在我看来,这个解决方案不是很正确,因为该GetComplianceResources方法应该与服务器上的字典交互,但是客户端不应该对这个字典有所了解...我可能会使用策略模式,将每个具体策略与具体的搜索标准.以这种方式可以将控制逻辑与数据(即搜索标准)分开.
更新(战略模式和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
}
}
Run Code Online (Sandbox Code Playgroud)