Skip to content

Commit

Permalink
Add more extensions on DbContext and DbSet
Browse files Browse the repository at this point in the history
  • Loading branch information
rungwiroon committed Jan 2, 2024
1 parent 1429b8f commit 49a03c0
Show file tree
Hide file tree
Showing 4 changed files with 191 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Version>3.0.0-preview-6</Version>
<Version>3.0.0-preview-7</Version>
<Description>A functional extensions for Entity Framework.</Description>
<PackageProjectUrl>https://github.com/codehardth/Codehard.Common/tree/main/src/Codehard.Functional</PackageProjectUrl>
<RepositoryUrl>https://github.com/codehardth/Codehard.Common/tree/main/src/Codehard.Functional</RepositoryUrl>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,33 @@ public static Eff<Unit> AddRangeEff<TEntity>(
return unit;
});
}

/// <summary>
/// Adds a range of entities to the DbContext in an effectful manner.
/// </summary>
/// <param name="dbContext">The DbContext instance.</param>
/// <param name="entities">The entities to be added.</param>
/// <typeparam name="TEntity">The type of the entities to be added.</typeparam>
/// <returns>An Eff&lt;Unit&gt; representing the effectful operation. The result is a Unit indicating the operation has been performed.</returns>
public static Eff<Unit> AddRangeEff<TEntity>(
this DbContext dbContext, IEnumerable<TEntity> entities)
where TEntity : class
{
return Eff(() =>
{
dbContext.AddRange(entities);

return unit;
});
}

/// <summary>
/// Updates an entity in the DbContext in an effectful manner.
/// </summary>
/// <param name="dbContext">The DbContext instance.</param>
/// <param name="entity">The entity to be updated.</param>
/// <typeparam name="TEntity">The type of the entity to be updated.</typeparam>
/// <returns>An Eff&lt;EntityEntry&lt;TEntity&gt;&gt; representing the effectful operation. The result is an EntityEntry indicating the operation has been performed.</returns>
public static Eff<EntityEntry<TEntity>> UpdateEff<TEntity>(
this DbContext dbContext, TEntity entity)
where TEntity : class
Expand All @@ -108,6 +134,13 @@ public static Eff<EntityEntry<TEntity>> UpdateEff<TEntity>(
Eff(() => dbContext.Update(entity));
}

/// <summary>
/// Updates a range of entities in the DbContext in an effectful manner.
/// </summary>
/// <param name="dbContext">The DbContext instance.</param>
/// <param name="entities">The entities to be updated.</param>
/// <typeparam name="TEntity">The type of the entities to be updated.</typeparam>
/// <returns>An Eff&lt;Unit&gt; representing the effectful operation. The result is a Unit indicating the operation has been performed.</returns>
public static Eff<Unit> UpdateRangeEff<TEntity>(
this DbContext dbContext, params TEntity[] entities)
where TEntity : class
Expand All @@ -120,6 +153,32 @@ public static Eff<Unit> UpdateRangeEff<TEntity>(
});
}

/// <summary>
/// Updates a range of entities in the DbContext in an effectful manner.
/// </summary>
/// <param name="dbContext">The DbContext instance.</param>
/// <param name="entities">The entities to be updated.</param>
/// <typeparam name="TEntity">The type of the entities to be updated.</typeparam>
/// <returns>An Eff&lt;Unit&gt; representing the effectful operation. The result is a Unit indicating the operation has been performed.</returns>
public static Eff<Unit> UpdateRangeEff<TEntity>(
this DbContext dbContext, IEnumerable<TEntity> entities)
where TEntity : class
{
return Eff(() =>
{
dbContext.UpdateRange(entities);

return unit;
});
}

/// <summary>
/// Removes an entity from the DbContext in an effectful manner.
/// </summary>
/// <param name="dbContext">The DbContext instance.</param>
/// <param name="entity">The entity to be removed.</param>
/// <typeparam name="TEntity">The type of the entity to be removed.</typeparam>
/// <returns>An Eff&lt;EntityEntry&lt;TEntity&gt;&gt; representing the effectful operation. The result is an EntityEntry indicating the operation has been performed.</returns>
public static Eff<EntityEntry<TEntity>> RemoveEff<TEntity>(
this DbContext dbContext, TEntity entity)
where TEntity : class
Expand All @@ -128,6 +187,13 @@ public static Eff<EntityEntry<TEntity>> RemoveEff<TEntity>(
Eff(() => dbContext.Remove(entity));
}

