1
votes

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.

2
you could treat WeeklySchedule as a DTO and populate it from yourDB entity. either in a manager class or some adapter style thing. - Mike Miller
i am sorry if i made you think that i have enough experience on OOP...:) can you please write a thing or two about your proposal? - e4rthdog
you mean not to use the WeeklySchedule class at all in my DAL class and return a plain list for example? - e4rthdog
This should make it make a bit more sense en.wikipedia.org/wiki/Data_Transfer_Object - Mike Miller
Thanks, i am reading about, but is there a way to have a common definition of my structure and share it in these 2 classes? - e4rthdog

2 Answers

2
votes

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. :)

0
votes

Due to my lack of knowledge , i thought that if i separated the DataContract and ServiceContract classes from the WCF service class i would have problem.

The solution to my question was to create a common class library for contracts and have them accessed by either the WCF class or the Data Access Layer class.