views:

124

answers:

1

Let's get this out of the way first: I know that SessionFactory is immutable - I'm trying to change the Configuration at runtime and regenerate ISessionFactory.

Specifically, I have a Customer mapped that will have some fields added to its dynamic-component node at runtime. I would like to do something like this

var newSessionFactory = previousConfiguration
  .RemoveClassMapping(typeof(Customer))
  .AddXmlString(newMappingForCustomer)
  .BuildSessionFactory();

However, I don't see any obvious way to remove a mapping, is there anything I can do short of regenerating the entire Configuration?

+1  A: 

It's not possible. You'll have to regenerate the Configuration.

My initial suggestion would be that you choose a different strategy for your model.

However, if you are determined to go with this :-), you can:

  • Create a "partial" Configuration (that does not include Customer)
  • Serialize it to a MemoryStream
  • Add the "base" Customer mapping, if needed
  • Create the temporary SessionFactory
  • Retrieve whatever information you need to map Customer
  • Deserialize your saved Configuration
  • Add the Customer mapping and create your final SessionFactory
Diego Mijelshon
What different strategy Diego? If there is fields added to dynamic components, and the mapped columns being added to databases at runtime (albeit somewhat infrequently), it would seem that regenerating SessionFactory would be by far the most straightforward.
George Mauer
The different strategy is not adding fields to a database at runtime. Instead, keep a separate table with your custom attribute definitions and another with your custom attribute values.
Diego Mijelshon
Yeah thats what I initially proposed too but that strategy kills both performance and legacy compatibility doesn't it? I'm a little surprised that NH was not designed with this scenario in mind. It isn't all that rare. I certainly understand that immutability of SessionFactory is important, but who cares if the Configuration class is diced up a dozen different ways if you're going to regenerate the factory anyways.
George Mauer
You have *a lot* of assumptions there. 1) Did you measure the performance? I don't think a select can kill anything (the meta-attributes themselves would be cached, so that's a non-issue). 2) NH is very well designed, supporting every possible scenario is not what it aims to. In fact, alternative frameworks (EF, Subsonic) don't even HAVE dynamic-component. 3) It *IS* rare (not the custom attributes, but your particular implementation). 4) You can fork NH and make it easier to modify the Configuration.
Diego Mijelshon
I have to second Diego. Modifying the schema at *runtime* is definitely not common; in-fact, I'd *seriously* recommend against it. Look into EAV, that's used to solve this exact problem.
James Gregory
Thanks James, I will look at it though I doubt it will help in this case since we've got a large amount of legacy data and personnel to integrate. Still I'm sure it can help by giving me ideas.
George Mauer