We have an n-tier app that includes a DB , on top of it EF4 , then WCF services and finally a silverlight 4 client.
We are thinking what is the best way to update a single entity property. We have a Customer entity which has about 15 properties. Now, in the client the user has changed the customer Name property and want to save this change to the DB. So two methods come in mind :
1) Just send the entire Customer entity over the WCF servie and update the DB. This is very simple, because in Update method in the server, all we need to do is attach the incoming entity to the context, change it’s objectState to modified, and hit SaveChanges().
2) Asumming all our entites have a single int key, we can expose a method in the WCF service that looks something like this:
public void UpdtaeEntityProperty(Type i_EntityType, int i_EntityId, string i_PropertyName, string i_NewValue);
In the first method, we are sending a lot of information over the wire which is not needed.
The second method sends just what is needed, plus it fits all the entites. Another advantage is that when the user is in the process of updating several fields in the UI, we have more control on throwing exceptions just as he enters the values.
In all EF4 examples\videos\books , the process of updating a value of a property always involves sending a complete object. You never see an aproach that updates a single value. It’s as if we are missing some kind of data binder in the client : A componnet that will bind a certain text field to the DB, even it’s over a WCF service.
1) Is such a solution is being in desgin for next realses of EF4/WCF ?
2) Is our solution of the UpdateEntityProperty method is a good practice ?
I think most examples you have seen handle the general case of updating detached entities which are passed around. That doesn’t mean you have to do this, it all depends on your particular scenario, since it’s a trade off generality vs. performance/custom solution.
If your entities are big and performance is important to you, by all means just send the ID and the updated value to the server – this solution will perform better, but is not very generic and depends on the properties of your entity type – but that might be ok for your project.