0

我正在创建一个接口,该接口将由将用作过滤器以创建查询字符串的类实现。每个过滤器将负责在查询中构造一个参数。用户可以定义是否包含运算符以及运算符应该是什么以及值。值的表达方式取决于数据类型。

布尔值: “= 0”

整数: “ >= 2”

日期时间: “ <> '2012-01-10'”

字符串: “='一些字符串'”

public interface IFilter<T> 
{
    Nullable<T> Value { get; set; }
    String Operator { get; set; }
    Boolean IncludeOperator { get; set; }
}

该接口将由一个抽象类实现,该类定义基本字段、属性并覆盖 ToString 方法;以及一个抽象的 GetValueAsString() 方法,应该重写该方法以执行从过滤器构造适当字符串所需的任何逻辑。

public abstract class Filter<T> : IFilter<T> 
{
    Nullable<T> _value;
    String _operator = "=";
    Boolean _includeOperator = false;

    public Nullable<T> Value { get { return this._value; } set { this._value = value; } }

    public String Operator { get { return this._operator; } set { this._operator = value; } }

    public Boolean IncludeOperator { get { return this._includeOperator; } set { this._includeOperator = value; } }


    public override string ToString()
    {
        String param = GetValueAsString();
        if (param != null && this.IncludeOperator)
            return this.Operator + " " + param;
        else if (param != null && !this.IncludeOperator)
            return param.Trim();
        else
            return null;
    }

    protected abstract String GetValueAsString();
}

我想创建从接口和抽象类派生的类型化过滤器。每个过滤器都知道如何将特定类型转换为字符串以包含在查询中,但每个过滤器都需要遵守 IFilter 接口,它可以通过继承 Filter 基类来做到这一点。

public class IntFilter: Filter<Int64>
{
    protected override string GetValueAsString()
    {
        // Convert the integer to a string
        return this.Value.Value.ToString();
    }
}

public class BooleanFilter : Filter<Boolean>
{
    protected override string GetValueAsString()
    {
        // convert the Boolean to a BIT value 1=True / 0=False
        return (this.Value.HasValue && this.Value.Value) ? "1" : "0";
    }
}

public class DateTimeFilter : Filter<DateTime>
{
    protected override string GetValueAsString()
    {
        // Convert the DateTime to a dateTime string in the following format - 2/27/2009 12:12:22 PM
        return (this.Value.HasValue) ? this.Value.Value.ToString("G") : null;
    }
}

这就是它变得棘手的地方......我需要为字符串类型创建一个过滤器,但使用相同的接口和抽象类。

public class StringFilter : Filter<String>
{
    protected override string GetValueAsString()
    {
        // Convert the integer to a string
        return this.Value.Value.ToString();
    }
}

我需要能够将这些过滤器中的任何一个传递到以下方法中......

ExecuteQuery( IFilter filter );

您可以看到对 Reference 和 Value 类型都使用泛型类型存在问题。有谁知道我如何为所有类型的过滤器类使用相同的接口?可能吗?

4

1 回答 1

0

您可以将 Nullable<> 从定义中提取出来,然后将它们传递给实现。这是一个例子:

public interface IFilter<T>
{
    T Value { get; set; }
    String Operator { get; set; }
    Boolean IncludeOperator { get; set; }
}

public abstract class Filter<T> : IFilter<T>
{
    T _value;
    String _operator = "=";
    Boolean _includeOperator = false;

    public T Value { get { return this._value; } set { this._value = value; } }

    public String Operator { get { return this._operator; } set { this._operator = value; } }

    public Boolean IncludeOperator { get { return this._includeOperator; } set { this._includeOperator = value; } }


    public override string ToString()
    {
        String param = GetValueAsString();
        if (param != null && this.IncludeOperator)
            return this.Operator + " " + param;
        else if (param != null && !this.IncludeOperator)
            return param.Trim();
        else
            return null;
    }

    protected abstract String GetValueAsString();
}

public class IntFilter : Filter<Nullable<Int64>>
{
    protected override string GetValueAsString()
    {
        // Convert the integer to a string
        return this.Value.HasValue ? this.Value.ToString() : "0";
    }
}

public class BooleanFilter : Filter<Nullable<Boolean>>
{
    protected override string GetValueAsString()
    {
        // convert the Boolean to a BIT value 1=True / 0=False
        return (this.Value.HasValue && this.Value.Value) ? "1" : "0";
    }
}

public class DateTimeFilter : Filter<Nullable<DateTime>>
{
    protected override string GetValueAsString()
    {
        // Convert the DateTime to a dateTime string in the following format - 2/27/2009 12:12:22 PM
        return (this.Value.HasValue) ? this.Value.Value.ToString("G") : null;
    }
}

public class StringFilter : Filter<String>
{
    protected override string GetValueAsString()
    {
        return string.IsNullOrWhiteSpace(Value) ? "" : Value;
    }
}
于 2012-08-25T04:35:18.490 回答