Abuse of Extension Methods
In the code base I’m working with we have a number of objects which augment existing objects. For instance I have a User object which is generated by my ORM so it looks like
In order to display user objects it is useful to have the name of the company and the location which are stored in another table. To limit the amount of stuff being passed around we defined an ExtendedUser which extends User and adds the fields
Creating these extended classes requires passing in a base class and then pulling all the properties off of it and assigning them to the extended class. This is suboptimal because it means that when a new property is added to the bass class it has to be added to the code which extracts the properties in the extended class. To address this I created a method which iterates over the properties in the base class and assigns them to the extended class.
If you have sharp eyes you’ll notice that I’ve defined this method as an extension method. This allows me to do insane things like
I can also do this for any other two classes which share property names.