This is a migrated thread and some comments may be shown as answers.

Binding to an ObservationCollection of a class with Equals and GetHashCode overrided

1 Answer 75 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Mauro
Top achievements
Rank 1
Mauro asked on 14 Sep 2012, 08:53 AM
Greetings,
i have the following problem:

for my domain model i have a base abstract class that override the equals and gethash code. I made this cause i work with NHibernate with differents unit of works, so i open and close session very often. At the bottom of topic you can find the class.

I found some problems using an observable collection that use the class inherited from ModelBase.
The problem arises when i use the ShowInsertRow on the gridview.
1. If i start adding a new row, if i cancel the process -> cannot see the added row, but it is in the observable collection and if i reorder the gridview i can see it.
2. For each row created (like that i would canceled in point 1, or just cause i had committed it) i cannot delete it.

Do you have any suggestion?

If i take on account to start use your orm, how is the best practice to manage this? does it need to override?

Thx in advance
Mauro

P.s.: sorry for my poor english, tell me if something is not clear

 public abstract class ModelBase<TPKey> : dlcts.IModelBase<TPKey>, INotifyPropertyChanged {
   ///EDIT: eliminated wrong comment, Id is the PrimaryKey of the entity in the database
    public virtual TPKey Id { get; set; }
    /// <summary>
    /// Return if the implementation of the PrimaryKey is not equals the default of its type
    /// </summary>
    public virtual bool IsPersisted { get { return !Id.Equals(default(TPKey)); } }

    ... omitted code

    #region Equals-HashCode
    /// <summary>
    /// Check the equality of the PrimaryKey of 2 objects (if they are of the same type)
    /// </summary>
    /// <param name="obj"></param>
    /// <returns></returns>
    public override bool Equals(object obj) {
      //with this line it is all ok
      return base.Equals(obj);
       //with these 4 lines i have the problems
       ModelBase<TPKey> other = obj as ModelBase<TPKey>;
       if (other == null) return false;
       if (this.Id.Equals(default(TPKey)) || other.Id.Equals(default(TPKey))) return false;
       return this.Id.Equals(other.Id);
    }
    /// <summary>
    /// Return the HashCode of the primary key
    /// </summary>
    /// <returns></returns>
    public override int GetHashCode() {
      //with this line it is all ok 
      return base.GetHashCode(); 
      //with this line i have the problem
      return this.Id.GetHashCode();
       
    }
    #endregion
}

1 Answer, 1 is accepted

Sort by
0
Mauro
Top achievements
Rank 1
answered on 25 Sep 2012, 02:37 PM
I resolved my problem starting to use a Guid as the primary key.
With NHibernate i set the generation of the primary key on "assigned".
On the AddingNewDataItem i always supply an item with the primary key already generated (Guid.NewGuid())
* using the version in nhibernate permit it to resolve the correct operation (Save or Update) during the persistence action
In the future, if i will going to use distribuited databases the guid could help me on keep the replica without problem (or so i hope) and also on nhibernate documentation they suggest to move on these types of primary keys that do not request additionals round-trips to database.

Hope this could help anyone who had my same problem.
Tags
GridView
Asked by
Mauro
Top achievements
Rank 1
Answers by
Mauro
Top achievements
Rank 1
Share this question
or