jeudi 11 février 2016

Avoiding Magic strings Reflection vs Constant

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.





Aucun commentaire:

Enregistrer un commentaire