I know how to map bidirectional relationships with NHibernate, but does anyone know an effective method to make them "safe for developers."
What I mean is that as soon as one end of the relationship is established, the other end must be set, for example:
[Test] public void TestBidirectionalRelationships_WhenAddingOptionToProduct() { var product = new Product(); var productOption = new ProductOption(); product.AddOption(productOption); Assert.That(product.Options.Contains(productOption)); Assert.That(productOption.Product, Is.EqualTo(product); } [Test] public void TestBidirectionalRelationships_WhenSettingProductOnOption() { var product = new Product(); var productOption = new ProductOption(); productOption.Product = product; Assert.That(product.Options.Contains(productOption)); Assert.That(productOption.Product, Is.EqualTo(product); }
How to achieve this without making the code terribly complicated, as well as supporting NHibernate?
source share