WCF Serialization and Value Object Template in Driven Driven Design

Eric Evans Domain Driven Design describes a template called a value object. One of the important characteristics of an object of value is that it is immutable.

As an example, I have a Clinic value object in which I must have a name and an identifier. To make an object a value, I do not provide setters by name and identifier. In addition, to make sure that there is no invalid instance, I take the name and identifier in the constructor and do not provide in the constructor with a lower value.

Public Class Clinic {

public Clinic(string name, string id)
{
    Name = name;
    Id = id;  
}

public string Name{get; private set;}
public string Id{get; private set;}

}

, WCF, , , . , , . ?

, Unmesh

+3
2

, ISerializable SerializationInfo /:

http://theburningmonk.com/2010/04/net-tips-making-a-serializable-immutable-struct/

, , , , . , Clinic :

[Serializable]
public class Clinic : ISerializable
{
public Clinic(string name, string id)
{
    Name = name;
    Id = id;  
}

public Clinic(SerializationInfo info, StreamingContext context)
{
    Name= info.GetString("Name");
    Id= info.GetString("Id");
}

public string Name{get; private set;}
public string Id{get; private set;}

[SecurityPermission(SecurityAction.Demand, SerializationFormatter = true)]
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
    info.AddValue("Name", Name);
    info.AddValue("Id", Id);
}
}

, WCF. , Ladislav, , (DataTransferObjects), , :

// the domain object (NOT EXPOSED through the WCF service)
public class Clinic
{
public Clinic(string name, string id)
{
    Name = name;
    Id = id;  
}

public string Name{ get; private set;}
public string Id{ get; private set;}

// other methods encapsulating some business logic, etc.
...
}
// the corresponding DTO object for the domain object Clinic
// this is the type exposed through the WCF layer, that the client knows about
[DataContract]
public class ClinicDTO
{
    [DataMember]
    public string Name { get; set; }
    [DataMember]
    public string Id { get; set; }
}
// WCF service contract, NOTE it returns ClinicDTO instead of Clinic
[ServiceContract]
public interface IClinicService
{
   [OperationContract]
   ClinicDTO GetClinicById(string id);
}

ClinicDTO, , , / . , : http://theburningmonk.com/2010/02/controlling-type-conversion-in-c/

, !

+3

, . ? / ? , IMO -, , . , , , , , . , ( ).

, , WCF. DTO DTO. DTO / .

+2

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


All Articles