1

I don't know very much of WCF...

I want to do a clean job to serve entities on client side using DataContracts. Imagine two DataContracts "System" and "Building": "System" may have many "Buildings" and "Building" may have many "Systems". So, we have a many-to-many relationship between them.

In service contract model, "System" have a "Buildings" property that is a collection. "Building" also have a collection of "Systems".

DataContracts

The WCF uses DataSets for the underlying data access (with stored procedures for CRUD) and I have a table between SYSTEM and BUILDING representing the relationship.

Database tables

So, how can I implement this scenario cleanly? I want the clients to be able to get a simple representation of "Buildings" in "System", for example, I could use:

system = GetSystem(id);
foreach (Building building in system.Buildings) {
    // do whatever with each buildings...
}

Thank you!

6
  • 1
    What's the question - it looks like you have a workable set of data contracts. What part isn't working with what you have? Commented Mar 29, 2011 at 20:01
  • Well, I don't know! :P I suppose DataSet code has to assign row data to the property collection (ex: Buildings in System). For this, I suppose I need a proper storedproc "GetSystems" to get records of Buildings associated with it... but really, I don't know where to go exactly. Commented Mar 29, 2011 at 20:20
  • 3
    Using WCF and DataSet in the same sentence is already filling me with dread; DataSet is not a good foundation for WCF code... Commented Mar 29, 2011 at 20:32
  • Agreed! My first thought was DataSets and stored procs with WCF? Would it be an option to move to EntityFramework or NHibernate, or has the technology already been decided? Commented Mar 29, 2011 at 20:52
  • Yep, I agree with both of you! I'd much rather use EF, I have the book of Julia Lerman next to me, but I'm modifying an existing project, and it was designed this way... Beyond philosophical considerations, any idea? :) Commented Mar 29, 2011 at 21:17

1 Answer 1

1

I think this question is too broad to cover in full detail, but I can give you a few pointers to get you started.

  1. Forget about WCF and build the Data Access Layer (DAL). This should be a library which contains code to query the database and return strongly typed objects. This library might contain a method called GetBuildings() which returns a list of Building objects. The library might work with DataSets (and other database specific types), but should not expose DataSets to external callers.
  2. Now that you have a library which can be used to get data from the database, write the WCF service. Code in the service component should call into the DAL and turn that information into DataContract objects to be sent over the web service boundary. Don't try to represent all your data in the DataContract objects - you want your data packets to be relatively small, so don't include information that isn't required. Balance this with trying to make as few web service calls as possible. In designing your DataContract classes, consider what the client application will be doing with the data.
  3. Write the Service Client component. This is code which makes calls to the WCF Service, and turns that information into Entity objects.
  4. The final (and most rewarding step) is to write the client application logic. Now you have another set off issues to confront about how you will structure client code (I recommend using MVVM). The client application should call into the Service Client component, and use the data to meet the requirements of your application.

By following the above 4 steps, you should end up with:

  • A Data Access Layer that talks to the database.
  • A Service Layer, which knows nothing about the database but is able to fetch data from the Data Access Layer.
  • A Service Client layer, which knows nothing about databases but knows how to fetch data from the Service Layer.
  • Application code, which knows nothing about databases or web services, but calls into the Service Client layer to get data and presents the data to a User Interface.

Everyone will do this differently, but the main thing is to separate concerns by using a layered architecture.

Sign up to request clarification or add additional context in comments.

2 Comments

This is a great answer! But, the purpose of my question is to get a collection of child entities (ex:Buildings) into a parent entity (ex:Systems) and vice-versa. The existing project I'm working on has many examples of this "vertical" approach, like you explain in your steps: from bottom, with database access, to top, with Service Client. In other words, I have a "LINQ-to-SQL-style" implementation where I've got a 1:1 representation of my entities with datatables. I want an "Entity Framework-style" where we can manage one-to-many, many-to-many, etc, representations of entities.
Maybe it is just impossible to implement this, I'll find a workaround if it's the case...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.