代码中的 Nhibernate 映射(如何开始)

发布于 2024-12-04 23:33:39 字数 1076 浏览 1 评论 0 原文

我正在尝试开始在 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 类)会有很大帮助。

我迷路了。

I'm trying to get started with mapping by code in NH 3.2 and I more then a little lost.

I need pointers back to basic documentation so I can understand what the examples I can find mean,
for example...

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);  
                                    });  
     } 

Now, where is the documentation that can tell me what the heck is happening here. There's an Id method in ClassMapping, but I how no idea what possible parameters it can take or what the map.Generator class is doing. 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 plcae!?! Documentation on the Property function ( heack the entire ClassMapping class) would help alot.

I'm lost.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

稚然 2024-12-11 23:33:39

首先值得一提的是,这是 FluentNhibernate 项目,它允许您使用以下代码将 nhibernate 配置存储在代码中:您上面使用的示例。网站上有相当多的文档。

也就是说,我将为您提供一些快速入门指南:

ClassMapping 中有一个 Id 方法,但我不知道它可以采用哪些可能的参数或映射类正在做什么。

Id 方法采用 Expression>,它让下面的代码获取表示 id 字段的表达式树。在简单的情况下,它会采用表达式树并生成如下所示的身份映射(如果您使用 xml 配置执行此操作。这也是一个猜测,可能是错误的):

<id name="Id"><generator class="native" /></id>

您还可以使用复合 ids,像这样:

CompositeId()
    .KeyProperty(t => t.Cat)
    .KeyProperty(t => t.Color)
    .KeyProperty(t => t.Name);

此外,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):

<id name="Id"><generator class="native" /></id>

You can also use composite ids, like this:

CompositeId()
    .KeyProperty(t => t.Cat)
    .KeyProperty(t => t.Color)
    .KeyProperty(t => t.Name);

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 to ClassMap<T>. So the x is a T, in your case a Customer, which can be inspected by the expression tree parser.

I hope this helps!

濫情▎り 2024-12-11 23:33:39
 "what is the x=> x.ID doing?"

该行表示“此类的 Id 字段映射到此类的名为 ID 的属性”。它是一个 lambda 表达式,x 是该 lambda 表达式的局部变量。

"From what I understand it should say that the reference to x goes to x.id, but x is used all over the plcae!?!"

我不确定你所说的“引用”是什么意思...这不是 NHibernate 特定的语法。这是对泛型和 linq 表达式的巧妙利用,NHibernate 选择将其解释为映射。

至于文档,Fluent 的内容与 XML 的内容非常相似。 XML 内容的文档位于 http://www.nhforge.org/doc/ nh/en/index.html

 "what is the x=> x.ID doing?"

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.

"From what I understand it should say that the reference to x goes to x.id, but x is used all over the plcae!?!"

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文