Welcome Guest! To enable all features please Login or Register.



How to save data objects through service calls
#1 Posted : Sunday, October 7, 2012 6:33:10 PM(UTC)

One of the standard design patterns to pass data back and forth between service operations is by using data transfer objects (DTO). With multi-tier applications that use WCF as a communication layer, DTOs are the data contracts that are used for input or output structures of the service operations, as the Xomega UI data objects that serve as great view models for the forms, don't quite lend themselves to be used as data transfer objects.

Xomega Framework provides an easy way to populate data contract DTOs from UI data objects, which is similar to populating the data objects from data contracts. Here are the steps involved in this process.
  1. Call the method ToDataContract on your data object and pass it the DTO that is used as an input to a service operation. By default, it will use reflection to copy all data properties of the data object to the corresponding simple members of the DTO with matching names. It will also try to populate any of the complex members of the DTO from the child data objects with the corresponding names.
    The following snippet shows how to populate an input structure for a service operation to update an employee record.
    Employee_UpdateInput updateDTO = new Employee_UpdateInput();

  2. If the names of any data properties or child objects don't match exactly the names of the corresponding members in the DTO, then you need to override the ToDataContract method in your data object class and handle such properties or child objects individually after calling the base method. Note that internal format, that a property stores its value in, is not necessarily suitable for assigning to a member of the DTO. Therefore, when retrieving the value of a data property that you need to set on the DTO, you should always use the TransportValue accessor and cast it to the right data type.
    The following example demonstrates how employee object overrides the ToDataContract method to set the current flag on the update input structure, since it's name doesn't match the name of the property.
    public partial class EmployeeObject
        public const string IsCurrent = "IsCurrent";
        public BooleanProperty IsCurrentProperty { get; private set; }
        protected override void OnInitialized()
            IsCurrentProperty = new BooleanProperty(this, IsCurrent);
        public override void ToDataContract(object dataContract)
            // call the base class to copy properties with the same names
            // copy properties and child objects where the DTO member names
            // don't match the property/child names (from constant strings).
            Employee_UpdateInput updateDTO = dataContract as Employee_UpdateInput;
            if (updateDTO != null)
                // cast the transport value to the appropriate type
                updateDTO.CurrentFlag = (bool)IsCurrentProperty.TransportValue;
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.