I am trying to implement ICloneable.Clone()
in an abstract base class, so that subclasses don't need to worry about it.
A fundamental responsibility of that class is to have an internal Guid
property that should be passed along to the clones - that is, it's more like a "property clone". This allows me to clone an item taken from a repository, change its properties without altering the original item, and afterwards submit the changed instance back to the repo in a way that it can recognize it by Id.
But my current implementation is facing a problem: I cannot create the instances to pass the Id along, because the class is abstract!
public abstract class RepoItem : ICloneable, IEquatable<RepoItem>
{
protected RepoItem()
{
Id = Guid.NewGuid();
}
private RepoItem(Guid id)
{
Id = id;
}
public Guid Id { get; private set; }
public object Clone()
{
return new RepoItem(Id); // cannot create instance of abstract class
}
public bool Equals(RepoItem other)
{
return other.Id == Id;
}
}
Is there a way to overcome this problem? Is this a decent design to begin with?