From f2d07dcf1f75f3c69d4d936533650291908d7c3a Mon Sep 17 00:00:00 2001 From: Andreas Mueller Date: Wed, 29 Mar 2023 18:10:28 +0200 Subject: [PATCH 1/3] Updated CI --- .editorconfig | 2 +- .gitlab-ci.yml | 105 ++++++++++++++++++++++++++++++++++++------ Directory.Build.props | 2 +- 3 files changed, 93 insertions(+), 16 deletions(-) diff --git a/.editorconfig b/.editorconfig index 5426f75..108919f 100644 --- a/.editorconfig +++ b/.editorconfig @@ -143,6 +143,6 @@ csharp_space_between_method_declaration_parameter_list_parentheses = false csharp_space_between_parentheses = false csharp_space_between_square_brackets = false -[*.{xml,csproj,targets,props,json}] +[*.{xml,csproj,targets,props,json,yml}] indent_size = 2 indent_style = space diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 37ec665..2e23646 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -6,33 +6,110 @@ variables: LANG: "de" -debug-job: + +stages: + - build + - test + - deploy + + + +debug-build: + stage: build tags: - docker + - lnx except: - tags - # branch-coverage - #coverage: '/Total[^|]*\|[^|]*\|\s*([0-9.%]+)/' - # line-coverage - coverage: '/Total[^|]*\|\s*([0-9.%]+)/' script: - dotnet restore --no-cache --force - dotnet build -c Debug --nologo --no-restore --no-incremental - - dotnet test -c Debug --nologo --no-restore --no-build - - dotnet nuget push -k $BAGET_APIKEY -s https://nuget.am-wd.de/v3/index.json --skip-duplicate **/*.nupkg + - mkdir ./artifacts + - mv ./AMWD.Common/bin/Debug/*.nupkg ./artifacts/ + - mv ./AMWD.Common/bin/Debug/*.snupkg ./artifacts/ + - mv ./AMWD.Common.AspNetCore/bin/Debug/*.nupkg ./artifacts/ + - mv ./AMWD.Common.AspNetCore/bin/Debug/*.snupkg ./artifacts/ + - mv ./AMWD.Common.EntityFrameworkCore/bin/Debug/*.nupkg ./artifacts/ + - mv ./AMWD.Common.EntityFrameworkCore/bin/Debug/*.snupkg ./artifacts/ + - mv ./AMWD.Common.Moq/bin/Debug/*.nupkg ./artifacts/ + - mv ./AMWD.Common.Moq/bin/Debug/*.snupkg ./artifacts/ + artifacts: + paths: + - artifacts/*.nupkg + - artifacts/*.snupkg + expire_in: 7 days - -release-job: +debug-test: + stage: test + dependencies: + - debug-build tags: - docker - only: + - lnx + except: - tags # branch-coverage - #coverage: '/Total[^|]*\|[^|]*\|\s*([0-9.%]+)/' + coverage: '/Total[^|]*\|[^|]*\|\s*([0-9.%]+)/' + script: + - dotnet restore --no-cache --force + - dotnet test -c Debug --nologo --no-restore + + + + +release-build: + stage: build + tags: + - docker + - lnx + - amd64 + only: + - tags + script: + - dotnet restore --no-cache --force + - dotnet build -c Release --nologo --no-restore --no-incremental + - mkdir ./artifacts + - mv ./AMWD.Common/bin/Release/*.nupkg ./artifacts/ + - mv ./AMWD.Common/bin/Release/*.snupkg ./artifacts/ + - mv ./AMWD.Common.AspNetCore/bin/Release/*.nupkg ./artifacts/ + - mv ./AMWD.Common.AspNetCore/bin/Release/*.snupkg ./artifacts/ + - mv ./AMWD.Common.EntityFrameworkCore/bin/Release/*.nupkg ./artifacts/ + - mv ./AMWD.Common.EntityFrameworkCore/bin/Release/*.snupkg ./artifacts/ + - mv ./AMWD.Common.Moq/bin/Release/*.nupkg ./artifacts/ + - mv ./AMWD.Common.Moq/bin/Release/*.snupkg ./artifacts/ + artifacts: + paths: + - artifacts/*.nupkg + - artifacts/*.snupkg + expire_in: 1 day + +release-test: + stage: test + dependencies: + - release-build + tags: + - docker + - lnx + - amd64 + only: + - tags # line-coverage coverage: '/Total[^|]*\|\s*([0-9.%]+)/' script: - dotnet restore --no-cache --force - - dotnet build -c Release --nologo --no-restore --no-incremental - - dotnet test -c Release --nologo --no-restore --no-build - - dotnet nuget push -k $BAGET_APIKEY -s https://nuget.am-wd.de/v3/index.json --skip-duplicate **/*.nupkg + - dotnet test -c Release --nologo --no-restore + + +release-deploy: + stage: deploy + dependencies: + - release-build + - release-test + tags: + - docker + - lnx + - amd64 + only: + - tags + script: + - dotnet nuget push -k $BAGET_APIKEY -s https://nuget.am-wd.de/v3/index.json --skip-duplicate artifacts/*.nupkg diff --git a/Directory.Build.props b/Directory.Build.props index e32b6ca..e903063 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -1,6 +1,6 @@ - {semvertag:main}{!:-mod} + {semvertag:main}{!:-dev} true false From 5c4165dfd0457ebdf4c8a88b6b9717e8db3c07e8 Mon Sep 17 00:00:00 2001 From: Andreas Mueller Date: Wed, 29 Mar 2023 18:11:09 +0200 Subject: [PATCH 2/3] Added DbExtensions for database transactions --- .../Extensions/DatabaseFacadeExtensions.cs | 4 +- .../Extensions/DbContextExensions.cs | 91 +++++++++++++++++++ AMWD.Common/Logging/FileLogger.cs | 4 +- 3 files changed, 95 insertions(+), 4 deletions(-) create mode 100644 AMWD.Common.EntityFrameworkCore/Extensions/DbContextExensions.cs diff --git a/AMWD.Common.EntityFrameworkCore/Extensions/DatabaseFacadeExtensions.cs b/AMWD.Common.EntityFrameworkCore/Extensions/DatabaseFacadeExtensions.cs index 184c247..c41c2fa 100644 --- a/AMWD.Common.EntityFrameworkCore/Extensions/DatabaseFacadeExtensions.cs +++ b/AMWD.Common.EntityFrameworkCore/Extensions/DatabaseFacadeExtensions.cs @@ -114,7 +114,7 @@ namespace Microsoft.EntityFrameworkCore } } - private static DatabaseProvider GetProviderType(this DatabaseFacade database) + internal static DatabaseProvider GetProviderType(this DatabaseFacade database) => GetProviderType(database.ProviderName); private static DatabaseProvider GetProviderType(this DbConnection connection) @@ -342,7 +342,7 @@ END;" } } - private enum DatabaseProvider + internal enum DatabaseProvider { MySQL = 1, Oracle = 2, diff --git a/AMWD.Common.EntityFrameworkCore/Extensions/DbContextExensions.cs b/AMWD.Common.EntityFrameworkCore/Extensions/DbContextExensions.cs new file mode 100644 index 0000000..d24fd71 --- /dev/null +++ b/AMWD.Common.EntityFrameworkCore/Extensions/DbContextExensions.cs @@ -0,0 +1,91 @@ +using System; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.EntityFrameworkCore.Storage; + +namespace Microsoft.EntityFrameworkCore +{ + /// + /// Extensions for the . + /// + public static class DbContextExensions + { + /// + /// Starts a new transaction. + /// + /// + /// See Transactions in EF Core for more information. + /// + /// The current . + /// + /// A that represents the started transaction. + /// + public static IDbContextTransaction BeginTransaction(this DbContext dbContext) + { + if (dbContext.Database.GetProviderType() == DatabaseFacadeExtensions.DatabaseProvider.InMemory) + return new DbContextTransactionStub(); + + return dbContext.Database.BeginTransaction(); + } + + /// + /// Asynchronously starts a new transaction. + /// + /// + /// + /// Entity Framework Core does not support multiple parallel operations being run on the same DbContext instance. This + /// includes both parallel execution of async queries and any explicit concurrent use from multiple threads. + /// Therefore, always await async calls immediately, or use separate DbContext instances for operations that execute + /// in parallel. See Avoiding DbContext threading issues + /// for more information. + /// + /// + /// See Transactions in EF Core for more information. + /// + /// + /// The current . + /// A to observe while waiting for the task to complete. + /// + /// A task that represents the asynchronous transaction initialization. The task result contains a that represents the started transaction. + /// + /// If the is canceled. + public static Task BeginTransactionAsync(this DbContext dbContext, CancellationToken cancellationToken) + { + if (dbContext.Database.GetProviderType() == DatabaseFacadeExtensions.DatabaseProvider.InMemory) + return Task.FromResult(new DbContextTransactionStub()); + + return dbContext.Database.BeginTransactionAsync(cancellationToken); + } + + /// + private class DbContextTransactionStub : IDbContextTransaction + { + /// + public Guid TransactionId { get; private set; } = Guid.NewGuid(); + + /// + public void Commit() + { } + + /// + public Task CommitAsync(CancellationToken cancellationToken = default) + => Task.CompletedTask; + + /// + public void Dispose() + { } + + /// + public ValueTask DisposeAsync() + => new(Task.CompletedTask); + + /// + public void Rollback() + { } + + /// + public Task RollbackAsync(CancellationToken cancellationToken = default) + => Task.CompletedTask; + } + } +} diff --git a/AMWD.Common/Logging/FileLogger.cs b/AMWD.Common/Logging/FileLogger.cs index 73d233a..834c34f 100644 --- a/AMWD.Common/Logging/FileLogger.cs +++ b/AMWD.Common/Logging/FileLogger.cs @@ -14,9 +14,9 @@ namespace AMWD.Common.Logging /// Implements a file logging based on the interface. /// /// - /// This implementation is also implementing the interface! + /// This implementation is also using the interface! ///
- /// Inspired by + /// Inspired by ConsoleLogger.cs ///
/// /// From cb133aea0c9baf9ddb7c62b3c1f447ee5b3cfc95 Mon Sep 17 00:00:00 2001 From: Andreas Mueller Date: Wed, 29 Mar 2023 18:11:36 +0200 Subject: [PATCH 3/3] Updating Changelog --- CHANGELOG.md | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6dd0cf3..9a6d56e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,13 +5,18 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). -## [Upcoming](https://git.am-wd.de/AM.WD/common/compare/v1.10.0...main) - 0000-00-00 +## [Upcoming](https://git.am-wd.de/AM.WD/common/compare/v1.11.0...main) - 0000-00-00 + +_no changes yet_ + +## [v1.11.0](https://git.am-wd.de/AM.WD/common/compare/v1.10.0...v1.11.0) - 2023-03-29 ### Added - `directory.build.props` and `directory.build.targets` - `GetDisplayName()` extension for enums (`DisplayAttribute(Name = "")`) - `FileLogger` as additional `ILogger` implementation (from `Microsoft.Extensions.Logging`) +- `StartTransaction(Async)` as `DbContext` extensions; prevents exceptions on `InMemory` database ## [v1.10.0](https://git.am-wd.de/AM.WD/common/compare/v1.9.0...v1.10.0) - 2022-09-18