Deep Copy Object with NHibernate

I am currently starting a new ASP.NET MVC project where we need to generate the project cost.

We use NHibernate, ASP.NET MVC 1.0, and StructureMap.

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

The client does not want to be able to save it under a name when it is completed, but we want to save it in the database, even if it has not yet saved it. Therefore, we had the idea to create a "draft mode", so the user will begin work on his project, fill in all the pages, and it will be saved in the database with the draft mode enabled.

But we need to manage drafts, I mean, when the user starts editing an existing project, we will need to create a copy of it, set the object and all its children to Drafts mode and create a copy of it in our database. We will need to change all the links from the children.

So, I'm trying to find a better way to deep copy objects and change all the links. I would prefer not to create a copy class for every object that I have to copy, maybe something more general if possible.

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

Thank,

Charles

+3
source share
4 answers

NHibernate , . IDeepCopy, DeepCopy(). , .

+8

, , , , , , , .

+1

SharpArchitecture . .
, . .
, . , SRP. , .

+1

:

    public static T DeepClone<T>(T obj)
    {
        using (var ms = new MemoryStream())
        {
            var formatter = new BinaryFormatter();
            formatter.Serialize(ms, obj);
            ms.Position = 0;

            return (T)formatter.Deserialize(ms);
        }
    }
+1

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


All Articles