通用实体的相同服务基类

本文关键字:服务 基类 实体 | 更新日期: 2023-09-27 17:49:24

我已经实现了一些转换器类,将特定类型转换为字节数组,反之亦然。该转换器被注入到包含附加信息的实体中。

我还实现了一些特定的实体服务,它们基于实体类中存储的信息来做一些事情。

问题是我需要x个不同的服务来处理所有可能的实体对象。是否有一种方法可以实例化服务而不指定转换器类型?

转换器类:

abstract class Converter<TValue>
{
    public abstract byte[] ConvertFromValue(TValue value);
    public abstract TValue ConvertToValue(byte[] value);
}
class DoubleConverter : Converter<double>
{
    public double Coefficient { get; private set; }
    public DoubleConverter(double coefficient) { Coefficient = coefficient; }
    public override byte[] ConvertFromValue(double value) { return BitConverter.GetBytes(value * Coefficient); }
    public override double ConvertToValue(byte[] value) { return BitConverter.ToDouble(value, 0) / Coefficient; }
}
class StringConverter : Converter<string>
{
    public Encoding Encoding { get; private set; }
    public StringConverter(Encoding encoding) { Encoding = encoding; }
    public override byte[] ConvertFromValue(string value) { return Encoding.GetBytes(value); }
    public override string ConvertToValue(byte[] value) { return Encoding.GetString(value); }
}

使用转换器的实体:

abstract class Base<TValue>
{
    protected Converter<TValue> Converter;
    public Base(Converter<TValue> converter) { Converter = converter; }
    public byte[] Convert(TValue value) { return Converter.ConvertFromValue(value); }
}
class Foo<TValue> : Base<TValue>
{
    public string Id { get; private set; }
    public Foo(string id, Converter<TValue> converter) : base(converter) { Id = id; }
}
class Bar<TValue> : Base<TValue>
{
    public Bar(Converter<TValue> converter) : base(converter) { }
}

实体服务:

abstract class Service<TValue, TBase>
    where TBase : Base<TValue>
{
    public abstract void Do(TBase entity, TValue value);
}
class FooService<TValue> : Service<TValue, Foo<TValue>>
{
    public override void Do(Foo<TValue> entity, TValue value) {/* Foo specific stuff */}
}
class BarService<TValue> : Service<TValue, Bar<TValue>>
{
    public override void Do(Bar<TValue> entity, TValue value) {/* Bar specific stuff */}
}

问题是我必须创建FooService n次,这取决于使用了多少不同的类型转换器。

var twice = new DoubleConverter(2);
var half = new DoubleConverter(0.5);
var str = new StringConverter(Encoding.ASCII);
var foo1 = new Foo<double>("Twice Foo", twice);
var foo2 = new Foo<string>("String Foo", str);
var bar = new Bar<double>(half);
var fooService = new FooService(); // requires 1 type argument
var barService = new BarService(); // requires 1 type argument
fooService.Do(foo1, 13.4);
fooService.Do(foo2, "123456");
barService.Do(bar, 1.1);

通用实体的相同服务基类

将服务作为方法移动到实体中:

abstract class Base<TValue>
{
    protected Converter<TValue> Converter;
    public Base(Converter<TValue> converter) { Converter = converter; }
    public byte[] Convert(TValue value) { return Converter.ConvertFromValue(value); }
    public abstract void DoService(TValue value);
}
class Foo<TValue> : Base<TValue>
{
    public string Id { get; private set; }
    public Foo(string id, Converter<TValue> converter) : base(converter) { Id = id; }
    public override void DoService(TValue value) {/* Foo specific stuff */}
}
class Bar<TValue> : Base<TValue>
{
    public Bar(Converter<TValue> converter) : base(converter) { }
    public override void DoService(TValue value) {/* Bar specific stuff */}
}

则用法如下:

        DoubleConverter twice = new DoubleConverter(2);
        DoubleConverter half = new DoubleConverter(0.5);
        StringConverter str = new StringConverter(Encoding.ASCII);
        Foo<double> foo1 = new Foo<double>("Twice Foo", twice);
        Foo<string> foo2 = new Foo<string>("String Foo", str);
        Bar<double> bar = new Bar<double>(half);
        foo1.DoService(13.4);
        foo2.DoService("123456");
        bar.DoService(1.1);