In his blog, Ayende suggests that using a one-to-one is probably not the best way to implement a traditional 1:1 object relationship (e.g. customer.Name == name.Customer).
One to one:
2 many-to-one:
Only reason I've come across for using one-to-many mapping is because of performance.
I've initially went with one-to-one until project hit the wall with performance issue. Problem happens because you usually can't have lazy loading for one-to-one mapping on reverse side. E.g. when you have entity A which can (but doesn't have to) have related entity B on that mapping. In that case, for each entity A you load, entity B is also loaded. This is done to prevent error with checking if related object exists. Proxy for lazy loading would mislead you to think that related entity exists, even when it does not. If you check for related entity existence like this you will be in a problem
if (entityA.EntityB == null) HandleNoEntityB();
If you use one-to-many mapping however, lazy loading is no problem, because developer is working with a collection for which we can create proxy.
if (entityA.EntitiesB.Count == 0) HandleNoEntityB();
This doesn't have to be a problem if you can make an assumption in your system that entity A always has exactly one related entity B. In that case, you should set contrained="true" on that mapping.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With