使用 FluentNHibernate 自动映射的奇怪异常
我的域模型中有以下实体。一个组(聚合根)包含项目(它们本身就是聚合根),但一个项目一次只能被一个组包含。下面的代码强制执行此操作。组拥有 Items 集合的原因是,在域中存在某些情况,通过组提供访问项最符合逻辑地解决这些情况。
但是,当我向组中添加项目时,我收到以下 FatalExecutionEngineError
:
运行时遇到致命错误 错误。错误的地址是 0x5f0b8442,在线程 0x99c 上。错误 代码是 0xc0000005。这个错误可能是 CLR 中或不安全或 用户代码的不可验证部分。 此错误的常见来源包括 COM 互操作的用户编组错误 或 PInvoke,这可能会损坏 堆栈。
紧接着,System.ExecutionEngineException
被抛出,没有内部异常。
UPDATE
似乎是 Item.AssignTo
方法中的 _items.Contains(item)
检查导致抛出此异常。我最初将此检查排除在示例之外,但现在我添加了它。当我删除检查时,我在 _items.Add(item);
语句中收到 IndexOutOfRangeException
。
这是我的(简化的)域模型:
public class Group : Entity
{
private List<Item> _items = new List<Item>();
public virtual IEnumerable<Item> Items
{
get
{
return _items;
}
}
public virtual void Assign(Item item)
{
if (_items.Contains(item))
{
throw new ArgumentException("Already assigned.", "item");
}
_items.Add(item);
}
}
public class Item : Entity
{
public virtual Group Group
{
get;
protected set;
}
public virtual void AssignTo(Group group)
{
group.Assign(this);
this.Group = group;
}
}
FluentNHibernate 映射覆盖:
// AutoMapping<Group>
mapping
.HasMany(n => n.Documents)
.Access.ReadOnlyPropertyThroughCamelCaseField(Prefix.Underscore)
.Cascade.Delete()
.Inverse();
因此,当我调用 Item.AssignTo
时会引发异常。当我从 Item.AssignTo
中删除 group.Assign(this);
行时,不会引发异常。当然,在这种情况下,Document.Items
会保持为空,直到稍后从存储库中检索文档为止。
我在这里做错了什么或错过了什么?
I have the following entities in my domain model. A group (an aggregate root) contains items (which are aggregate roots themselves) but an item can only be contained by one group at the time. The code below enforces this. The reason for the group to have an Items collection is that in the domain there are certain cases that are most logically solved by providing access items by means of a group.
When I add an item to my group however, I get this FatalExecutionEngineError
:
The runtime has encountered a fatal
error. The address of the error was at
0x5f0b8442, on thread 0x99c. The error
code is 0xc0000005. This error may be
a bug in the CLR or in the unsafe or
non-verifiable portions of user code.
Common sources of this bug include
user marshaling errors for COM-interop
or PInvoke, which may corrupt the
stack.
Right after that, a System.ExecutionEngineException
is thrown with no inner exception.
UPDATE
It seems that the _items.Contains(item)
check in the Item.AssignTo
method causes this exception to be thrown. I initially left this check out of the example but I have now added it. When I remove the check, I get an IndexOutOfRangeException
at the _items.Add(item);
statement.
This is my (simplified) domain model:
public class Group : Entity
{
private List<Item> _items = new List<Item>();
public virtual IEnumerable<Item> Items
{
get
{
return _items;
}
}
public virtual void Assign(Item item)
{
if (_items.Contains(item))
{
throw new ArgumentException("Already assigned.", "item");
}
_items.Add(item);
}
}
public class Item : Entity
{
public virtual Group Group
{
get;
protected set;
}
public virtual void AssignTo(Group group)
{
group.Assign(this);
this.Group = group;
}
}
The FluentNHibernate mapping overrides:
// AutoMapping<Group>
mapping
.HasMany(n => n.Documents)
.Access.ReadOnlyPropertyThroughCamelCaseField(Prefix.Underscore)
.Cascade.Delete()
.Inverse();
So, the exception is thrown when I call Item.AssignTo
. When I remove the group.Assign(this);
line from Item.AssignTo
the exception is not raised. But ofcourse, in that case Document.Items
stays empty until the document is retrieved from a repository sometime later.
What am I doing wrong or missing here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
男孩,我想知道这里到底发生了什么。我创建了一个独立的测试用例来重现该问题,但不知何故它运行得很好。当我对整个项目进行更多实验时,我发现更改
private List- _items = new List
- ();
到private IList- ; _items = new List
- ();
,问题解决了!所有这些让我怀疑 NHibernate 中的某个地方存在错误,但我以前对外部库的大多数错误怀疑最终证明是我自己的代码中的错误,所以我在提出此类指控时要小心一些。
Boy, I wonder what's really going on here. I created a standalone testcase to reproduce the problem but somehow it ran just fine. While I was experimenting some more with the full project, I found that changing
private List<Item> _items = new List<Item>();
toprivate IList<Item> _items = new List<Item>();
, solved the problem!All of this makes me suspect there is bug in NHibernate somewhere but most of my former bug suspicions regarding external libraries turned out being bugs in my own code so I'm being a bit careful making such accusations.