简体   繁体   English

一个人如何重用相似但不同的类属性

[英]How can one reuse similar but different class properties

I would like to know the best practice to handle the below situation in C#.NET: 我想知道在C#.NET中处理以下情况的最佳实践:

I have a Table Client 我有桌子客户

ClientId
LastUpdatedBy
LastUpdatedOn

So I created a class having properties 所以我创建了一个具有属性的类

class Client
{      
    Guid ClientId {get; set;}    
    Guid LastUpdatedBy {get; set;}  
    DateTime LastUpdatedOn {get; set;}
}

Similarly, I have a Table Project 同样,我有一个表项目

ProjectId 
LastUpdatedBy   
LastUpdatedOn

So I created a Project class having properties: 因此,我创建了一个具有属性的Project类:

class Project
{     
    Guid ProjectId {get; set;}    
    Guid LastUpdatedBy {get; set;}  
    DateTime LastUpdatedOn {get; set;}
}

Likewise, I have around 100 more table and all possess these LastUpdatedBy and LastUpdatedOn columns. 同样,我还有大约100个表,并且都拥有这些LastUpdatedByLastUpdatedOn列。

In the code, is it right to create a property of these columns in each class or is there a way, I can generalize these properties and utilize those in each class? 在代码中,是否应该在每个类中创建这些列的属性,或者有一种方法可以概括这些属性并在每个类中利用它们? If so, An example would be highly appreciated. 如果是这样,一个例子将不胜感激。

Well simply rename ProjectId and ClientId Id and use a base abstract class 好吧,只需重命名ProjectIdClientId Id并使用基本抽象类

abstract class ClassBase
{
     protected Guid Id {get; set;}
     protected Guid LastUpdatedBy {get; set;}
     protected DateTime LastUpdatedOn {get; set;}
}

Then: 然后:

class Client : ClassBase
{
}

class Project : ClassBase
{
}

That's what inheritance is for: 那就是继承的目的:

public class BaseEntity
{
  Guid LastUpdatedBy {get; set;}  
  DateTime LastUpdatedOn {get; set;}
}

public class Client: BaseEntity
{
  Guid ClientId {get; set;}    
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM