none
Domain object and data transfer object

    คำถาม

  • Hi,

    Is it bad practice to make a reference to a domain object (in this case a domain object through the constructor of a data transfer object)?

    Example:

    [DataContract()]
    public class DtoCompany
    {
    	private Company company;
    	
    	#region " Constructor(s) "
    
    	public DtoCompany()
    	{
    
    		this.company = new Company();
    	}
    
    	public DtoCompany(Company company)
    	{
    
    		this.company = company;
    	}
    
    	#endregion
    
    	[DataMember(name = "name")]
    	public string CompanyName {
    		get { return company.Name; }
    		set { company.Name = value;	}
    	}
    
    	public Company DomainObject {
    		get { return company; }
    	}
    }

    Kind Regards,

    Flaminio

    6 มีนาคม 2555 12:55

คำตอบ

  • Hello, this approach should work fine. As long as you don't add a DataMember on the domain object, it won't be serialized. This approach is actually recommended if the class is quite complex, because you can encapsulate domain to data contract conversion inside this class.

    Lante, shanaolanxing This posting is provided "AS IS" with no warranties, and confers no rights.
    If you have feedback about forum business, please contact msdnmg@microsoft.com. But please do not ask technical questions in the email.

    • ทำเครื่องหมายเป็นคำตอบโดย Flaminio 8 มีนาคม 2555 10:24
    7 มีนาคม 2555 2:14

ตอบทั้งหมด

  • Hello, this approach should work fine. As long as you don't add a DataMember on the domain object, it won't be serialized. This approach is actually recommended if the class is quite complex, because you can encapsulate domain to data contract conversion inside this class.

    Lante, shanaolanxing This posting is provided "AS IS" with no warranties, and confers no rights.
    If you have feedback about forum business, please contact msdnmg@microsoft.com. But please do not ask technical questions in the email.

    • ทำเครื่องหมายเป็นคำตอบโดย Flaminio 8 มีนาคม 2555 10:24
    7 มีนาคม 2555 2:14
  • Hi,

    Thanks for your response. When i use this approach with DataContractJsonSerializer i get a NullReferenceException on each member of the DTO class saying company (domain object) is null . Even when i declare a new domain object company in each constructor like above.

    string jsonString = "[{\"name\":\"my test company\"}]";
    
    // convert json string to data transfer objects
    DtoCompanies dtoReports = JSONHelper.Deserialize<DtoCompanies>(jsonString);
    
    public static T Deserialize<T>(string json)
    {
    	T obj = Activator.CreateInstance<T>();
    	MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(json));
    	DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
    	obj = (T)serializer.ReadObject(ms);
    	ms.Close();
    	ms.Dispose();
    	return obj;
    }
    

    Kind Regards,

    Flaminio

    7 มีนาคม 2555 9:24
  • Looks like DataContractJsonSerializer will not invoke the constructor. So you have to check for null in the setter.

                set
                {
                    if (this.company == null)
                    {
                        this.company = new Company();
                    }
                    company.Name = value;
                }

    If you have multiple properties, don't worry, this.company will only be initialized once.


    Lante, shanaolanxing This posting is provided "AS IS" with no warranties, and confers no rights.
    If you have feedback about forum business, please contact msdnmg@microsoft.com. But please do not ask technical questions in the email.

    8 มีนาคม 2555 3:13
  • Hi,

    Thanks again for your reponse. You are right about the constructor. I found Carlos blog on MSDN blogs:
    http://blogs.msdn.com/b/carlosfigueira/archive/2011/09/06/wcf-extensibility-serialization-callbacks.aspx

    We need to look at OnDeserializing

    Kind Regards,

    Flaminio

    8 มีนาคม 2555 10:23