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?
If you set the
Product.categoryascascade.SaveUpdate(or some other kind of cascade) in your mapping you only need to save theProductobject and all child object will be saved (or update) automatically.