This is my first post and I don't have much experience in ORM/nHibernate.
I am comfortable with setting up and Configuring nHibernate now. But I have a Question - How is the best way to create a new Object that has Child Objects?
I have included a simplified example of what I'm trying to do below.
Objects UsersDetails Guid UsersDetailsId Users Users (Relationship with users on UsersId) String UsersDetailsString
Users Guid UsersId string UserName
Here is how I am currently trying to Add my new UsersDetails object to the DB.
Users ThisUser = MyUsersDal.Getusers(TheUsersID)
UsersDetails ThisUsersDetails = new UsersDetails(ThisNewGuid, ThisUser, ThisString);
Guid ThisUsersDetailsId = MyUsersDetailsDal.AddUsersDetails(ThisUsersDetails);
public int AddUsersDetails(UsersDetails usersdetails, bool NonExtension) { using (ISession session = GetSession()) { int NewID = (int)session.Save(usersdetails); session.Flush(); return NewID; } }
This was the way I assumed I would do it as all I have is the UsersId (which is passed from the frontend)!?!
because I'm Using a JSON type front end application the other option would be to construct the Object in JSON on the Client Side and then pass back a JSON string in the queryString. I'd then deserialize it on the server and pass this Object to my Dal to save. But this seems to have many pit falls to me as the object will need to be cobbled together by client side code??
The other way that I was considering was to have two Objects - One usersDetails with Child Objects.... and then another "Non Mapped" version with just the Id's (Example below).
Objects UsersDetails Guid UsersDetailsId Guid UsersId (Relationship with users on UsersId) String UsersDetailsString
Users Guid UsersId string UserName
Much easier to add my new UsersDetails object to the DB.
UsersDetails ThisUsersDetails = new UsersDetails(ThisNewGuid, MyUsersID, ThisString);
Guid ThisUsersDetailsId = MyUsersDetailsDal.AddUsersDetails(ThisUsersDetails);
public int AddUsersDetails(UsersDetails usersdetails, bool NonExtension) { using (ISession session = GetSession()) { int NewID = (int)session.Save(usersdetails); session.Flush(); return NewID; } }
I am trying to get my head around how to construct my new object and it would be much easier without the Child Objects mapped as I can just pass in the Related child ID's into the constructor..... But I lose out then when I need to display as I'd have to get the User and the UsersDetails objects separately.
Any help/advice will be much appreciated. Cheers
|