代码中的 Nhibernate 映射(如何开始)
我正在尝试开始在 NH 3.2 中通过代码进行映射,但我有点迷失了。
我需要返回基本文档的指针,以便我能够理解我能找到的示例的含义, 例如...
public class CustomerMap : ClassMapping<Customer>
{
public CustomerMap()
{
Lazy(false);
Id(x => x.ID, map => map.Generator(Generators.HighLow,
gmap => gmap.Params(new {max_low = 100})));
Property(x => x.FirstName, map => map.NotNullable(true));
Property(x => x.LastName, map => map.NotNullable(true));
Property(x => x.Email, map =>
{
map.Unique(true);
map.Length(50);
map.NotNullable(true);
});
}
现在,可以告诉我这里到底发生了什么的文档在哪里。 ClassMapping 中有一个 Id 方法,但我不知道它可以采用哪些可能的参数或 map.Generator 类正在做什么。更进一步,x=>是多少? x.ID在做什么?根据我的理解,应该说对 x 的引用转到 x.id,但 x 在整个 plcae 中使用!?!有关 Property 函数的文档(破解整个 ClassMapping 类)会有很大帮助。
我迷路了。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先值得一提的是,这是 FluentNhibernate 项目,它允许您使用以下代码将 nhibernate 配置存储在代码中:您上面使用的示例。网站上有相当多的文档。
也就是说,我将为您提供一些快速入门指南:
ClassMapping 中有一个 Id 方法,但我不知道它可以采用哪些可能的参数或映射类正在做什么。
Id 方法采用
Expression>
,它让下面的代码获取表示 id 字段的表达式树。在简单的情况下,它会采用表达式树并生成如下所示的身份映射(如果您使用 xml 配置执行此操作。这也是一个猜测,可能是错误的):您还可以使用复合 ids,像这样:
此外,x=>是什么? x.ID在做什么?根据我的理解,应该说对 x 的引用指向 x.id,但是 x 到处都在使用!?!
我建议阅读表达式树以更好地理解它们可以做什么,这样您就可以了解如何使用 lambda 表达式来获取有关对象图的信息。
x
只是 lambda 期望的对象的占位符,该对象由传递给ClassMap
的通用参数键入。因此,x
是一个T
,在您的情况下是一个Customer
,可以由表达式树解析器检查。我希望这有帮助!
One thing that is worth mentioning first is that this is FluentNhibernate, a project that lets you store your nhibernate configuration in code using the example you used above. There's quite a bit of documentation on the website.
That said, I'll give you a few quick pointers to get you started:
There's an Id method in ClassMapping, but I how no idea what possible parameters it can take or what the map class is doing.
the Id Method takes an
Expression<Func<T, object>>
, which lets the code underneath get an expression tree that represents your id field. In the simple case you've got it will take the expression tree and generate an identity mapping like this (if you were doing this using the xml config. this is also a guess and might be wrong):You can also use composite ids, like this:
Further more, what is the x=> x.ID doing? From what I understand it should say that the reference to x goes to x.id, but x is used all over the place!?!
I recommend reading up expression trees to get a good understanding of what they can do, so you can see how lambda expressions can be used to get information about your object graph. The
x
is just a place holder for the object that the lambda expects, which is typed by the generic parameter passed toClassMap<T>
. So thex
is aT
, in your case aCustomer
, which can be inspected by the expression tree parser.I hope this helps!
该行表示“此类的 Id 字段映射到此类的名为 ID 的属性”。它是一个 lambda 表达式,x 是该 lambda 表达式的局部变量。
我不确定你所说的“引用”是什么意思...这不是 NHibernate 特定的语法。这是对泛型和 linq 表达式的巧妙利用,NHibernate 选择将其解释为映射。
至于文档,Fluent 的内容与 XML 的内容非常相似。 XML 内容的文档位于 http://www.nhforge.org/doc/ nh/en/index.html。
That line says, "The Id field for this class is mapped to a property called ID of this class". It is a lambda expression and x is a local variable to that lambda expression.
I'm not sure what you mean by 'reference' ... It's not a NHibernate specific syntax. It's a clever exploit of generics and linq expressions that NHibernate chooses to interpret as a mapping.
As for documentation, the fluent stuff closely resembles the XML stuff. Documentation for the XML stuff is here http://www.nhforge.org/doc/nh/en/index.html.