Skip to main content

Definition

Assembly: CloudNimble.EasyAF.Business.EF6.dll Namespace: CloudNimble.EasyAF.Business Inheritance: CloudNimble.EasyAF.Business.ManagerBase<TContext>

Syntax

CloudNimble.EasyAF.Business.EntityManager<TContext, TEntity>

Summary

Provides a base class for entity-specific business logic managers with built-in CRUD operations, audit trail support, and lifecycle event hooks. Handles common entity operations and automatically manages audit fields for entities that implement auditing interfaces.

Remarks

This manager provides comprehensive entity lifecycle management including:
  • Automatic audit trail creation for entities implementing ICreatedAuditable, IUpdatedAuditable
  • User tracking for entities implementing ICreatorTrackable1, IUpdaterTrackable1
  • Virtual hooks for custom business logic before and after CRUD operations
  • Batch operations support for improved performance
  • Thread-safe interface caching for performance optimization

Type Parameters

  • TContext - The type of DbContext used for database operations.
  • TEntity - The type of entity managed by this manager.

Examples

public class UserManager : EntityManager&lt;MyDbContext, User&gt;
{
    public UserManager(MyDbContext context, IMessagePublisher publisher)
        : base(context, publisher) { }

    public override async Task OnInsertingAsync(User entity)
    {
        await base.OnInsertingAsync(entity); // Handles audit fields
        entity.IsActive = true; // Custom business logic
    }

    public override async Task&lt;bool&gt; OnInsertedAsync(User entity)
    {
        await MessagePublisher.PublishAsync(new UserCreatedEvent { UserId = entity.Id });
        return await base.OnInsertedAsync(entity);
    }
}

Constructors

.ctor

Initializes a new instance of the EntityManager2` class.

Syntax

public EntityManager(TContext dataContext, CloudNimble.SimpleMessageBus.Publish.IMessagePublisher messagePublisher)

Parameters

NameTypeDescription
dataContextTContextThe database context instance for data operations. Should be injected by the DI container.
messagePublisherCloudNimble.SimpleMessageBus.Publish.IMessagePublisherThe message publisher instance for publishing events. Should be injected by the DI container.

.ctor Inherited

Inherited from CloudNimble.EasyAF.Business.ManagerBase<TContext>
Initializes a new instance of the ManagerBase1` class.

Syntax

public ManagerBase(TContext dataContext, CloudNimble.SimpleMessageBus.Publish.IMessagePublisher messagePublisher)

Parameters

NameTypeDescription
dataContextTContextThe database context instance for data operations. Should be injected by the DI container.
messagePublisherCloudNimble.SimpleMessageBus.Publish.IMessagePublisherThe message publisher instance for publishing events. Should be injected by the DI container.

.ctor Inherited

Inherited from object

Syntax

public Object()

Properties

DataContext Inherited

Inherited from CloudNimble.EasyAF.Business.ManagerBase<TContext>
Gets the database context instance used for data operations. This context is injected through the constructor and provides access to the database.

Syntax

public TContext DataContext { get; private set; }

Property Value

Type: TContext

MessagePublisher Inherited

Inherited from CloudNimble.EasyAF.Business.ManagerBase<TContext>
Gets the message publisher instance used for publishing events and messages to the message bus. This publisher is injected through the constructor and enables event-driven architecture patterns.

Syntax

public CloudNimble.SimpleMessageBus.Publish.IMessagePublisher MessagePublisher { get; private set; }

Property Value

Type: CloudNimble.SimpleMessageBus.Publish.IMessagePublisher

Methods

DeleteAsync

Delete a specific DbSet`1 with optional save operation.

Syntax

public System.Threading.Tasks.Task<bool> DeleteAsync(TEntity entity, bool save = true)

Parameters

NameTypeDescription
entityTEntity-
savebool-

Returns

Type: System.Threading.Tasks.Task<bool>

DeleteAsync

Delete a specific DbSet`1 with optional save operation using a specified DbContext.

Syntax

public System.Threading.Tasks.Task<bool> DeleteAsync(TEntity entity, TContext context, bool save = true)

Parameters

NameTypeDescription
entityTEntity-
contextTContext-
savebool-

