Generic Entity Base Class

蓝咒 提交于 2019-12-04 15:47:12

Why do we have two different Id property in both IEntity and IEntity<T> interfaces?

IEntity<T> derives from IEntity, but allows you to pass a specific type that you want your Id property to be. The base interface definition IEntity has the Id property define as an object, which is not type safe and, if used in Entity Framework, would not translate to a database friendly data type.

What does the new keyword doing there? What's going on? :O

The new keyword of the property definition makes the code easier to read and understand. Since IEntity<T> has a property named Id defined that is hiding the base implementation IEntity.Id. The "new" keyword makes it easier to understand that IEntity<T>.Id hides the base implementation of IEntity.Id

A bit further

In your derived classes of the base abstract class of Entity you would be providing the type of the ID property like so:

public class DerivedEntity : Entity<int>
{
    public string AnotherProperty { get; set; }
}   

This tells the compiler that the Id property is of type "int" through the type parameter "T" of Entity<T> making easier to use and understand what the Id property is supposed to be in your derived classes.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!