A more pure DDD'y feely thing would probably be to model both User
and Case
as aggregate roots and then model their relationship with a role object RelatedCase
(that's aggregated inside User
and thus should be cascade="all-delete-orphan").
Aggregate roots should be saved in repositories, which (if you delegate the call to session.Save(...)
) will give you the id you need at a time where you can use it.
Then the role can (and in my experience often will, at least after some time) contain extra information that characterizes the relationship, and does not belong on neither on the user nor on the case. Let's say the relationship keeps track on why the user and the case are related.
This way your code could look like this:
var case = caseFactory.Create("name");
caseRepository.Save(case);
user.AssignCase(case, "Assigned by some dude");
-and inside AddCase:
public void AddCase(Case case, string reason)
{
cases.Add(new RelatedCase(case, reason));
}
This is in my opinion the prettiest way to model this kind of thing, but you will of course be penalized a little bit performance-wise. If a pretty model is more important to you, then you should go for something like this.