New Posts New Posts RSS Feed: Attach entities from Server Method result
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Attach entities from Server Method result

 Post Reply Post Reply
Author
kimj View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 09-May-2007
Posts: 1391
Post Options Post Options   Quote kimj Quote  Post ReplyReply Direct Link To This Post Topic: Attach entities from Server Method result
    Posted: 27-Dec-2011 at 11:04am
You can select the entities to be included in the ECS - several of the overloads for GetCacheState and SaveCacheState accept an IEnumerable of entities.  I'll post some code snippets to your other thread on this subject.
Back to Top
rbautistaole View Drop Down
Newbie
Newbie


Joined: 01-Apr-2011
Posts: 37
Post Options Post Options   Quote rbautistaole Quote  Post ReplyReply Direct Link To This Post Posted: 23-Dec-2011 at 4:20pm
Ok. I guess that if i send the ECS as parameter to my server method ALL objects in the chache are sended to th server?
Do you have an example to the techniques suggested?


Edited by rbautistaole - 23-Dec-2011 at 6:17pm
Back to Top
kimj View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 09-May-2007
Posts: 1391
Post Options Post Options   Quote kimj Quote  Post ReplyReply Direct Link To This Post Posted: 23-Dec-2011 at 3:46pm
1.  Thanks for the suggestions, we're always updating the DRC based on feedback.
2.  Well, you can use the ECS if you want, both to and from.  If not that, you can do a FindEntities to retrieve the entities wanted, and then pass them in a list or custom type.
 
Back to Top
rbautistaole View Drop Down
Newbie
Newbie


Joined: 01-Apr-2011
Posts: 37
Post Options Post Options   Quote rbautistaole Quote  Post ReplyReply Direct Link To This Post Posted: 23-Dec-2011 at 9:18am
Hi.
I have some questions about this topic
1.- SBelini wrote "The reason I am insisting on using the ECS it because that's one of the reasons the ECS is there for", i think that the wiki should be suggest in the remote server topics that using ECS is the recomended way to return object results.
2.- How can i to return or send to/from remote server methods only the modified or new objects in the cache?
3.- resuming, how can i merge the results to the client cache without use ECS?
thanks.
Back to Top
sbelini View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 13-Aug-2010
Location: Oakland
Posts: 786
Post Options Post Options   Quote sbelini Quote  Post ReplyReply Direct Link To This Post Posted: 09-Feb-2011 at 9:45am
Hi Guillermo,
 
That's definitely a better approach considering that retrieving entities by key is a far easier task than trying to attach entities that might or might not already be present in the EM.
 
Silvio.
Back to Top
gkneo View Drop Down
Newbie
Newbie
Avatar

Joined: 23-Jun-2010
Posts: 21
Post Options Post Options   Quote gkneo Quote  Post ReplyReply Direct Link To This Post Posted: 09-Feb-2011 at 12:12am
Imagine that I have a  server method which does some complicated process and returns 3 different list of entities.  If I use the EntityCacheState approach, I would have to do the following:

1.) The server method should return an object array of size 4.   One slot for the ECS and the other 3 for a list of entityKeys.  I could also use a wrapper class instead of the array.

2.) In SL client,  I would use InvokeServerMethodOperation's Result property, which contains the object array (indexes 0,1,2 are list of entityKeys; index 3 is the ECS), to restore the ECS into the EntityManager and, for each list of entityKeys, I would call EntityManager.FindEntity(EntityKey ek) so I can build up the  list of entities.  


Is there a better approach for this scenario?

Back to Top
sbelini View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 13-Aug-2010
Location: Oakland
Posts: 786
Post Options Post Options   Quote sbelini Quote  Post ReplyReply Direct Link To This Post Posted: 08-Feb-2011 at 3:37pm
Guillermo,
 
That's one of the problems sending raw entities across the wire. (i.e. it "brings" its EntityState from the server)
Even in the event the entities come back to client with Detached EntityState (you can do that manually before sending them across the wire with EM.RemoveEntity(myEntity) ) you can still face the issue of trying to attach an already existing entity in the client's EM.
 
It's still unclear why not use ECS in your case. You mention "I am using the same method for a web application, where I don't want the entitycachestate, just my wrapper class back".
In this web app where you just want the wrapper class back, I assume you don't need to attach/integrate the entities to an EM.
In the other place you actually need to merge these entites into an EM, you most likely want to consider sending the ECS. (i.e. having 2 similar methods)
 
The reason I am insisting on using the ECS it because that's one of the reasons the ECS is there for. When you merge the retrieved ECS with the EM's, all the work of combining/fixing all entities/navigation properties is done behind the scenes for you.
If you insist in using AttachEntities, you'd have to do this work, i.e. ensuring you are not attaching an entity already present in the cache.
 
Silvio.
Back to Top
gkneo View Drop Down
Newbie
Newbie
Avatar