/// <summary>
/// Removes a range of entities from the DbContext in an effectful manner.
/// </summary>
/// <param name="dbContext">The DbContext instance.</param>
/// <param name="entities">The entities to be removed.</param>
/// <typeparam name="TEntity">The type of the entities to be removed.</typeparam>
/// <returns>An Eff&lt;Unit&gt; representing the effectful operation. The result is a Unit indicating the operation has been performed.</returns>
public static Eff<Unit> RemoveRangeEff<TEntity>(
this DbContext dbContext, params TEntity[] entities)
where TEntity : class
Expand All @@ -139,4 +205,23 @@ public static Eff<Unit> RemoveRangeEff<TEntity>(
return unit;
});
}

/// <summary>
/// Removes a range of entities from the DbContext in an effectful manner.
/// </summary>
/// <param name="dbContext">The DbContext instance.</param>
/// <param name="entities">The entities to be removed.</param>
/// <typeparam name="TEntity">The type of the entities to be removed.</typeparam>
/// <returns>An Eff&lt;Unit&gt; representing the effectful operation. The result is a Unit indicating the operation has been performed.</returns>
public static Eff<Unit> RemoveRangeEff<TEntity>(
this DbContext dbContext, IEnumerable<TEntity> entities)
where TEntity : class
{
return Eff(() =>
{
dbContext.RemoveRange(entities);

return unit;
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,32 @@
// ReSharper disable once CheckNamespace
namespace Microsoft.EntityFrameworkCore;

/// <summary>
/// Provides extension methods for the DbSet class.
/// </summary>
public static class DbSetExtensions
{
/// <summary>
/// Adds an entity to the DbSet in an effectful manner.
/// </summary>
/// <param name="dbSet">The DbSet instance.</param>
/// <param name="entity">The entity to be added.</param>
/// <typeparam name="TEntity">The type of the entity to be added.</typeparam>
/// <returns>An Eff&lt;EntityEntry&lt;TEntity&gt;&gt; representing the effectful operation. The result is an EntityEntry indicating the operation has been performed.</returns>
public static Eff<EntityEntry<TEntity>> AddEff<TEntity>(
this DbSet<TEntity> dbSet, TEntity entity)
where TEntity : class
{
return Eff(() => dbSet.Add(entity));
}

/// <summary>
/// Adds a range of entities to the DbSet in an effectful manner.
/// </summary>
/// <param name="dbSet">The DbSet instance.</param>
/// <param name="entities">The entities to be added.</param>
/// <typeparam name="TEntity">The type of the entities to be added.</typeparam>
/// <returns>An Eff&lt;Unit&gt; representing the effectful operation. The result is a Unit indicating the operation has been performed.</returns>
public static Eff<Unit> AddRangeEff<TEntity>(
this DbSet<TEntity> dbSet, params TEntity[] entities)
where TEntity : class
Expand All @@ -26,13 +43,46 @@ public static Eff<Unit> AddRangeEff<TEntity>(
});
}

/// <summary>
/// Adds a range of entities to the DbSet in an effectful manner.
/// </summary>
/// <param name="dbSet">The DbSet instance.</param>
/// <param name="entities">The entities to be added.</param>
/// <typeparam name="TEntity">The type of the entities to be added.</typeparam>
/// <returns>An Eff&lt;Unit&gt; representing the effectful operation. The result is a Unit indicating the operation has been performed.</returns>
public static Eff<Unit> AddRangeEff<TEntity>(
this DbSet<TEntity> dbSet, IEnumerable<TEntity> entities)
where TEntity : class
{
return Eff(() =>
{
dbSet.AddRange(entities);

return unit;
});
}

/// <summary>
/// Updates an entity in the DbSet in an effectful manner.
/// </summary>
/// <param name="dbSet">The DbSet instance.</param>
/// <param name="entity">The entity to be updated.</param>
/// <typeparam name="TEntity">The type of the entity to be updated.</typeparam>
/// <returns>An Eff&lt;EntityEntry&lt;TEntity&gt;&gt; representing the effectful operation. The result is an EntityEntry indicating the operation has been performed.</returns>
public static Eff<EntityEntry<TEntity>> UpdateEff<TEntity>(
this DbSet<TEntity> dbSet, TEntity entity)
where TEntity : class
{
return Eff(() => dbSet.Update(entity));
}

/// <summary>
/// Updates a range of entities in the DbSet in an effectful manner.
/// </summary>
/// <param name="dbSet">The DbSet instance.</param>
/// <param name="entities">The entities to be updated.</param>
/// <typeparam name="TEntity">The type of the entities to be updated.</typeparam>
/// <returns>An Eff&lt;Unit&gt; representing the effectful operation. The result is a Unit indicating the operation has been performed.</returns>
public static Eff<Unit> UpdateRangeEff<TEntity>(
this DbSet<TEntity> dbSet, params TEntity[] entities)
where TEntity : class
Expand All @@ -45,13 +95,46 @@ public static Eff<Unit> UpdateRangeEff<TEntity>(
});
}

