Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NHibernate & Cancelling Changes to Entities

This seems like it would be a common issue to be but I don't know the best way to solve it. I want to be able to send an Entity to a view, have changes be made to the entity in the view, but then cancel (remove) those changes if the user cancels out of the view. What is the proper way to do this.

Here are two options I have but I think there should be others that are better

1) Take an entity, create a clone, send the clone to the view...if changes are accepted, update the original entity with the clone's values

2) Send the entity to the view, if the user cancels, remove the entity from NHibernate's cache and reload it from the database

For (2), the issue for me would be that the old entity could still be referenced throughout my project after it has been removed from the cache.

Edit:

Ok, so the evict method is the way to go if I am implementing method (2). Thanks, I could not remember the details of that one. However, the issue of view objects referencing my old evicted entities makes the issue tough to deal with. I can't just have my view automatically update to a new entity without having custom code in each one to rebind when my custom eviction event is raised. And rebinding may not be trivial in certain cases. I need to think on this some more as I may be over complicating but at the moment, this method seems trickier.

I suspect I am going to be stuck with method (1) which has its own set of problems but will wait a bit longer to see if anyone else has some ideas.

Edit 2: Just found this. I think it pretty much covers the answer in detail and comes with a great demo project - Building a Desktop To-Do Application with NHibernate - http://msdn.microsoft.com/en-us/magazine/ee819139.aspx

In addition to this, NHibernate has a Session.Refresh(Object entity) function which seems to solve the exact problem. So, when an entity is changed but then cancelled before save, I can just call Session.Refresh to reload it from the database and discard the changes.

like image 798
i8abug Avatar asked Nov 06 '22 14:11

i8abug


1 Answers

I'll go for option 1 and use what is called a ViewModel instead of your entity. The ViewModel is representation of you model for a specific view. In the ViewModel you can mix data from different entities and pre-format values to fit the view. Is an elegant way of passing data to a view and you can accomplish what you want easily.

Using ViewModels is becoming the preferred way of working in ASP.net MVC and Silverlight / WPF.

To read more about Viewmodels: http://blogs.msdn.com/dphill/archive/2009/01/31/the-viewmodel-pattern.aspx

like image 108
Ariel Popovsky Avatar answered Nov 15 '22 06:11

Ariel Popovsky