I've just read a post about Generic Entity Base Classes. Simply and if I'm not wrong, the main idea in behind is collecting all generic, non-entity-spesific fields in one interface, than implement it in main entities. It's going to be a TL:DR; Let's see some code.
This is the base entity interface and it's generic impementation to another interface
public interface IEntity : IModifiableEntity
{
object Id { get; set; }
DateTime CreatedDate { get; set; }
DateTime? ModifiedDate { get; set; }
string CreatedBy { get; set; }
string ModifiedBy { get; set; }
byte[] Version { get; set; }
}
public interface IEntity<T> : IEntity
{
new T Id { get; set; }
}
And this is it's implementation into an abstract class
public abstract class Entity<T> : IEntity<T>
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public T Id { get; set; }
object IEntity.Id
{
get { return this.Id; }
}
public string Name { get; set; }
private DateTime? createdDate;
[DataType(DataType.DateTime)]
public DateTime CreatedDate
{
get { return createdDate ?? DateTime.UtcNow; }
set { createdDate = value; }
}
[DataType(DataType.DateTime)]
public DateTime? ModifiedDate { get; set; }
public string CreatedBy { get; set; }
public string ModifiedBy { get; set; }
[Timestamp]
public byte[] Version { get; set; }
}
It seems perfectly clear and understandable but one point about Id's. My question is(yeah, finally)
Why do we have two different Id property in both IEntity and IEntity interfaces?
What does the new keyword doing there? What's going on? :O