How to create persistent object in XPO

How to create persistent object in XPO


Here are simple steps to create persistent object in XPO

1 - Create a new project in visual studio, then add Devexpress XPO tool to your project via NuGet tool.
2 - Add a new class "Employees", then add the namespace DevExpress.Xpo.


C#



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DevExpress.Xpo;

3 - You can derive persistent objects from the PersistentBase, XPBaseObject, XPObject, XPLiteObject and XPCustomObject classes.
You can know the difirence between XPO Classes via this link XPO classes comparison
In our example we will inherit from XPObject class so no need to create a Key proberty.


C#



namespace XpoExample
{
public class Employees : XPObject
{

}
}

4 - Create a constructor


C#



public class Employees : XPObject
{
public Employees(Session session) :base(session) { }
}


5 - override AfterConstruction method


C#



public class Employees : XPObject
{
public Employees(Session session) :base(session) { }
public override void AfterConstruction()
{
base.AfterConstruction();
}
}

6 - Now we are ready to add properties in our Employees class
To create a proberty we use setter and getter methods also we can use data annotation as you see in the code


C#



private string _EmployeeName;
[Size(100)]
public string EmployeeName
{
get { return _EmployeeName; }
set { SetPropertyValue(nameof(EmployeeName), ref _EmployeeName, value); }
}

private DateTime _BirthDay;
public DateTime BirthDay
{
get { return _BirthDay; }
set { SetPropertyValue(nameof(BirthDay), ref _BirthDay, value); }
}

private decimal _Salary;
public decimal Salary
{
get { return _Salary; }
set { SetPropertyValue(nameof(Salary), ref _Salary, value); }
}

private bool _IsActive;
public bool IsActive
{
get { return _IsActive; }
set { SetPropertyValue(nameof(IsActive), ref _IsActive, value); }
}


We have finished our persistent object


C#



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DevExpress.Xpo;

namespace XpoExample
{
public class Employees : XPObject
{
public Employees(Session session) : base(session) { }
public override void AfterConstruction()
{
base.AfterConstruction();
}


private string _EmployeeName;
public string EmployeeName
{
get { return _EmployeeName; }
set { SetPropertyValue(nameof(EmployeeName), ref _EmployeeName, value); }
}

private DateTime _BirthDay;
public DateTime BirthDay
{
get { return _BirthDay; }
set { SetPropertyValue(nameof(BirthDay), ref _BirthDay, value); }
}

private decimal _Salary;
public decimal Salary
{
get { return _Salary; }
set { SetPropertyValue(nameof(Salary), ref _Salary, value); }
}

private bool _IsActive;
public bool IsActive
{
get { return _IsActive; }
set { SetPropertyValue(nameof(IsActive), ref _IsActive, value); }
}

}
}


How to create persistent object in XPO