How to create NHibernate dynamic mappings without generating HBM XML files?

I am working on a dynamic application with NHibernate. My goal is to create dynamic objects (both class type and xml mapping file) based on some data. For example, suppose I want to create a Person object dynamically at runtime and on the fly.

I am using Reflection.Emit to dynamically create a class type. To dynamically create the display, I used Ayende code. . But, unfortunately, this code does not work, because mappings does not have the Classes property. I tried to code the same as Castle ActiveRecord and Fluent NHibernate codes, but they generate HBM XML files. Since I do not want to create / create mapping files, therefore I cannot use these solutions.

Is there a way that, like Ayende's solution, should not be forced to generate HBM XML mapping files and just do everything dynamically and in memory?

+1
source share
2 answers

NHibernate 3.2 has a mapping-by-code layer that does what you want.

I'm not sure if dynamic classes will work, but it doesn't bother me to try.

+1
source

fluentnhibernate creates hbm in memory just to feed them before nhibernate. Fluentnhibernate has a nice automatic feature with costumizable conventions, perfect for this situation. Also in FNH 2.0 they work to skip hbm for better performance, but usually you will never see an out-of-memory display.

Example:

 Assembly assembly = GetDynamicallyCreatedTypesAssembly(); ISessionFactory sf = Fluently.Configure() .Database(...) .Mappings(m => m.AutoMappings.Add(AutoMap.Assembly(assembly))) .BuildSessionFactory(); 
+2
source

All Articles