Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It took me quite a while to get it, but I think the answer to my problem is actually deceptively simple. The best approach, as has been long advocated by the Hibernate team, is just to not override equals and gethashcode. What I did not get was that when I call Contains on a set of business objects, obviously I want to know whether that set contains an object with a specific business value. But that was something I did not get from the Nhibernate persistence set. But Stefan Steinegger put it right in a comment on a different question on this subject I was asking: 'the persistence set is not a business collection'! I completely failed to understand his remark the first time. </p> <p>The key issue was that I should not try to make the persistence set to behave as a business collection. Instead I should make use of a persistence set wrapped in a business collection. Then things get much easier. So, in my code I created a wrapper:</p> <pre><code>internal abstract class EntityCollection&lt;TEnt, TParent&gt; : IEnumerable&lt;TEnt&gt; { private readonly Iesi.Collections.Generic.ISet&lt;TEnt&gt; _set; private readonly TParent _parent; private readonly IEqualityComparer&lt;TEnt&gt; _comparer; protected EntityCollection(Iesi.Collections.Generic.ISet&lt;TEnt&gt; set, TParent parent, IEqualityComparer&lt;TEnt&gt; comparer) { _set = set; _parent = parent; _comparer = comparer; } public IEnumerator&lt;TEnt&gt; GetEnumerator() { return _set.GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } public bool Contains(TEnt entity) { return _set.Any(x =&gt; _comparer.Equals(x, entity)); } internal Iesi.Collections.Generic.ISet&lt;TEnt&gt; GetEntitySet() { return _set; } internal protected virtual void Add(TEnt entity, Action&lt;TParent&gt; addParent) { if (_set.Contains(entity)) return; if (Contains(entity)) throw new CannotAddItemException&lt;TEnt&gt;(entity); _set.Add(entity); addParent.Invoke(_parent); } internal protected virtual void Remove(TEnt entity, Action&lt;TParent&gt; removeParent) { if (_set.Contains(entity)) return; _set.Remove(entity); removeParent.Invoke(_parent); } } </code></pre> <p>This is a generic wrapper that implements the business meaning of a set. It knows when two business objects are equal by value through a IEqualityComparer, it presents itself as a true business collection exposing the entity as an enumerable of entity interfaces (much cleaner than exposing the persistence set) and it even knows how to handle bidirectional relations with the parent. </p> <p>The parent entity that owns this business collection has the following code:</p> <pre><code> public virtual IEnumerable&lt;IProduct&gt; Products { get { return _products; } } public virtual Iesi.Collections.Generic.ISet&lt;Product&gt; ProductSet { get { return _products.GetEntitySet(); } protected set { _products = new ProductCollection&lt;Brand&gt;(value, this); } } public virtual void AddProduct(IProduct product) { _products.Add((Product)product, ((Product)product).SetBrand); } public virtual void RemoveProduct(IProduct product) { _products.Remove((Product)product, ((Product)product).RemoveFromBrand); } </code></pre> <p>So, the entity has in fact two interfaces, a business interface exposing the business collection and a entity interface that is exposed to Nhibernate to deal with persistency of the collection. Note that the same persistence set is returned to Nhibernate as is passed in using the ProductSet property.</p> <p>It basically all boils down to separation of concerns:</p> <ul> <li>the persistence set is not my concern but is handled by nhibernate to persist my collecion</li> <li>the business meaning of equality by value is handled by equality comparers</li> <li>the business meaning of a set, i.e. when a set already contains an entity with the same business value, I should not be able to pass in a second different object with the same business value, is handled by the business collection object.</li> </ul> <p>Only, when I want to intermix entities between sessions I would have to resort to other solutions as mentioned above. But I think that if you can avoid that situation, you should.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload