Class constructor in Web API









up vote
2
down vote

favorite












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;











share|improve this question

















  • 1




    probably you can use INotifyPropertyChanged (reference: docs.microsoft.com/en-us/dotnet/api/…)
    – Kien Chu
    Nov 12 at 1:46










  • Once the Class is construted, the constructor never runs Again. To set properties afterwards, simply do: Num1 = whatever;
    – Poul Bak
    Nov 12 at 1:46






  • 1




    What you considered changing result1 to be a read-only property?
    – mjwills
    Nov 12 at 2:17














up vote
2
down vote

favorite












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;











share|improve this question

















  • 1




    probably you can use INotifyPropertyChanged (reference: docs.microsoft.com/en-us/dotnet/api/…)
    – Kien Chu
    Nov 12 at 1:46










  • Once the Class is construted, the constructor never runs Again. To set properties afterwards, simply do: Num1 = whatever;
    – Poul Bak
    Nov 12 at 1:46






  • 1




    What you considered changing result1 to be a read-only property?
    – mjwills
    Nov 12 at 2:17












up vote
2
down vote

favorite









up vote
2
down vote

favorite











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;











share|improve this question













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;








c# asp.net-core-webapi






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 12 at 1:39









Aaron Ross

196




196







  • 1




    probably you can use INotifyPropertyChanged (reference: docs.microsoft.com/en-us/dotnet/api/…)
    – Kien Chu
    Nov 12 at 1:46










  • Once the Class is construted, the constructor never runs Again. To set properties afterwards, simply do: Num1 = whatever;
    – Poul Bak
    Nov 12 at 1:46






  • 1




    What you considered changing result1 to be a read-only property?
    – mjwills
    Nov 12 at 2:17












  • 1




    probably you can use INotifyPropertyChanged (reference: docs.microsoft.com/en-us/dotnet/api/…)
    – Kien Chu
    Nov 12 at 1:46










  • Once the Class is construted, the constructor never runs Again. To set properties afterwards, simply do: Num1 = whatever;
    – Poul Bak
    Nov 12 at 1:46






  • 1




    What you considered changing result1 to be a read-only property?
    – mjwills
    Nov 12 at 2:17







1




1




probably you can use INotifyPropertyChanged (reference: docs.microsoft.com/en-us/dotnet/api/…)
– Kien Chu
Nov 12 at 1:46




probably you can use INotifyPropertyChanged (reference: docs.microsoft.com/en-us/dotnet/api/…)
– Kien Chu
Nov 12 at 1:46












Once the Class is construted, the constructor never runs Again. To set properties afterwards, simply do: Num1 = whatever;
– Poul Bak
Nov 12 at 1:46




Once the Class is construted, the constructor never runs Again. To set properties afterwards, simply do: Num1 = whatever;
– Poul Bak
Nov 12 at 1:46




1




1




What you considered changing result1 to be a read-only property?
– mjwills
Nov 12 at 2:17




What you considered changing result1 to be a read-only property?
– mjwills
Nov 12 at 2:17












2 Answers
2






active

oldest

votes

















up vote
1
down vote













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;







share|improve this answer



























    up vote
    0
    down vote













    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 numi 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);







    share|improve this answer




















      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53255001%2fclass-constructor-in-web-api%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      1
      down vote













      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;







      share|improve this answer
























        up vote
        1
        down vote













        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;







        share|improve this answer






















          up vote
          1
          down vote










          up vote
          1
          down vote









          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;







          share|improve this answer












          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;








          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 12 at 2:53









          JohnB

          1,6431116




          1,6431116






















              up vote
              0
              down vote













              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 numi 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);







              share|improve this answer
























                up vote
                0
                down vote













                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 numi 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);







                share|improve this answer






















                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  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 numi 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);







                  share|improve this answer












                  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 numi 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);








                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 12 at 2:05









                  Kien Chu

                  3,585827




                  3,585827



























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53255001%2fclass-constructor-in-web-api%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      這個網誌中的熱門文章

                      Barbados

                      How to read a connectionString WITH PROVIDER in .NET Core?

                      Node.js Script on GitHub Pages or Amazon S3