This example stores a list of books. When a user clicks the Show Duplicate Books action, a popup dialog displays a list of duplicated books and their copy counts. This list is shown with a detail view.
Note:
This example uses an intermediate container class to display a list non-persistent objects. In this case, the ObjectsGetting method cannot be applied. If your application logic does not require complex operations to create non-persistent objects, use the ObjectsGetting method. Refer to the following help topic for implementation details: How to: Display a Non-Persistent Object's List View from the Navigation.
Implementation Details
- Declare the
Book
persistent class. Objects of this class denote books in a collection. - Declare two non-persistent classes—
Duplicate
to store unique book titles and the total number of books with these titles, andDuplicatesList
to aggregate theDuplicate
objects. - In a controller, implement a method that iterates through persistent
Book
objects, counts the number of copies of each book, and saves this information in a dictionary. - Implement a method that iterates through dictionary items and creates
Duplicate
objects for items with a value greater than one (books with more than one copy). AddDuplicate
objects to aDuplicatesList
collection. - Add the PopupWindowShowAction to display a popup dialog when a user clicks the Show Duplicate Books action. Handle the CustomizePopupWindowParams event and call the CreateDetailView method to create a Detail View for the
DuplicatesList
object.
See the following help topic for more detailed information: How to: Display a List of Non-Persistent Objects in a Popup Dialog
Files to Review
Documentation
- Non-Persistent Objects
- How to: Display a List of Non-Persistent Objects with an Intermediate Container Class and Its Detail View
More Examples
- XAF - How to Implement CRUD Operations for Non-Persistent Objects Stored Remotely
- XAF - How to edit non-persistent objects nested in a persistent object
- XAF - How to filter and sort non-persistent objects
- XAF - How to refresh non-persistent objects and reload nested persistent objects
- XAF - How to edit a collection of persistent objects linked to a non-persistent object
Does this example address your development requirements/objectives?
(you will be redirected to DevExpress.com to submit your response)
Example Code
C#using DevExpress.Persistent.Base;
using DevExpress.Persistent.BaseImpl.EF;
namespace NonPersistentListView.Module {
[DefaultClassOptions]
public class Book : BaseObject {
public virtual string Title { get; set; }
}
}
C#using System.ComponentModel;
using DevExpress.ExpressApp.DC;
using DevExpress.ExpressApp;
namespace NonPersistentListView.Module {
[DomainComponent]
public class Duplicate: NonPersistentLiteObject {
public string Title { get; set; }
public int Count { get; set; }
}
[DomainComponent]
public class DuplicatesList: NonPersistentLiteObject {
private BindingList<Duplicate> duplicates;
public DuplicatesList() {
duplicates = new BindingList<Duplicate>();
}
public BindingList<Duplicate> Duplicates { get { return duplicates; } }
}
}
C#using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Actions;
using DevExpress.Persistent.Base;
using System.Collections;
namespace NonPersistentListView.Module {
public class ShowDuplicateBooksController : ObjectViewController<ListView, Book> {
public ShowDuplicateBooksController() {
PopupWindowShowAction showDuplicatesAction = new PopupWindowShowAction(this, "ShowDuplicateBooks", PredefinedCategory.View);
showDuplicatesAction.CustomizePopupWindowParams += showDuplicatesAction_CustomizePopupWindowParams;
}
private void showDuplicatesAction_CustomizePopupWindowParams(object sender, CustomizePopupWindowParamsEventArgs e) {
var duplicatesDictionary = GetDuplicatesDictionary();
var nonPersistentObjectSpace = Application.CreateObjectSpace<DuplicatesList>();
var duplicatesList = CreateDuplicatesList(duplicatesDictionary, nonPersistentObjectSpace);
e.View = Application.CreateDetailView(nonPersistentObjectSpace, duplicatesList);
e.DialogController.SaveOnAccept = false;
e.DialogController.CancelAction.Active["NothingToCancel"] = false;
}
private Dictionary<string, int> GetDuplicatesDictionary() {
var dictionary = new Dictionary<string, int>();
foreach(Book book in View.CollectionSource.List) {
if(string.IsNullOrWhiteSpace(book.Title)) continue;
dictionary[book.Title] = dictionary.GetValueOrDefault(book.Title) + 1;
}
return dictionary;
}
private DuplicatesList CreateDuplicatesList(Dictionary<string, int> duplicatesDictionary, IObjectSpace objectSpace) {
DuplicatesList duplicatesList = objectSpace.CreateObject<DuplicatesList>();
foreach(var (title, count) in duplicatesDictionary) {
if(count <= 1) continue;
var duplicate = objectSpace.CreateObject<Duplicate>();
duplicate.Title = title;
duplicate.Count = count;
duplicatesList.Duplicates.Add(duplicate);
}
objectSpace.CommitChanges();
return duplicatesList;
}
}
}