首页 > 解决方案 > 如何在我的库中添加扩展点?

问题描述

在我发现并使用弹性搜索及其美妙的 lucene 语法后,我不久前就开始编写DataFilters 。该项目背后的想法是首先学习新东西,但我也想知道我是否可以创建类似的东西来与其他数据源一起工作。

长话短说,我现在有一些可以很好地(我认为)与 BCL 类一起工作的东西,我现在想扩展它以支持像 NodaTime 这样的第三方库。

主要部分是IFilter接口

using System;

namespace DataFilters
{
    /// <summary>
    /// Defines the basic shape of a filter
    /// </summary>
    public interface IFilter : IEquatable<IFilter>
    {
        /// <summary>
        /// Gets the JSON representation of the filter
        /// </summary>
        /// <returns></returns>
        string ToJson();

        /// <summary>
        /// Computes a new <see cref="IFilter"/> instance which is the exact opposite of the current instance.
        /// </summary>
        /// <returns>The exact opposite of the current instance.</returns>
        IFilter Negate();

#if NETSTANDARD2_1
        public virtual void ToString() => ToJson();
#endif
    }
}

有两种实现: 过滤器

using DataFilters.Converters;

using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Schema;
using static Newtonsoft.Json.DefaultValueHandling;
using static Newtonsoft.Json.Required;
using System.Text.RegularExpressions;
#if !NETSTANDARD1_3
using System.Text.Json.Serialization;
#endif

namespace DataFilters
{
    /// <summary>
    /// An instance of this class holds a filter
    /// </summary>
#if NETSTANDARD1_3
    [JsonObject]
    [JsonConverter(typeof(FilterConverter))]
#else
    [System.Text.Json.Serialization.JsonConverter(typeof(FilterConverter))]
#endif
    public class Filter : IFilter, IEquatable<Filter>
    {
        /// <summary>
        /// Filter that always returns <c>true</c>
        /// </summary>
        public static Filter True => new Filter(default, default);

        /// <summary>
        /// Pattern that field name should respect.
        /// </summary>
        /// <returns></returns>
        public const string ValidFieldNamePattern = @"[a-zA-Z_]+((\[""[a-zA-Z0-9_]+""]|(\.[a-zA-Z0-9_]+))*)";

        /// <summary>
        /// Regular expression used to validate
        /// </summary>
        /// <returns></returns>
        public static readonly Regex ValidFieldNameRegex = new Regex(ValidFieldNamePattern, RegexOptions.IgnoreCase, TimeSpan.FromSeconds(1));

        /// <summary>
        /// Name of the json property that holds the field name
        /// </summary>
        public const string FieldJsonPropertyName = "field";

        /// <summary>
        /// Name of the json property that holds the operator
        /// </summary>
        public const string OperatorJsonPropertyName = "op";

        /// <summary>
        /// Name of the json property that holds the value
        /// </summary>
        public const string ValueJsonPropertyName = "value";

        /// <summary>
        /// <see cref="FilterOperator"/>s that required <see cref="Value"/> to be null.
        /// </summary>
        public static IEnumerable<FilterOperator> UnaryOperators { get; } = new[]{
            FilterOperator.IsEmpty,
            FilterOperator.IsNotEmpty,
            FilterOperator.IsNotNull,
            FilterOperator.IsNull
        };

        /// <summary>
        /// Generates the <see cref="JSchema"/> for the specified <see cref="FilterOperator"/>.
        /// </summary>
        /// <param name="op"></param>
        /// <returns></returns>
        public static JSchema Schema(FilterOperator op)
        {
            JSchema schema;
            switch (op)
            {
                case FilterOperator.Contains:
                case FilterOperator.StartsWith:
                case FilterOperator.EndsWith:
                    schema = new JSchema
                    {
                        Type = JSchemaType.Object,
                        Properties =
                        {
                            [FieldJsonPropertyName] = new JSchema { Type = JSchemaType.String },
                            [OperatorJsonPropertyName] = new JSchema { Type = JSchemaType.String },
                            [ValueJsonPropertyName] = new JSchema { Type = JSchemaType.String }
                        },
                        Required = { FieldJsonPropertyName, OperatorJsonPropertyName }
                    };
                    break;
                case FilterOperator.IsEmpty:
                case FilterOperator.IsNotEmpty:
                case FilterOperator.IsNotNull:
                case FilterOperator.IsNull:
                    schema = new JSchema
                    {
                        Type = JSchemaType.Object,
                        Properties =
                        {
                            [FieldJsonPropertyName] = new JSchema { Type = JSchemaType.String },
                            [OperatorJsonPropertyName] = new JSchema { Type = JSchemaType.String }
                        },
                        Required = { FieldJsonPropertyName, OperatorJsonPropertyName }
                    };
                    break;
                default:
                    schema = new JSchema
                    {
                        Type = JSchemaType.Object,
                        Properties =
                        {
                            [FieldJsonPropertyName] = new JSchema { Type = JSchemaType.String,  },
                            [OperatorJsonPropertyName] = new JSchema { Type = JSchemaType.String },
                            [ValueJsonPropertyName] = new JSchema {
                                Not = new JSchema() { Type = JSchemaType.Null }
                            }
                        },
                        Required = { FieldJsonPropertyName, OperatorJsonPropertyName, ValueJsonPropertyName }
                    };
                    break;
            }
            schema.AllowAdditionalProperties = false;

            return schema;
        }

