Search code examples
c#wcflistdatacontractoperationcontract

Collection return type of OperationContract and how to add to a list of DataContracts?


I have a Business Domain Object (BDO) class:

public class BDO_LIST_DEPARTMENTS
{
    public int DEPARTMENT_ID { get; set; }
    public string DEPARTMENT_NAME { get; set; }
}

And then I have a DataContract class:

[DataContract]
public class DC_LIST_DEPARTMENTS
{
    [DataMember]
    public int DEPARTMENT_ID { get; set; }

    [DataMember]
    public string DEPARTMENT_NAME { get; set; }
}

And in my service layer, I have tried to convert the BDO into a DataContract and then return a List of this DataContract, being the return type of the OperationContract:

[OperationContract]
    IList<DC_LIST_DEPARTMENTS> GetAllDepartments();

Here was my original attempt in my service layer:

public IList<DC_LIST_DEPARTMENTS> GetAllDepartments()
    {
        DC_LIST_DEPARTMENTS DCDepartment = new DC_LIST_DEPARTMENTS();
        IList<DC_LIST_DEPARTMENTS> DCListOfDepartments = new List<DC_LIST_DEPARTMENTS>();
        ICollection<BDO_LIST_DEPARTMENTS> BDOListOfDepartments = Bal.GetAllDepartments();

        foreach (BDO_LIST_DEPARTMENTS BDODepartment in BDOListOfDepartments)
        {
              DCDepartment.DEPARTMENT_ID = BDODepartment.DEPARTMENT_ID;
              DCDepartment.DEPARTMENT_NAME = BDODepartment.DEPARTMENT_NAME;
              DCListOfDepartments.Add(DCDepartment);
        }

        return DCListOfDepartments;
    }

As you can see, I have attempted to add a single DataContract to a IList<DataContract>, but when I do so, it changes every other item of that list into the same DataContract with the same details.

Therefore, my question is, what is the correct return type for an OperationContract, how do I return a list of a DataContract class and how do I add to that list without changing the data of all other items in the list?


Solution

  • define a collection data contract

    [CollectionDataContract]
    public class DC_LIST_DEPARTMENTS_LIST : List<DC_LIST_DEPARTMENTS> {
      ...
    }
    

    and use this collection data contract as return type of operation method.

    public DC_LIST_DEPARTMENTS_LIST  GetAllDepartments() {
       ...
    }
    

    and here is the modified implementation for service method:

    public DC_LIST_DEPARTMENTS_LIST   GetAllDepartments() {
    
                IList<DC_LIST_DEPARTMENTS> DCListOfDepartments = new List<DC_LIST_DEPARTMENTS>();
                ICollection<BDO_LIST_DEPARTMENTS> BDOListOfDepartments = Bal.GetAllDepartments();
    
                foreach (BDO_LIST_DEPARTMENTS BDODepartment in BDOListOfDepartments) {
                    DC_LIST_DEPARTMENTS DCDepartment = new DC_LIST_DEPARTMENTS() {
                        DEPARTMENT_ID = BDODepartment.DEPARTMENT_ID,
                        DEPARTMENT_NAME = BDODepartment.DEPARTMENT_NAME
                    };
                    DCListOfDepartments.Add(DCDepartment);
                }
    
                return DCListOfDepartments;
            }