locked
Updating a calculated (or derived) field based on the sum of values in a related entity RRS feed

  • Question

  • Hi,

    I have a data structure which models a set of Fee rows featuring Fee Amount field, each individual Fee has a number of Invoices related to it, each Invoice features an Invoice Amount.  Upon submission the multiple Invoice Amounts on each Invoice row are totalled and should be exactly equal to the Fee Amount on the fee row to which they are related.  The business rule I'd like to implement is that it should be impossible to submit the form if a Fee row exists which does not have Invoices associated with it whose Invoice Amounts do not total the Fee Amount.

    My approach has been to add a calculated field called InvoiceSum to the Fee entity which is implemented like this:

      public partial class Fee : INotifyPropertyChanged
      {
    
        [CustomValidation(typeof(FeeValidation), "InvoicesMustEqualFee")]
        public int InvoiceSum
        {
          get
          {
            return Convert.ToInt32(this.Amount - this.Invoices.Sum(fs => fs.Amount));
          }
        }
    
    
      }

    The calculation is done by summing the Invoices via the relationship. There is also a validation field on the derived property which compares the calculated value to the actual Fee Amount to determine if a ValidationResult message should be raised.

    The problem I have is that I cannot find a way to notify the related entity (Fee) when a value in the child table (Invoice) changes.  I have two Silverlight data grids, one displays Fees and when selecting a specific Fee the other grid is repopulated to display the related invoices.  At the moment when I have added or modified a Invoice amount the parent grid does not refresh (because nothing is causing it to).

    Can I solve this problem by generating property changed events to the related entity through the entity framework or do I need to look at refreshing the grids through events in the UI.  Bearing in mind some of the challenges inherent in customizing Domain Services classes I need to be cognizant of the re-work required when the data model changes (as it inevitably will).

    My application features a number of areas where the overall validity of the form is based on assessing the state of a number of collections.  Of course I can do this at form submission time but I want to be able to indicate graphically to users as they are completing the form that there are problems with the data they have supplied that is going to prevent successful submission.

    Any help would be greatly appreciated.

    psifry.

     


    Just because it's childish doesn't make it wrong
    Thursday, April 29, 2010 10:44 AM

Answers

All replies