复制对象图并在nhibernate中作为新的持久化 [英] duplicate object graph and persist back as new in nhibernate

查看:70
本文介绍了复制对象图并在nhibernate中作为新的持久化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建以前的版本",以便用户可以撤消所做的更改并回滚到以前的版本.

I want to create "previous versions" so a user can undo changes made and roll back to a previous version.

我有一个具有各种属性的管理器对象和一组托管人员.这涉及数据库中的两个表,其中人员通过外键链接到经理.

I've got a manager object with various properties and a collection of managed staff. this relates to two tables in the database with staff linked to the manager by a foreign key.

我想做的是复制经理和他所有的员工,并将其保存为数据库,作为经理表中的新条目以及员工表中与新经理相关的一系列新条目. .

What i'd like to do is duplicate the manager and all his staff and save it back to the database as a new entry in the manager table and a series of new entries in the staff table that our related to the new manager.

我正在使用nhibernate,想知道这样做是否有聪明的方法.

I'm using nhibernate and wondered if there was a clever way of doing it with this.

我想到的唯一方法是手动:

The only way i can think of doing this is manually:

manager old = getManager(); // get the original for copying 

manager newManager = new manager(); // create a blank object
newManager .name = old.name //give the new manager the old one's props;

//cycle through the staff duplicate and add to new managers staff collection
foreach(staff s in old.staffCollection)
{
  staff newStaff = new staff();
  newstaff.name = s.name;
  newManager.staffCollection.Add(newstaff); 
}

上面的示例并不完全是我要怎么做,但是您可以理解我希望的想法.

the above example is not exactly how i'd do it but you get the idea i hope.

我已经考虑过使用反射来获取道具,而不是手动设置道具,但这与我所拥有的一样聪明.

I've thought about using reflection to get the props instead of manually setting them but that's about as clever as i've got.

nhibernate中是否有一种方法可以复制对象图并将其作为新条目持久化?

is there a way in nhibernate to copy the object graph and persist it back as new entries?

或者有人有什么好主意吗?

or has anybody got any bright ideas??

推荐答案

如果将实体标记为可序列化,则可以执行二进制序列化.

If you mark your Entities as Serializable you could do Binary Serialization.

   public static MemoryStream Serialize(object data)
    {

        MemoryStream streamMemory = new MemoryStream();
        BinaryFormatter formatter = new BinaryFormatter();
        formatter.AssemblyFormat = FormatterAssemblyStyle.Simple;

        formatter.Serialize(streamMemory, data);

        return streamMemory;
    }
    public static Object Deserialize(MemoryStream stream)
    {

        BinaryFormatter formatter = new BinaryFormatter();
        formatter.AssemblyFormat = FormatterAssemblyStyle.Simple;
        return formatter.Deserialize(stream);

    }

基本上,您将调用序列化,然后调用DeSerialize方法,这将为您提供图形的深层副本,然后您必须更新可能拥有的任何ID.

Essentially you would call the Serialize then the DeSerialize method this will give you a deep copy of your graph, then you'd have to update any ID's you might have.

请注意,我不确定nHibernates的延迟加载功能将如何发挥作用.我已经做了很多,但是没有使用从nHibernate中提取的对象.同样不要忘记在您的对象上放置Serializable.

Word of caution, I'm not sure how this will play with nHibernates lazy loading functionality. I've done this a lot but not with objects I pull from nHibernate. Also don't forget to put Serializable on your objects.

这篇关于复制对象图并在nhibernate中作为新的持久化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