Configure nHibernate for multiple-project solution
Im doing a project with C# winforms. This project is composed by:
alt text http://img139.imageshack.us/img139/2540/esquemaw.png
- Client project: Windows Forms where user will call the CRUD operations;
- Server project;
- Common Project: This project will hold the models (in the image only have the model Item);
- ListSingleton Project: Remote Object that will do the operations on the models;
Im trying to use a 3 layers style in which the CLIENT and the SERVER will be the views layer, the LISTSINGLETON will be the controller layer (where all objects are created), and COMMON project is the models layer...
I already have all the communication working, but now I need to work on the persistence of the data in a mysql database. I was trying to use nHibernate but I’m having some troubles.
My main problem is how to organize my hibernate configuration. - In which project do I keep the mapping? Common project?
In which project do I keep the hibernate configuration file (App.config)? ListSingleton project?
In which project do I do this:
Configuration cfg = new Configuration(); cfg.AddXmlFile("Item.hbm.xml"); ISessionFactory factory = cfg.BuildSessionFactory(); ISession session = factory.OpenSession(); ITransaction transaction = session.BeginTransaction(); Item newItem = new Item("BLAA"); // Tell NHibernate that this object should be saved session.Save(newItem); // commit all of the changes to the DB and close the ISession transaction.Commit(); session.Close();
In the ListSingleton project? Altho I had reference to the Common Project in the ListSingleton I keep getting error in the addXml line…
- And when I have 2 projects one with the models & mappin开发者_高级运维gs and another where ill use the hibernate (this project has a reference to the models project), how do i do "cfg.AddXmlFile("User.hbm.xml");" I keep geting "Could not configure datastore from file User.hbm.xml" When i use this 2 project structure.
My mapping is correct because I tried with a one-project solution and it worked :X
In which project do I keep the mapping? Common project?
Yes. The mapping files should be in the same project as the models.
In which project do I keep the hibernate configuration file (App.config)? ListSingleton project?
In the client project. I'm assuming that the client project will pass objects to ListSingleton which will act on them.
In which project do I do this:
In the client project. I do want to point out that you should only create the ISessionFactory once.
Well I found out the answers to my questions
In which project do I keep the mapping? Common project?
As Jamie Ide said, yes it should be in the models layer.
In which project do I keep the hibernate configuration file (App.config)? ListSingleton project?
In which project do I do this:
This configuration file and all hibernate configuration should be in the controller layer, so its in the ListSingleton project;
And when I have 2 projects one with the models & mappings and another where ill use the hibernate (this project has a reference to the models project), how do i do "cfg.AddXmlFile("User.hbm.xml");" I keep geting "Could not configure datastore from file User.hbm.xml" When i use this 2 project structure.
You should configure the mapping file as Embedded Resource and Copy if newer at the propierties.
精彩评论