繁体   English   中英

泛型中的值和引用类型的接口

[英]Interface for both Value and Reference types in a Generic

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

布尔值: “ = 0”

整数: “> = 2”

DateTime: “ <>'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 );

您会看到在引用类型和值类型中都使用泛型类型存在问题。 有谁知道如何将相同的接口用于所有类型的Filter类? 可能吗?

您可以将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;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM