Reputation: 11
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.
Upvotes: 1
Views: 1549
Reputation: 1333
"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.
Upvotes: 1
Reputation: 28332
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!
Upvotes: 1