开发者

RIAServices: Editing entities on client-side with no intention of saving

开发者 https://www.devze.com 2023-03-03 19:09 出处:网络
I often find myself needing to edit an entity on the client side in RIAServices for mere UI purposes with absolutely no intention of saving the changes back to the database. However, if I don\'t enabl

I often find myself needing to edit an entity on the client side in RIAServices for mere UI purposes with absolutely no intention of saving the changes back to the database. However, if I don't enable editing of that entity in my domain service, RIAS开发者_JS百科ervices throws and error as soon as I try to change a field on one of my entities, saying that editing has not been enabled on that entity.

But the problem is I DON'T want to allow actual editing of the entity in the database, I simply want to mess around with it on the client. I have yet to find a decent way to solve my problem, and it seems like the only option is to either go ahead and enable actual editing, and just never call SubmitChanges() (which is a very scary idea), or do a foreach loop when I load in the entities and create a new instance of each entity and copy all of its properties over (basically clone my loaded entities so that I'm not using the ones RIAServices loaded), but this is hardly practical.

What I'm looking for (if it exists) is to, after I load them, somehow "detach" my list of entities on the client side, so that RIAServices stops monitoring them and I can edit them at will (I'll lose the ability to save them back, but that's ok since I don't need or want to anyway). Any ideas? This has been driving me nuts.


You can easily detach the whole list of entities from the DomainContext by calling

DomainContext.MyEntities.Clear();

Before you do this, just copy the entities into another list so that you can access them later. (I suggest keeping them in the ViewModel.)

In short, just do this:

  1. Load the entities via RIA Services.
  2. Copy the loaded entities into a List<MyEntity> in your ViewModel.
  3. Call DomainContext.MyEntities.Clear();
  4. Modify your entities as much as you like; they will not be sent back to the server on SubmitChanges(), because they are no longer kept in the DomainContext.


SubmitChanges will simply call the necessary Insert, Update, Delete methods on your Domain Service so all you need to do is just make the methods call throw new NotImplementedException(); or just delete the methods altogether. There's no secret method for SubmitChanges that is being called.

0

精彩评论

暂无评论...
验证码 换一张
取 消