Skip to content

Commit

Permalink
⚙️refactor: Cleaned up overloads for Add, Update & Upsert
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaferlez committed Apr 3, 2024
1 parent 0533f58 commit 2520060
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 35 deletions.
42 changes: 21 additions & 21 deletions src/Simpleverse.Repository.Db/Entity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ protected virtual void Join(QueryBuilder<TModel> builder, TFilter filter) { }
#endregion
}

public class Entity<T> : IAddDb<T>, IUpdateDb<T>, IDeleteDb<T>, IAggregateDb
public class Entity<T> : IAddDb<T>, IUpdateDb<T>, IDeleteDb<T>, IAggregateDb, IUpsertDb<T>
where T : class
{
protected DbRepository Repository { get; }
Expand All @@ -359,12 +359,15 @@ public virtual Task<T> GetAsync(IDbConnection connection, dynamic id, IDbTransac

#region Add

public async Task<int> AddAsync(T model)
public async Task<int> AddAsync(
T model,
Action<IEnumerable<T>, IEnumerable<T>, IEnumerable<PropertyInfo>, IEnumerable<PropertyInfo>> outputMap = null
)
{
return await Repository.ExecuteAsync((conn, tran) => AddAsync(conn, model, transaction: tran));
return await Repository.ExecuteAsync(
(conn, tran) => AddAsync(conn, new[] { model }, outputMap: outputMap, transaction: tran)
);
}
public virtual Task<int> AddAsync(IDbConnection connection, T model, IDbTransaction transaction = null)
=> connection.InsertAsync(model, transaction: transaction);

public async Task<int> AddAsync(
IEnumerable<T> models,
Expand Down Expand Up @@ -398,13 +401,15 @@ public virtual Task<int> AddAsync(

#region Update

public async Task<bool> UpdateAsync(T model)
public async Task<int> UpdateAsync(
T model,
Action<IEnumerable<T>, IEnumerable<T>, IEnumerable<PropertyInfo>, IEnumerable<PropertyInfo>> outputMap = null
)
{
return await Repository.ExecuteAsync((conn, tran) => UpdateAsync(conn, model, transaction: tran));
return await Repository.ExecuteAsync(
(conn, tran) => UpdateAsync(conn, new[] { model }, outputMap: outputMap, transaction: tran)
);
}
public virtual Task<bool> UpdateAsync(IDbConnection connection, T model, IDbTransaction transaction = null)
=> connection.UpdateAsync(model, transaction: transaction);

public async Task<int> UpdateAsync(
IEnumerable<T> models,
Action<IEnumerable<T>, IEnumerable<T>, IEnumerable<PropertyInfo>, IEnumerable<PropertyInfo>> outputMap = null
Expand Down Expand Up @@ -438,20 +443,15 @@ public virtual async Task<int> UpdateAsync(

#region Upsert

public async Task<int> UpsertAsync(T model)
public async Task<int> UpsertAsync(
T model,
Action<IEnumerable<T>, IEnumerable<T>, IEnumerable<PropertyInfo>, IEnumerable<PropertyInfo>> outputMap = null
)
{
return await Repository.ExecuteAsync(
(conn, tran) => UpsertAsync(conn, model, transaction: tran)
(conn, tran) => UpsertAsync(conn, new[] { model }, outputMap: outputMap, transaction: tran)
);
}
public virtual Task<int> UpsertAsync(IDbConnection connection, T model, IDbTransaction transaction = null)
{
if (!(Repository is SqlRepository))
throw new NotSupportedException("Upsert is not supported on non-SQL repository connections.");

return connection.UpsertAsync(model, outputMap: OutputMapper.MapOnce, transaction: transaction);
}

public async Task<int> UpsertAsync(
IEnumerable<T> models,
Action<IEnumerable<T>, IEnumerable<T>, IEnumerable<PropertyInfo>, IEnumerable<PropertyInfo>> outputMap = null
Expand All @@ -471,7 +471,7 @@ public virtual Task<int> UpsertAsync(
if (!(Repository is SqlRepository))
throw new NotSupportedException("Upsert is not supported on non-SQL repository connections.");

return connection.UpsertBulkAsync(models, transaction: transaction);
return connection.UpsertBulkAsync(models, transaction: transaction, outputMap: outputMap);
}

#endregion
Expand Down
1 change: 0 additions & 1 deletion src/Simpleverse.Repository.Db/Operations/IAddDb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ namespace Simpleverse.Repository.Db.Operations
public interface IAddDb<T> : IAdd<T>
where T : class
{
Task<int> AddAsync(IDbConnection connection, T model, IDbTransaction transaction = null);
Task<int> AddAsync(
IDbConnection connection,
IEnumerable<T> models,
Expand Down
8 changes: 6 additions & 2 deletions src/Simpleverse.Repository.Db/Operations/IUpdateDb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@ namespace Simpleverse.Repository.Db.Operations
public interface IUpdateDb<T> : IUpdate<T>
where T : class
{
Task<bool> UpdateAsync(IDbConnection connection, T model, IDbTransaction transaction = null);
Task<int> UpdateAsync(IDbConnection connection, IEnumerable<T> models, Action<IEnumerable<T>, IEnumerable<T>, IEnumerable<PropertyInfo>, IEnumerable<PropertyInfo>> outputMap = null, IDbTransaction transaction = null);
Task<int> UpdateAsync(
IDbConnection connection,
IEnumerable<T> models,
Action<IEnumerable<T>, IEnumerable<T>, IEnumerable<PropertyInfo>, IEnumerable<PropertyInfo>> outputMap = null,
IDbTransaction transaction = null
);
}

public interface IUpdateDb<TUpdate, TFilter, TOptions> : IUpdate<TUpdate, TFilter, TOptions>
Expand Down
3 changes: 1 addition & 2 deletions src/Simpleverse.Repository.Db/Operations/IUpsertDb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@

namespace Simpleverse.Repository.Db.Operations
{
public interface IUpsertDb<T> : IAddDb<T>, IUpdateDb<T>, IUpsert<T>
public interface IUpsertDb<T> : IUpsert<T>
where T : class
{
Task<int> UpsertAsync(IDbConnection connection, T model, IDbTransaction transaction = null);
Task<int> UpsertAsync(IDbConnection connection, IEnumerable<T> models, Action<IEnumerable<T>, IEnumerable<T>, IEnumerable<PropertyInfo>, IEnumerable<PropertyInfo>> outputMap = null, IDbTransaction transaction = null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
<PackageTags>Dapper, Bulk, Merge, Upsert, Delete, Insert, Update, Repository</PackageTags>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<Version>2.0.31</Version>
<Version>2.0.32</Version>
<Description>High performance operation for MS SQL Server built for Dapper ORM. Including bulk operations Insert, Update, Delete, Get as well as Upsert both single and bulk.</Description>
<AssemblyVersion>2.0.31</AssemblyVersion>
<FileVersion>2.0.31</FileVersion>
<AssemblyVersion>2.0.32</AssemblyVersion>
<FileVersion>2.0.32</FileVersion>
<RepositoryUrl>https://github.com/lukaferlez/Simpleverse.Repository</RepositoryUrl>
<PackageReadmeFile>README.md</PackageReadmeFile>
<EmbedAllSources>true</EmbedAllSources>
Expand Down
2 changes: 1 addition & 1 deletion src/Simpleverse.Repository/Operations/IAdd.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Simpleverse.Repository.Operations
public interface IAdd<T>
where T : class
{
Task<int> AddAsync(T model);
Task<int> AddAsync(T model, Action<IEnumerable<T>, IEnumerable<T>, IEnumerable<PropertyInfo>, IEnumerable<PropertyInfo>> outputMap = null);
Task<int> AddAsync(IEnumerable<T> models, Action<IEnumerable<T>, IEnumerable<T>, IEnumerable<PropertyInfo>, IEnumerable<PropertyInfo>> outputMap = null);
}
}
2 changes: 1 addition & 1 deletion src/Simpleverse.Repository/Operations/IUpdate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Simpleverse.Repository.Operations
public interface IUpdate<T>
where T : class
{
Task<bool> UpdateAsync(T model);
Task<int> UpdateAsync(T model, Action<IEnumerable<T>, IEnumerable<T>, IEnumerable<PropertyInfo>, IEnumerable<PropertyInfo>> outputMap = null);
Task<int> UpdateAsync(IEnumerable<T> models, Action<IEnumerable<T>, IEnumerable<T>, IEnumerable<PropertyInfo>, IEnumerable<PropertyInfo>> outputMap = null);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Simpleverse.Repository/Operations/IUpsert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Simpleverse.Repository.Operations
public interface IUpsert<T> : IAdd<T>, IUpdate<T>
where T : class
{
Task<int> UpsertAsync(T model);
Task<int> UpsertAsync(T model, Action<IEnumerable<T>, IEnumerable<T>, IEnumerable<PropertyInfo>, IEnumerable<PropertyInfo>> outputMap = null);
Task<int> UpsertAsync(IEnumerable<T> models, Action<IEnumerable<T>, IEnumerable<T>, IEnumerable<PropertyInfo>, IEnumerable<PropertyInfo>> outputMap = null);
}
}
6 changes: 3 additions & 3 deletions src/Simpleverse.Repository/Simpleverse.Repository.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
<PackageTags>Repository</PackageTags>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<Version>1.0.8</Version>
<Version>1.0.9</Version>
<Description>Base repository elements.</Description>
<AssemblyVersion>1.0.8</AssemblyVersion>
<FileVersion>1.0.8</FileVersion>
<AssemblyVersion>1.0.9</AssemblyVersion>
<FileVersion>1.0.9</FileVersion>
<RepositoryUrl>https://github.com/lukaferlez/Simpleverse.Repository</RepositoryUrl>
<PackageReadmeFile>README.md</PackageReadmeFile>
<!-- Optional: Publish the repository URL in the built .nupkg (in the NuSpec <Repository> element) -->
Expand Down

0 comments on commit 2520060

Please sign in to comment.