Implement Dependent Reference Properties (XPO)
- 4 minutes to read
Tip
For .NET 6 applications, see: Implement Dependent Reference Properties (XPO, .NET 6).
In this lesson, you will learn how to implement properties whose values can depend on other properties. The Manager property will be added to the Contact class. By default, it will be represented by a lookup editor containing all Contacts that exist in the database. However, you may need this editor to contain Contacts from the same Department. In addition, you may need the Position of these Contacts to be “Manager”. To do this, use the DataSourcePropertyAttribute and DataSourceCriteriaAttribute attributes for the Manager property.
Note
Before proceeding, take a moment to review the following lessons:
Add a new Manager property of the Contact type to the Contact class. Apply the DataSourceProperty attribute to this property, as shown below.
[DefaultClassOptions] public class Contact : Person { //... private Contact manager; [DataSourceProperty("Department.Contacts")] public Contact Manager { get { return manager; } set { SetPropertyValue(nameof(Manager), ref manager, value); } } //... }
With the DataSourceProperty attribute applied, the Manager lookup editor will contain Contact objects that are specified by the Department object’s Contacts property.
Run the application and select Contact in the drop-down list of the New combo box. The Contact Detail View will be invoked. Specify the Department property and expand the Manager lookup editor. Make sure that the Department property of the listed objects is the same as those you specified above.
Apply the DataSourceCriteria attribute to the Contact class’ Manager property as shown below.
public class Contact : Person { //... [DataSourceProperty("Department.Contacts")] [DataSourceCriteria("Position.Title = 'Manager' AND Oid != '@This.Oid'")] public Contact Manager { // ... } // ... }
With the DataSourceCriteria attribute applied, the Manager lookup editor will contain Contact objects that satisfy the criteria specified in the attribute parameter.
Run the application. Set the Position property to “Manager” for several Contact objects.
Select Contact in the New () button’s drop-down list. The Contact Detail View will be invoked. Specify the Department property and expand the Manager lookup editor. Check to make sure that the Position property is set to “Manager” for each of the listed objects.
If the Department property is not specified for a Contact, you can provide another data source for the Manager lookup editor. To do this, specify the second parameter for the DataSourceProperty attribute. In the code below, this parameter is set to the DataSourcePropertyIsNullMode.SelectAll value. You can also set the DataSourcePropertyIsNullMode.SelectNothing or DataSourcePropertyIsNullMode.CustomCriteria values. In the latter case, a third parameter is required to specify a criterion.
[DefaultClassOptions] public class Contact : Person { //... [DataSourceProperty("Department.Contacts",DataSourcePropertyIsNullMode.SelectAll)] [DataSourceCriteria("Position.Title = 'Manager' AND Oid != '@This.Oid'")] public Contact Manager { // ... } // ... }
The code above will show all contacts in the Manager lookup editor, if the Department property is not specified.
- Run the application and check the results.
Note
You can implement the same behavior at design time. For details, refer to the Filter Lookup Editor Data Source lesson.
You can see the code demonstrated here in the MySolution.Module | Business Objects | Contact.cs (Contact.vb) file of the Main Demo installed with XAF. The MainDemo application is installed in %PUBLIC%\Documents\DevExpress Demos 22.1\Components\XAF\MainDemo by default. The ASP.NET Web Forms version is available online at https://demos.devexpress.com/XAF/MainDemo.
Next Lesson: Implement Property Value Validation in Code