IsDirty

IsDirty

Old forum URL: forums.lhotka.net/forums/t/8185.aspx


KJosh posted on Friday, December 18, 2009

Hi,
   I loaded by object using factory method by passing customer id.
  Customer cust = Customer.GetCustomer('456').
  cust = cust.Save();
  With out making any changes to cust if I call save on cust variable why it is not calling DataPortal_Update of 
  Customer class. When I debugged found that IsDirty property on cust variable is false. Is this a bug?

Thanks in advance.

rsbaker0 replied on Friday, December 18, 2009

Based on what you have posted above, this is correct.

A freshly fetched but unmodified object generally is considered "clean" until at least one property has been changed. Since you didn't change any properties, IsDirty will be indeed be false.

The default implementation of Save() explicitly tests for IsDirty, and it is a NOP if the object hasn't been changed.

KJosh replied on Friday, December 18, 2009

The problem is when I want to add an existing customer I am adding one method to the Customers which inheritingfrom BusinessListBase:

Customers : BusinessListBase<Customers, Customer>
{
     Public void AddExisitngCustomer(string customerId)
     {
            this.Add(Customer.GetCustomer(customerId);
      }
}

When Save is called, in DataPortal_Update I need to create the association between customer and my main object. Customer row already existed in database. I just want to create association. But it is not calling the Dataportal_Update. How to achieve these type of situations like adding existing one?

Thnaks

RockfordLhotka replied on Friday, December 18, 2009

You need to mark the customer object as being changed (dirty) after you retrieve it. You can do that by setting a property, or you could add an internal method to your Customer class like:

internal void Assocate()
{
  MarkDirty();
}

Then in your collection:

public void AddExistingCustomer(string customerId)
{
  var cust = Customer.GetCustomer(customerId);
  this.Add(cust);
  cust.Associate();
}

That will tell the customer object it has been "changed", so the DP_Update() method will be run.

Copyright (c) Marimer LLC