Class Design: Serializing Inside a Domain Object or Helper Class

Suppose I have some domain objects that need to be serialized / packaged using a custom binary format so that they can be sent via sockets to external processes. My first instinct would be to create an interface representing any object that can be packed into this binary format.

public interface IPackable
{
    byte[] Pack();
}

Then my domain objects (such as Entity, State, Vector, etc.) will implement this interface. This is similar to how default serialization works in .NET and Java.

public class Vector : IPackable
{
  public double X { get; set; }
  public double Y { get; set; }
  public double Z { get; set; }

  // other operators and methods...

  public byte[] Pack
  {
    MemoryStream stream = new MemoryStream();
    BinaryWriter writer = new BinaryWriter(stream);

    writer.Write(X);
    writer.Write(Y);
    writer.Write(Z);

    return stream.ToArray();
}

, (, SOLID Single Responsibility Principle), , .

. ? , (, EntityPacker, StatePacker, VectorPacker ..)?

+3
2
+1

, .NET, . SOAP, XML, , , .

.NET , XML SOAP .

, .NET, SOAP , SOAP.

+1

Source: https://habr.com/ru/post/1721845/


All Articles