通用实体基类 [英] Generic Entity Base Class

查看:145
本文介绍了通用实体基类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚阅读了有关通用实体基类的文章.简单地说,如果我没记错的话,后面的主要思想是在一个接口中收集所有通用的,非实体专用的字段,而不是在主要实体中实现它.这将是TL:DR;让我们看一些代码.

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.

这是基本实体接口,是对另一个接口的通用实现

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; }
}

这是将其实现为抽象类

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; }
}

似乎很清楚也很容易理解,但是关于ID的一点. 我的问题是(是,最后)

It seems perfectly clear and understandable but one point about Id's. My question is(yeah, finally)

  • 为什么在IEntity和IEntity接口中都有两个不同的Id属性?

  • Why do we have two different Id property in both IEntity and IEntity interfaces?

关键字在那里做什么? 这是怎么回事? :O

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

推荐答案

为什么我们在IEntity和IEntity< T>中都具有两个不同的Id属性界面?

IEntity< T>派生自IEntity,但允许您传递希望Id属性成为的特定类型.基本接口定义IEntity的Id属性定义为一个对象,该类型不是安全类型,如果在Entity Framework中使用,则不会转换为数据库友好的数据类型.

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.

新关键字在那里做什么?这是怎么回事? :O

属性定义的new关键字使代码更易于阅读和理解.由于IEntity< T>有一个定义为名为Id的属性,该属性隐藏了基本实现IEntity.Id. "new"关键字使您更容易理解IEntity< T> .Id隐藏了IEntity.Id的基本实现.

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

再远一点

在您的Entity基础抽象类的派生类中,您将提供ID属性的类型,如下所示:

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; }
}   

这通过Entity< T>的类型参数"T"告诉编译器ID属性的类型为"int".可以更轻松地使用和了解您的派生类中的Id属性.

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.

这篇关于通用实体基类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