Skip to content

Commit

Permalink
Modified data providers to receive a transaction object per method
Browse files Browse the repository at this point in the history
  • Loading branch information
Yuck committed Jul 3, 2021
1 parent 582c508 commit d68548c
Show file tree
Hide file tree
Showing 21 changed files with 181 additions and 179 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>Kevin J Lambert</Authors>
<Version>0.8.0</Version>
<Version>0.8.1</Version>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<Description>An implementation of YuckQi.Data using Dapper for MySQL.</Description>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>Kevin J Lambert</Authors>
<Version>0.8.0</Version>
<Version>0.8.1</Version>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<Description>An implementation of YuckQi.Data using Dapper and SimpleCRUD for MSSQL.</Description>
</PropertyGroup>
Expand Down
24 changes: 0 additions & 24 deletions src/YuckQi.Data.Sql.Dapper/Providers/Abstract/DataProviderBase.cs

This file was deleted.

31 changes: 19 additions & 12 deletions src/YuckQi.Data.Sql.Dapper/Providers/ActivationProvider.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
using System;
using System.Data;
using System.Threading.Tasks;
using YuckQi.Data.Abstract;
using YuckQi.Data.Providers.Abstract;
using YuckQi.Data.Sql.Dapper.Providers.Abstract;
using YuckQi.Domain.Aspects.Abstract;
using YuckQi.Domain.Entities.Abstract;