Joined: 23-Jun-2010
Posts: 21
Post Options Post Options   Quote gkneo Quote  Post ReplyReply Direct Link To This Post Posted: 08-Feb-2011 at 2:59pm
Hi, Silvio.

I am using the same method for a web application, where I don't want the entitycachestate, just my wrapper class back.

As in my SL client using my wrapper, is it considered "ok" to have entities in unchanged state but without entity manager assigned?  It is somehow confusing, these entities should have detached states.


Guillermo





Edited by gkneo - 08-Feb-2011 at 3:00pm
Back to Top
sbelini View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 13-Aug-2010
Location: Oakland
Posts: 786
Post Options Post Options   Quote sbelini Quote  Post ReplyReply Direct Link To This Post Posted: 08-Feb-2011 at 11:43am

Hi Guillermo,

 
In the sample you produced you get the exception because some entities already exist in cache.
Sending raw Entities across the wire and using AttachEntity to incorporate them into the client's EM is not the recommended way of doing this. Also you will always get an exception if the entity being attached already exists in the EM.
 
Can you be more clear why you can't/don't want to use EntityCacheState? Sending ECS and merging it to the client's cache is the recommended way of doing this.
By sending the ECS back to the client you'd be sending only the queried entities around a wrapper. (the wrapper is not too much overhead)
 
I did, however, find an erratic behavior using AttachEntity where when attaching Territory (and the Included Region) the navigation between them is not completely resolved. I am further investigating the issue and will file a bug on the subject.
 
Silvio.
Back to Top
gkneo View Drop Down
Newbie
Newbie
Avatar

Joined: 23-Jun-2010
Posts: 21
Post Options Post Options   Quote gkneo Quote  Post ReplyReply Direct Link To This Post Posted: 07-Feb-2011 at 2:27am
I am attaching you an example project.

In the SL client, I want to attach both  TerritoryA and TerritoryB entities to the current manager.  Both entities are in unchanged state, but their EntityManager properties are set to NULL.     I have preloaded the Region entities before calling the server method (I have excluded region with id=1).   After calling the server method and getting its response in the Sl client,  TerritoryA has associated a region with id=1, so when attaching it to the current manager the exception is not thrown.  But, as TerritoryB has a region which is already in the entity manager's cache, when using Attach method the exception is thrown. 



uploads/825/SLDevforceServerMethodResult.zip

Edited by gkneo - 07-Feb-2011 at 2:27am
Back to Top
sbelini View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 13-Aug-2010
Location: Oakland
Posts: 786
Post Options Post Options   Quote sbelini Quote  Post ReplyReply Direct Link To This Post Posted: 04-Feb-2011 at 5:21pm
Hi Guillermo,
 
I couldn't reproduce the issue.
Can you provide a reproducible test case?
 
Here's what I tried:
 
[AllowRpc]
public static Employee SaveAndReturnEmployee(IPrincipal principal, EntityManager entityManager, params Object[] args) {
  Employee employee = args[0] as Employee;
  entityManager.AddEntity(employee);
 
  Order order1 = new Order();
  Order order2 = new Order();
 
  employee.Orders.Add(order1);
  employee.Orders.Add(order2);
 
  SaveResult op1 = entityManager.SaveChanges();
 
if (op1.Ok) {
   return employee;
  }
  return null;
}
 
My returned employee has an Unchanged EntityState (and the orders as well).
 
Silvio.
 
Back to Top
gkneo View Drop Down
Newbie
Newbie
Avatar

Joined: 23-Jun-2010
Posts: 21
Post Options Post Options   Quote gkneo Quote  Post ReplyReply Direct Link To This Post Posted: 04-Feb-2011 at 1:25am
Hi.

I have a Silverlight Client which invokes a server method using InvokeServerMethodAsync from the Entity Manager.

This server methods returns the custom class "CustomWrapper", which has 3 properties: Description (string), ItemA, ItemB.  Both ItemA and ItemB are of type CustomEntity which belongs to my devforce model.


My issue here is that in the client callback the CustomEntity properties (ItemA and ItemB) are filled with detached entities.  I want to know the best way to attach these entities into my entity manager without getting exceptions like  "key already in the cache"

Right know, i have in my callback:
  var tr = (CustomWrapper)args.Result;
var ce= em.FindEntity(tr.ItemA.EntityAspect.EntityKey, true) as CustomEntity;
 if (ce== null)
  em.AttachEntity(tr.ItemA);


Keep in mind that ItemA may have navigation or relatedList properties filled with other entities, and using "AttachEntity" will cause the "already key in the cache" exception.  Do I have to go all the ItemA graph tree to check and add entities?

I don't want to use CacheStateManager to restore cache  from the server result, as I use the method  in other type of client(web) so don't really need it.


Regards,

Guillermo
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down