Skip to content

update #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Mar 21, 2025
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions .github/workflows/vale.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: vale-validation
on:
pull_request:
paths:
- README.md
- readme.md
- Readme.md

jobs:
vale:
name: runner / vale
runs-on: ubuntu-latest
steps:
- name: clone repo
uses: actions/checkout@v4
- name: clone vale-styles repo
uses: actions/checkout@v4
with:
repository: DevExpress/vale-styles
path: vale-styles
ssh-key: ${{ secrets.VALE_STYLES_ACCESS_KEY }}
- name: copy vale rules to the root repo
run: shopt -s dotglob && cp -r ./vale-styles/vale/* .
- name: vale linter check
uses: DevExpress/vale-action@reviewdog
with:
files: '["README.md", "readme.md", "Readme.md"]'
fail_on_error: true
filter_mode: nofilter
reporter: github-check
115 changes: 101 additions & 14 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -1,27 +1,114 @@
<!-- default badges list -->
![](https://img.shields.io/endpoint?url=https://codecentral.devexpress.com/api/v1/VersionRange/128592807/22.2.6%2B)
[![](https://img.shields.io/badge/Open_in_DevExpress_Support_Center-FF7200?style=flat-square&logo=DevExpress&logoColor=white)](https://supportcenter.devexpress.com/ticket/details/E1744)
[![](https://img.shields.io/badge/📖_How_to_use_DevExpress_Examples-e9f6fc?style=flat-square)](https://docs.devexpress.com/GeneralInformation/403183)
[![](https://img.shields.io/badge/💬_Leave_Feedback-feecdd?style=flat-square)](#does-this-example-address-your-development-requirementsobjectives)
<!-- default badges end -->
<!-- default file list -->
*Files to look at*:


# How to search for XAF objects using a complex criterion

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

![MySearchClass window](my-search-class-window.png)

## Implementation Details

1. Create a [non-persistent](https://docs.devexpress.com/eXpressAppFramework/116516/business-model-design-orm/non-persistent-objects) class with properties that we will be used to search persistent objects.
_File to review: [MySearchClass.cs](CS/EFCore/ComplexSearchEF/ComplexSearchEF.Module/BusinessObjects/MySearchClass.cs)_
```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 will contain the search results.
_File to review: [MySearchClass.cs](CS/EFCore/ComplexSearchEF/ComplexSearchEF.Module/BusinessObjects/MySearchClass.cs)_
```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 will populate the collection.
_File to review: [MySearchController.cs](CS/EFCore/ComplexSearchEF/ComplexSearchEF.Module/Controllers/MySearchController.cs)_
```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](CS/EFCore/ComplexSearchEF/ComplexSearchEF.Module/Controllers/MySearchController.cs)_
```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](CS/EFCore/ComplexSearchEF/ComplexSearchEF.Module/Controllers/MyShowSearchController.cs)_
```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
* [MySearchClass.cs](CS/EFCore/ComplexSearchEF/ComplexSearchEF.Module/BusinessObjects/MySearchClass.cs)
* [Model.DesignedDiffs.xafml](CS/EFCore/ComplexSearchEF/ComplexSearchEF.Module/Model.DesignedDiffs.xafml)
* [MySearchController.cs](CS/EFCore/ComplexSearchEF/ComplexSearchEF.Module/Controllers/MySearchController.cs)
* [MyShowSearchController.cs](CS/EFCore/ComplexSearchEF/ComplexSearchEF.Module/Controllers/MyShowSearchController.cs)
<!-- default file list end -->
# How to search for XAF objects using a complex criterion


To accomplish this task, take the following steps:

## Documentation

1. Create a [non-persistent](https://docs.devexpress.com/eXpressAppFramework/116516/business-model-design-orm/non-persistent-objects) class with properties that we will use to find persistent objects: [MySearchClass.cs](CS/EFCore/ComplexSearchEF/ComplexSearchEF.Module/BusinessObjects/MySearchClass.cs)
2. In this class add a collection of persistent objects as described at [How to: Show Persistent Objects in a Non-Persistent Object's View](https://docs.devexpress.com/eXpressAppFramework/116106/business-model-design-orm/non-persistent-objects/how-to-show-persistent-objects-in-a-non-persistent-objects-view#persistent-collection). This collection will show the search results.
3. In this class' non-persistent detail view, add a custom 'Search' action as described at [How to: Include an Action to a Detail View Layout](https://docs.devexpress.com/eXpressAppFramework/112816/task-based-help/miscellaneous-ui-customizations/how-to-include-an-action-to-a-detail-view-layout)
4. When a user presses this 'Search' action, create a criterion based on the properties from point 1 and get persistent objects that fit this criterion as described at the 'Get a collection' section of [Create, Read, Update and Delete Data](https://docs.devexpress.com/eXpressAppFramework/113711/concepts/data-manipulation-and-business-logic/create-read-update-and-delete-data): [MySearchController.cs](CS/EFCore/ComplexSearchEF/ComplexSearchEF.Module/Controllers/MySearchController.cs)
5. To show this non-persistent class view, use the solution from [Ways to Show a View](https://docs.devexpress.com/eXpressAppFramework/112803/ui-construction/views/ways-to-show-a-view/ways-to-show-a-view): [MyShowSearchController.cs](CS/EFCore/ComplexSearchEF/ComplexSearchEF.Module/Controllers/MyShowSearchController.cs)
- [Non-Persistent classes](https://docs.devexpress.com/eXpressAppFramework/116516/business-model-design-orm/non-persistent-objects)
- [How to: Show Persistent Objects in a Non-Persistent Object's View](https://docs.devexpress.com/eXpressAppFramework/116106/business-model-design-orm/non-persistent-objects/how-to-show-persistent-objects-in-a-non-persistent-objects-view#persistent-collection)
- [How to: Include an Action to a Detail View Layout](https://docs.devexpress.com/eXpressAppFramework/112816/task-based-help/miscellaneous-ui-customizations/how-to-include-an-action-to-a-detail-view-layout)
- [Create, Read, Update and Delete Data](https://docs.devexpress.com/eXpressAppFramework/113711/data-manipulation-and-business-logic/create-read-update-and-delete-data)
- [Ways to Show a View](https://docs.devexpress.com/eXpressAppFramework/112803/ui-construction/views/ways-to-show-a-view/ways-to-show-a-view)

![image](https://github.com/DevExpress-Examples/XAF_how-to-search-for-objects-by-using-all-the-properties-or-by-using-more-complex-criteria-e1744/assets/14300209/c5b8ac4b-9e88-4f29-99cd-b2a0bdf64890)
<!-- feedback -->
## Does this example address your development requirements/objectives?

[<img src="https://www.devexpress.com/support/examples/i/yes-button.svg"/>](https://www.devexpress.com/support/examples/survey.xml?utm_source=github&utm_campaign=XAF-search-objects-using-complex-criterion&~~~was_helpful=yes) [<img src="https://www.devexpress.com/support/examples/i/no-button.svg"/>](https://www.devexpress.com/support/examples/survey.xml?utm_source=github&utm_campaign=XAF-search-objects-using-complex-criterion&~~~was_helpful=no)

(you will be redirected to DevExpress.com to submit your response)
<!-- feedback end -->
Binary file added my-search-class-window.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.