user427165
user427165

Reputation:

nhibernate generic repositories with object relationships

I am new to nhibernate and have been looking at repository patterns. The problem I am having is how should I deal with object relationships especially saving new sub objects with a generic-repository?

Am I right in thinking that the best solution is to create a new instance of a generic-repository of the type of the sub object and use that to save them? (pseudo code below)

GenericRepository<Product> genrep1 = new GenericRepository<Product>(session);
Product prod = genrep1.find(1);
Category cat = new Category();
GenericRepository<Category>() genrep2 = new GenericRepository<Category>(session)
genrep2..save(cat);
prod.category = cat;
genrep1.save(prod);

Or am I missing something? Or perhaps there is a better way?

Upvotes: 1

Views: 223

Answers (1)

Mr Mush
Mr Mush

Reputation: 1538

If you set the Product.category as cascade.SaveUpdate (or some other kind of cascade) in your mapping you only need to save the Product object and all child object will be saved (or update) automatically.

Upvotes: 1

Related Questions