NHibernate,DTO和NonUniqueObjectException [英] NHibernate, DTOs and NonUniqueObjectException

查看:183
本文介绍了NHibernate,DTO和NonUniqueObjectException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用DTO模式将域对象从服务层封送到我们的存储库,然后通过NHibernate向下存储到数据库.

We're using the DTO pattern to marshal our domain objects from the service layer into our repository, and then down to the database via NHibernate.

我遇到了一个问题,我将DTO从存储库中拉出(例如CustomerDTO),然后将其转换为服务层中的域对象(客户).然后,我尝试并保存一个包含相同Customer对象的新对象(例如SalesOrder).依次将其转换为SalesOrderDTO(和CustomerDTO),以推送到存储库中.

I've run into an issue whereby I pull a DTO out of the repository (e.g. CustomerDTO) and then convert it into the domain object (Customer) in my service layer. I then try and save a new object back (e.g. SalesOrder) which contains the same Customer object. This is in turn converted to a SalesOrderDTO (and CustomerDTO) for pushing into the repository.

NHibernate不喜欢这样-它抱怨CustomerDTO是重复记录.我以为这是因为它在同一会话中拉出了第一个CustomerDTO,并且由于返回已被来回转换,因此它不能将其识别为同一对象.

NHibernate does not like this- it complains that the CustomerDTO is a duplicate record. I'm assuming that this is because it pulled out the first CustomerDTO in the same session and because the returning has been converted back and forth it cannot recognise this as the same object.

我被困在这里还是有解决办法?

Am I stuck here or is there a way around this?

谢谢

詹姆斯

推荐答案

您可以使用Lock将对象重新附加到NHibernate中的会话-例如.

You can re-attach an object to a session in NHibernate by using Lock - e.g.

_session.Lock(myDetachedObject, NHibernate.LockMode.None);

这可能会或可能不会有所帮助,具体取决于此处发生的情况.附带说明一下,将NHibernate与DTO结合使用并不是最常见的做法,NHibernate(主要)支持持久性无知这一事实意味着,通常DTO不会像其他ORM框架那样广泛使用.

which may or may not help depending on exactly what is happening here. On a side note, using DTO's with NHibernate is not the most common practice, the fact that NHibernate (mostly) supports persistence ignorance means that typically DTO's aren't as widely used as with some other ORM frameworks.

这篇关于NHibernate,DTO和NonUniqueObjectException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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