I am working with an abstract class RepositoryItemBase
as follows:
public abstract class RepositoryItemBase : ICloneable, IEquatable<Dto>
{
protected internal Guid Id => Guid.NewGuid();
protected RepositoryItemBase() { }
public object Clone()
{
return MemberwiseClone();
}
public bool Equals(Dto other)
{
//??
}
}
Since the intention is to save it to a repository, I made the following design choices:
- When I call
repo.Save(repoItem)
, I want the item to be cloned upon saving, so that I can keep editing it without changing the stored reference, until I callrepo.Save(repoItem)
again; - If I call
Save
with an item with same Id but different property values, I want it to call the privaterepo.Update(repoItem)
; - If I call
Save
and a stored item with same Id also has the same property values, the method returns immediately witout actually persisting anything;
My questions are:
Are there common C#/.Net idioms / interfaces to differentiate an Id-based comparison and a property-value-based comparison? I would like to independently perform these two types of comparison.
Is
IEquatable
the best interface to implement?Does .Net have some sort of
IIdentifiable
orIIdentity
interface, with aGuid Id { get; }
property or something like that?