简体   繁体   English

使用NHibernate的深层复制实体

[英]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. 我目前正在开始一个新的ASP.NET MVC项目,我们需要生成项目成本估算。

We are using NHibernate, ASP.NET MVC 1.0 and StructureMap. 我们正在使用NHibernate,ASP.NET MVC 1.0和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 查尔斯

I would try to have my domain solve this issue and leave NHibernate to just saving the data instead of copying it. 我会尝试让我的域解决这个问题,让NHibernate只保存数据而不是复制它。 I would create an IDeepCopy interface that defines a DeepCopy() method. 我将创建一个定义DeepCopy()方法的IDeepCopy接口。 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. 然后在每个方法中,您将创建一个新对象,并自己复制值并控制您想要如何正确处理子对象或父对象。

这可能是个坏主意,我不确定它是否会起作用,但是如果你只是通过对象图并将所有必需的id设置为默认的未保存值然后让级联保存将所有引用拼接回来的工作怎么办?在数据库中。

I recommend to use SharpArchitecture for your project. 我建议您将SharpArchitecture用于您的项目。 It'll save you a lot of time and money. 它会为你节省大量的时间和金钱。
There are no easy way to do a deep copy of data because it's very specific to a case. 没有简单的方法来执行数据的深层复制,因为它非常特定于案例。 So you have to create all copies manually. 所以你必须手动创建所有副本。
I also recommend to use different tables for draft entities and not draft ones. 我还建议对草案实体使用不同的表而不是草稿实体。 IMO in this case it'll be much more easier to implement and it doesn't violates with SRP . 在这种情况下,IMO将更容易实施,并且不会违反SRP As it was said, divide and rule. 正如所说,分而治之。

I use this function: 我用这个函数:

    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);
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM