From 49a03c0ee321979b35efc7747ea7c00360e4fbbb Mon Sep 17 00:00:00 2001 From: Rungwiroon Komalitipong Date: Thu, 28 Dec 2023 23:32:17 +0700 Subject: [PATCH] Add more extensions on DbContext and DbSet --- ...Codehard.Functional.EntityFramework.csproj | 2 +- .../Extensions/DbContextExtensions.cs | 85 +++++++++++++++ .../Extensions/DbSetExtensions.cs | 102 ++++++++++++++++++ .../ModelBuilderExtensionsTests.cs | 4 +- 4 files changed, 191 insertions(+), 2 deletions(-) diff --git a/src/Codehard.Functional/Codehard.Functional.EntityFramework/Codehard.Functional.EntityFramework.csproj b/src/Codehard.Functional/Codehard.Functional.EntityFramework/Codehard.Functional.EntityFramework.csproj index 7a4dd10..e9827df 100644 --- a/src/Codehard.Functional/Codehard.Functional.EntityFramework/Codehard.Functional.EntityFramework.csproj +++ b/src/Codehard.Functional/Codehard.Functional.EntityFramework/Codehard.Functional.EntityFramework.csproj @@ -4,7 +4,7 @@ net6.0 enable enable - 3.0.0-preview-6 + 3.0.0-preview-7 A functional extensions for Entity Framework. https://github.com/codehardth/Codehard.Common/tree/main/src/Codehard.Functional https://github.com/codehardth/Codehard.Common/tree/main/src/Codehard.Functional diff --git a/src/Codehard.Functional/Codehard.Functional.EntityFramework/Extensions/DbContextExtensions.cs b/src/Codehard.Functional/Codehard.Functional.EntityFramework/Extensions/DbContextExtensions.cs index 5f0b271..5907a11 100644 --- a/src/Codehard.Functional/Codehard.Functional.EntityFramework/Extensions/DbContextExtensions.cs +++ b/src/Codehard.Functional/Codehard.Functional.EntityFramework/Extensions/DbContextExtensions.cs @@ -99,7 +99,33 @@ public static Eff AddRangeEff( return unit; }); } + + /// + /// Adds a range of entities to the DbContext in an effectful manner. + /// + /// The DbContext instance. + /// The entities to be added. + /// The type of the entities to be added. + /// An Eff<Unit> representing the effectful operation. The result is a Unit indicating the operation has been performed. + public static Eff AddRangeEff( + this DbContext dbContext, IEnumerable entities) + where TEntity : class + { + return Eff(() => + { + dbContext.AddRange(entities); + + return unit; + }); + } + /// + /// Updates an entity in the DbContext in an effectful manner. + /// + /// The DbContext instance. + /// The entity to be updated. + /// The type of the entity to be updated. + /// An Eff<EntityEntry<TEntity>> representing the effectful operation. The result is an EntityEntry indicating the operation has been performed. public static Eff> UpdateEff( this DbContext dbContext, TEntity entity) where TEntity : class @@ -108,6 +134,13 @@ public static Eff> UpdateEff( Eff(() => dbContext.Update(entity)); } + /// + /// Updates a range of entities in the DbContext in an effectful manner. + /// + /// The DbContext instance. + /// The entities to be updated. + /// The type of the entities to be updated. + /// An Eff<Unit> representing the effectful operation. The result is a Unit indicating the operation has been performed. public static Eff UpdateRangeEff( this DbContext dbContext, params TEntity[] entities) where TEntity : class @@ -120,6 +153,32 @@ public static Eff UpdateRangeEff( }); } + /// + /// Updates a range of entities in the DbContext in an effectful manner. + /// + /// The DbContext instance. + /// The entities to be updated. + /// The type of the entities to be updated. + /// An Eff<Unit> representing the effectful operation. The result is a Unit indicating the operation has been performed. + public static Eff UpdateRangeEff( + this DbContext dbContext, IEnumerable entities) + where TEntity : class + { + return Eff(() => + { + dbContext.UpdateRange(entities); + + return unit; + }); + } + + /// + /// Removes an entity from the DbContext in an effectful manner. + /// + /// The DbContext instance. + /// The entity to be removed. + /// The type of the entity to be removed. + /// An Eff<EntityEntry<TEntity>> representing the effectful operation. The result is an EntityEntry indicating the operation has been performed. public static Eff> RemoveEff( this DbContext dbContext, TEntity entity) where TEntity : class @@ -128,6 +187,13 @@ public static Eff> RemoveEff( Eff(() => dbContext.Remove(entity)); } + /// + /// Removes a range of entities from the DbContext in an effectful manner. + /// + /// The DbContext instance. + /// The entities to be removed. + /// The type of the entities to be removed. + /// An Eff<Unit> representing the effectful operation. The result is a Unit indicating the operation has been performed. public static Eff RemoveRangeEff( this DbContext dbContext, params TEntity[] entities) where TEntity : class @@ -139,4 +205,23 @@ public static Eff RemoveRangeEff( return unit; }); } + + /// + /// Removes a range of entities from the DbContext in an effectful manner. + /// + /// The DbContext instance. + /// The entities to be removed. + /// The type of the entities to be removed. + /// An Eff<Unit> representing the effectful operation. The result is a Unit indicating the operation has been performed. + public static Eff RemoveRangeEff( + this DbContext dbContext, IEnumerable entities) + where TEntity : class + { + return Eff(() => + { + dbContext.RemoveRange(entities); + + return unit; + }); + } } \ No newline at end of file diff --git a/src/Codehard.Functional/Codehard.Functional.EntityFramework/Extensions/DbSetExtensions.cs b/src/Codehard.Functional/Codehard.Functional.EntityFramework/Extensions/DbSetExtensions.cs index 1c3d983..312ece7 100644 --- a/src/Codehard.Functional/Codehard.Functional.EntityFramework/Extensions/DbSetExtensions.cs +++ b/src/Codehard.Functional/Codehard.Functional.EntityFramework/Extensions/DbSetExtensions.cs @@ -5,8 +5,18 @@ // ReSharper disable once CheckNamespace namespace Microsoft.EntityFrameworkCore; +/// +/// Provides extension methods for the DbSet class. +/// public static class DbSetExtensions { + /// + /// Adds an entity to the DbSet in an effectful manner. + /// + /// The DbSet instance. + /// The entity to be added. + /// The type of the entity to be added. + /// An Eff<EntityEntry<TEntity>> representing the effectful operation. The result is an EntityEntry indicating the operation has been performed. public static Eff> AddEff( this DbSet dbSet, TEntity entity) where TEntity : class @@ -14,6 +24,13 @@ public static Eff> AddEff( return Eff(() => dbSet.Add(entity)); } + /// + /// Adds a range of entities to the DbSet in an effectful manner. + /// + /// The DbSet instance. + /// The entities to be added. + /// The type of the entities to be added. + /// An Eff<Unit> representing the effectful operation. The result is a Unit indicating the operation has been performed. public static Eff AddRangeEff( this DbSet dbSet, params TEntity[] entities) where TEntity : class @@ -26,6 +43,32 @@ public static Eff AddRangeEff( }); } + /// + /// Adds a range of entities to the DbSet in an effectful manner. + /// + /// The DbSet instance. + /// The entities to be added. + /// The type of the entities to be added. + /// An Eff<Unit> representing the effectful operation. The result is a Unit indicating the operation has been performed. + public static Eff AddRangeEff( + this DbSet dbSet, IEnumerable entities) + where TEntity : class + { + return Eff(() => + { + dbSet.AddRange(entities); + + return unit; + }); + } + + /// + /// Updates an entity in the DbSet in an effectful manner. + /// + /// The DbSet instance. + /// The entity to be updated. + /// The type of the entity to be updated. + /// An Eff<EntityEntry<TEntity>> representing the effectful operation. The result is an EntityEntry indicating the operation has been performed. public static Eff> UpdateEff( this DbSet dbSet, TEntity entity) where TEntity : class @@ -33,6 +76,13 @@ public static Eff> UpdateEff( return Eff(() => dbSet.Update(entity)); } + /// + /// Updates a range of entities in the DbSet in an effectful manner. + /// + /// The DbSet instance. + /// The entities to be updated. + /// The type of the entities to be updated. + /// An Eff<Unit> representing the effectful operation. The result is a Unit indicating the operation has been performed. public static Eff UpdateRangeEff( this DbSet dbSet, params TEntity[] entities) where TEntity : class @@ -45,6 +95,32 @@ public static Eff UpdateRangeEff( }); } + /// + /// Updates a range of entities in the DbSet in an effectful manner. + /// + /// The DbSet instance. + /// The entities to be updated. + /// The type of the entities to be updated. + /// An Eff<Unit> representing the effectful operation. The result is a Unit indicating the operation has been performed. + public static Eff UpdateRangeEff( + this DbSet dbSet, IEnumerable entities) + where TEntity : class + { + return Eff(() => + { + dbSet.UpdateRange(entities); + + return unit; + }); + } + + /// + /// Removes an entity from the DbSet in an effectful manner. + /// + /// The DbSet instance. + /// The entity to be removed. + /// The type of the entity to be removed. + /// An Eff<EntityEntry<TEntity>> representing the effectful operation. The result is an EntityEntry indicating the operation has been performed. public static Eff> RemoveEff( this DbSet dbSet, TEntity entity) where TEntity : class @@ -52,6 +128,13 @@ public static Eff> RemoveEff( return Eff(() => dbSet.Remove(entity)); } + /// + /// Removes a range of entities from the DbSet in an effectful manner. + /// + /// The DbSet instance. + /// The entities to be removed. + /// The type of the entities to be removed. + /// An Eff<Unit> representing the effectful operation. The result is a Unit indicating the operation has been performed. public static Eff RemoveRangeEff( this DbSet dbSet, params TEntity[] entities) where TEntity : class @@ -63,4 +146,23 @@ public static Eff RemoveRangeEff( return unit; }); } + + /// + /// Removes a range of entities from the DbSet in an effectful manner. + /// + /// The DbSet instance. + /// The entities to be removed. + /// The type of the entities to be removed. + /// An Eff<Unit> representing the effectful operation. The result is a Unit indicating the operation has been performed. + public static Eff RemoveRangeEff( + this DbSet dbSet, IEnumerable entities) + where TEntity : class + { + return Eff(() => + { + dbSet.RemoveRange(entities); + + return unit; + }); + } } \ No newline at end of file diff --git a/src/Codehard.Infrastructure/Codehard.Infrastructure.EntityFramework.Tests/ModelBuilderExtensionsTests.cs b/src/Codehard.Infrastructure/Codehard.Infrastructure.EntityFramework.Tests/ModelBuilderExtensionsTests.cs index 22ed8df..7e621e5 100644 --- a/src/Codehard.Infrastructure/Codehard.Infrastructure.EntityFramework.Tests/ModelBuilderExtensionsTests.cs +++ b/src/Codehard.Infrastructure/Codehard.Infrastructure.EntityFramework.Tests/ModelBuilderExtensionsTests.cs @@ -30,7 +30,9 @@ public void WhenUseApplyConfigurationsFromAssemblyForSpecificContext_ShouldApply // Assert var actualEntityTypes = context.Model.GetEntityTypes().Count(); - Assert.Equal(expectedEntityTypes, actualEntityTypes); + + // +2 because of the Money type and the Nullable Money type + Assert.Equal(expectedEntityTypes + 2, actualEntityTypes); static SqliteConnection CreateInMemoryDatabase() {