views:

59

answers:

1

Hi I am little confused on designing WCf services with EF.

When using WCf and EF, where do we draw this line on what properties to return and what not to with the entity.

Here is my scenario

I have User. Here are the relations.

User [1 to many] Address, User [ 1 to many] Email, User [ 1 to many] Phone

So now on the webform, on page1 I can edit user information. say I can edit few properties on the user entity and can also edit address, phone, email entities[ like add / delete and update any]

On page2, i can only update user properties and nothing related to navigation properties [ address, email, phone].

So when I return the User Entity [ OR DTO] should i be returning the navigation properties too? Or should the client make multiple calls to get navigation properites. Also, how does it go with Save? Like should the client make multiple calls to save user and related entites or just one call to save the graph?

Lets say, if I just have a Save(User user) [ where user has all the related entities too] both page1 and page2 will call save and pass me the user. but one page1 i will need a lot more information. but on page2 i just need the user primitive properties.

So my question is, where do we draw this line, how do we design theses services ? Is the WCF operation designed on the page and the fields it has ?

I am hoping i explained my problem well enough.

A: 

A classic problem - and one that doesn't have one right answer - as always, it depends :-)

The core issue is: do I retrieve a user with all its associated information (potentially making it a really big chunk of data being transferred), or do I make multiple calls to retrieve certain parts of information when needed.

I typically tend to lean more towards the second option - retrieve only as much data as you need right now - and go back for more if you really need it.

In my experience, often times, users will only use certain aspects of your app - so in some cases, someone might need the core user info, plus addresses. In another, another user would require core user info and their permission - or whatever. Hardly ever will someone need all the available data.

If you load "on demand", you do incur a bit of additional overhead and time to load the data - but typically, you're saving a lot of time transferring data that you don't need in the end (when you load everything at once).

Again: this is really just a "tendency" in my apps - I don't have any cold hard numbers to back up my observations and prove that this is the right way to go. It's just my own "best practice" for most cases.

But again: getting this right borders on black magic :-) so you'll probably just have to try one approach and see if it works (or not) and then potentially rearchitect your solution once you have some facts as to how your solution works in real life use.

marc_s
Ok, you have answered on half of my question. How does it go with Save method. Should this save method take the entire graph [ i am hoping definitely not] or just data related to the form or data that you plan on updating.
Nihilist
If you retrieve the data in small chunks, you should also save it in similarly small chunks, I would think. Only save those parts that actually have any changes - don't save a huge graph, if only a single phone number has changed etc.
marc_s
So say for example on page1 i save User with Address, one page2 just the user, on page 3 i save user with address and phone, Should i then expose three different service methods for save ?like SaveUserSaveUserAndAddressSaveUserAddressandPhone???
Nihilist
Have a `SaveUser(User)`, and one for `SaveUserAddress(UserID, Address)` and possibly a third for `SaveUserPhones(UserID, Phones)` - yes, why not? You can optimize this and make it work really well. Package this up into a `UserRepository` class which handles all those calls, and call only those methods that you really need to call (where you have any changes at all, that need to be saved)
marc_s
Hmm. I knew I could do this but didnt know if this is a good design..
Nihilist