KB Article K18240
Visible to All Users

How and where to store global application settings and user data

Scenario

Sometimes you may want to have a platform-agnostic storage for options in your application. Where should you store these options, especially if you develop a solution for both Windows Forms and ASP.NET platforms? Based on your business scenario, you may want your application options to be persistent (can be restored after the application is stopped) or non-persistent (temporarily exist only in memory while the application is run). Below you can find the most popular solutions including both standard and specific to XAF.

Persistent options

1. Database: you can create a separate database table that will hold all your options. To access this settings table, you can use the same familiar approach provided by the ORM supported by XAF. You can find more information about this approach here: How to: Implement a Singleton class
2. A file system or any other disk storage (local or external) that can be used to store application data. For instance, this can be XML configuration or text files.
3. Application Model: this is already the default storage for the application UI settings and user data in XAF. If you need to store custom settings there, you can extend the model by adding new elements and then set their attributes to store your options. To learn more on how to access data from the application model, check out the How to: Access the Application Model in Code article.

Non-persistent options

1.  Memory of the current Process: you can create classes with static or instance variables that will exist only during the life time of a certain UI screen or the whole application.
Depending on for how long you would need to access your custom data, this approach may require different implementations for different application types and platforms (e.g., a WinForms client or client-server ASP.NET app). Refer to public community resources on sites like MSDN and StackOverFlow for additional guidance on this general programming task.
As far as XAF is concerned, when you need to hold your custom data while a certain View is alive or in use, you can create a ViewController class with non-static variables. When you need to hold your custom data during the whole application life cycle, you can create a WindowController targeting the main application window (TargetWindowType = Main) with non-static variables. These Controllers can be accessed and reused via the Frame.GetController method. Of course, you are not limited to Controllers only and can use other XAF or non-XAF classes whose life time meets your scenario requirements.
2.  ValueManager (for WinForms and ASP.NET WebForms): this is actually an XAF implementation of option #1, which is typically needed in advanced scenarios when you need to hold your custom data during the whole user session or while the application is running.  You can create a Controller or another class within a platform-agnostic module to store temporary data accessible from any part of the application on a per-user basis. The benefit of using ValueManager is that it will automatically use the required platform-dependent runtime storage (WinForms or ASP.NET WebForms, while allowing you to expose a platform-independent interface for accessing your data. For example, in a Windows Forms application it would use static variables stored within the AppDomain, while in an ASP.NET application it would store the required data in ASP.NET session.
3. Custom ASP.NET Core services and standard API (for Blazor and Web API Service): you can implement custom services and register them in the ConfigureServices method of the Startup.cs file of your ASP.NET Core app (example). Alternatively, you can implement other standard solutions from the Microsoft documentation: Session and state management in ASP.NET Core. NOTE: you cannot use ValueManager in XAF Blazor UI, Web API Service, and other non-XAF UI apps: Core - InvalidOperationException (ValueManagerContext.Storage is null) may occur in XAF Blazor, Web API Service, and other non-XAF UI apps.

Common considerations

Accessing global application settings from within the business data model is often impossible (e.g., accessing Application Model from persistent class code) and is a bad idea as well. That is because normally the data model layer should not be directly aware of the way your global application settings are stored and accessed, unless these settings are not a part of the data model itself (e.g., you implemented a persistent Singleton class). Bear in mind that the same data model can often be reused in other applications, which do not have any knowledge of how the global settings were stored in the original application where this data model was taken off.

This dependency or knowledge can be injected without harm in the application/business logic layer (e.g., via Controllers), which manages the interaction between the application UI and data model. For instance, a ViewController can track creation of new data records and assign default values (e.g., default currency or tax) to them from the application settings.

Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.