ssmoot wrote:
I really have to question the wisdom of reconfiguring your database schema at runtime, but you sound like you know more than your average bear, so I'll lay off. :)
Yes, I know it's unusual, and often unwise. But I'll tell you why I'm doing it, because maybe you or others know a good way to accomplish what I need. In this case, I am writing a web app that accepts file uploads where the file describes a questionnaire that needs to be made available online. The questionnaires may have any number and any type of questions on it, and a database table needs to be created to store the answers collected with the questionnaire.
One thought I had rather than creating a table for each questionnaire is to just have one table for all questionnaires, and have a binary field in the table where a HashTable or some other object that stores all the answers serializes itself. The problem with this is that I anticipate hundreds of thousands of rows to these questionnaires, and I'll want to do analysis on the answers, and using the database server's built-in aggregate functions to operate on the variables makes more sense.
sergey wrote:
As ssmoot said above, session factories are used to create sessions. They contain the configuration information, so if you want to change the configuration, you'd have to probably replace the entire factory (they are pretty much immutable). You will probably have one global ISessionFactory instance, then when you change the schema you would replace it with the newly-configured one. This would leave two session factories around for a while, until all the sessions using the first one are finished and it is garbage-collected.
Of course you'll have problems if you change the schema so that the older sessions are unable to function at all, but I guess you know that already Smile
So, as long as I am adding tables, the leftover sessions that are running when I create a new session factory should be able to finish their work just fine? There will be use cases of modifying and even removing tables, so perhaps during those operations, I can write a Monitor to prevent new sessions from starting, then wait for the concurrent ones to die out, and then create the new session factory so I don't have the concurrency issues. Does that sound best?
sergey wrote:
Yes, if your tables don't all follow some predefined structure, you would probably need a generic DataTable -> hbm converter. But then it would of course be easier to write DataTable -> SQL converter than to use SchemaExport.
Hmmm... I already have a DataTable -> SQL Server 2000 DDL converter, but wouldn't it be better for me to write a DataTable -> hbm converter so I can leverage nHibernate's knowledge of multiple database server dialects? I'm actually surprised that/if nHibernate doesn't already have a DataSet <-> hbm converter written. It seems that it would be very generally useful. Maybe I'll write a good, general one and offer it to the project. Interested, PMs?
sergey wrote:
By the way, are you going to dynamically add new entity classes, too? I don't see any big problems with it, I'm just curious.
No. I'll have a generic entity class for those dynamically created tables, and it will have a NameValueCollection or some other map in it so I can access all the questionnaire's variables and their values at runtime.
Thank you all for your ideas and suggestions. Incidentally, what I'm building here will soon be turned into an open source project. I'm excited, as it will be my first.