c# - 接口(interface)和对象反序列化

标签 c# oop interface

我有一个接口(interface),它定义了一个可序列化为字节数组的类。

public interface IByteSerializable
{
    byte[] GetBytes();
}

它的自然搭档是反序列化方法,我想返回一个实现了 IByteSerializable 的对象。

我正在为如何设计这样的界面而苦恼。

这似乎没有意义:

public interface IByteSerializable
{
    byte[] GetBytes();
    IByteSerializable GetObject(byte[] bytes);
}

因为 GetObject() 的实现不能是 static 并且只使用虚拟 IByteSerializable 对象没有意义调用 GetObject() 方法来反序列化我正在寻找的实际对象。

这样做似乎也没有意义:

public interface IByteSerializableFactory
{
    IByteSerializable GetObject(byte[] bytes);
}

工厂类可以解决问题,但这感觉会导致类爆炸。此外,给定的 IByteSerializable 子类如何序列化然后反序列化的细节是相互依赖的,因此将它们放在同一个地方而不是在两个不同的类中是有意义的。显然,反序列化给定 IByteSerializable 对象所需的确切过程完全取决于该对象的 GetBytes() 方法的编写方式。

有没有我可以用来解决这个问题的通用设计或模式?

最佳答案

当涉及到您的问题时,对于接口(interface)、类和模式有很多不同的看法。我的个人偏好是实现一个带有 byte[] 属性的接口(interface)和一个带有虚拟方法的抽象类(或者甚至完全失去接口(interface),这对你来说可能不是一个选择并且确实不能很好地进行 DI 和单元测试):

public interface IByteSerializable
{
    byte[] SerializableByteObject { get; }
}

public abstract class ByteSerializable : IByteSerializable
{
    public byte[] SerializableByteObject { get; }
    protected virtual byte[] GetBytes() {
        return SerializableByteObject;
    }
    public abstract IByteSerializable GetObject();
    //{    // You can make this method virtual and use Impl method:
           // GetObjectImpl(SerializableByteObject);
    //}
    protected internal IByteSerializable GetObjectImpl(byte[] bytes) {
        // If you need a default implementation (GetObject() should be protected virtual then)
        // return IByteSerializable...;
    }
}

我想强调的是,接口(interface) VS 抽象类是一个无休止的讨论。如果您可以在不实现接口(interface)并仅使用抽象类的情况下做一些事情 - 我强烈建议您这样做。

2017 年 3 月 18 日更新:回复评论(定义行为是接口(interface)的目的)并解释我如何看待它并在下面添加解释。

在这种情况下,我们定义的“行为”是“一个对象应该可以转换为字节数组。转换结果应该可以转换回同一个对象。”所以我们实际上是在为一个对象和一个字节数组定义行为(因为在一个对象被反序列化之后 - 它不再是同一个对象,它只是一个字节数组)。

在我看来,这是纯工厂模式场景。

// Let's define an interface for our serializable type of objects factory
public interface IByteSerializableFactory<T>
{
     T CreateFromBytes(byte[] objectDataToUse);
     byte[] CovertToBytes(T objectToConvert);
}

// Interface for any class that needs a serialization factory
// This is not even necessary, but I like it to enforce people to implement simple methods that reference the factory.

public interface IByteSerializable<T>
{
    IByteSerializableFactory<T> GetFactory();
}

// Now a moment comes for us to have this kind of class. We need to build a factory first (because our interface requires a GetFactory() implementation. We can lose the IByteSerializable interface altogether, but then we lose a way to let people know which factory should be used.

public class SomeBaseClassSerializationFactory : IByteSerializableFactory<SomeBaseClass>
{
    public SomeBaseClass CreateFromBytes(byte[] objectDataToUse) { //...
        return new SomeClass();
    }
    public byte[] CovertToBytes(SomeBaseClass objectToConvert) { //...
        return new byte[1];
    }
}

// We have a factory, let's implement a class.

public abstract class SomeBaseClass : IByteSerializable<SomeBaseClass>
{
    public virtual IByteSerializableFactory<SomeBaseClass> GetFactory() { 
        return new SomeBaseClassSerializationFactory();                                             
    }
}

