Skip to content

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.

  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. Post-creation (With Errors): After the data is saved, List<GenericError> AfterCreateWithError<X>(List<X> values, ResultWithError<List<X>> result) is executed.
  6. Post-creation (Without Errors): The next function called is void AfterCreate<X>(List<X> values, ResultWithError<List<X>> result).
  7. Event Trigger: Finally, the OnCreated event in the DM is triggered.

Lifecycle Diagram

--- title: Create Operation Lifecycle --- flowchart TD A[Start Create Operation] --> B[CanCreate] B -- Valid --> C[BeforeCreateWithError & BeforeCreate] B -- Errors Returned --> G[Stop - Errors Encountered] C --> E[Execute DM Create Logic] C -- Errors Returned --> G[Stop - Errors Encountered] E --> F[AfterCreateWithError & AfterCreate] F --> I[Trigger OnCreated Event] I --> J[End of Create Operation]

Example: Generating a GUID on User Creation

In this example, we create a unique GUID for each user during the creation process:

UserDM.cs
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 the Type and Id 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.