Scenario
This example demonstrates how to create custom XAF modules with custom business objects and logic that would work with separate databases. These modules do not depend on each other and thus can be reused in other applications as a whole.
Usually, the connection to the database is set up in the executable application project. Typically, it is performed in the configuration file or directly in the code of the application's designer or within the Main function/Global application class. To learn more, please check out this help topic: Connect an XAF Application to a Database Provider In this example, you will learn how to establish a connection to the database directly from your module projects.
Steps to implement
0. Create a new XAF solution using one of predefined project templates;
1. Add two XAF modules into this solution using the DevExpress vXX.X XAF Module project template;
2. Add required persistent classes into these modules as shown in the ClassLibraryN/PersistentClassN.xx files of this example solution;
3. Add service ModuleInfo classes into these modules as shown in the ClassLibraryN/ModuleInfoN.xx files of this example solution;
4. In YourModuleName/Module.xx files, override the Setup(XafApplication application) methods of the ModuleBase descendants to handle the CreateCustomObjectSpaceProvider event of the XafApplication class as shown in the ClassLibraryX/XafModuleN.xx files of this example solution;
5. Build the solution, invoke the Module Designer for the platform-agnostic module (YourSolutionName.Module/Module.xx), and drag the created custom modules from the Toolbox:
Alternatively, you can add the same modules via the Application Designer invoked for the executable projects (as demonstrated in this example).
6. Declare connection strings in the configuration files of your application as shown in the TwoXpoModelsForDifferentDatabases.Web\Web.config and TwoXpoModelsForDifferentDatabases.Win\App.config files (see ConnectionStringDatabaseX under the <connectionStrings/> element). These connection strings are used in the modules via the ConfigurationManager.ConnectionStrings API, but you can always modify the way your modules obtains this data.
Important notes
1. Each module has a single static XPObjectSpaceProvider instance, which is initialized only once during the application life cycle.
2. Each ModuleUpdater class checks the ObjectSpace property to determine whether it is valid to create initial data of a certain type from this module.
3. Business classes linked to different ObjectSpaceProviders are considered to be isolated from each other and thus cannot have any links between them (e.g., have an association). Consider using the How to prevent altering the legacy database schema when creating an XAF application or alternative solutions if you need interlinks between classes from different data stores.
See also:
How to prevent altering the legacy database schema when creating an XAF application