nhibernate - Add child object to parent without querying parent -
how can add child object parent collection while keeping integrity of domain model without issuing select
statement fetch parent?
i have following model:
public class post { private readonly ilist<comment> _comments = new list<comment>(); public virtual ilist<comment> comments { { return _comments; } } public virtual void addcomment(string text) { var comment = new comment { post = this, text = text }; _comments.add(comment); } } public class comment { public virtual int id { get; set; } public virtual post post { get; set; } public virtual string text { get; set; } }
here i'm doing:
var post = session.load<post>(1); post.addcomment("test comment."); transaction.commit();
the issue approach when call addcomment()
on post
proxy nhibernate loads entire post, issuing select statement against database. protect the _comments
list having call addcomment()
add comments.
is possible keep integrity of domain models , adding children parents without querying parent?
there several ways can think of:
don't map comments list, map post of each comment. when need entire comments list, load dynamically finding comments parent add nhibernate collection without initializing collection. keep domain model integrity.
map both ways make update comment's parent, not list. lists updated next time you'll load them db. more problematic because domain not correct during process, may still ok - depends on requirements, , whether domain accessed during process (i assume not need lists, since not wish load them)
bypass nhibernate entirely. once had this. had scenario parent object had huge set of strings , needed add more strings each set. there's no parent field on string , did not wish wrap them in object. don't remember if had other reasons, did have pressing deadline , severe performance issue. created simple sql query job , did well. not solution i'm proud of, 1 of instances in felt orm not tool job. worried future changes column or table names, since mapping done in code using fluent nhibernate matter resolved.
Comments
Post a Comment