DM - Internal Logic
To explore the capabilities of a DataManager
(DM), start by typing override
in your IDE. This will show you the list of available methods you can implement.
When overriding, it’s essential to call base.{FunctionName}
to avoid disrupting the default behavior of the code.
Lifecycle of Create, Read, Update, Delete Operations
Each CRUD operation follows a similar lifecycle. Let’s take the Create
operation as an example to illustrate this process.
- Validation: The process starts with
List<GenericError> CanCreate<X>(List<X> values)
, which determines if the items in the list are allowed to be created. Either the entire list is valid, or it isn’t; individual item validation is not supported. - Pre-creation (With Errors): Next,
List<GenericError> BeforeCreateWithError<X>(List<X> values)
is called. This function runs prior to the actual creation and can be used to perform calls on other DMs. Errors can still be returned here. - Pre-creation (Without Errors): Immediately after,
void BeforeCreate<X>(List<X> values)
is called. This method has a similar purpose to the previous step but does not return errors. - Execution: If no errors were returned in previous steps, the DM-specific creation logic is executed. For
DatabaseDM
, this involves saving the data to the database. - Post-creation (With Errors): After the data is saved,
List<GenericError> AfterCreateWithError<X>(List<X> values, ResultWithError<List<X>> result)
is executed. - Post-creation (Without Errors): The next function called is
void AfterCreate<X>(List<X> values, ResultWithError<List<X>> result)
. - Event Trigger: Finally, the
OnCreated
event in the DM is triggered.
Lifecycle Diagram
Example: Generating a GUID on User Creation
In this example, we create a unique GUID for each user during the creation process:
using AventusSharp.Data.Manager.DB;using AventusSharp.Tools;using Demo.Data;
namespace Demo.Logic.DM{ public class UserDM : DatabaseDM<UserDM, User> { protected override List<GenericError> CanCreate<X>(List<X> values) { List<GenericError> errors = new List<GenericError>(); foreach (X item in values) { if (string.IsNullOrEmpty(item.Username)) { // In real code, use a custom error instead of GenericError errors.Add(new GenericError(500, "The username must be provided")); } } return errors; }
protected override void BeforeCreate<X>(List<X> values) { foreach (X item in values) { item.Guid = Guid.NewGuid().ToString(); } } }}
Additional Functionality
- OnItemLoaded: This method is called each time an object is created after a database read operation. You can override this function to apply global changes to all loaded objects.
- Initialize: Override this method to perform actions when the DM is instantiated. For example,
DatabaseDM
checks if the corresponding database table exists and creates it if it doesn’t.
Configuration Options
Several configuration functions are available to modify default behaviors:
- UseLocalCache: Enables or disables local caching. When enabled, all objects are stored in RAM, improving access speed but potentially increasing memory usage.
- UseShortLink: If set to
true
, only theType
andId
of linked elements are loaded. This reduces memory usage and load times, especially for large datasets. - DefineStorage: Specifies the storage connection used by your
DatabaseDM
. - DefineShortLinks: Allows manual definition of short links for specific relations using the
ShortLink
function. - DefineManualDependencies: If data is loaded incorrectly or requires custom ordering, you can define dependencies manually. The function must return the required dependency types.