2

I've joined on a team with a lot of legacy code, and I'm trying to go through and make things a little neater. I see a lot of model and data classes that have copied code that looks like this:

private string _Info = null;
public string Info
{
    get 
    {
        return _Info;
    }
    set
    {
        if (Info != value)
        {
            _Info = value;
            SomeFunctionThatShouldRunOnChange();
        }
    }
}

And this pattern is copied for MANY properties (500+ lines). Similar but slightly different patterns abound where code is essentially copied for each property.

What approaches would address this, if any?

My guesses are:

  • Have a small custom class that implements this property
  • Have a generic collection backing it that does the same

I was hoping there would be a clean way to adjust a property as a pattern and use it for multiple variables.

Thanks!

  • 1
    You might want to look at C# 9 source generators: https://devblogs.microsoft.com/dotnet/introducing-c-source-generators/ - I haven't looked at them myself yet, but I suspect they'd help. – Jon Skeet Feb 01 '21 at 20:23
  • 1
    Are you saying that there is a function `SomeFunctionThatShouldRunOnChange()` that is called when any property is changed, or that each property has a different method that is being called? – bisen2 Feb 01 '21 at 20:23
  • 3
    In particular, https://github.com/dotnet/roslyn/blob/master/docs/features/source-generators.cookbook.md#inotifypropertychanged – Jon Skeet Feb 01 '21 at 20:24

0 Answers0