我的代理规则的设计模式

时间:2014-12-27 09:20:05

标签: c# .net design-patterns

我有一个代理HttpHandler,它从用户获取一个url作为查询字符串参数,如下所示。

public class MagicProxy : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
       string requestUrl = HttpUtility.UrlDecode(context.Request.QueryString["url"]);
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}

url参数是这样的:

http://server1.com/service?method=get&request=GetInfo
http://server1.com/service?method=get&request=GetFeatures
http://server1.com/service?method=get&request=GetImage&Format=PNG

我正在解析url querystring。因为某些用户可以使用 method=get 而无法使用method=post。有些用户可以使用 request=GetInfo,有些用户无法使用reguest = GetImage。

这些是我的用户规则。我想使用desgin模式来实现这些规则。但我无法决定使用。 规格,策略,或者。我是设计模式的新手。

public interface IRule{}
public class UserCanUseGetMethod:IRule{}

如何组合规则和用户?

1 个答案:

答案 0 :(得分:1)

Specification应该是一个不错的选择:

if(someSpec.IsSatisfiedBy(request)) 
{

}

request可以是HTTP请求,也可以是,也可以是可以设计为具有所需属性的值对象的任意类。

看起来你的界面看起来像这样:

public interface ISpecification<T>
{
    bool IsSatisfiedBy(T some);
}

这将允许您在任何地方使用此模式,并且由于它支持T泛型类型参数来提供要指定的对象类型,因此您似乎可以在其他情况下重复使用在哪里你需要规格!