1

What would be a better approach to avoid the magic, based on the following 2 examples?


Known relationship
we know the Class which is meant

    public string Notes
    {
        get { return notes; }
        set { SetPropertyValue("Notes", ref notes, value); }
    }

Unknow relationship

we do not know the class (because multiple classes could have a property with this name) or even if the property exist here

public void SomeStuffeChanged()
{
    PropertyEditor propertyEditor = ((DetailView)View).FindItem("Anniversary") as PropertyEditor;
    // do some stuff
}


I could create a define class/es which will contain all properties as const so i would avoid redundance of my properties, but it's also work to maintain this define.


When i would go for reflection i would do just MemberName.GetMemberName<Contact>( x=> x.Anniversary) and any changed would be automatically applied, but when i have the second case where i just assume there could be this property. I can't use the show reflection because even if the code is still valid the propertyname could have changed in a other class. Also reflection add's some overhead.


Update

The Question is strongly related with DevExpress because they use "MagicStrings" all over the place and you can't change the baseclass

WiiMaxx
  • 5,322
  • 8
  • 51
  • 89
  • 1
    Isn't this what base classes (and interfaces) are made for? Otherwise a mechanism like WPF properties (but really overkill, IMO). Also note that may use `CallerMemberNameAttribute` and `nameof()` (according to C# version you're using). – Adriano Repetti Feb 11 '16 at 11:16
  • @AdrianoRepetti i'm using devexpress so i cant modify the baseclass directly, the same thing with the `FindItem` example – WiiMaxx Feb 11 '16 at 11:42
  • devexpress components are fixed but your _model classes_ are fixed too? Contact, for example. In your first example you can use caller information and in second one you can use nameof(Customer.Anniversary). No hard-coded string (again assuming that Anniversary is a property shared across your model, IPerson.Anniversary or something like that). – Adriano Repetti Feb 11 '16 at 11:44
  • @AdrianoRepetti in the second example is no "customer" i don't even know where they hide the object from "Contact" and if it is a "Contact" or a "RainbowWarrior" :) because without "TargetObjectType" it can be anything – WiiMaxx Feb 11 '16 at 11:53
  • Yes but you wrote in code "Anniversary". You can hard-code that string (pain for refactoring) or introduce a IContact interface with Anniversary member. In your second example you then write nameof(IContact.Anniversary). You still have a string but it works with refactoring tools. Formally you don't even need such interface and you may directly write nameof(Contact.Anniversary) (which returns "Anniversary") but...too hacky, IMO. Devexpress components will still get a string, it's YOU that don't hard-code it. – Adriano Repetti Feb 11 '16 at 11:58
  • 1
    Chicken-and-egg. If you don't like magic strings then you don't like Reflection. – Hans Passant Feb 11 '16 at 12:05
  • @AdrianoRepetti ok i see your Point you are right – WiiMaxx Feb 11 '16 at 12:37
  • @HansPassant when i reflect the `Member.Name` than is there no Magic string or do i miss something? – WiiMaxx Feb 11 '16 at 12:39

0 Answers0