diff --git a/src/Microservices/Common/ClassifiedAds.Domain/Repositories/IConcurrencyHandler.cs b/src/Microservices/Common/ClassifiedAds.Domain/Repositories/IConcurrencyHandler.cs new file mode 100644 index 000000000..f3eac4bfc --- /dev/null +++ b/src/Microservices/Common/ClassifiedAds.Domain/Repositories/IConcurrencyHandler.cs @@ -0,0 +1,11 @@ +using System; + +namespace ClassifiedAds.Domain.Repositories +{ + public interface IConcurrencyHandler + { + void SetRowVersion(TEntity entity, byte[] version); + + bool IsDbUpdateConcurrencyException(Exception ex); + } +} diff --git a/src/Microservices/Common/ClassifiedAds.Domain/Repositories/IRepository.cs b/src/Microservices/Common/ClassifiedAds.Domain/Repositories/IRepository.cs index 1be9c56d4..b5fcf7dc9 100644 --- a/src/Microservices/Common/ClassifiedAds.Domain/Repositories/IRepository.cs +++ b/src/Microservices/Common/ClassifiedAds.Domain/Repositories/IRepository.cs @@ -8,7 +8,7 @@ namespace ClassifiedAds.Domain.Repositories { - public interface IRepository + public interface IRepository : IConcurrencyHandler where TEntity : AggregateRoot { IUnitOfWork UnitOfWork { get; } diff --git a/src/Microservices/Common/ClassifiedAds.Infrastructure/ClassifiedAds.Infrastructure.csproj b/src/Microservices/Common/ClassifiedAds.Infrastructure/ClassifiedAds.Infrastructure.csproj index 98f6394a5..4cad242f6 100644 --- a/src/Microservices/Common/ClassifiedAds.Infrastructure/ClassifiedAds.Infrastructure.csproj +++ b/src/Microservices/Common/ClassifiedAds.Infrastructure/ClassifiedAds.Infrastructure.csproj @@ -40,7 +40,7 @@ - + diff --git a/src/Microservices/Common/ClassifiedAds.Infrastructure/Persistence/DbContextRepository.cs b/src/Microservices/Common/ClassifiedAds.Infrastructure/Persistence/DbContextRepository.cs index 5ff99a2ec..fc01cf04c 100644 --- a/src/Microservices/Common/ClassifiedAds.Infrastructure/Persistence/DbContextRepository.cs +++ b/src/Microservices/Common/ClassifiedAds.Infrastructure/Persistence/DbContextRepository.cs @@ -111,5 +111,15 @@ public void BulkMerge(IEnumerable entities, Expression + { + void SetRowVersion(TEntity entity, byte[] version); + + bool IsDbUpdateConcurrencyException(Exception ex); + } +} diff --git a/src/ModularMonolith/ClassifiedAds.Domain/Repositories/IRepository.cs b/src/ModularMonolith/ClassifiedAds.Domain/Repositories/IRepository.cs index 1be9c56d4..b5fcf7dc9 100644 --- a/src/ModularMonolith/ClassifiedAds.Domain/Repositories/IRepository.cs +++ b/src/ModularMonolith/ClassifiedAds.Domain/Repositories/IRepository.cs @@ -8,7 +8,7 @@ namespace ClassifiedAds.Domain.Repositories { - public interface IRepository + public interface IRepository : IConcurrencyHandler where TEntity : AggregateRoot { IUnitOfWork UnitOfWork { get; } diff --git a/src/ModularMonolith/ClassifiedAds.Infrastructure/ClassifiedAds.Infrastructure.csproj b/src/ModularMonolith/ClassifiedAds.Infrastructure/ClassifiedAds.Infrastructure.csproj index 0b42801a5..7f6d83713 100644 --- a/src/ModularMonolith/ClassifiedAds.Infrastructure/ClassifiedAds.Infrastructure.csproj +++ b/src/ModularMonolith/ClassifiedAds.Infrastructure/ClassifiedAds.Infrastructure.csproj @@ -38,7 +38,7 @@ - + diff --git a/src/ModularMonolith/ClassifiedAds.Infrastructure/Persistence/DbContextRepository.cs b/src/ModularMonolith/ClassifiedAds.Infrastructure/Persistence/DbContextRepository.cs index 5ff99a2ec..fc01cf04c 100644 --- a/src/ModularMonolith/ClassifiedAds.Infrastructure/Persistence/DbContextRepository.cs +++ b/src/ModularMonolith/ClassifiedAds.Infrastructure/Persistence/DbContextRepository.cs @@ -111,5 +111,15 @@ public void BulkMerge(IEnumerable entities, Expression + { + void SetRowVersion(TEntity entity, byte[] version); + + bool IsDbUpdateConcurrencyException(Exception ex); + } +} diff --git a/src/Monolith/ClassifiedAds.Domain/Repositories/IRepository.cs b/src/Monolith/ClassifiedAds.Domain/Repositories/IRepository.cs index 1be9c56d4..b5fcf7dc9 100644 --- a/src/Monolith/ClassifiedAds.Domain/Repositories/IRepository.cs +++ b/src/Monolith/ClassifiedAds.Domain/Repositories/IRepository.cs @@ -8,7 +8,7 @@ namespace ClassifiedAds.Domain.Repositories { - public interface IRepository + public interface IRepository : IConcurrencyHandler where TEntity : AggregateRoot { IUnitOfWork UnitOfWork { get; } diff --git a/src/Monolith/ClassifiedAds.Persistence/ClassifiedAds.Persistence.csproj b/src/Monolith/ClassifiedAds.Persistence/ClassifiedAds.Persistence.csproj index 8a5e862de..326ccc49d 100644 --- a/src/Monolith/ClassifiedAds.Persistence/ClassifiedAds.Persistence.csproj +++ b/src/Monolith/ClassifiedAds.Persistence/ClassifiedAds.Persistence.csproj @@ -7,7 +7,7 @@ - + diff --git a/src/Monolith/ClassifiedAds.Persistence/Repositories/Repository.cs b/src/Monolith/ClassifiedAds.Persistence/Repositories/Repository.cs index 5ab40dc10..d54692560 100644 --- a/src/Monolith/ClassifiedAds.Persistence/Repositories/Repository.cs +++ b/src/Monolith/ClassifiedAds.Persistence/Repositories/Repository.cs @@ -110,5 +110,15 @@ public void BulkMerge(IEnumerable entities, Expression> idSel { _dbContext.BulkMerge(entities, idSelector, updateColumnNamesSelector, insertColumnNamesSelector); } + + public void SetRowVersion(T entity, byte[] version) + { + _dbContext.Entry(entity).OriginalValues[nameof(entity.RowVersion)] = version; + } + + public bool IsDbUpdateConcurrencyException(Exception ex) + { + return ex is DbUpdateConcurrencyException; + } } }