        /// <summary>
        /// Name of the field  the filter will be applied to
        /// </summary>
#if NETSTANDARD1_3
        [JsonProperty(FieldJsonPropertyName, Required = Always)]
#else
        [JsonPropertyName(FieldJsonPropertyName)]
#endif
        public string Field { get; }

        /// <summary>
        /// Operator to apply to the filter
        /// </summary>
#if NETSTANDARD1_3
        [JsonProperty(OperatorJsonPropertyName, Required = Always)]
        [JsonConverter(typeof(CamelCaseEnumTypeConverter))]
#else
        [JsonPropertyName(OperatorJsonPropertyName)]
        //[System.Text.Json.Serialization.JsonConverter(typeof(FilterOperatorConverter))]
#endif
        public FilterOperator Operator { get; }

        /// <summary>
        /// Value of the filter
        /// </summary>
#if NETSTANDARD1_3
        [JsonProperty(ValueJsonPropertyName,
            Required = AllowNull,
            DefaultValueHandling = IgnoreAndPopulate,
            NullValueHandling = NullValueHandling.Ignore)]
#else
        [JsonPropertyName(ValueJsonPropertyName)]
#endif
        public object Value { get; }

        /// <summary>
        /// Builds a new <see cref="Filter"/> instance.
        /// </summary>
        /// <param name="field">name of the field</param>
        /// <param name="operator"><see cref="Filter"/> to apply</param>
        /// <param name="value">value of the filter</param>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="field"/> does not conform with <see cref="ValidFieldNamePattern"/></exception>
        public Filter(string field, FilterOperator @operator, object value = null)
        {
            if (!string.IsNullOrEmpty(field) && !ValidFieldNameRegex.IsMatch(field))
            {
                throw new ArgumentOutOfRangeException(nameof(field), field, $"field name is not valid ({ValidFieldNamePattern}).");
            }

            Field = field;
            switch (@operator)
            {
                case FilterOperator.EqualTo when value is null:
                    Operator = FilterOperator.IsNull;
                    break;
                case FilterOperator.NotEqualTo when value is null:
                    Operator = FilterOperator.IsNotNull;
                    break;
                default:
                    Operator = @operator;
                    Value = value;
                    break;
            }
        }

#if NETSTANDARD1_3
        public string ToJson()
        {
            return this.Jsonify(new JsonSerializerSettings());
        }
#else
        public string ToJson() => this.Jsonify();
#endif

        public override string ToString() => ToJson();

        public bool Equals(Filter other)
            => other != null
            && (ReferenceEquals(other, this)
            || (Equals(other.Field, Field) && Equals(other.Operator, Operator) && Equals(other.Value, Value)));

        public override bool Equals(object obj) => Equals(obj as Filter);

#if NETSTANDARD1_3 || NETSTANDARD2_0
        public override int GetHashCode() => (Field, Operator, Value).GetHashCode();
#else
        public override int GetHashCode() => HashCode.Combine(Field, Operator, Value);
#endif

        public IFilter Negate()
        {
            FilterOperator @operator = Operator switch
            {
                FilterOperator.EqualTo => FilterOperator.NotEqualTo,
                FilterOperator.NotEqualTo => FilterOperator.EqualTo,
                FilterOperator.IsNull => FilterOperator.IsNotNull,
                FilterOperator.IsNotNull => FilterOperator.IsNull,
                FilterOperator.LessThan => FilterOperator.GreaterThan,
                FilterOperator.GreaterThan => FilterOperator.LessThan,
                FilterOperator.GreaterThanOrEqual => FilterOperator.LessThanOrEqualTo,
                FilterOperator.StartsWith => FilterOperator.NotStartsWith,
                FilterOperator.NotStartsWith => FilterOperator.StartsWith,
                FilterOperator.EndsWith => FilterOperator.NotEndsWith,
                FilterOperator.NotEndsWith => FilterOperator.EndsWith,
                FilterOperator.Contains => FilterOperator.NotContains,
                FilterOperator.IsEmpty => FilterOperator.IsNotEmpty,
                FilterOperator.IsNotEmpty => FilterOperator.IsEmpty,
                FilterOperator.LessThanOrEqualTo => FilterOperator.GreaterThanOrEqual,
                _ => throw new ArgumentOutOfRangeException(nameof(Operator), "Unknown operator"),
            };
            return new Filter(Field, @operator, Value);
        }

