Suppose I have objects of type A and objects of type B. Many-to-many relationship is set. Domain model don't allow creation of new instances of type A and type B. The only way I can modify domain model:
1) For particular instance of type A add or remove references to instances of type B
2) For particular instance of type B add or remove references to instances of type A
That is there are no new instances at all. Only association table in many-to-many relationship is changing. Presentation layer and domain model are separated, so retrieved objects are always detached. Mapping files are written.
1) How can I work with such model on client side? For example:
ICollection<A> cA contains Ainstances;
ICollection<B> cB contains Binstances;
Ainstance.Remove(Binstance) - this won't work, because Ainstance.CollectionOfB does not contain object equal to Binstance. I have to find it by id, save reference to it and then remove.
I don't think that this is the right way.
2) How can I persist such model? NHibernate always throws exceptions that "a different object with the same identifier value was already associated with the session" when I write this: foreach (var Ainstance in cA) Session.SaveOrUpdate(Ainstance);
|