Linq to SQL - Save DataContext post-back?

I read the message Rica Trayla of lifecycle management DataContext and other related issues in Stackoverflow. If they contained an answer to my question, I must have missed it.

I usually follow the atomic approach and instantiate the DataContext for the unit of work, when necessary, and dispose of it later. This worked fine until I hit the script with a complex page containing a control with multiple views with multiple grids and pop-up panels that represent one unit of work. The data is stored in memory (in fact, I find the root object in the session so that the entire hierarchy is accessible via post-backs). Obviously, the DataContext is long gone after the user clicks "Save."

Tom Brune’s comment first got my attention because it seemed like such an elegant approach - to use reflection for a “wet” fresh copy of an object and update the database using the new DataContext. However, “Rick” refers to this approach, are valid, and since my data structures are complex and hierarchical, I don’t think I will try this.

So, I leave a few options, as far as I know.

  • or use Rick’s sentence to deserialize / serialize the object and re-attach it to the new context.
  • hand-code logic that compares and updates a new copy of an object

Which one should I follow, and is there a third option, i.e. Is it possible to maintain a DataContext between post-backs? If feasible, it will require the least amount of coding since my root object has about a dozen children.

+3
source share
1 answer

My suggestion would be to go with your first marker point there and deserialize / serialize the object and then reattach it to the new context.

I have used this approach in the past and it worked well for me. I think you will run into less problems and an easier implementation ahead.

+1
source

Source: https://habr.com/ru/post/1718704/


All Articles