根据ID指定何时返回null以及何时抛出Fluent nHibernate
我们正在使用一个遗留代码库,它有一个非常粗糙的数据模型。现在,我们有一个如下所示的对象映射:
using FluentNHibernate.Mapping;
using Validation.Domain;
namespace Validation.DomainMaps
{
public sealed class BookMap : SubclassMap<Book>
{
public BookMap()
{
Map(x => x.Genre);
References(x => x.Shelf, "ShelfId")
.Nullable()
.Not.LazyLoad()
.NotFound.Ignore()
.Cascade.All()
.Fetch.Join();
}
}
}
在应用程序中,一本没有书架的书的 ShelfId 为 0。 Shelf 表中没有 ID 为 0 的行,我们依赖于 nhibernate 的 .NotFound.Ignore()
返回 null,我们稍后将检查并处理。
这已经让我们走到了这一步,但是现在,当我们尝试访问 Shelf 表中没有条目的非 0 ShelfId 时,我们尝试抛出异常。
理想情况下,nhibernate 仅在找不到 Id 为非 0 的 Shelf 时才会抛出异常,并在请求 Id 为 0 的 Shelf 时返回 null。
任何帮助都将是例外!
We're working in a legacy code base that's got a pretty rough data model. Right now, we have a Object Mapping that looks like this:
using FluentNHibernate.Mapping;
using Validation.Domain;
namespace Validation.DomainMaps
{
public sealed class BookMap : SubclassMap<Book>
{
public BookMap()
{
Map(x => x.Genre);
References(x => x.Shelf, "ShelfId")
.Nullable()
.Not.LazyLoad()
.NotFound.Ignore()
.Cascade.All()
.Fetch.Join();
}
}
}
In the application, a book without a shelf will have a ShelfId of 0. There is no row in the Shelf table with an Id of 0 and we are depending on nhibernate's .NotFound.Ignore()
to return null which we'll check for and handle later on.
This has gotten us this far but, now we're trying to throw an exception when we try and access non-0 ShelfId's that don't have entries in the Shelf table.
Ideally, nhibernate would throw an exception only in the case that it could not find a Shelf with a non-0 Id and return null when asked for a Shelf with an Id of 0.
Any help would be exceptional!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这并不完美,但任何想要做这种事情的人都应该看看 此处
This isn't perfect, but anyone looking to do this kind of thing should take a gander here