C#属性完全相同,在两个地方定义(C# property exactly the same, defined in two places)

编程入门 行业动态 更新时间:2024-10-23 18:23:47
C#属性完全相同,在两个地方定义(C# property exactly the same, defined in two places)

我有以下类:

Defect - 表示可以在数据库中找到的一种数据 FilterQuery - 通过设置简单的布尔过滤器来提供查询数据库的方法

Defect和FilterQuery实现了相同的接口: IDefectProperties 。 该接口指定数据库中的特定字段。 不同的类具有返回Defect实例列表的方法。 使用FilterQuery ,可以为作为IDefectProperties一部分实现的特定属性指定一些过滤器,然后运行查询并返回Defect实例的列表。

我的问题是我最终在FilterQuery和Defect实现了一些完全相同的属性。 这两者本质上是不同的类别,他们只是分享一些相同的属性。 例如:

public DateTime SubmitDateAsDate { get { return DateTime.Parse(SubmitDate); } set { SubmitDate = value.ToString(); } }

这是IDefectProperties所需的属性,它依赖于不同的属性SubmitDate ,它返回一个string而不是DateTime 。 现在SubmitDate在Defect和SubmitDate中的实现方式不同,但SubmitDateAsDate完全相同。 有没有一种方法可以唯一定义SubmitDateAsDate ,但Defect和SubmitDateAsDate都将它作为属性提供? FilterQuery和Defect已经从两个不同的类继承而来,我认为无论如何他们无法分享祖先是没有意义的。 我也很乐意接受我的设计方面的建议。

I have the following classes:

Defect - represents a type of data that can be found in a database FilterQuery - provides a way of querying the database by setting simple Boolean filters

Both Defect and FilterQuery implement the same interface: IDefectProperties. This interface specifies particular fields that are in the database. Different classes have methods that return lists of Defect instances. With FilterQuery, you specify some filters for the particular properties implemented as part of IDefectProperties, and then you run the query and get back a list of Defect instances.

My problem is that I end up implementing some properties exactly the same in FilterQuery and Defect. The two are inherently different classes, they just share some of the same properties. For example:

public DateTime SubmitDateAsDate { get { return DateTime.Parse(SubmitDate); } set { SubmitDate = value.ToString(); } }

This is a property required by IDefectProperties that depends on a different property, SubmitDate, which returns a string instead of a DateTime. Now SubmitDate is implemented differently in Defect and FilterQuery, but SubmitDateAsDate is exactly the same. Is there a way that I can define SubmitDateAsDate in only place, but both Defect and FilterQuery provide it as a property? FilterQuery and Defect already inherit from two different classes, and it wouldn't make sense for them to share an ancestor anyway, I think. I am open to suggestions as to my design here as well.

最满意答案

如果继承不合适,并且您想要重用代码,那么您必须使用其中的任意一个组合(对于您描述的简单情况可能是过度使用)或扩展方法 。

扩展方法是模拟C#中多重继承的好方法,尽管没有扩展属性( Eric Lippert说:“也许有一天” )。 如果你愿意放弃财产语义,你可以这样做:

public static DateTime GetSubmitDateAsDate(this IDefectProperties defectProperties) { return DateTime.Parse(defectProperties.SubmitDate); } public static void SetSubmitDateAsDate(this IDefectProperties defectProperties, DateTime dateTime) { defectProperties.SubmitDate = dateTime.ToString(); }

If inheritance isn't appropriate and you want to reuse code, you have to use either composition (probably overkill for the simple case you've described) or extension methods.

Extension methods are a great way to simulate multiple inheritance in C#, though there are no extension properties ("maybe someday" says Eric Lippert). If you're willing to give up property semantics, you could do this:

public static DateTime GetSubmitDateAsDate(this IDefectProperties defectProperties) { return DateTime.Parse(defectProperties.SubmitDate); } public static void SetSubmitDateAsDate(this IDefectProperties defectProperties, DateTime dateTime) { defectProperties.SubmitDate = dateTime.ToString(); }

更多推荐

本文发布于:2023-07-24 00:21:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1239206.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:完全相同   属性   定义   两个   地方

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!