1

So with S#arp Architecture, if you have a (say) Dog Entity which has a one-to-many relationship with a Trick Entity, how do you keep the collection in sync when adding objects etc?

That is, if there is a Dog.Tricks collection, and you want to add a new Trick, whats the best practice?

In general the approach with NHibernate seems to be to hide the mutable Tricks collection (that NHibernate binds to) and show an immutable IEnumberable one. See this question for a discussion of the technique: Best practice for Handling NHibernate parent-child collections. This seems like a nice tidy way to ensure that there's only one way to add tricks - via a dog.AddTrick() method - and keep the collections in sync.

But it seems that because S#arp uses Fluent, this approach is not so easy to implement (unless I've missed something?)

So whats the equivalent best practice for S#arp?

Community
  • 1
  • 1
codeulike
  • 22,514
  • 29
  • 120
  • 167

1 Answers1

2

That is the best practice for keeping your collections in sync in S# as well. Look at the Fluent NHibernate for mapping a private field. You would need to create a mapping override for your Dog entity. This override would say that the tricks collect is accessed by a camelcase field.

http://wiki.fluentnhibernate.org/Mapping_a_collection_that_uses_a_private_backing_field

Alec
  • 691
  • 4
  • 12
  • OK. And for the `trick.Dog` property (pointing from the child back to the parent) is a `protected internal set` the best way to go? (so that it only gets set during the `dog.AddTrick()` method) – codeulike Feb 09 '11 at 18:47
  • To be honest, that could go either way. I prefer to use the protected internal, but have done it both ways depending on the scenario. – Alec Feb 10 '11 at 19:37