namespace YuckQi.Data.Sql.Dapper.Providers
{
public class ActivationProvider<TEntity, TKey> : DataProviderBase, IActivationProvider<TEntity, TKey> where TEntity : IEntity<TKey>, IActivated, IRevised where TKey : struct
public class ActivationProvider<TEntity, TKey> : IActivationProvider<TEntity, TKey> where TEntity : IEntity<TKey>, IActivated, IRevised where TKey : struct
{
#region Private Members

Expand All @@ -19,7 +18,7 @@ public class ActivationProvider<TEntity, TKey> : DataProviderBase, IActivationPr

#region Constructors

public ActivationProvider(IUnitOfWork context, IRevisionProvider<TEntity, TKey> reviser) : base(context)
public ActivationProvider(IRevisionProvider<TEntity, TKey> reviser)
{
_reviser = reviser ?? throw new ArgumentNullException(nameof(reviser));
}
Expand All @@ -29,56 +28,64 @@ public ActivationProvider(IUnitOfWork context, IRevisionProvider<TEntity, TKey>

#region Public Methods

public TEntity Activate(TEntity entity)
public TEntity Activate(TEntity entity, IDbTransaction transaction)
{
if (entity == null)
throw new ArgumentNullException(nameof(entity));
if (transaction == null)
throw new ArgumentNullException(nameof(transaction));

if (entity.ActivationMomentUtc != null)
return entity;

entity.ActivationMomentUtc = DateTime.UtcNow;

return _reviser.Revise(entity);
return _reviser.Revise(entity, transaction);
}

public Task<TEntity> ActivateAsync(TEntity entity)
public Task<TEntity> ActivateAsync(TEntity entity, IDbTransaction transaction)
{
if (entity == null)
throw new ArgumentNullException(nameof(entity));
if (transaction == null)
throw new ArgumentNullException(nameof(transaction));

if (entity.ActivationMomentUtc != null)
return Task.FromResult(entity);

entity.ActivationMomentUtc = DateTime.UtcNow;

return _reviser.ReviseAsync(entity);
return _reviser.ReviseAsync(entity, transaction);
}

public TEntity Deactivate(TEntity entity)
public TEntity Deactivate(TEntity entity, IDbTransaction transaction)
{
if (entity == null)
throw new ArgumentNullException(nameof(entity));
if (transaction == null)
throw new ArgumentNullException(nameof(transaction));

if (entity.ActivationMomentUtc == null)
return entity;

entity.ActivationMomentUtc = null;

return _reviser.Revise(entity);
return _reviser.Revise(entity, transaction);
}

public Task<TEntity> DeactivateAsync(TEntity entity)
public Task<TEntity> DeactivateAsync(TEntity entity, IDbTransaction transaction)
{
if (entity == null)
throw new ArgumentNullException(nameof(entity));
if (transaction == null)
throw new ArgumentNullException(nameof(transaction));

if (entity.ActivationMomentUtc == null)
return Task.FromResult(entity);

entity.ActivationMomentUtc = null;

return _reviser.ReviseAsync(entity);
return _reviser.ReviseAsync(entity, transaction);
}

#endregion
Expand Down
24 changes: 10 additions & 14 deletions src/YuckQi.Data.Sql.Dapper/Providers/CreationProvider.cs
Original file line number Diff line number Diff line change
@@ -1,48 +1,44 @@
using System;
using System.Data;
using System.Threading.Tasks;
using Dapper;
using Mapster;
using YuckQi.Data.Abstract;
using YuckQi.Data.Exceptions;
using YuckQi.Data.Providers.Abstract;
using YuckQi.Data.Sql.Dapper.Providers.Abstract;
using YuckQi.Domain.Aspects.Abstract;
using YuckQi.Domain.Entities.Abstract;

namespace YuckQi.Data.Sql.Dapper.Providers
{
public class CreationProvider<TEntity, TKey, TRecord> : DataProviderBase, ICreationProvider<TEntity, TKey> where TEntity : IEntity<TKey>, ICreated where TKey : struct
public class CreationProvider<TEntity, TKey, TRecord> : ICreationProvider<TEntity, TKey> where TEntity : IEntity<TKey>, ICreated where TKey : struct
{
#region Constructors

public CreationProvider(IUnitOfWork context) : base(context) { }

#endregion


#region Public Methods

public TEntity Create(TEntity entity)
public TEntity Create(TEntity entity, IDbTransaction transaction)
{
if (entity == null)
throw new ArgumentNullException(nameof(entity));
if (transaction == null)
throw new ArgumentNullException(nameof(transaction));

entity.CreationMomentUtc = DateTime.UtcNow;

if (! (Context.Db.Insert(entity.Adapt<TRecord>(), Context.Transaction) > 0))
if (! (transaction.Connection.Insert(entity.Adapt<TRecord>(), transaction) > 0))
throw new RecordInsertException<TKey>();

return entity;
}

public async Task<TEntity> CreateAsync(TEntity entity)
public async Task<TEntity> CreateAsync(TEntity entity, IDbTransaction transaction)
{
if (entity == null)
throw new ArgumentNullException(nameof(entity));
if (transaction == null)
throw new ArgumentNullException(nameof(transaction));

entity.CreationMomentUtc = DateTime.UtcNow;

if (! (await Context.Db.InsertAsync(entity.Adapt<TRecord>(), Context.Transaction) > 0))
if (! (await transaction.Connection.InsertAsync(entity.Adapt<TRecord>(), transaction) > 0))
throw new RecordInsertException<TKey>();

return entity;
Expand Down
31 changes: 19 additions & 12 deletions src/YuckQi.Data.Sql.Dapper/Providers/LogicalDeletionProvider.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
using System;
using System.Data;
using System.Threading.Tasks;
using YuckQi.Data.Abstract;
using YuckQi.Data.Providers.Abstract;
using YuckQi.Data.Sql.Dapper.Providers.Abstract;
using YuckQi.Domain.Aspects.Abstract;
using YuckQi.Domain.Entities.Abstract;

namespace YuckQi.Data.Sql.Dapper.Providers
{
public class LogicalDeletionProvider<TEntity, TKey> : DataProviderBase, ILogicalDeletionProvider<TEntity, TKey> where TEntity : IEntity<TKey>, IDeleted, IRevised where TKey : struct
public class LogicalDeletionProvider<TEntity, TKey> : ILogicalDeletionProvider<TEntity, TKey> where TEntity : IEntity<TKey>, IDeleted, IRevised where TKey : struct
{
#region Private Members

Expand All @@ -19,7 +18,7 @@ public class LogicalDeletionProvider<TEntity, TKey> : DataProviderBase, ILogical

#region Constructors

public LogicalDeletionProvider(IUnitOfWork context, IRevisionProvider<TEntity, TKey> reviser) : base(context)
public LogicalDeletionProvider(IRevisionProvider<TEntity, TKey> reviser)
{
_reviser = reviser ?? throw new ArgumentNullException(nameof(reviser));
}
Expand All @@ -29,56 +28,64 @@ public LogicalDeletionProvider(IUnitOfWork context, IRevisionProvider<TEntity, T

#region Public Methods

public TEntity Delete(TEntity entity)
public TEntity Delete(TEntity entity, IDbTransaction transaction)
{
if (entity == null)
throw new ArgumentNullException(nameof(entity));
if (transaction == null)
throw new ArgumentNullException(nameof(transaction));

if (entity.DeletionMomentUtc != null)
return entity;

entity.DeletionMomentUtc = DateTime.UtcNow;

return _reviser.Revise(entity);
return _reviser.Revise(entity, transaction);
}

public Task<TEntity> DeleteAsync(TEntity entity)
public Task<TEntity> DeleteAsync(TEntity entity, IDbTransaction transaction)
{
if (entity == null)
throw new ArgumentNullException(nameof(entity));
if (transaction == null)
throw new ArgumentNullException(nameof(transaction));

if (entity.DeletionMomentUtc != null)
return Task.FromResult(entity);

entity.DeletionMomentUtc = DateTime.UtcNow;

return _reviser.ReviseAsync(entity);
return _reviser.ReviseAsync(entity, transaction);
}

public TEntity Restore(TEntity entity)
public TEntity Restore(TEntity entity, IDbTransaction transaction)
{
if (entity == null)
throw new ArgumentNullException(nameof(entity));
if (transaction == null)
throw new ArgumentNullException(nameof(transaction));

if (entity.DeletionMomentUtc == null)
return entity;

entity.DeletionMomentUtc = null;

return _reviser.Revise(entity);
return _reviser.Revise(entity, transaction);
}

public Task<TEntity> RestoreAsync(TEntity entity)
public Task<TEntity> RestoreAsync(TEntity entity, IDbTransaction transaction)
{
if (entity == null)
throw new ArgumentNullException(nameof(entity));
if (transaction == null)
throw new ArgumentNullException(nameof(transaction));

if (entity.DeletionMomentUtc == null)
return Task.FromResult(entity);

entity.DeletionMomentUtc = null;

return _reviser.ReviseAsync(entity);
return _reviser.ReviseAsync(entity, transaction);
}

#endregion
Expand Down
24 changes: 10 additions & 14 deletions src/YuckQi.Data.Sql.Dapper/Providers/PhysicalDeletionProvider.cs
Original file line number Diff line number Diff line change
@@ -1,43 +1,39 @@
using System;
using System.Data;
using System.Threading.Tasks;
using Dapper;
using Mapster;
using YuckQi.Data.Abstract;
using YuckQi.Data.Exceptions;
using YuckQi.Data.Providers.Abstract;
using YuckQi.Data.Sql.Dapper.Providers.Abstract;
using YuckQi.Domain.Entities.Abstract;

namespace YuckQi.Data.Sql.Dapper.Providers
{
public class PhysicalDeletionProvider<TEntity, TKey, TRecord> : DataProviderBase, IPhysicalDeletionProvider<TEntity, TKey> where TEntity : IEntity<TKey> where TKey : struct
public class PhysicalDeletionProvider<TEntity, TKey, TRecord> : IPhysicalDeletionProvider<TEntity, TKey> where TEntity : IEntity<TKey> where TKey : struct
{
#region Constructors

public PhysicalDeletionProvider(IUnitOfWork context) : base(context) { }

#endregion


#region Public Methods

public TEntity Delete(TEntity entity)
public TEntity Delete(TEntity entity, IDbTransaction transaction)
{
if (entity == null)
throw new ArgumentNullException(nameof(entity));
if (transaction == null)
throw new ArgumentNullException(nameof(transaction));

if (Context.Db.Delete(entity.Adapt<TRecord>(), Context.Transaction) <= 0)
if (transaction.Connection.Delete(entity.Adapt<TRecord>(), transaction) <= 0)
throw new RecordDeleteException<TRecord, TKey>(entity.Key);

return entity;
}

public async Task<TEntity> DeleteAsync(TEntity entity)
public async Task<TEntity> DeleteAsync(TEntity entity, IDbTransaction transaction)
{
if (entity == null)
throw new ArgumentNullException(nameof(entity));
if (transaction == null)
throw new ArgumentNullException(nameof(transaction));

if (await Context.Db.DeleteAsync(entity.Adapt<TRecord>(), Context.Transaction) <= 0)
if (await transaction.Connection.DeleteAsync(entity.Adapt<TRecord>(), transaction) <= 0)
throw new RecordDeleteException<TRecord, TKey>(entity.Key);

return entity;
Expand Down
Loading

0 comments on commit d68548c

Please sign in to comment.