Aaron Ross
Aaron Ross

Reputation: 49

Class constructor in Web API

Ive been researching and I am a little stuck on finding the right answer.

Lets say I have a c# class with auto properties. I want to have some of these properties calculated based of the properties that a user will change. I understand that you can use a constructor to do this calculation on creation of a new object.

What I am trying to find out is, in a web api does the class constructor get called on an update? Do i do the following or create customer setters?

eg

class myclass 
{
    public int Num1 { get; set; }
    public int Num2 { get; set; }
    public int Num3 { get; set; }
    public int Num4 { get; set; }

    //these get changed when values above get changed by API
    public int result1 { get; set; } 
    public int result2 { get; set; }
    public int result3 { get; set; } 

    //constructor
    public myClass()
    {
        result1 = Num1 + Num2;
        result2 = Num3 + Num2;
        result3 = Num4 + Num2;
    {
}

Upvotes: 2

Views: 416

Answers (2)

jazb
jazb

Reputation: 5791

if I understand correctly, you simply need some derived readonly properties:

class myclass
{
    public myclass()
    {

    }

    public int Num1 { get; set; }
    public int Num2 { get; set; }
    public int Num3 { get; set; }
    public int Num4 { get; set; }


    public int result1 => Num1 + Num2;
    public int result2 => Num3 + Num2;
    public int result3 => Num4 + Num2;

}

Upvotes: 1

Kien Chu
Kien Chu

Reputation: 4895

What you need to use is INotifyPropertyChanged, see the minimal example below

void Main()
{
    var test = new myClass();
    test.Num1 = 1;
    test.Num2 = 2;
    test.Num3 = 3;
    test.Num4 = 4;

    test.result1.Should().Be(3);
    test.result2.Should().Be(5);
    test.result3.Should().Be(6);

    test.Num1 = 2;
    test.result1.Should().Be(4);
    test.Num2 = 0;
    test.result2.Should().Be(3);
    test.result3.Should().Be(4);
}

class myClass : INotifyPropertyChanged
{
    // private setter since these values are only being set when the num{i} are updated
    public int result1 { get; private set; }
    public int result2 { get; private set; }
    public int result3 { get; private set; }

    public event PropertyChangedEventHandler PropertyChanged;
    public myClass()
    {
        PropertyChanged += new PropertyChangedEventHandler(UpdateResultValue);
    }

    private void UpdateResultValue(object sender, PropertyChangedEventArgs e)
    {
        result1 = num1 + num2;
        result2 = num2 + num3;
        result3 = num2 + num4;
    }

    protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    protected bool SetField<T>(ref T field, T value, [CallerMemberName] string propertyName = null)
    {
        if (EqualityComparer<T>.Default.Equals(field, value)) return false;
        field = value;
        OnPropertyChanged(propertyName);
        return true;
    }

    private int num1;
    public int Num1
    {
        get => num1;
        set => SetField(ref num1, value);
    }

    private int num2;
    public int Num2
    {
        get => num2;
        set => SetField(ref num2, value);
    }

    private int num3;
    public int Num3
    {
        get => num3;
        set => SetField(ref num3, value);
    }

    private int num4;
    public int Num4
    {
        get => num4;
        set => SetField(ref num4, value);
    }
}

Upvotes: 0

Related Questions