Search code examples
c#.netwcfdatacontractcircular-reference

DataContract to be used in a dedicated Data Access Layer class?


I am playing around with WCF 4.0. I have a simple service with the following as DataContract:

[DataContract]
public class WeeklySchedule
{
    [DataMember]
    public DateTime DateMon;
    [DataMember]
    public string DishMon;
    [DataMember]
    public DateTime DateTue;
    [DataMember]
    public string DishTue;
    [DataMember]
    public DateTime DateWed;
    [DataMember]
    public string DishWed;
    [DataMember]
    public DateTime DateThu;
    [DataMember]
    public string DishThu;
    [DataMember]
    public DateTime DateFri;
    [DataMember]
    public string DishFri;
}

I want to build a separate class library to hold the database access methods. I want these methods to return the WeeklySchedule objects.

How can i do this avoiding circular reference?

If i reference the DAL class to the WCF service i can use the db methodss, but the DAL class cannot "see" the DataContract class.


Solution

  • We added a third 'layer' which we call application. It has access to the DTO (your class defined in your question) and the data layer. The application layer then acts as the one that has a view of everything. It gets the data from the data layer and has the job of populating the data contract prior to returning it to the caller. So it will do a bulk of your service work.

    This avoids your circular reference. :)