Skip to content

DM - External Logic

When writing logic that will be called from outside the class (e.g., loading all active users), we recommend using functions that always return an error-handling object, such as VoidWithError or ResultWithError. This approach ensures consistent error management and makes it easy to handle and report issues.

The following example demonstrates a GetJohn function in UserDM that retrieves a user with the username “John” while handling potential errors.

UserDM.cs
using AventusSharp.Data.Manager.DB;
using AventusSharp.Tools;
using Demo.Data;
namespace Demo.Logic.DM
{
public class UserDM : DatabaseDM<UserDM, User>
{
public ResultWithError<User> GetJohn()
{
ResultWithError<User> response = new ResultWithError<User>();
// Execute actions
response.Run(() => SingleWithError(p => p.Username == "John"));
// Return the response
return response;
}
}
}

Explanation:

  • Error Handling: The ResultWithError<User> object response is used to wrap the result and capture any potential errors.
  • Execution: The response.Run(...) function executes the retrieval of the user named “John” using the SingleWithError method.
  • Return: The response object is returned, containing either the retrieved User or any error that occurred during the process.

This pattern ensures that all external logic methods return error-handling objects, promoting reliable and maintainable error management across the application.