Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to shorten a property, with repeating get and setter

I have a problem with repeating code and would like to know a way to short the code further.

Thats how my code currently looks like:

private string _description = null;    
public string Description
{
    get
    {
        _description = GetLang(this.TabAccountLangs, "TextAccount");
        return _description;
    }
    set
    {
        if (object.Equals(value, _description))
            return;

        SetLang(this.TabAccountLangs, "TextAccount", value);

        OnPropertyChanged();
    }
}

This property and the code within can come several times within one class and in serval classes within the whole project, the only things changing are the name of the property and backing field it self, as well as the parameters from the method calls.

Now I would like to know, if there is a way to further shorten this code for example like this: (is just pseudo code)

[DoYourSuff(FirstParam=this.TabAccountLangs, SecondParam="TextAccount", ThirdParam=value)]
public string Description { get; set; }

This example would use an attribute, but maybe there is something better or if the attribute is the best way to do this. How would I implement such an attribute?

like image 203
Rand Random Avatar asked Nov 29 '22 07:11

Rand Random


1 Answers

Several answers seem worthy, but here's another option.

Have a look at Fody.
They have a plethora of plugins, some of them do quite similar things. If you can't find the one you like , you can probably modify it to do your will (and post it back to contribute to the community at the same time).

The PropertyChanged plugin for Fody,for example, will change these 51 lines of code:

public class Person : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    string givenNames;
    public string GivenNames
    {
        get { return givenNames; }
        set
        {
            if (value != givenNames)
            {
                givenNames = value;
                OnPropertyChanged("GivenNames");
                OnPropertyChanged("FullName");
            }
        }
    }

    string familyName;
    public string FamilyName
    {
        get { return familyName; }
        set 
        {
            if (value != familyName)
            {
                familyName = value;
                OnPropertyChanged("FamilyName");
                OnPropertyChanged("FullName");
            }
        }
    }

    public string FullName
    {
        get
        {
            return string.Format("{0} {1}", GivenNames, FamilyName);
        }
    }

    public virtual void OnPropertyChanged(string propertyName)
    {
        var propertyChanged = PropertyChanged;
        if (propertyChanged != null)
        {
            propertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

To 14:

[ImplementPropertyChanged]
public class Person 
{        
    public string GivenNames { get; set; }
    public string FamilyName { get; set; }

    public string FullName
    {
        get
        {
            return string.Format("{0} {1}", GivenNames, FamilyName);
        }
    }
}
like image 71
Noctis Avatar answered Dec 10 '22 16:12

Noctis