Models configuration
To configure models in your application, you can use the following code snippet. The Configure method accepts a lambda function where you can set various properties on the DataManagerConfig
object.
DataMainManager.Configure((config) =>{ // Set configuration properties here // Example: // config.defaultStorage = myStorage;});
The DataManagerConfig
class provides a set of configuration options for managing your data models, including database connections, logging, caching, and more. This class allows you to set default behaviors for data storage and access, as well as logging levels to monitor various processes. Below are detailed explanations of each configuration option available.
Properties
defaultStorage
- Type:
IDBStorage?
- Description: Specifies the default storage connection to be used with your
DatabaseDM
.
defaultDM
- Type:
Type
- Default:
typeof(DummyDM<>)
- Description: Sets the default
DataManager
to use for storing models. By default, it is set toDummyDM<>
, a placeholder data manager.
log
- Type:
DataManagerConfigLog
- Description: Defines the log settings for monitoring different aspects of data management. See
DataManagerConfigLog
for detailed log options.
nullByDefault
- Type:
bool
- Default:
false
- Description: Determines whether database fields should be nullable by default. Set to
true
if you want fields to allow null values.
preferLocalCache
- Type:
bool
- Default:
false
- Description: Enables or disables the use of a local cache. When enabled, all objects are stored in RAM, which can speed up access times but may increase memory usage.
preferShortLink
- Type:
bool
- Default:
false
- Description: If set to
true
, only theType
andId
of linked elements will be loaded. This can reduce memory and improve load times, especially when working with large datasets.
allowNonAbstractExtension
- Type:
bool
- Default:
false
- Description: By default, only abstract classes are allowed as base classes for inheritance. Set this to
true
to allow non-abstract classes as well.
GetSQLTableName
- Type:
Func<Type, string>
- Description: A function that defines the SQL table name for a given model type. If the type has a
[SqlName]
attribute, the table name will be based on this attribute. Otherwise, the table name defaults to the model’s type name, without generic type indicators.
DataManagerConfigLog
Class
The DataManagerConfigLog
class is used to define logging options within DataManagerConfig
. These options allow for detailed monitoring of model dependencies, data manager initialization, ordering, and error reporting.
Properties
monitorDataDependances
(bool
): Enables logging of dependencies for all models.monitorManagerAnalyze
(bool
): Logs each step during the data manager analysis process.monitorManagerInit
(bool
): Logs the time taken to initialize each data manager.monitorManagerOrdering
(bool
): Logs the time taken to order data managers.monitorManagerOrdered
(bool
): Displays the ordered data managers after the ordering process completes.monitorDataOrdering
(bool
): Logs the time taken to order each model.monitorDataOrdered
(bool
): Displays the ordered models after ordering.printErrorInConsole
(bool
): Prints all errors from storable actions directly to the console, providing immediate feedback during development.
Default Configuration
To enable data persistence, a database setup is required. For this purpose, an appropriate DataManager
must be defined. The default data manager, DummyDM
, is non-functional and does not perform any operations. To achieve persistence, use DatabaseDM
, which requires a connection to a database. Currently, only DatabaseDM
with a MySQL connection via MySQLStorage
is available. However, it’s easy to develop additional DataManagers
for alternative storage solutions or different database connections. If you need support for other databases or custom managers, feel free to open an issue on our GitHub.
Example: Setting up a MySQL Database Connection
To configure a MySQL database connection, use the following code:
MySQLStorage storage = new(new StorageCredentials( host: "localhost", database: "demo", username: "root", password: ""));
DataMainManager.Configure((config) =>{ config.defaultDM = typeof(SimpleDatabaseDM<>); config.defaultStorage = storage;});
In this example, MySQLStorage
is initialized with the required credentials and assigned to defaultStorage
. defaultDM
is set to SimpleDatabaseDM<>
, which is a data manager that supports MySQL persistence. Customize these parameters to fit your database setup.
This configuration provides a straightforward method for setting up data persistence in AventusSharp
.