Note that the dirty flag approach (in its simple form) works for value types (int, bool, ...) and string, but not for reference types. For instance. if the property is of type List<int> or Address , you can make it dirty without calling the setter method ( myCust.Address.City = "..." only calls the getter method).
If so, you can find a useful reflection-based approach (add the following method to your BO):
public bool IsDirty(object other) { if (other == null || this.GetType() != other.GetType()) throw new ArgumentException("other"); foreach (PropertyInfo pi in this.GetType().GetProperties()) { if (pi.GetValue(this, null) != pi.GetValue(other, null)) return true; } return false; }
You can use it as follows:
Customer customer = new Customer(); // ... set all properties if (customer.IsDirty(CustomerController.GetCustomerInformation(id))) CustomerController.Save(customer);
Panos source share