/// <summary>
/// Updates a range of entities in the DbSet in an effectful manner.
/// </summary>
/// <param name="dbSet">The DbSet instance.</param>
/// <param name="entities">The entities to be updated.</param>
/// <typeparam name="TEntity">The type of the entities to be updated.</typeparam>
/// <returns>An Eff&lt;Unit&gt; representing the effectful operation. The result is a Unit indicating the operation has been performed.</returns>
public static Eff<Unit> UpdateRangeEff<TEntity>(
this DbSet<TEntity> dbSet, IEnumerable<TEntity> entities)
where TEntity : class
{
return Eff(() =>
{
dbSet.UpdateRange(entities);

return unit;
});
}

/// <summary>
/// Removes an entity from the DbSet in an effectful manner.
/// </summary>
/// <param name="dbSet">The DbSet instance.</param>
/// <param name="entity">The entity to be removed.</param>
/// <typeparam name="TEntity">The type of the entity to be removed.</typeparam>
/// <returns>An Eff&lt;EntityEntry&lt;TEntity&gt;&gt; representing the effectful operation. The result is an EntityEntry indicating the operation has been performed.</returns>
public static Eff<EntityEntry<TEntity>> RemoveEff<TEntity>(
this DbSet<TEntity> dbSet, TEntity entity)
where TEntity : class
{
return Eff(() => dbSet.Remove(entity));
}

/// <summary>
/// Removes a range of entities from the DbSet in an effectful manner.
/// </summary>
/// <param name="dbSet">The DbSet instance.</param>
/// <param name="entities">The entities to be removed.</param>
/// <typeparam name="TEntity">The type of the entities to be removed.</typeparam>
/// <returns>An Eff&lt;Unit&gt; representing the effectful operation. The result is a Unit indicating the operation has been performed.</returns>
public static Eff<Unit> RemoveRangeEff<TEntity>(
this DbSet<TEntity> dbSet, params TEntity[] entities)
where TEntity : class
Expand All @@ -63,4 +146,23 @@ public static Eff<Unit> RemoveRangeEff<TEntity>(
return unit;
});
}

/// <summary>
/// Removes a range of entities from the DbSet in an effectful manner.
/// </summary>
/// <param name="dbSet">The DbSet instance.</param>
/// <param name="entities">The entities to be removed.</param>
/// <typeparam name="TEntity">The type of the entities to be removed.</typeparam>
/// <returns>An Eff&lt;Unit&gt; representing the effectful operation. The result is a Unit indicating the operation has been performed.</returns>
public static Eff<Unit> RemoveRangeEff<TEntity>(
this DbSet<TEntity> dbSet, IEnumerable<TEntity> entities)
where TEntity : class
{
return Eff(() =>
{
dbSet.RemoveRange(entities);

return unit;
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down

0 comments on commit 49a03c0

Please sign in to comment.