In one of the standard design patterns, service operations pass data back and forth 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, since 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 UI data objects from DTOs returned by a service call. Here are the steps involved in this process.
- Call the method FromDataContract on your data object and pass it the DTO returned by the service operation, e.g. employeeObj.FromDataContract(svcEmployee.Read(employeeId));. By default, it will use reflection to copy all simple members of the DTO to the corresponding data properties of the data object with matching names. It will also try to populate any of the child data objects from the complex members of the DTO with the corresponding names.
- 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 FromDataContract method in your data object class and handle such properties or child objects individually after calling the base method. The following example demonstrates this step.
Code:
public partial class EmployeeObject
{
public const string IsCurrent = "IsCurrent";
public const string PayHistoryList = "PayHistoryList";
public BooleanProperty IsCurrentProperty { get; private set; }
public DataObjectList<EmployeePayHistory> PayHistoryChildList
{
get { return (DataObjectList<EmployeePayHistory>)GetChildObject(PayHistory); }
}
protected override void OnInitialized()
{
IsCurrentProperty = new BooleanProperty(this, IsCurrent);
AddChildObject(PayHistoryList, new DataObjectList<EmployeePayHistory>());
}
public override void FromDataContract(object dataContract)
{
// call the base class to copy properties with the same names
base.FromDataContract(dataContract);
// copy properties and child objects where the DTO member names
// don't match the property/child names (from constant strings).
Employee_ReadOutput readDTO = dataContract as Employee_ReadOutput;
if (readDTO != null)
{
IsCurrentProperty.Value = readDTO.CurrentFlag;
PayHistoryChildList.FromDataContract(readDTO.PayHistory);
}
}
}