Skip to content

Commit

Permalink
! Checking arguments for validity and null references before processing
Browse files Browse the repository at this point in the history
  • Loading branch information
artiomchi committed Jan 3, 2019
1 parent b517f20 commit fad589e
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,15 @@ Also supports injecting sql command generators to add support for other provider
<RepositoryUrl>https://github.com/artiomchi/FlexLabs.Upsert</RepositoryUrl>
<PackageLicenseUrl>https://opensource.org/licenses/MIT</PackageLicenseUrl>
<PackageTags>Entity Framework Core entity-framework-core EF EntityFramework EntityFrameworkCore EFCore Upsert</PackageTags>
<VersionPrefix>2.0.3</VersionPrefix>
<VersionPrefix>2.0.4</VersionPrefix>
<PackageReleaseNotes>
v2.0.4:
! Checking arguments for validity and null references before processing

v2.0.3:
! Patching incorrectly constructed SQL statement in case no columns need updating

v2.0.0:
+ Adding support for EF 2.1 Type Conversions
+ Adding support for static property/field accessors (e.g. DateTime.Now)
* Explicitly throwing an exception when using identity keys as upsert match columns (since it wouldn't have worked correctly anyway)
Expand Down
14 changes: 13 additions & 1 deletion src/FlexLabs.EntityFrameworkCore.Upsert/UpsertCommandBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class UpsertCommandBuilder<TEntity> where TEntity : class
internal UpsertCommandBuilder(DbContext dbContext, ICollection<TEntity> entities)
{
_dbContext = dbContext;
_entities = entities ?? throw new ArgumentNullException(nameof(entities));
_entities = entities;

_entityType = dbContext.GetService<IModel>().FindEntityType(typeof(TEntity));
}
Expand All @@ -46,6 +46,8 @@ internal UpsertCommandBuilder(DbContext dbContext, ICollection<TEntity> entities
/// <returns>The current instance of the UpsertCommandBuilder</returns>
public UpsertCommandBuilder<TEntity> On(Expression<Func<TEntity, object>> match)
{
if (match == null)
throw new ArgumentNullException(nameof(match));
if (_matchExpression != null)
throw new InvalidOperationException($"Can't call {nameof(On)} twice!");

Expand All @@ -60,6 +62,8 @@ public UpsertCommandBuilder<TEntity> On(Expression<Func<TEntity, object>> match)
/// <returns>The current instance of the UpsertCommandBuilder</returns>
public UpsertCommandBuilder<TEntity> WhenMatched(Expression<Func<TEntity, TEntity>> updater)
{
if (updater == null)
throw new ArgumentNullException(nameof(updater));
if (_updateExpression != null)
throw new InvalidOperationException($"Can't call {nameof(WhenMatched)} twice!");
if (_noUpdate)
Expand All @@ -83,6 +87,8 @@ public UpsertCommandBuilder<TEntity> WhenMatched(Expression<Func<TEntity, TEntit
/// <returns>The current instance of the UpsertCommandBuilder</returns>
public UpsertCommandBuilder<TEntity> WhenMatched(Expression<Func<TEntity, TEntity, TEntity>> updater)
{
if (updater == null)
throw new ArgumentNullException(nameof(updater));
if (_updateExpression != null)
throw new InvalidOperationException($"Can't call {nameof(WhenMatched)} twice!");
if (_noUpdate)
Expand Down Expand Up @@ -134,6 +140,9 @@ private IUpsertCommandRunner GetCommandRunner()
/// </summary>
public void Run()
{
if (_entities.Count == 0)
return;

var commandRunner = GetCommandRunner();
commandRunner.Run(_dbContext, _entityType, _entities, _matchExpression, _updateExpression, _noUpdate, _useExpressionCompiler);
}
Expand All @@ -145,6 +154,9 @@ public void Run()
/// <returns>The asynchronous task for this transaction</returns>
public Task RunAsync(CancellationToken token = default)
{
if (_entities.Count == 0)
return Task.CompletedTask;

var commandRunner = GetCommandRunner();
return commandRunner.RunAsync(_dbContext, _entityType, _entities, _matchExpression, _updateExpression, _noUpdate, _useExpressionCompiler, token);
}
Expand Down
33 changes: 32 additions & 1 deletion src/FlexLabs.EntityFrameworkCore.Upsert/UpsertExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
using FlexLabs.EntityFrameworkCore.Upsert;
using Microsoft.EntityFrameworkCore.Infrastructure;
Expand All @@ -21,6 +22,11 @@ public static class UpsertExtensions
public static UpsertCommandBuilder<TEntity> Upsert<TEntity>(this DbContext dbContext, TEntity entity)
where TEntity : class
{
if (dbContext == null)
throw new ArgumentNullException(nameof(dbContext));
if (entity == null)
throw new ArgumentNullException(nameof(entity));

return UpsertRange(dbContext, entity);
}

Expand All @@ -34,6 +40,11 @@ public static UpsertCommandBuilder<TEntity> Upsert<TEntity>(this DbContext dbCon
public static UpsertCommandBuilder<TEntity> UpsertRange<TEntity>(this DbContext dbContext, params TEntity[] entities)
where TEntity : class
{
if (dbContext == null)
throw new ArgumentNullException(nameof(dbContext));
if (entities == null)
throw new ArgumentNullException(nameof(entities));

return new UpsertCommandBuilder<TEntity>(dbContext, entities);
}

Expand All @@ -47,6 +58,11 @@ public static UpsertCommandBuilder<TEntity> UpsertRange<TEntity>(this DbContext
public static UpsertCommandBuilder<TEntity> UpsertRange<TEntity>(this DbContext dbContext, IEnumerable<TEntity> entities)
where TEntity : class
{
if (dbContext == null)
throw new ArgumentNullException(nameof(dbContext));
if (entities == null)
throw new ArgumentNullException(nameof(entities));

ICollection<TEntity> collection;
if (entities is ICollection<TEntity> entityCollection)
collection = entityCollection;
Expand All @@ -65,6 +81,11 @@ public static UpsertCommandBuilder<TEntity> UpsertRange<TEntity>(this DbContext
public static UpsertCommandBuilder<TEntity> Upsert<TEntity>(this DbSet<TEntity> dbSet, TEntity entity)
where TEntity : class
{
if (dbSet == null)
throw new ArgumentNullException(nameof(dbSet));
if (entity == null)
throw new ArgumentNullException(nameof(entity));

var dbContext = dbSet.GetService<ICurrentDbContext>().Context;
return Upsert(dbContext, entity);
}
Expand All @@ -79,6 +100,11 @@ public static UpsertCommandBuilder<TEntity> Upsert<TEntity>(this DbSet<TEntity>
public static UpsertCommandBuilder<TEntity> UpsertRange<TEntity>(this DbSet<TEntity> dbSet, params TEntity[] entities)
where TEntity : class
{
if (dbSet == null)
throw new ArgumentNullException(nameof(dbSet));
if (entities == null)
throw new ArgumentNullException(nameof(entities));

var dbContext = dbSet.GetService<ICurrentDbContext>().Context;
return UpsertRange(dbContext, entities);
}
Expand All @@ -93,6 +119,11 @@ public static UpsertCommandBuilder<TEntity> UpsertRange<TEntity>(this DbSet<TEnt
public static UpsertCommandBuilder<TEntity> UpsertRange<TEntity>(this DbSet<TEntity> dbSet, IEnumerable<TEntity> entities)
where TEntity : class
{
if (dbSet == null)
throw new ArgumentNullException(nameof(dbSet));
if (entities == null)
throw new ArgumentNullException(nameof(entities));

var dbContext = dbSet.GetService<ICurrentDbContext>().Context;
return UpsertRange(dbContext, entities);
}
Expand Down

0 comments on commit fad589e

Please sign in to comment.