简体   繁体   English

添加属性而不接触类? (不是继承)

[英]Adding a property without touching the class? (not inheritance)

I got a requirement in my project to add another property to some class. 我的项目中有一个要求是在某个类中添加另一个属性。 Now I want to avoid changing the class because I figured it shouldn't be aware that he has this property (this property only has significance in the context of this project). 现在我想避免更改类,因为我认为它不应该知道他有这个属性(这个属性只在这个项目的上下文中有意义)。

The way I thought to accomplish this was (Please critic this because I wanna know if there are simpler ways of doing this) 我想要实现这个目标的方式是(请批评这个,因为我想知道是否有更简单的方法可以做到这一点)

  1. Adding a new singleton class that has a mapping between objects of my class and the type of the property I wanted to add 添加一个新的单例类,它在我的类的对象和我想要添加的属性的类型之间有一个映射
  2. adding in this class an extension method (extension property?) to access the mapping and fetch the property. 在此类中添加一个扩展方法(扩展属性?)来访问映射并获取属性。

Is there a simpler alternative? 有更简单的替代方案吗? Is this just unnecessary complexity? 这只是不必要的复杂性吗? Maybe I should just add a new property to my class? 也许我应该在我班上添加一个新属性?

Thanks! 谢谢!

The design you've described is actually the one used by Microsoft to implement the DependencyProperty system and, in particular, Attached Properties , though in the greater context of a binding framework. 您所描述的设计实际上是Microsoft用于实现DependencyProperty系统的设计,特别是附加属性 ,尽管在绑定框架的更大上下文中。 That said, using a dictionary with 'attached' data is a very typical solution when you need to tag a class with additional context for a particular use, but don't want to modify the class. 也就是说,使用带有“附加”数据的字典是一种非常典型的解决方案,当您需要为特定用途标记具有附加上下文的类时,但不希望修改该类。

Why do you say "not inheritance"? 为什么你说“不继承”? Surely the way to do this, if you don't want to alter the original class, would be to inherit from the original class and then add your property to the derived class? 当然,如果你不想改变原始类,那么这样做的方法是继承原始类,然后将你的属性添加到派生类中?

BTW, there are only extension methods, not properties, so you can't do it via property. 顺便说一句,只有扩展方法,而不是属性,所以你不能通过属性来做。

I would suggest the DECORATOR pattern. 我会建议DECORATOR模式。 I know you say you don't want to use inheritence, but sometimes it's cleaner to do so. 我知道你说你不想使用遗产,但有时它更干净。 The pattern only uses inheritance to define the interface. 该模式仅使用继承来定义接口。

An extension method makes sense and it's also relatively simple. 扩展方法有意义,而且也相对简单。

[visibility] [type] [methodName](this [class to extend] c, ... more args if necessary)
{
....
}

Adding a property doesn't break the class's interactions with existing clients, so that really seems the "simplest" approach. 添加属性不会破坏类与现有客户端的交互,因此这似乎是“最简单”的方法。

More important, though, is the function of the new property. 但更重要的是新物业的功能。 Is it logically part of the existing class? 它在逻辑上是现有类的一部分吗? Change the class. 改变班级。 If not, then an extension method might be preferable, but the problem then becomes the visibility of the extension method, and scope of its clients. 如果没有,那么扩展方法可能更可取,但问题就变成了扩展方法的可见性和客户端的范围。

As always, complexity is the enemy. 一如既往,复杂性是敌人。 In this case, it sounds as though the singleton is a very complex solution, and the extension method is hit-and-miss, depending on scope and visilbity issues. 在这种情况下,听起来好像单例是一个非常复杂的解决方案,并且扩展方法是根据范围和visilbity问题而命中注定。 Changing the class is simplest, and will probably make long-term maintenance much easier. 改变班级是最简单的,可能会使长期维护变得更加容易。

UPDATE: Note that extension methods are static, and that makes it pretty difficult for the extension method to hold data of any time, as a property would be exptected to do. 更新:请注意,扩展方法是静态的,这使得扩展方法很难保存任何时间的数据,因为属性将被删除。

SECOND UPDATE: If you have access to the source for the class, consider making it a partial class, and put your new property in a separate file, but part of the same partial class. 第二个更新:如果您可以访问类的源,请考虑将其设置为部分类,并将新属性放在单独的文件中,但应该是同一个部分类的一部分。 This keeps it separate from the main body of the class for maintenance purposes, and will work with most ORMs. 这样可以将它与类的主体分开以进行维护,并且可以与大多数ORM一起使用。 However, there is a restriction that the partial class members have to be in a single assembly. 但是,存在一个限制,即部分类成员必须位于单个程序集中。

Define a Nullable values with their Properties(while the Property has significance only for this project) 使用其属性定义Nullable值(而Property仅对此项目有意义)

your major problem is that you don't want to change the class itself because this requirement is ONLY for 1 project (build), i think you are considering SOLID priniciples, one of these principles is OCP (Open-Closed Principle), that is, 您的主要问题是您不想更改类本身,因为此要求仅适用于1个项目(构建),我认为您正在考虑SOLID原则,其中一个原则是OCP(开放原则),即,

your Entity must be open for extension but closed for modification 您的实体必须开放以进行扩展,但已关闭以进行修改

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

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