        public bool Equals(IFilter other) => Equals(other as Filter)
;

        public void Deconstruct(out string field, out FilterOperator @operator, out object value)
        {
            field = Field;
            @operator = Operator;
            value = Value;
        }
    }
}

多重过滤器

using DataFilters.Converters;
using Newtonsoft.Json;
using Newtonsoft.Json.Schema;
using System;
using System.Collections.Generic;
using System.Linq;
using static Newtonsoft.Json.DefaultValueHandling;
using static Newtonsoft.Json.Required;

#if !NETSTANDARD1_3
using System.Text.Json.Serialization;
#endif

namespace DataFilters
{
    /// <summary>
    /// An instance of this class holds combination of <see cref="IFilter"/>
    /// </summary>
    [JsonObject]
#if NETSTANDARD1_3
    [JsonConverter(typeof(MultiFilterConverter))]
#else
    [System.Text.Json.Serialization.JsonConverter(typeof(MultiFilterConverter))]
#endif
    public class MultiFilter : IFilter, IEquatable<MultiFilter>
    {
        /// <summary>
        /// Name of the json property that holds filter's filters collection.
        /// </summary>
        public const string FiltersJsonPropertyName = "filters";

        /// <summary>
        /// Name of the json property that holds the composite filter's logic
        /// </summary>
        public const string LogicJsonPropertyName = "logic";

        public static JSchema Schema => new JSchema
        {
            Type = JSchemaType.Object,
            Properties =
            {
                [FiltersJsonPropertyName] = new JSchema { Type = JSchemaType.Array, MinimumItems = 2 },
                [LogicJsonPropertyName] = new JSchema { Type = JSchemaType.String, Default = "and"}
            },
            Required = { FiltersJsonPropertyName },
            AllowAdditionalProperties = false
        };

        /// <summary>
        /// Collections of filters
        /// </summary>
#if NETSTANDARD1_3
        [JsonProperty(PropertyName = FiltersJsonPropertyName, Required = Always)]
#else
        [JsonPropertyName(FiltersJsonPropertyName)]
#endif
        public IEnumerable<IFilter> Filters { get; set; } = Enumerable.Empty<IFilter>();

        /// <summary>
        /// Operator to apply between <see cref="Filters"/>
        /// </summary>
#if NETSTANDARD1_3
        [JsonProperty(PropertyName = LogicJsonPropertyName, DefaultValueHandling = IgnoreAndPopulate)]
        [JsonConverter(typeof(CamelCaseEnumTypeConverter))]
#else
        [JsonPropertyName(LogicJsonPropertyName)]
#endif
        public FilterLogic Logic { get; set; }

        public virtual string ToJson() => this.Jsonify();


        public IFilter Negate()
        {
            MultiFilter filter = new MultiFilter
            {
                Logic = Logic switch
                {
                    FilterLogic.And => FilterLogic.Or,
                    FilterLogic.Or => FilterLogic.And,
                    _ => throw new ArgumentOutOfRangeException($"Unsupported {Logic}")
                },
                Filters = Filters.Select(f => f.Negate())
#if DEBUG
                .ToArray()
#endif
            };

            return filter;
        }
#if NETSTANDARD1_3 || NETSTANDARD2_0
        public override int GetHashCode() => (Logic, Filters).GetHashCode();
#else
        public override int GetHashCode()
        {
            HashCode hash = new HashCode();
            hash.Add(Logic);
            foreach (IFilter filter in Filters)
            {
                hash.Add(filter);
            }
            return hash.ToHashCode();
        }
#endif

        public bool Equals(IFilter other) => Equals(other as MultiFilter);

        public override bool Equals(object obj) => Equals(obj as MultiFilter);

        public bool Equals(MultiFilter other)
            => Logic == other?.Logic
            && Filters.Count() == other?.Filters?.Count()
            && Filters.All(filter => other?.Filters?.Contains(filter) ?? false)
            && (other?.Filters.All(filter => Filters.Contains(filter)) ?? false);
    
}
}

DataFilters.ExpressionsDataFilters.Queries是我也编写的两个库,它们允许在给定实例作为输入(扩展方法)的情况下创建 C# 表达式或WHERESQL 。IFilter

我现在要做的是提供一个扩展点,以便我可以编写一个新库(DataFilters.NodaTime例如称为),它可以以某种方式处理 NodaTime 类型,同时将其他所有内容推迟到DataFilters.Expressions(我已经发布的一个库)。

该扩展点应该增加处理 nodatime 类型的能力,但我不知道如何开始

现在,我正在考虑这样的事情:

目标是能够使用DataFilters.ExpressionsDataFilters.Queries处理 NodaTime 类型。

这可能是一个好方法还是有更好的方法来处理这个问题?

提前感谢任何可以帮助我的人

标签: c#design-patterns

解决方案


推荐阅读