public class SomeClass : SomeBaseClass {
    // Now we're independent. Our derived classes do not need to implement anything.
    // If the way the derived class is serialized is different - we simply override the method
}

2 2017 年 3 月 18 日更新:回复不同答案下的评论(使用简单接口(interface)的通用实现)。

不幸的是,没有干净的方法来做到这一点。有一种肮脏的(我个人的观点:“BAD BAD BAD!”)方式,通过使用一些作弊,定义定义序列化方法的类并使用反射返回正确的类型。下面的示例将需要序列化方法中的大量自定义逻辑,以使用具有不同类型的正确字段:

// You define an enum with action and a dictionary with a collection of serialization methods.
public enum SerializationAction {
    ToBytes,
    ToObject    
}

// It can also be an enum, but it's easier to test with a collection of strings.
public static readonly string[] SerializationKindList = new string[] {
    "FirstKind",
    "SecondKind"
};

// This generic class can have an implementation of all the handlers. Additional switching can be done by type, or reflection can be used to find properties for different classes and construct different classes.
public class SerializationMethod {
    public object ProcessByKind (string kindToUse, SerializationAction action, object objectToProcess) {
        if (kindToUse == "FirstKind") {
            if (action == SerializationAction.ToBytes) {
                return new byte[1]; 
            }

            return new SomeClass(); // These would need to be your hard implementations. Not clean.
        } else {
            throw new NotImplementedException();    
        }
    }
}

// This struct type defines the serialization method and is required for the interface implementation
public struct ByteSerialization
{
    public string SerializationTypeName { get; private set; }
    public ByteSerialization(string kindToUse) {
        if (!SerializationKindList.Contains(kindToUse)) {
            throw new ArgumentException();
        }

        SerializationTypeName = kindToUse;
    }
    public byte[] Deserialize(object objectToProcess) {
        var serializationMethod = new SerializationMethod();
        return (byte[])serializationMethod.ProcessByKind(this.SerializationTypeName, SerializationAction.ToBytes, objectToProcess);
    }
    public object Serialize(byte[] byteArrayToProcess) {
        var serializationMethod = new SerializationMethod();
        return serializationMethod.ProcessByKind(this.SerializationTypeName, SerializationAction.ToObject, byteArrayToProcess);
    }
}


// Interface for any class that needs to use generic serialization
public interface IByteSerializable
{
    ByteSerialization serializationType { get; }
}

// Creating extension methods for the interface to make the life easier
public static class IByteSerializableExtensions {
    public static byte[] DeserializeObjectIntoBytes(this IByteSerializable objectToProcess) {
        return objectToProcess.serializationType.Deserialize(objectToProcess);
    }
    public static void SerializeObjectFromBytes(this IByteSerializable objectToProcess, byte[] fromBytes) {
        var someObjectData = objectToProcess.serializationType.Serialize(fromBytes);
    }
}


// Abstract base class implementation with static readonly field.
// Only downside - there is no way to enforce the config of this field in the constructor from the interface.
// There also no way to make sure this field always gets set for other implementations of IByteSerializable
public abstract class SomeBaseClass : IByteSerializable
{
    private static readonly ByteSerialization _serializationType = new ByteSerialization("FirstKind");

    public ByteSerialization serializationType { get { return _serializationType; } }
}

public class SomeClass : SomeBaseClass {

}


// And here's how one would use it. You will need to create a new object of the class before serializing from bytes.
var someClass = new SomeClass();
var bytes = someClass.DeserializeObjectIntoBytes();
var someClass2 = new SomeClass();
var byteArray = new byte[1];
someClass2.SerializeObjectFromBytes(byteArray);

关于c# - 接口(interface)和对象反序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42870501/

相关文章:

c# - 在未安装 Visual Studio 的情况下以编程方式即时创建 csproj

php - OOP概念理解

php - SQL 和 PHP : Find all "assets" connected to "presentation"

Java面向对象编程: referencing subclass object

c# - 从内部派生的公共(public)接口(interface)?

java - Comparable Generic 如何使用 ist

c# - 单击时功能区按钮不触发 onAction 设置的事件

c# - 硬编码的东西是什么意思?

c# - 如何列出网络计算机上的所有打印机

c# - 模拟兄弟方法