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

I'm working on a dynamic application with NHibernate. My goal is to create dynamic entities (both class type and mapping xml file) based on some data. For example suppose I want to create a Person entity dynamically in runtime and on the fly.
I use Reflection.Emit to generate class type dynamically. For creating mapping dynamically I used Ayende's code.. But unfortunately this code does not work because mappings does not have Classes property. I tried to code as same as codes of Castle ActiveRecord and Fluent NHibernate but they generate HBM XML files. As I don't want to generate/create mapping files so I can not use these solutions.
Is there any way that like Ayende's solution not to be forced to generate HBM XML mapping files and just doing everything dynamically and in memory?

fluentnhibernate creates hbm in memory just to feed them to nhibernate. fluentnhibernate has the nice automapping feature with costumizable conventions, perfect for this situation. Also in FNH 2.0 they are working to skip hbm for better performance, but normally you'll never see the mappings outside memory.
Sample:
Assembly assembly = GetDynamicallyCreatedTypesAssembly();
ISessionFactory sf = Fluently.Configure()
.Database(...)
.Mappings(m => m.AutoMappings.Add(AutoMap.Assembly(assembly)))
.BuildSessionFactory();

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 hurt to try.

Related

EclipseLink class and xml mix mappings

I wanted to mix class based JPA configuration with using annotations together with xml based configuration and it works fine in terms of Dynamic Fields Extensions - I can add to xml fields that are not defined in class and they are being used fine.
The question is how to do that with mappings - I wanted to add OneToOne or OneToMany mapping to the xml and have that working but no luck at the moment. Is that even possible to add for existing class new mapping to new class, in xml orm file?

NHIbernate 3 can't automap XmlDocument property

I'm trying to map a type where one of the properties is an XmlDocument but I get this error:
NHibernate.MappingException : An association from the table ChangeLog_TestAuditHistory refers to an unmapped class: System.Xml.XmlDocument
I am using Fluent NHibernate automappings. NHibernate version 3.0.0.4000 and Fluent NHibernate version 1.2.0.694. I know NHibernate is supposed to support xml columns, but I've never seen any examples using auto mappings.
That's probably a bug in Fluent, or a wrong convention... you probably need to tell it that XmlDocument is not an entity.
If you map it as a regular property, NH will use the correct mapping by default.

Nhibernate Mappings

Is there any way to get references to the mapping objects that NHibernate creates from the XML files? How about the ClassMap objects that FluentNhibernate creates? I wanted to create some query generation functions (for row counts, etc.) using this information. Since I went through the trouble of mapping it, I ought to have access to it in code, right?
Actually, Fluent ClassMaps are translated to XML.
The ISessionFactory exposes a GetAllClassMetadata method that is probably what you need.

Can NHibernate use internal types?

I'm a little bit of an NHibernate noobie, and I was wondering if NHibernate can work with internal types. I have a project with a bunch of internal entities, and I would like to use NHibernate within the project to access my data store. If I put the mapping files in the same assembly (or is this even necessary?), will NHibernate work with my internal entities? Or do they need to be declare public?
It can. The mapping files location is not relevant.

NHibernate (not Fluent NH) - Is it possible to reuse components?

Is there any way to reuse NHibernate components (<component>) in more than one mapping?
For example: an Address class in Employee and Customer classes - The only way to do this which I see now is to copy-paste the Address component mapping from one of the class mappings into another.
I haven't tried this with NHibernate but it worked with another OR Mapper that used XML configuration files. Create the component mapping in it's own XML file, then in the XML file that uses the component use an XML external entity to reference it. Here's a snippet from my Company mapping file:
<!DOCTYPE mappings [
<!ENTITY Address SYSTEM "MyCompany.MyApplication.Mappings.Address.xml">
]>
<mappings version="4.2" defaultNamespace="MyCompany.Model" defaultSchema="dbo">
<entity type="Company" table="Company" keyMember="CompanyId" keyType="Auto">
&Address;
Apart from the solution that Jamie proposed, using code-based mappings provides better possibilities for reuse.
Currently, there are two such projects, Fluent NHibernate and ConfORM.
Unfortunally not. I really need that feature too ;)