Skip to content

DevExpress-Examples/XAF-search-objects-using-complex-criterion

Repository files navigation

How to search for XAF objects using a complex criterion

This example creates a pop-up window that allows users to perform a custom object search.

MySearchClass window

Implementation Details

  1. Create a non-persistent class with properties used to search persistent objects.
    File to review: MySearchClass.cs

    [DomainComponent]
    public class MySearchClass : NonPersistentBaseObject {
        [XafDisplayName("FirstName contains:")]
        public string FirstName { get; set; }
        [XafDisplayName("Age is equal to:")]
        public int Age { get; set; }
        // ...
    }
  2. Add a collection of persistent objects that contains the search results.
    File to review: MySearchClass.cs

    [DomainComponent]
    public class MySearchClass : NonPersistentBaseObject {
        // ...
        private IList<Contact> _contacts = new List<Contact>();
        [XafDisplayName("Results:")]
        public IList<Contact> Contacts {
            get {
                return _contacts;
            }
        }
    }
  3. Add the MySearch action that populates the collection. File to review: MySearchController.cs

    public class MySearchController : ObjectViewController<DetailView, MySearchClass> {
        public MySearchController() {
            var myAction1 = new SimpleAction(this, "MySearch", "MySearchCategory");
            myAction1.Execute += MyAction1_Execute;
        }
        // ...
    }
  4. When a user clicks the MySearch action, create a criterion based on the properties described in the first step and get persistent objects that fit this criterion.
    File to review: MySearchController.cs

    public class MySearchController : ObjectViewController<DetailView, MySearchClass> {
        // ...
        private void MyAction1_Execute(object sender, SimpleActionExecuteEventArgs e) {
            var mySearchObject = (MySearchClass)View.CurrentObject;
            var persistentOS = Application.CreateObjectSpace(typeof(Contact));
            var criterion = CriteriaOperator.FromLambda<Contact>(x => x.FirstName.Contains(mySearchObject.FirstName) || x.Age == mySearchObject.Age);
            var results = persistentOS.GetObjects<Contact>(criterion);
            mySearchObject.SetContacts(results);
        }
    }
  5. Create the MyShowSearchAction to display the MySearchClass detail view from the Contact list view in a pop-up window. File to review: MyShowSearchController.cs

    public class MyShowSearchController : ObjectViewController<ListView, Contact> {
        public MyShowSearchController() {
            var mypopAction1 = new PopupWindowShowAction(this, "MyShowSearchAction", PredefinedCategory.Edit);
            mypopAction1.TargetViewNesting = Nesting.Root;
            mypopAction1.CustomizePopupWindowParams += MyAction1_CustomizePopupWindowParams;
        }
        private void MyAction1_CustomizePopupWindowParams(object sender, CustomizePopupWindowParamsEventArgs e) {
            var nonPersistentOS = (NonPersistentObjectSpace)Application.CreateObjectSpace(typeof(MySearchClass));
            var persistentOS = Application.CreateObjectSpace(typeof(Contact));
            nonPersistentOS.AdditionalObjectSpaces.Add(persistentOS);
            var obj = nonPersistentOS.CreateObject<MySearchClass>();
            nonPersistentOS.CommitChanges();
            var view = Application.CreateDetailView(nonPersistentOS, obj);
            e.View = view;
        }
    }

Files to Review

Documentation

Does this example address your development requirements/objectives?

(you will be redirected to DevExpress.com to submit your response)