Register a Built-in XAF Module
- 6 minutes to read
XAF includes a number of ready-to-use built-in modules. This topic describes how to register a built-in module in a new or existing XAF application.
Register a Built-in XAF Module in an Existing .NET Application
- Install the NuGet package that contains the module.
- Navigate to the MyApplication.Blazor.Server\Startup.cs (Blazor) or MyApplication.Win\Startup.cs (WinForms) file and call the module-related
Add*
method to register the module.
The following code samples register Reports, Dashboards, and Office modules in the application.
public class Startup {
// ...
public void ConfigureServices(IServiceCollection services) {
// ...
services.AddXaf(Configuration, builder => {
// ...
builder.Modules
.AddReports(/*...*/)
.AddDashboards(/*...*/)
.AddOffice(/*...*/)
// ...
});
}
}
Register a Module in the Module Project
You can use the ModuleBase.RequiredModuleTypes property to specify required dependencies for your module. XAF loads these dependent modules with the current module. Follow the steps below to register additional modules in the Module project:
- Install the NuGet package that contains the module to register.
- Add additional modules to the RequiredModuleTypes list in the MyApplication.Module\Module.cs file.
public sealed class _MyApplicationModule : ModuleBase {
//...
public _MyApplicationModule() {
InitializeComponent();
this.RequiredModuleTypes.Add(typeof(DevExpress.ExpressApp.SystemModule.SystemModule));
this.RequiredModuleTypes.Add(typeof(DevExpress.ExpressApp.ReportsV2.ReportsModuleV2));
this.RequiredModuleTypes.Add(typeof(DevExpress.ExpressApp.Dashboards.DashboardsModule));
this.RequiredModuleTypes.Add(typeof(DevExpress.ExpressApp.Office.OfficeModule));
// ...
}
}
Add a Built-in XAF Module in a New .NET Application
You can add modules to your application when you use the Template Kit to create a new XAF solution. Select modules in the Additional Modules section.
.NET Framework
Register the Additional Module in the Module Project
In an existing module project, you can register an extra module that will be loaded with the current module.
- Add a reference to the additional module assembly.
- In the module project’s constructor (declared in the Module.cs file by default), add the additional module to the ModuleBase.RequiredModuleTypes list.
public sealed class MySolutionModule : ModuleBase {
//...
public MySolutionModule() {
InitializeComponent();
this.RequiredModuleTypes.Add(typeof(MyCustomModule.CustomModule));
}
}
Register the Additional Module in the Application Project
In the application project, you can register the additional module as follows.
- Add a reference to the additional module assembly.
- Add an instance of the additional module to the XafApplication.Modules list. For example, you can add it to the WinApplication/WebApplication descendant constructor.
public MySolutionWinApplication() {
InitializeComponent();
this.Modules.Add(new MyCustomModule.CustomModule());
}
Register a Dynamically Loaded Module
You can register additional modules that will be loaded dynamically by their names when required.
To register a dynamically loaded module, pass the additional module’s name to the XafApplication.Setup method. The Setup
method has overloads that take the moduleAssemblies
parameter - a string array that specifies module assembly names to be loaded.
For instance, you can use this method to load modules listed in the application configuration file. Refer to the Add a Module to the Application Configuration File section to see the example.
Add a Module to the Application Configuration File
This approach allows third-parties to plug-in their own modules without recompiling your application. Please note that third-party modules may contain insecure code that overrides security restrictions, modifies your business logic, etc. That is why adding modules from the configuration file is disabled by default. Enable it in a trusted environment only.
In a WinForms application, edit the Program.cs (Program.vb) file.
static class Program {
static void Main() {
//...
MySolutionWindowsFormsApplication winApplication = new MySolutionWindowsFormsApplication();
//...
winApplication.Setup("MySolution", winApplication.ConnectionString,
ConfigurationManager.AppSettings["Modules"].Split(';'));
winApplication.Start();
//...
}
//...
}
In an ASP.NET Web Forms application, edit the Global.asax.cs (Global.asax.vb) file.
public class Global : System.Web.HttpApplication {
protected void Session_Start(object sender, EventArgs e) {
WebApplication.SetInstance(Session, new MySolutionWebApplication());
//...
WebApplication.Instance.Setup("MySolution", WebApplication.Instance.ConnectionString,
ConfigurationManager.AppSettings["Modules"].Split(';'));
WebApplication.Instance.Start();
}
//...
}
You can now add a module assembly name to the modules list in the application configuration file (App.config and/or Web.config).
<configuration>
<appSettings>
<add key="Modules" value="MySolution.MyCustomModule" />
</appSettings>
</configuration>
Here, MySolution.MyCustomModule is a custom module assembly name.
Note
If the module to be added includes the Entity Framework DbContext, register the EFCoreObjectSpace provider for it at the module level. Then, override the ModuleBase.Setup method, and handle the XafApplication.CreateCustomObjectSpaceProvider event.
Use the Module Designer or Application Designer
In an existing XAF solution, start the Application Designer or Module Designer. Drag the required module from the Toolbox to the Designer’s Required Modules / Modules section.
Modules Shipped With XAF are available in the DX.25.1: XAF Modules tab of the Toolbox.
You can register a custom module declared in an external library in the Toolbox; drag a registered module to the Application or Module Designer. For details, refer to the following topic: How to: Add Items to the Toolbox.