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

I have a requirement in my project to add another property to some class. Now I want to avoid changing the class, because I thought that he should not know that he has this property (this property only matters in the context of this project).

The way I thought to do this (please criticize this because I want to know if there are easier ways to do this)

  • Adding a new singleton class that has a mapping between the objects of my class and the type of property that I wanted to add
  • adding an extension method (extension property?) to this class to access the display and selecting the property.

Is there a simpler alternative? Is it just too much complexity? Maybe I should just add a new property to my class?

Thanks!

+4
source share
6 answers

The design that you described is actually the one used by Microsoft to implement the DependencyProperty system and, in particular, the Attached Properties , although in a larger context of the binding structure. However, using a dictionary with “attached” data is a very typical solution when you need to mark the class with additional context for specific use, but don’t want to change the class.

+4
source

Why do you say no inheritance? Of course, the way to do this, if you do not want to change the source class, was to inherit from the source class, and then add your property to the derived class?

By the way, there are only extension methods, not properties, so you cannot do this through a property.

+2
source

I would suggest a sample DECORATOR. I know you say you don’t want to use inheritance, but sometimes it’s cleaner for that. A template uses only inheritance to define an interface.

+2
source

The extension method makes sense, and it is also relatively simple.

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

Adding a property does not interfere with the interaction of classes with existing clients, so it really seems "the easiest."

More important, however, is the function of the new property. Is this logically part of an existing class? Change the class. If not, then the extension method may be preferable, but then the problem becomes the visibility of the extension method and the volume of its clients.

As always, complexity is the enemy. In this case, it sounds as if the singleton is a very complex solution, and the extension method is “miss” and “miss” depending on the volume and visual problems. Changing a class is the easiest and is likely to facilitate long-term maintenance.

UPDATE: note that extension methods are static, and this makes it difficult for an extension method to store data at any time since this property will be output.

SECOND UPDATE: if you have access to the source for the class, consider making it an incomplete class, and put the new property in a separate file, but part of the same partial class. This allows you to separate it from the main part of the class for maintenance purposes and will work with most ORMs. However, there is a limitation that partial class members must be in the same assembly.

0
source

Define Nullable values ​​with their properties (while the property is relevant only for this project)

Your main problem is that you do not want to change the class itself, because this is a requirement ONLY for 1 project (assembly), I think that you are considering SOLID priniciples, one of these principles is OCP (principle of open closure), etc. e.

your face should be open for expansion but closed for modification

0
source

All Articles