nhibernate fluent repository pattern insert problem
- by voam
I am trying to use Fluent NHibernate and the repository pattern. I would like my business layer to not be knowledgeable of the data persistence layer. Ideally I would pass in an initialized domain object to the insert method of the repository and all would be well.
Where I run into problems is if the object being passed in has a child object. For example say I want to insert an a new order for a customer, and the customer is a property of the order object. I would like to do something like this:
Customer c = new Customer;
c.CustomerId = 1;
Order o = new Order;
o.Customer = c;
repository.InsertOrder(o);
The problem is that using NHiberate the CustomerId field is only privately settable so I can not set it directly like this.
so what I have ended up doing is have my repository have an interface of
Order InsertOrder(int customerId)
where all the foreign keys get passed in as parameters. Somehow this just doesn't seem right.
The other approach was to use the NHibernate session variable to load a customer object in my business model and then have the order passed in to the repository but this defeats my persistence ignorance ideal.
Should I throw this persistence ignorance out the window or am I missing something here?
Thanks