views:

48

answers:

2

I am trying to learn fluent nhibernate better so am doing a basic sample application from scratch, instead of using someone elses framework. However, I am finding I really don't understand what is going on in assigning mapping files. I have seen a lot of code examples which are all showing the same code, but nothing that spells it out. No description of how it works just that it works. Here is a code example that I see often.

return Fluently.Configure()
            .Database(config)
            .Mappings(m => m.FluentMappings.AddFromAssemblyOf<Entity>())
            .BuildSessionFactory();

So in the code example what is Entity? and how does that piece of code work?

Part of me thinks it is the name of the assembly, but seeing as how the namespace I am using is usually the name of the assembly the compiler complains that I am using a namespace as a type.

I feel this is important and am rather flustered by the fact I can't figure it out.

Thanks

+2  A: 

Add from assembly adds all mappings in the assembly that the type Entity belongs to. AddFromAssembly will do something along the lines of:

typeof(TEntity).GetType().GetAssembly();

This will get the assembly that Entity belongs to, it will then use reflection to find all of your mapping classes. Rather than take my word for it why not download the source and take a look for yourself :O)

s1mm0t
So do I leave the word Entity in there? Or do I need to change it based on project? If so what stipulates what the name I add in there? I'll try to look at the source and see what I can figure out. Thanks.
percent20
Entity is the name of a class in the assembly that contains your mappings.
s1mm0t
And what if I have more than one entity that has a mapping? Or is it just mention one entity with a mapping and it will add them all? Actually was thinking more about it. Everytime you are ready for a fresh call to the db you would call this. So wouldn't you add that entity at that time?
percent20
This entity is just used to tell NHibernate where the assembly is that has ALL of your mappings.This operation is very expensive so you would only ever call it once on start up of your application.
s1mm0t
Ah ok. so you can just point it to any entity in the assembly a mapping file is in and it will load them all. Cool. thanks
percent20
+1  A: 

s1mm0t has answered your question, but I'll just expand on it a bit...

In a lot of the FNH examples you see, Entity is a simple base class that usually just contains the Id property required by the classes being mapped.

All the other classes you wish to map can then inherit from Entity. This allows you to do things like change your Id type from int to GUID in a single place, etc.

BTW, you're not the only person who's struggled with the whole "fluent configuration" concept... ;-)

Going a bit off topic here, but...

Contrary to what so many open sourcers suggest, I'm not comfortable diving into a huge code base I barely understand when I need a simple question answered - at least so far. But I do want to give something back to the FNH project that I've benefitted so much from. So, I try to update the wiki and other documents once I understand how something works. Editing the wiki is very easy, and the FNH authors seem to appreciate it.

Perhaps you could do something similar?

Tom Bushell
That is a really good idea I might have to do that. Thanks for the expansion on this.
percent20