Wednesday, October 31, 2012

Get submitted Entity with server made changes back to the Client when Insert on SubmitChanges()

Get submitted Entity with server made changes back to the Client when Insert on SubmitChanges()

I am working on a SOA Architecture. So I donot have access to database. I use DTO's on the server as entities.I fill in an entity on the client and submit it to the server and that is when a primary key is generated for the entity.

And I use (client Side)

_customerDomainContext.SubmitChanges(SubmitChangesCallback, null);

In the callback when I try to access the newly added enity, I only can see the properties which I filled in.

I have access to the submited entity on the domain Service(server Side) as show below.

   [Insert]     public void InsertCustomerDTO(CustomerDTO customer)     {       CustomerDTO customerFromDB = CreateCustomerDTOCore(customer);     } 

Is there a way I get the newly generated Entity to the client so that I can access the entity's newly generated Key on SubmitChanges.

Any Suggestion is kindly appreciated.

Answers & Comments...

Answer: 1

Simply use the argument of the callback:

context.SubmitChanges(x =>             {                 //Contains your entities fulfilled by the server                 x.ChangeSet             }, null); 

It contains the entities RETURNED by the server after thy have been saved, so in your key field you'll find the actual key value.
In order for the above process to run, may not be obvious that you need to "edit" your entities.

[Insert] public void InsertJeopardyModel(CustomerDTO jeopardy) {     CustomerDTO customerFromDB = CreateCustomerDTOCore(customer);     jeopardy.Id = customerFromDB.Id;     jeopardy.MyProperty = customerFromDB.MyDBEquivalentProperty; } 

As you can see in the edited sample I'm actually editing the CustomerDTO object, just like Enitity Framework or LINQ2SQL does.
That edited entity will then be serialized to the client that will expose it in the Changeset property.

by : mCasamentohttp://stackoverflow.com/users/363198




No comments:

Post a Comment

Send us your comment related to the topic mentioned on the blog