Distribute an Analysis with the Application
- 6 minutes to read
This topic describes how to distribute and update analyses with an application. For this purpose, an Analysis object will be created in code and the layout of its fields in the pivot grid will be saved to the database.
Note
You can find the code demonstrated here in the Main Demo installed with XAF. The MainDemo application is installed in %PUBLIC%\Documents\DevExpress Demos 23.1\Components\XAF\MainDemo.NET.EFCore by default.
Add the following references to the module project.
DevExpress.ExpressApp.PivotChart
DevExpress.PivotGrid.Core
To create an analysis for the DemoTask business object, open the DatabaseUpdate\Updater.cs (Updater.vb) file from the module project and create a new Analysis object as demonstrated below.
public class Updater : ModuleUpdater { //... public override void UpdateDatabaseAfterUpdateSchema() { base.UpdateDatabaseAfterUpdateSchema(); //... Analysis taskAnalysis1 = ObjectSpace.FirstOrDefault<Analysis>(analysis => analysis.Name == "Completed tasks"); if (taskAnalysis1 == null) { taskAnalysis1 = ObjectSpace.CreateObject<Analysis>(); taskAnalysis1.Name = "Completed tasks"; taskAnalysis1.ObjectTypeName = typeof(DemoTask).FullName; taskAnalysis1.Criteria = "[Status] = ##Enum#DevExpress.Persistent.Base.General.TaskStatus,Completed#"; } } }
Check that the analysis is created when running the application.
To save the configuration of the pivot grid fields for the created analysis in code, add the following code to the Updater.cs file inside the module project.
using DevExpress.ExpressApp.PivotChart; using DevExpress.Persistent.Base; using DevExpress.Persistent.BaseImpl.EF; namespace MySolution.Module.DatabaseUpdate { //... public abstract class TaskAnalysisLayoutUpdaterBase { protected IObjectSpace objectSpace; protected abstract IAnalysisControl CreateAnalysisControl(); protected abstract IPivotGridSettingsStore CreatePivotGridSettingsStore(IAnalysisControl control); public TaskAnalysisLayoutUpdaterBase(IObjectSpace objectSpace) { this.objectSpace = objectSpace; } public void Update(Analysis analysis) { if (analysis != null && !PivotGridSettingsHelper.HasPivotGridSettings(analysis)) { IAnalysisControl control = CreateAnalysisControl(); control.DataSource = new AnalysisDataSource(analysis, objectSpace.GetObjects(typeof(DemoTask))); control.Fields["Priority"].Area = DevExpress.XtraPivotGrid.PivotArea.ColumnArea; control.Fields["Subject"].Area = DevExpress.XtraPivotGrid.PivotArea.DataArea; control.Fields["AssignedTo.DisplayName"].Area = DevExpress.XtraPivotGrid.PivotArea.RowArea; PivotGridSettingsHelper.SavePivotGridSettings(CreatePivotGridSettingsStore(control), analysis); } } } }
The CreateAnalysisControl and CreatePivotGridSettingsStore methods are abstract because platform-specific controls should be created within them. So, the TaskAnalysisLayoutUpdaterBase class should be inherited in the WinForms and ASP.NET Web Forms modules.
Implement a descendant of the TaskAnalysisLayoutUpdaterBase class in the WinForms application project and define the WinForms-specific Updater. Right-click the MySolution.Module.Win project and choose Add | New Folder. Set the new folder name to DatabaseUpdate. Right-click the DatabaseUpdate folder and choose Add | Class…. In the invoked Add New Item dialog, set the class name to “Updater” and click Add. Add the following code to the newly created Updater.cs (Updater.vb) file.
using System; using DevExpress.Data.Filtering; using DevExpress.ExpressApp; using DevExpress.ExpressApp.PivotChart; using DevExpress.ExpressApp.PivotChart.Win; using DevExpress.ExpressApp.Updating; using DevExpress.Persistent.BaseImpl; //For XPO using DevExpress.Persistent.BaseImpl.EF; //For EF namespace MySolution.Module.Win.DatabaseUpdate { public class Updater : ModuleUpdater { public Updater(IObjectSpace objectSpace, Version currentDBVersion) : base(objectSpace, currentDBVersion) { } public override void UpdateDatabaseAfterUpdateSchema() { base.UpdateDatabaseAfterUpdateSchema(); new TaskAnalysisLayoutUpdater(ObjectSpace).Update(ObjectSpace.FirstOrDefault<Analysis>(analysis => analysis.Name == "Completed tasks")); ObjectSpace.CommitChanges(); } } public class TaskAnalysisLayoutUpdater : MySolution.Module.DatabaseUpdate.TaskAnalysisLayoutUpdaterBase { public TaskAnalysisLayoutUpdater(IObjectSpace objectSpace) : base(objectSpace) { } protected override IAnalysisControl CreateAnalysisControl() { return new AnalysisControlWin(); } protected override DevExpress.Persistent.Base.IPivotGridSettingsStore CreatePivotGridSettingsStore(IAnalysisControl control) { return new PivotGridControlSettingsStore(((AnalysisControlWin)control).PivotGrid); } } }
Implement a descendant of the TaskAnalysisLayoutUpdaterBase class in the ASP.NET Web Forms module project and define the web-specific Updater. Right-click the MySolution.Module.Web project and select Add | New Folder. Set the new folder name to DatabaseUpdate. Right-click the DatabaseUpdate folder and choose Add | Class…. In the invoked Add New Item dialog, set the class name to “Updater” and click Add. Add the following code to the newly created Updater.cs (Updater.vb) file.
using System; using DevExpress.Data.Filtering; using DevExpress.ExpressApp; using DevExpress.ExpressApp.PivotChart; using DevExpress.ExpressApp.PivotChart.Web; using DevExpress.ExpressApp.Updating; using DevExpress.Persistent.BaseImpl.EF; using MySolution.Module.BusinessObjects; using MySolution.Module.DatabaseUpdate; namespace MySolution.Module.Web.DatabaseUpdate { public class Updater : ModuleUpdater { public Updater(IObjectSpace objectSpace, Version currentDBVersion) : base(objectSpace, currentDBVersion) { } public override void UpdateDatabaseAfterUpdateSchema() { base.UpdateDatabaseAfterUpdateSchema(); new TaskAnalysisLayoutUpdater(ObjectSpace).Update(ObjectSpace.FirstOrDefault<Analysis>(analysis => analysis.Name == "Completed tasks")); ObjectSpace.CommitChanges(); } } public class TaskAnalysisLayoutUpdater : MySolution.Module.DatabaseUpdate.TaskAnalysisLayoutUpdaterBase { public TaskAnalysisLayoutUpdater(IObjectSpace objectSpace) : base(objectSpace) { } protected override IAnalysisControl CreateAnalysisControl() { return new AnalysisControlWeb(); } protected override DevExpress.Persistent.Base.IPivotGridSettingsStore CreatePivotGridSettingsStore(IAnalysisControl control) { return new ASPxPivotGridSettingsStore(((AnalysisControlWeb)control).PivotGrid); } } }
When the application is loading, the specified pivot grid configuration for the “Completed tasks” analysis will be saved if the analysis is found in the database.
To register the created Updater classes, rewrite the GetModuleUpdaters method implementation in both the WinForms and ASP.NET Web Forms modules (WinModule.cs and WebModule.cs, or WinModule.vb and WebModule.vb) as follows.
- Now, if you run the WinForms or ASP.NET Web Forms application, invoke the automatically created “Completed tasks” analysis and click the Bind Analysis Data button (). You will see that the pivot grid fields are automatically configured.