Returns

Type: System.Threading.Tasks.Task<bool>

DeleteAsync

Delete all DbSet`1 from a list with optional save operation.

Syntax

public System.Threading.Tasks.Task<bool> DeleteAsync(System.Collections.Generic.List<TEntity> entities, bool save = true)

Parameters

NameTypeDescription
entitiesSystem.Collections.Generic.List<TEntity>-
savebool-

Returns

Type: System.Threading.Tasks.Task<bool>

Remarks

RWM: This will need to be Deleted to be generic if it’s going to be in a NuGet package.

DeleteAsync

Delete all DbSet`1 from a list with optional save operation using a specified DbContext.

Syntax

public System.Threading.Tasks.Task<bool> DeleteAsync(System.Collections.Generic.List<TEntity> entities, TContext context, bool save = true)

Parameters

NameTypeDescription
entitiesSystem.Collections.Generic.List<TEntity>-
contextTContext-
savebool-

Returns

Type: System.Threading.Tasks.Task<bool>

DirectDelete

Delete entities returned by the specified query without individual entity processing.

Syntax

public int DirectDelete(System.Linq.Expressions.Expression<System.Func<TEntity, bool>> predicate)

Parameters

NameTypeDescription
predicateSystem.Linq.Expressions.Expression<System.Func<TEntity, bool>>An Expression1](https://learn.microsoft.com/dotnet/api/system.linq.expressions.expression-1) to execute against the [DbSet1

Returns

Type: int

Remarks

This overload will give you all of the performance of deleting a set of data without loading entities in the context but none of the extra processing provided by OnDeleting / OnDeleted.

DirectDeleteAsync

Delete entities returned by the specified query without individual entity processing.

Syntax

public System.Threading.Tasks.Task<int> DirectDeleteAsync(System.Linq.Expressions.Expression<System.Func<TEntity, bool>> predicate)

Parameters

NameTypeDescription
predicateSystem.Linq.Expressions.Expression<System.Func<TEntity, bool>>An Expression1](https://learn.microsoft.com/dotnet/api/system.linq.expressions.expression-1) to execute against the [DbSet1

Returns

Type: System.Threading.Tasks.Task<int>

Remarks

This overload will give you all of the performance of deleting a set of data without loading entities in the context but none of the extra processing provided by OnDeleting / OnDeleted.

DirectUpdate

Executes a direct UPDATE query on the database without returning objects or processing them through the interceptors.

Syntax

public int DirectUpdate(System.Linq.Expressions.Expression<System.Func<TEntity, bool>> predicate, System.Linq.Expressions.Expression<System.Func<TEntity, TEntity>> updateExpression)

Parameters

NameTypeDescription
predicateSystem.Linq.Expressions.Expression<System.Func<TEntity, bool>>An Expression1](https://learn.microsoft.com/dotnet/api/system.linq.expressions.expression-1) to execute against the [DbSet1
updateExpressionSystem.Linq.Expressions.Expression<System.Func<TEntity, TEntity>>An Expression`1 defining the updates to be performed on the records returned by the predicate.

Returns

Type: int

Remarks

This overload will give you all of the performance of updating a set of data without loading entities in the context but none of the extra processing provided by OnUpdating / OnUpdated.

DirectUpdateAsync

Executes a direct UPDATE query on the database without returning objects or processing them through the interceptors.

Syntax

public System.Threading.Tasks.Task<int> DirectUpdateAsync(System.Linq.Expressions.Expression<System.Func<TEntity, bool>> predicate, System.Linq.Expressions.Expression<System.Func<TEntity, TEntity>> updateExpression)

Parameters

NameTypeDescription
predicateSystem.Linq.Expressions.Expression<System.Func<TEntity, bool>>An Expression1](https://learn.microsoft.com/dotnet/api/system.linq.expressions.expression-1) to execute against the [DbSet1
updateExpressionSystem.Linq.Expressions.Expression<System.Func<TEntity, TEntity>>An Expression`1 defining the updates to be performed on the records returned by the predicate.

Returns

Type: System.Threading.Tasks.Task<int>

Remarks

This overload will give you all of the performance of updating a set of data without loading entities in the context but none of the extra processing provided by OnUpdating / OnUpdated.

Equals Inherited Virtual

Inherited from object

Syntax

public virtual bool Equals(object obj)

Parameters

NameTypeDescription
objobject?-

Returns

Type: bool

Equals Inherited

Inherited from object

Syntax

public static bool Equals(object objA, object objB)

Parameters

NameTypeDescription
objAobject?-
objBobject?-

Returns

Type: bool

GetHashCode Inherited Virtual

Inherited from object

Syntax

public virtual int GetHashCode()

Returns

Type: int

GetType Inherited

Inherited from object

Syntax

public System.Type GetType()

Returns

Type: System.Type

InsertAsync

Inserts a single entity into the database with optional save operation. Executes the OnInsertingAsync and OnInsertedAsync lifecycle hooks.

Syntax

public System.Threading.Tasks.Task<bool> InsertAsync(TEntity entity, bool save = true)

Parameters

NameTypeDescription
entityTEntityThe entity to be inserted.
saveboolWhether to immediately save changes to the database. Defaults to true.

Returns

Type: System.Threading.Tasks.Task<bool> True if the entity was successfully inserted; otherwise, false.

Remarks

RWM: This will need to be updated to be generic if it’s going to be in a NuGet package.

InsertAsync

Inserts a single entity into the database using a specified context with optional save operation. Executes the OnInsertingAsync and OnInsertedAsync lifecycle hooks.

Syntax

public System.Threading.Tasks.Task<bool> InsertAsync(TEntity entity, TContext context, bool save = true)

Parameters

NameTypeDescription
entityTEntityThe entity to be inserted.
contextTContextThe database context to use for the operation.
saveboolWhether to immediately save changes to the database. Defaults to true.

Returns

Type: System.Threading.Tasks.Task<bool> True if the entity was successfully inserted; otherwise, false.

InsertAsync

Inserts a collection of entities into the database with optional save operation. Executes the OnInsertingAsync and OnInsertedAsync lifecycle hooks for each entity.

Syntax

public System.Threading.Tasks.Task<bool> InsertAsync(System.Collections.Generic.List<TEntity> entities, bool save = true)

Parameters

NameTypeDescription
entitiesSystem.Collections.Generic.List<TEntity>The collection of entities to be inserted.
saveboolWhether to immediately save changes to the database. Defaults to true.

Returns

Type: System.Threading.Tasks.Task<bool> True if the entities were successfully inserted; otherwise, false.

Remarks

RWM: This will need to be updated to be generic if it’s going to be in a NuGet package.

InsertAsync

Inserts a collection of entities into the database using a specified context with optional save operation. Executes the OnInsertingAsync and OnInsertedAsync lifecycle hooks for each entity.

Syntax

public System.Threading.Tasks.Task<bool> InsertAsync(System.Collections.Generic.List<TEntity> entities, TContext context, bool save = true)

Parameters

NameTypeDescription
entitiesSystem.Collections.Generic.List<TEntity>The collection of entities to be inserted.
contextTContextThe database context to use for the operation.
saveboolWhether to immediately save changes to the database. Defaults to true.

Returns

Type: System.Threading.Tasks.Task<bool> True if the entities were successfully inserted; otherwise, false.

MemberwiseClone Inherited

Inherited from object

Syntax

protected internal object MemberwiseClone()

Returns

Type: object

OnDeletedAsync Virtual

Called after successfully deleting an entity from the database. Use this method for post-deletion business logic such as cleanup operations, sending notifications, or triggering external systems.

Syntax

public virtual System.Threading.Tasks.Task<bool> OnDeletedAsync(TEntity entity)

Parameters

NameTypeDescription
entityTEntityThe entity that was deleted.

Returns

Type: System.Threading.Tasks.Task<bool> True if post-deletion processing was successful; otherwise, false.

OnDeletedAsync Virtual

Called after successfully deleting a collection of entities from the database. Applies OnDeletedAsync logic to each entity in the collection.

Syntax

public virtual System.Threading.Tasks.Task OnDeletedAsync(System.Collections.Generic.List<TEntity> entities)

Parameters

NameTypeDescription
entitiesSystem.Collections.Generic.List<TEntity>The collection of entities that were deleted.

Returns

Type: System.Threading.Tasks.Task

OnDeletingAsync Virtual

Called before deleting an entity from the database. Override this method to add custom business logic or validation before deletion.

Syntax

public virtual System.Threading.Tasks.Task OnDeletingAsync(TEntity entity)

Parameters

NameTypeDescription
entityTEntityThe entity to be deleted.

Returns

Type: System.Threading.Tasks.Task

OnDeletingAsync Virtual

Called before deleting a collection of entities from the database. Applies OnDeletingAsync logic to each entity in the collection.

Syntax

public virtual System.Threading.Tasks.Task OnDeletingAsync(System.Collections.Generic.List<TEntity> entities)

Parameters

NameTypeDescription
entitiesSystem.Collections.Generic.List<TEntity>The collection of entities to be deleted.

Returns

Type: System.Threading.Tasks.Task

OnInsertedAsync Virtual

Called after successfully inserting an entity into the database. Use this method for post-insertion business logic such as sending notifications, publishing events, or triggering external systems.

Syntax

public virtual System.Threading.Tasks.Task<bool> OnInsertedAsync(TEntity entity)

Parameters

NameTypeDescription
entityTEntityThe entity that was inserted.

Returns

Type: System.Threading.Tasks.Task<bool> True if post-insertion processing was successful; otherwise, false.

OnInsertedAsync Virtual

Called after successfully inserting a collection of entities into the database. Applies OnInsertedAsync logic to each entity in the collection.

Syntax

public virtual System.Threading.Tasks.Task OnInsertedAsync(System.Collections.Generic.List<TEntity> entities)

Parameters

NameTypeDescription
entitiesSystem.Collections.Generic.List<TEntity>The collection of entities that were inserted.

Returns

Type: System.Threading.Tasks.Task

OnInsertingAsync Virtual

Called before inserting an entity into the database. Automatically handles audit field population and user tracking for entities implementing the appropriate interfaces.

Syntax

public virtual System.Threading.Tasks.Task OnInsertingAsync(TEntity entity)

Parameters

NameTypeDescription
entityTEntityThe entity to be inserted.

Returns

Type: System.Threading.Tasks.Task

Remarks

This method automatically sets:
  • CreatedById for entities implementing ICreatorTrackable1`
  • DateCreated for entities implementing ICreatedAuditable Override this method to add custom business logic before insertion.

OnInsertingAsync

Called before inserting a collection of entities into the database. Applies OnInsertingAsync logic to each entity in the collection.

Syntax

public System.Threading.Tasks.Task OnInsertingAsync(System.Collections.Generic.List<TEntity> entities)

Parameters

NameTypeDescription
entitiesSystem.Collections.Generic.List<TEntity>The collection of entities to be inserted.

Returns

Type: System.Threading.Tasks.Task

OnUpdatedAsync Virtual

Called after successfully updating an entity in the database. Use this method for post-update business logic such as sending notifications, publishing events, or triggering external systems.

Syntax

public virtual System.Threading.Tasks.Task<bool> OnUpdatedAsync(TEntity entity)

Parameters

NameTypeDescription
entityTEntityThe entity that was updated.

Returns

Type: System.Threading.Tasks.Task<bool> True if post-update processing was successful; otherwise, false.

OnUpdatedAsync Virtual

Called after successfully updating a collection of entities in the database. Applies OnUpdatedAsync logic to each entity in the collection.

Syntax

public virtual System.Threading.Tasks.Task OnUpdatedAsync(System.Collections.Generic.List<TEntity> entities)

Parameters

NameTypeDescription
entitiesSystem.Collections.Generic.List<TEntity>The collection of entities that were updated.

Returns

Type: System.Threading.Tasks.Task

OnUpdatingAsync Virtual

Called before updating an entity in the database. Automatically handles audit field population and user tracking for entities implementing the appropriate interfaces.

Syntax

public virtual System.Threading.Tasks.Task OnUpdatingAsync(TEntity entity)

Parameters

NameTypeDescription
entityTEntityThe entity to be updated.

Returns

Type: System.Threading.Tasks.Task

Remarks

This method automatically sets:
  • UpdatedById for entities implementing IUpdaterTrackable1`
  • DateUpdated for entities implementing IUpdatedAuditable Override this method to add custom business logic before updating.

OnUpdatingAsync Virtual

Called before updating a collection of entities in the database. Applies OnUpdatingAsync logic to each entity in the collection.

Syntax

public virtual System.Threading.Tasks.Task OnUpdatingAsync(System.Collections.Generic.List<TEntity> entities)

Parameters

NameTypeDescription
entitiesSystem.Collections.Generic.List<TEntity>The collection of entities to be updated.

Returns

Type: System.Threading.Tasks.Task

ReferenceEquals Inherited

Inherited from object

Syntax

public static bool ReferenceEquals(object objA, object objB)

Parameters

NameTypeDescription
objAobject?-
objBobject?-

Returns

Type: bool

ResetAuditProperties

Resets audit properties to an “Inserted” state by setting creation fields and clearing update fields. Sets CreatedById and DateCreated to current values, while clearing UpdatedById and DateUpdated.

Syntax

public void ResetAuditProperties<TDbObservable>(TDbObservable entity) where TDbObservable : CloudNimble.EasyAF.Core.DbObservableObject

Parameters

NameTypeDescription
entityTDbObservableThe entity whose audit properties should be reset.

Type Parameters

  • TDbObservable - Any DbObservableObject in the object model. DOES NOT have to be the entity for this Manager.

ToString Inherited Virtual

Inherited from object

Syntax

public virtual string ToString()

Returns

Type: string?

UpdateAsync

Updates a single entity in the database with optional save operation. Executes the OnUpdatingAsync and OnUpdatedAsync lifecycle hooks.

Syntax

public System.Threading.Tasks.Task<bool> UpdateAsync(TEntity entity, bool save = true)

Parameters

NameTypeDescription
entityTEntityThe entity to be updated.
saveboolWhether to immediately save changes to the database. Defaults to true.

Returns

Type: System.Threading.Tasks.Task<bool> True if the entity was successfully updated; otherwise, false.

Remarks

RWM: This will need to be updated to be generic if it’s going to be in a NuGet package.

UpdateAsync

Updates a single entity in the database using a specified context with optional save operation. Executes the OnUpdatingAsync and OnUpdatedAsync lifecycle hooks.

Syntax

public System.Threading.Tasks.Task<bool> UpdateAsync(TEntity entity, TContext context, bool save = true)

Parameters

NameTypeDescription
entityTEntityThe entity to be updated.
contextTContextThe database context to use for the operation.
saveboolWhether to immediately save changes to the database. Defaults to true.

Returns

Type: System.Threading.Tasks.Task<bool> True if the entity was successfully updated; otherwise, false.

UpdateAsync

Updates a collection of entities in the database with optional save operation. Executes the OnUpdatingAsync and OnUpdatedAsync lifecycle hooks for each entity.

Syntax

public System.Threading.Tasks.Task<bool> UpdateAsync(System.Collections.Generic.List<TEntity> entities, bool save = true)

Parameters

NameTypeDescription
entitiesSystem.Collections.Generic.List<TEntity>The collection of entities to be updated.
saveboolWhether to immediately save changes to the database. Defaults to true.

Returns

Type: System.Threading.Tasks.Task<bool> True if the entities were successfully updated; otherwise, false.

Remarks

RWM: This will need to be updated to be generic if it’s going to be in a NuGet package.

UpdateAsync

Updates a collection of entities in the database using a specified context with optional save operation. Executes the OnUpdatingAsync and OnUpdatedAsync lifecycle hooks for each entity.

Syntax

public System.Threading.Tasks.Task<bool> UpdateAsync(System.Collections.Generic.List<TEntity> entities, TContext context, bool save = true)

Parameters

NameTypeDescription
entitiesSystem.Collections.Generic.List<TEntity>The collection of entities to be updated.
contextTContextThe database context to use for the operation.
saveboolWhether to immediately save changes to the database. Defaults to true.

Returns

Type: System.Threading.Tasks.Task<bool> True if the entities were successfully updated; otherwise, false.