【问题标题】:Sharing properties among multiple C# Classes在多个 C# 类之间共享属性
【发布时间】:2016-08-11 16:28:52
【问题描述】:

我试图在这里最大限度地重用我的代码。我有一个SearchQueryDto,其字段如下:

public class SearchQueryDto{
    // some other properties 
    public string SearchUsername { get; set; }         // repetition 1
    public string SearchProfile { get; set; }          // repetition 1
    public SearchInterest SearchInterest { get; set; } // repetition 1
    // some other properties 
}

然后,我有一个Broadcast 的数据库模型,其内容如下:

public class Broadcast {
    // some other different properties 
    public string SearchUsername { get; set; }          // repetition 1     
    public string SearchProfile { get; set; }           // repetition 1
    public SearchInterest SearchInterest { get; set; }  // repetition 1
    // some other different properties 
    public Status Status {get; set;}                    // repetition 2
    public DateTime DateStarted {get; set;}             // repetition 2
}

然后,我有一个BroadcastMessage 的数据库模型,其内容如下:

public class BroadcastMessage {
    // some other properties 
    public Status Status {get; set;}                    // repetition 2
    public DateTime DateStarted {get; set;}             // repetition 2
}

就目前而言,添加新的搜索条件意味着我必须将字段添加到我的数据库模型Broadcast 和 DTO。如何在两个类之间共享属性?继承似乎不是答案,因为我最终可能会在 Broadcast 和其他类型之间共享属性。

我正在寻找一些简单的东西,例如:

// handles repetition 1
public class SearchCriteria{
    public string SearchUsername { get; set; }
    public string SearchProfile { get; set; }
    public SearchInterest SearchInterest { get; set; }
}
// handles repetition 2
public class StatusProperties{
    public Status Status {get; set;}
    public DateTime DateStarted {get; set;}
}

public class SearchQueryDto: SearchCriteria{               
    // some other properties 
}
public class Broadcast : SearchCriteria, StatusProperties{ // multiple inheritance :(
    // some other different properties 
}
public class BroadcastMessage : StatusProperties{
    // some other properties 
}

由于Broadcast 的需要,我不认为继承会起作用,而且我认为接口不会起作用,因为我朝错误的方向迈出了一步——更新n+1 的东西而不仅仅是@987654332 @things 为了方便编译器强制继承(不是我正在寻找的方便)。

我也不想将这些属性隐藏在另一个类的实例化之后,因为我认为这里超出了范围。

【问题讨论】:

  • 也许一个解决方案是创建一个中间类来保存你想要共享的公共属性,然后你的两个类都有对该类的引用......
  • @SuperPeanut - 我喜欢这样...但是如何在不将这些对象嵌入另一个属性的情况下进行引用位?
  • 您所描述的是 DTO 的主要目标:通过限制其属性并仅选择您实际想要发送的属性,您可以避免一些可能发生的问题,例如错误地序列化惰性关系(例如,如果您使用自动映射器将数据从一个对象复制到另一个对象)。
  • 我只见过这种导致代码过于复杂且难以更改的情况。这些对象的用途不同,因此它们不需要从单个实现中提取。我不认为这是一个值得解决的问题。如果您认为编写代码很费力,请考虑代码生成
  • 我会将它们完全分开,以尽可能最短、最有说服力的方式将每个类和属性输入为副本,并编写单元测试来断言设计一致性。

标签: c# .net composition


【解决方案1】:

我不太喜欢使用共享容器类或继承,因为在我看来,这会将模型、DTO 对象、ViewModel 和其他分离的类过多地结合在一起。
我认为这是在每个类中实现属性的最佳解决方案。


但我认为使用接口来避免被遗忘的属性是没有问题的。

public interface IBroadcastMessage{
    public Status Status {get; set;}
    public DateTime DateStarted {get; set;}
}

public class Broadcast : IBroadcastMessage, ISearchQueryDto { /* ... */ }
public class BroadcastMessage : IBroadcastMessage { /* ... */ }

这无助于避免类似的代码,恰恰相反,你必须再次编写所有你的属性。但是如果你在接口中实现它,你就不能忘记在所有类中实现它。
像这样的界面在其他地方也很有帮助。例如用于传递 IBroadcastMessage 并且不在乎它是哪种确切类型。


在这种情况下,重构呢?如果您可以创建一个可以根据继承的实现过滤项目并将它们添加到列表中的基类,为什么要为每个新条件添加一个属性?只是一个想法。

【讨论】:

  • 正如我的问题中所述,继承和接口都不能解决我的特定问题。我完全了解它们提供的好处,但这些并不是我想要的好处。
  • @RobVious 我写这个答案主要是因为我第一段的第三行。
【解决方案2】:

创建两个类都有实例的对象。 SearchCriteria 的更新会更新这两个类中的属性。您的两个类(broadcast 和 profilesearchdto)保持解耦。

SearchCriteria{
    public string SearchUsername { get; set; }
    public string SearchProfile { get; set; }
    public SearchInterest SearchInterest { get; set; }
}

public class ProfileSearchDto {
    public SearchCriteria searchCriteria {get; set;}
}

public class Broadcast {
    public SearchCriteria searchCriteria {get; set;}
}

【讨论】:

  • 我正要发布这个。这是我可以在没有继承的情况下看到它的唯一方法。
  • 我真的想避免将这些属性隐藏在另一个属性后面。
  • 然后使用一个接口。
  • @wizardzz 如我的问题所述,接口不能解决我的问题。
  • @RobVious,我想看看为什么接口不能解决你的问题。如果您使用界面,还有什么额外的更新。接口和底层类?但并非所有使用该接口的类,该接口将有助于防止对属性的更改破坏,因为您不会直接访问属性。真的是 1 对 2,而不是 n 对 n+1
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多