Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deep copy entity with NHibernate

I am currently starting a new ASP.NET MVC Project at work where we need to generate a project cost estimate.

We are using NHibernate, ASP.NET MVC 1.0 and StructureMap.

The client wants to be able to fill all the information about the project, the information is in different pages and we need to persist in between each post back.

The client does not want to have the option to Save it under a name when it is completed, but we want to persist it in the database even when he did not save it yet. So we had the idea to create a "Draft mode", so the user will start working on his project, will fill all the pages, and it will be persisted in the database with the "Draft mode" on.

But we need to manage the drafts, I mean, when the user will start editing an existing project, we will need to create a copy of it, set the object and all its childs to Draft mode and create a copy of it in our database. We will need to alter all the references from the childs.

So, I am trying to find the best way to deep copy the objects and alter all the references, I would prefer to do not have to create a copying class for every entity that I will have to copy, maybe something more generic if this is possible.

Please let me know if you need more details or if something is unclear.

Thanks,

Charles

like image 552
Charles Ouellet Avatar asked Dec 22 '22 05:12

Charles Ouellet


2 Answers

I would try to have my domain solve this issue and leave NHibernate to just saving the data instead of copying it. I would create an IDeepCopy interface that defines a DeepCopy() method. And then in each of these method you would create a new object and just copy the values yourself and control how you want to handle children or parent objects correctly.

like image 198
Andrew Smith Avatar answered Jan 02 '23 05:01

Andrew Smith


This is probably bad idea and I'm not sure it would work, but what if you simply went through the object graph and set all required id's to the default unsaved values and then let cascading saves do the work of piecing all the references back together in the database.

like image 27
dotjoe Avatar answered Jan 02 '23 04:01

dotjoe