30th April 2015

DevExpress XAF – Default Administration Items on Object Creation

DevExpress XAF – Default Administration Items on Object Creation

Scenario

When entering client information into tables, many of the clients may have the same value such as VAT rate, payment terms or membership status. Many clients will all have the same default value in these fields except for the odd few which are the exception. To enable quicker data entry we can add a default flag to the administration tables so that when a new client record is created the system will automatically pull through the default value into these fields against the client.

Solution

Within your Administration Item XAF Business Object is necessary to add an IsDefault Boolean as shown below:

/// <summary>
/// The administration item name
/// </summary>
private string administrationItemName;
 
/// <summary>
/// The is default
/// </summary>
private bool isDefault;
 
/// <summary>
/// Initializes a new instance of the <see cref="AdministrationItem"/> class.
/// </summary>
/// <param name="session">An object which represents a persistent object's cache where the business object will be instantiated.</param>
public AdministrationItem(Session session) : base(session)

/// <summary> /// Gets or sets the name of the administration item. /// </summary> /// <value> /// The name of the administration item. /// </value> [Size(100)] public string AdministrationItemName

get

return this.administrationItemName;

set

this.SetPropertyValue<string>("AdministrationItemName", ref this.administrationItemName, value);

/// <summary> /// Gets or sets a value indicating whether this instance is default. /// </summary> /// <value> /// <c>true</c> if this instance is default; otherwise, <c>false</c>. /// </value> public bool IsDefault

get

return this.isDefault;

set

this.SetPropertyValue<bool>("IsDefault", ref this.isDefault, value);

For better data entry within the Administration Item table it is advised to set the IsDefault value to false on object creation so that it won’t be the default Administration Item unless it is explicitly set to:
/// <summary>
/// Invoked when the current object is about to be initialized after its creation.
/// </summary>
public override void AfterConstruction()

base.AfterConstruction(); this.isDefault = false;

To ensure that there are never two Administration Items where IsDefault is true it is required to implement the below View Controller within the Solution.Module. When adding or editing Administration Items from within the table, upon saving the below view controller will activate and set the current Administration Item where IsDefault is true to false so that there is only ever a single default Administration Item:
/// <summary>
/// Initializes a new instance of the <see cref="AdministrationItemViewController"/> class.
/// </summary>
public AdministrationItemViewController()

this.InitializeComponent(); this.RegisterActions(this.components); this.TargetObjectType = typeof(AdministrationItem);

/// <summary> /// Called when [activated]. /// </summary> protected override void OnActivated()

base.OnActivated(); this.ObjectSpace.Committing += new EventHandler<System.ComponentModel.CancelEventArgs>(this.ObjectSpace_Committing);

/// <summary> /// Called when [deactivated]. /// </summary> protected override void OnDeactivated()

base.OnDeactivated(); this.ObjectSpace.Committing -= this.ObjectSpace_Committing;

/// <summary> /// Handles the Committing event of the ObjectSpace control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="System.ComponentModel.CancelEventArgs"/> instance containing the event data.</param> protected void ObjectSpace_Committing(object sender, System.ComponentModel.CancelEventArgs e)

List<AdministrationItem> administrationItems = this.ObjectSpace.GetObjectsToSave(false).OfType<AdministrationItem>().ToList(); foreach (AdministrationItem administrationItem in administrationItems)

if (administrationItem.IsDefault)

this.ResetDefaults(administrationItem);

/// <summary> /// Resets the defaults. /// </summary> /// <param name="administrationItem">The administration item.</param> private void ResetDefaults(AdministrationItem administrationItem)

List<AdministrationItem> defaultAdministrationItems = this.ObjectSpace.GetObjects<AdministrationItem>(CriteriaOperator.Parse(string.Format("IsDefault == true and Oid != '

0

'
", administrationItem.Oid.ToString()))).ToList(); foreach (AdministrationItem defaultAdministrationItem in defaultAdministrationItems)

defaultAdministrationItem.IsDefault = false; defaultAdministrationItem.Save();

The final stage is to set the default Administration Item as the chosen item upon object creation within your Client business object by implementing the following within it:
/// <summary>
/// Invoked when the current object is about to be initialized after its creation.
/// </summary>
public override void AfterConstruction()

base.AfterConstruction(); this.administrationItemId = this.Session.FindObject<AdministrationItem>(CriteriaOperator.Parse("IsDefault == true"));

Fill in this quick form and discover your digital future
Choose your interests:

Where to find us

We'd love to welcome you into our office! We're only 20 miles north of Peterborough, conveniently just off the A16.

Carver House
Apex Court, Elsoms Way
Pinchbeck
Lincolnshire
PE11 3UL