Maintaining Dirty and New state of objects

I have worked on CSLA on few projects and I am very much impressed from all of its features. However not every application wants to use CSLA but may be some features out of it. This post is about adding IsDirty and IsNew capabilities to our object. I used CSLA as reference for create these classes.

Base Classes Definitions

ObjectBase

public class ObjectBase
{
    private bool _isDirty;
    private bool _isNew = true;

    public bool IsDirty
    {
        get { return _isDirty; }
    }

    public bool IsNew
    {
        get { return _isNew; }
    }

    protected void MarkDirty()
    {
        _isDirty = true;
    }

    protected void MarkClean()
    {
        _isDirty = false;
    }

    protected void MarkAsNew()
    {
        _isNew = true;
    }

    protected void MarkAsOld()
    {
        _isNew = false;
        _isDirty = false;
    }
}

ObjectListBase

public class ObjectListBase<T> : List<T> where T : ObjectBase
{
    public bool IsDirty
    {
        get
        {
            foreach (T item in this)
            {
                if (item.IsDirty)
                    return true;
            }
            return false;
        }
    }
}

Using Base Classes

To use these base classes, simply inherit your business object from ObjectBase and collection objects from ObjectListBase. Let take example:

Suppose a class Employee represents an employee entity and its collection class EmployeeList.

Employee

public class Employee : ObjectBase
{
    // Member Variables
    private int _employeeCode;
    private string _employeeName;
    private decimal _salary;

    // Public properties
    public int EmployeeCode
    {
        get { return _employeeCode; }
        set
        {
            if (_employeeCode != value)
            {
                _employeeCode = value;
                base.MarkDirty();
            }
        }
    }

    public string EmployeeName
    {
        get { return _employeeName; }
        set
        {
            if (_employeeName != value)
            {
                _employeeName = value;
                base.MarkDirty();
            }
        }
    }

    public decimal Salary
    {
        get { return _salary; }
        set
        {
            if (_salary != value)
            {
                _salary = value;
                base.MarkDirty();
            }
        }
    }

    // Constructor
    public Employee()
    {
    }

    public void Fetch(int employeeCode)
    {
        // Code to fetch employee
        base.MarkAsOld();
    }

    public void Save()
    {
        // Code to save employee
        if (base.IsNew)
        {
            // Code Insert into database
        }
        else
        {
            // Code to Update into databse
        }

        base.MarkClean();
        base.MarkAsOld();
    }
}

EmployeeList

public class EmployeeList : ObjectListBase<Employee>
{
    public static EmployeeList GetAllEmployees()
    {
        EmployeeList employes = new EmployeeList();
        employes.FetchAll();
    }

    protected EmployeeList()
    {
    }

    protected void FetchAll()
    {
        // Code to fetch employee list and 
        // add to this list
    }

    public void Save()
    {
        foreach (Employee employee in this)
        {
            if (employee.IsDirty)
            {
                employee.Save();
            }
        }
    }
}

Using Objects

See the code below:

EmployeeList employess = EmployeeList.GetAllEmployees();

// ..
// do operation of list
// ..

// Saves the list
if (employess.IsDirty)
{
    employess.Save();
}

Above code fetch list employee from database. You may perform add/edit operations on this. The class will manage the dirty and new state of object itself and Saves only those object in which changes are done. In this way we can save unnecessary trips to database.

5 comment(S)


andrerm on May 8, 2009 07:49 AM

And if Employee class have a List of objects, and you need to set dirty if a property in that list was changed?

say

public class EmployeeList : ObjectListBase<Employee>
{
List<Job> _jobs = new List<Job>();


Sattha on Jul 23, 2009 09:37 PM

If the Employee class say, has a Collection of Role class, ie.

public class Employee : ObjectBase
{
IList<Role> roles = new List<Role>();
.....
}

then how can we handle the states of roles..

Plz reply...

Popo on Aug 29, 2010 09:44 PM

Hi, excellent article. Just one thing for beginners, what is the base class ? Where is it innitialized ? If I try to exectue myObject.IsNew I get an error. How to mark my object as new ? Thanks.

Popo on Aug 29, 2010 09:54 PM

Say If I am adding a new Object to the list I need to set IsNew to true from presentation layer somehow.

Leave a comment