具有对象关系的 nhibernate 通用存储库
我是 nhibernate 的新手,一直在研究存储库模式。我遇到的问题是我应该如何处理对象关系,特别是使用通用存储库保存新的子对象?
我是否正确地认为最好的解决方案是创建子对象类型的通用存储库的新实例并使用它来保存它们? (下面的伪代码)
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);
或者我错过了什么? 或者也许有更好的方法?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您在映射中将
Product.category
设置为cascade.SaveUpdate
(或某种其他类型的级联),则只需保存Product
对象和所有子对象将自动保存(或更新)。If you set the
Product.category
ascascade.SaveUpdate
(or some other kind of cascade) in your mapping you only need to save theProduct
object and all child object will be saved (or update) automatically.