From a2dd49548dbd48891d5dfde276b45fad4da6ac4a Mon Sep 17 00:00:00 2001 From: dncsvr Date: Mon, 11 Dec 2023 12:11:41 +0300 Subject: [PATCH 01/16] update unreleased.md --- unreleased.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/unreleased.md b/unreleased.md index 79e701b84..5bc72c7a9 100644 --- a/unreleased.md +++ b/unreleased.md @@ -1 +1,6 @@ # Unreleased + +## Features + +- Beta features are available in do-blueprints-service package; + - `Caching` feature is now added with `MemoryCache` implementation From 1385eb6dfa9b58d48509eec4342b8c6ebb2b057a Mon Sep 17 00:00:00 2001 From: dncsvr Date: Mon, 11 Dec 2023 12:38:00 +0300 Subject: [PATCH 02/16] add business extensions with service and implementation type --- .../Business/BusinessExtensions.cs | 22 ++++++++++++++----- 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/src/blueprints/Do.Blueprints.Service.Application/Business/BusinessExtensions.cs b/src/blueprints/Do.Blueprints.Service.Application/Business/BusinessExtensions.cs index 6f2cbb0f5..5685a8ad2 100644 --- a/src/blueprints/Do.Blueprints.Service.Application/Business/BusinessExtensions.cs +++ b/src/blueprints/Do.Blueprints.Service.Application/Business/BusinessExtensions.cs @@ -8,15 +8,25 @@ public static class BusinessExtensions { public static void AddBusiness(this List source, Func> configure) => source.Add(configure(new())); - public static void AddTransientWithFactory(this IServiceCollection source) where T : class + public static void AddTransientWithFactory(this IServiceCollection source) where TService : class => + source.AddTransientWithFactory(); + + public static void AddTransientWithFactory(this IServiceCollection source) + where TService : class + where TImplementation : class, TService { - source.AddSingleton>(sp => () => sp.GetRequiredServiceUsingRequestServices()); - source.AddTransient(); + source.AddSingleton>(sp => () => sp.GetRequiredServiceUsingRequestServices()); + source.AddTransient(); } - public static void AddScopedWithFactory(this IServiceCollection source) where T : class + public static void AddScopedWithFactory(this IServiceCollection source) where TService : class => + source.AddScopedWithFactory(); + + public static void AddScopedWithFactory(this IServiceCollection source) + where TService : class + where TImplementation : class, TService { - source.AddSingleton>(sp => () => sp.GetRequiredServiceUsingRequestServices()); - source.AddScoped(); + source.AddSingleton>(sp => () => sp.GetRequiredServiceUsingRequestServices()); + source.AddScoped(); } } From 4def6e640028564145005fc3106938c0e598af72 Mon Sep 17 00:00:00 2001 From: dncsvr Date: Mon, 11 Dec 2023 13:55:09 +0300 Subject: [PATCH 03/16] add scoped caching feature implementation and tests --- Directory.Packages.props | 5 +-- .../Caching/CachingConfigurator.cs | 3 ++ .../Caching/CachingExtensions.cs | 9 +++++ .../ScopedMemory/ScopedMemoryExtensions.cs | 9 +++++ .../ScopedMemory/ScopedMemoryFeature.cs | 15 +++++++++ .../ForgeExtensions.cs | 4 +++ .../ServiceSpec.cs | 5 +++ .../ServiceSpecExtensions.cs | 23 +++++++++++++ .../Do.Blueprints.Service.csproj | 1 + .../Caching/ScopedMemory.cs | 33 +++++++++++++++++++ 10 files changed, 105 insertions(+), 2 deletions(-) create mode 100644 src/blueprints/Do.Blueprints.Service.Application/Caching/CachingConfigurator.cs create mode 100644 src/blueprints/Do.Blueprints.Service.Application/Caching/CachingExtensions.cs create mode 100644 src/blueprints/Do.Blueprints.Service.Application/Caching/ScopedMemory/ScopedMemoryExtensions.cs create mode 100644 src/blueprints/Do.Blueprints.Service.Application/Caching/ScopedMemory/ScopedMemoryFeature.cs create mode 100644 test/blueprints/Do.Test.Blueprints.Service.Test/Caching/ScopedMemory.cs diff --git a/Directory.Packages.props b/Directory.Packages.props index 6a0a04640..27883a993 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -8,6 +8,7 @@ + @@ -22,5 +23,5 @@ - - + + \ No newline at end of file diff --git a/src/blueprints/Do.Blueprints.Service.Application/Caching/CachingConfigurator.cs b/src/blueprints/Do.Blueprints.Service.Application/Caching/CachingConfigurator.cs new file mode 100644 index 000000000..882616c1c --- /dev/null +++ b/src/blueprints/Do.Blueprints.Service.Application/Caching/CachingConfigurator.cs @@ -0,0 +1,3 @@ +namespace Do.Caching; + +public class CachingConfigurator { } diff --git a/src/blueprints/Do.Blueprints.Service.Application/Caching/CachingExtensions.cs b/src/blueprints/Do.Blueprints.Service.Application/Caching/CachingExtensions.cs new file mode 100644 index 000000000..0093a44cd --- /dev/null +++ b/src/blueprints/Do.Blueprints.Service.Application/Caching/CachingExtensions.cs @@ -0,0 +1,9 @@ +using Do.Architecture; +using Do.Caching; + +namespace Do; + +public static class CachingExtensions +{ + public static void AddCaching(this List source, Func> configure) => source.Add(configure(new())); +} diff --git a/src/blueprints/Do.Blueprints.Service.Application/Caching/ScopedMemory/ScopedMemoryExtensions.cs b/src/blueprints/Do.Blueprints.Service.Application/Caching/ScopedMemory/ScopedMemoryExtensions.cs new file mode 100644 index 000000000..22b85252a --- /dev/null +++ b/src/blueprints/Do.Blueprints.Service.Application/Caching/ScopedMemory/ScopedMemoryExtensions.cs @@ -0,0 +1,9 @@ +using Do.Caching; +using Do.Caching.ScopedMemory; + +namespace Do; + +public static class ScopedMemoryExtensions +{ + public static ScopedMemoryFeature ScopedMemory(this CachingConfigurator _) => new(); +} diff --git a/src/blueprints/Do.Blueprints.Service.Application/Caching/ScopedMemory/ScopedMemoryFeature.cs b/src/blueprints/Do.Blueprints.Service.Application/Caching/ScopedMemory/ScopedMemoryFeature.cs new file mode 100644 index 000000000..1bf217bf0 --- /dev/null +++ b/src/blueprints/Do.Blueprints.Service.Application/Caching/ScopedMemory/ScopedMemoryFeature.cs @@ -0,0 +1,15 @@ +using Do.Architecture; +using Microsoft.Extensions.Caching.Memory; + +namespace Do.Caching.ScopedMemory; + +public class ScopedMemoryFeature : IFeature +{ + public void Configure(LayerConfigurator configurator) + { + configurator.ConfigureServiceCollection(services => + { + services.AddScopedWithFactory(); + }); + } +} diff --git a/src/blueprints/Do.Blueprints.Service.Application/ForgeExtensions.cs b/src/blueprints/Do.Blueprints.Service.Application/ForgeExtensions.cs index 3332f3338..e13cd0783 100644 --- a/src/blueprints/Do.Blueprints.Service.Application/ForgeExtensions.cs +++ b/src/blueprints/Do.Blueprints.Service.Application/ForgeExtensions.cs @@ -1,5 +1,6 @@ using Do.Architecture; using Do.Business; +using Do.Caching; using Do.Core; using Do.Database; using Do.Documentation; @@ -14,6 +15,7 @@ public static class ForgeExtensions { public static Application Service(this Forge source, Func> business, + Func>? caching = default, Func>? core = default, Func>? database = default, Func>? documentation = default, @@ -24,6 +26,7 @@ public static Application Service(this Forge source, Action? configure = default ) { + caching ??= c => c.ScopedMemory(); core ??= c => c.Dotnet(); database ??= c => c.Sqlite(); documentation ??= c => c.Default(); @@ -43,6 +46,7 @@ public static Application Service(this Forge source, app.Layers.AddRestApi(); app.Features.AddBusiness(business); + app.Features.AddCaching(caching); app.Features.AddCore(core); app.Features.AddDatabase(database); app.Features.AddDocumentation(documentation); diff --git a/src/blueprints/Do.Blueprints.Service.Application/ServiceSpec.cs b/src/blueprints/Do.Blueprints.Service.Application/ServiceSpec.cs index d6064ad39..8acdb6a60 100644 --- a/src/blueprints/Do.Blueprints.Service.Application/ServiceSpec.cs +++ b/src/blueprints/Do.Blueprints.Service.Application/ServiceSpec.cs @@ -1,5 +1,6 @@ using Do.Architecture; using Do.Business; +using Do.Caching; using Do.Core; using Do.Database; using Do.ExceptionHandling; @@ -22,6 +23,7 @@ public abstract class ServiceSpec : Spec protected static ApplicationContext Init( Func> business, + Func>? caching = default, Func>? core = default, Func>? database = default, Func>? exceptionHandling = default, @@ -30,6 +32,7 @@ protected static ApplicationContext Init( Action? configure = default ) { + caching ??= c => c.ScopedMemory(); core ??= c => c.Mock(); database ??= c => c.InMemory(); exceptionHandling ??= c => c.Default(); @@ -46,6 +49,7 @@ protected static ApplicationContext Init( app.Layers.AddTesting(); app.Features.AddBusiness(business); + app.Features.AddCaching(caching); app.Features.AddCore(core); app.Features.AddDatabase(database); app.Features.AddExceptionHandling(exceptionHandling); @@ -84,6 +88,7 @@ public override void TearDown() Session.Clear(); GiveMe.The().Reset(); + GiveMe.AMemoryCache(clear: true); } protected virtual string? GetDefaultSettingsValue(string key) => diff --git a/src/blueprints/Do.Blueprints.Service.Application/ServiceSpecExtensions.cs b/src/blueprints/Do.Blueprints.Service.Application/ServiceSpecExtensions.cs index 7109dd280..e3ef3218f 100644 --- a/src/blueprints/Do.Blueprints.Service.Application/ServiceSpecExtensions.cs +++ b/src/blueprints/Do.Blueprints.Service.Application/ServiceSpecExtensions.cs @@ -1,6 +1,7 @@ using Do.Core; using Do.MockOverrider; using Do.Testing; +using Microsoft.Extensions.Caching.Memory; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Moq; @@ -52,6 +53,28 @@ public static Guid AGuid(this Stubber _, #endregion + #region MemoryCache + + public static IMemoryCache AMemoryCache(this Stubber giveMe, + bool clear = false + ) + { + var getMemoryCache = giveMe.The>(); + var memoryCache = getMemoryCache(); + + if (clear) + { + (memoryCache as MemoryCache)?.Clear(); + } + + return memoryCache; + } + + public static void VerifyCount(this IMemoryCache memoryCache, Func condition) => + condition((memoryCache as MemoryCache)?.Count ?? throw new("`MemoryCache` should have existed")).ShouldBe(true); + + #endregion + #region MockOverrider public static T The(this Stubber _, params object?[] mockOverrides) where T : notnull => diff --git a/src/blueprints/Do.Blueprints.Service/Do.Blueprints.Service.csproj b/src/blueprints/Do.Blueprints.Service/Do.Blueprints.Service.csproj index e87b8acee..40e713160 100644 --- a/src/blueprints/Do.Blueprints.Service/Do.Blueprints.Service.csproj +++ b/src/blueprints/Do.Blueprints.Service/Do.Blueprints.Service.csproj @@ -13,6 +13,7 @@ + diff --git a/test/blueprints/Do.Test.Blueprints.Service.Test/Caching/ScopedMemory.cs b/test/blueprints/Do.Test.Blueprints.Service.Test/Caching/ScopedMemory.cs new file mode 100644 index 000000000..9497a0591 --- /dev/null +++ b/test/blueprints/Do.Test.Blueprints.Service.Test/Caching/ScopedMemory.cs @@ -0,0 +1,33 @@ +using Microsoft.Extensions.Caching.Memory; + +namespace Do.Test.Caching; + +public class ScopedMemory : TestServiceSpec +{ + IMemoryCache _cache = default!; + + public override void SetUp() + { + base.SetUp(); + + _cache = GiveMe.AMemoryCache(); + } + + public override void TearDown() + { + base.TearDown(); + + _cache.VerifyCount(c => c is 0); + } + + [Test] + public void Objects_can_be_cached_in_memory() + { + var expected = _cache.GetOrCreate("key", _ => new()); + + var actual = _cache.GetOrCreate("key", _ => new()); + + actual.ShouldBeSameAs(expected); + _cache.VerifyCount(c => c is 1); + } +} From 0b9d4d1b5987c12782f1690b611816e3e0384e6a Mon Sep 17 00:00:00 2001 From: dncsvr Date: Mon, 11 Dec 2023 14:05:46 +0300 Subject: [PATCH 04/16] edit `ByRequest` --- .../RestApi/Analyzer/Entity.generated.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/blueprints/Do.Test.Blueprints.Service.Application/RestApi/Analyzer/Entity.generated.cs b/test/blueprints/Do.Test.Blueprints.Service.Application/RestApi/Analyzer/Entity.generated.cs index e1c9108a0..b26a4360e 100644 --- a/test/blueprints/Do.Test.Blueprints.Service.Application/RestApi/Analyzer/Entity.generated.cs +++ b/test/blueprints/Do.Test.Blueprints.Service.Application/RestApi/Analyzer/Entity.generated.cs @@ -14,13 +14,13 @@ public EntityController(IServiceProvider serviceProvider) => _serviceProvider = serviceProvider; public record ByRequest( - Guid guid = default, + Guid? guid = default, string @string = default, string stringData = default, - int int32 = default, + int? int32 = default, Uri uri = default, - Status status = default, - DateTime dateTime = default + Status? status = default, + DateTime? dateTime = default ); [HttpGet] From 6ca590b420aba7bcdd7dbbd6ef2bae1ac24813d0 Mon Sep 17 00:00:00 2001 From: dncsvr Date: Mon, 11 Dec 2023 14:29:41 +0300 Subject: [PATCH 05/16] fix feature implementation name --- .../ScopedMemory/ScopedMemoryCachingExtensions.cs | 9 +++++++++ ...pedMemoryFeature.cs => ScopedMemoryCachingFeature.cs} | 2 +- .../Caching/ScopedMemory/ScopedMemoryExtensions.cs | 9 --------- 3 files changed, 10 insertions(+), 10 deletions(-) create mode 100644 src/blueprints/Do.Blueprints.Service.Application/Caching/ScopedMemory/ScopedMemoryCachingExtensions.cs rename src/blueprints/Do.Blueprints.Service.Application/Caching/ScopedMemory/{ScopedMemoryFeature.cs => ScopedMemoryCachingFeature.cs} (82%) delete mode 100644 src/blueprints/Do.Blueprints.Service.Application/Caching/ScopedMemory/ScopedMemoryExtensions.cs diff --git a/src/blueprints/Do.Blueprints.Service.Application/Caching/ScopedMemory/ScopedMemoryCachingExtensions.cs b/src/blueprints/Do.Blueprints.Service.Application/Caching/ScopedMemory/ScopedMemoryCachingExtensions.cs new file mode 100644 index 000000000..23f5c0183 --- /dev/null +++ b/src/blueprints/Do.Blueprints.Service.Application/Caching/ScopedMemory/ScopedMemoryCachingExtensions.cs @@ -0,0 +1,9 @@ +using Do.Caching; +using Do.Caching.ScopedMemory; + +namespace Do; + +public static class ScopedMemoryCachingExtensions +{ + public static ScopedMemoryCachingFeature ScopedMemory(this CachingConfigurator _) => new(); +} diff --git a/src/blueprints/Do.Blueprints.Service.Application/Caching/ScopedMemory/ScopedMemoryFeature.cs b/src/blueprints/Do.Blueprints.Service.Application/Caching/ScopedMemory/ScopedMemoryCachingFeature.cs similarity index 82% rename from src/blueprints/Do.Blueprints.Service.Application/Caching/ScopedMemory/ScopedMemoryFeature.cs rename to src/blueprints/Do.Blueprints.Service.Application/Caching/ScopedMemory/ScopedMemoryCachingFeature.cs index 1bf217bf0..17aa7ee21 100644 --- a/src/blueprints/Do.Blueprints.Service.Application/Caching/ScopedMemory/ScopedMemoryFeature.cs +++ b/src/blueprints/Do.Blueprints.Service.Application/Caching/ScopedMemory/ScopedMemoryCachingFeature.cs @@ -3,7 +3,7 @@ namespace Do.Caching.ScopedMemory; -public class ScopedMemoryFeature : IFeature +public class ScopedMemoryCachingFeature : IFeature { public void Configure(LayerConfigurator configurator) { diff --git a/src/blueprints/Do.Blueprints.Service.Application/Caching/ScopedMemory/ScopedMemoryExtensions.cs b/src/blueprints/Do.Blueprints.Service.Application/Caching/ScopedMemory/ScopedMemoryExtensions.cs deleted file mode 100644 index 22b85252a..000000000 --- a/src/blueprints/Do.Blueprints.Service.Application/Caching/ScopedMemory/ScopedMemoryExtensions.cs +++ /dev/null @@ -1,9 +0,0 @@ -using Do.Caching; -using Do.Caching.ScopedMemory; - -namespace Do; - -public static class ScopedMemoryExtensions -{ - public static ScopedMemoryFeature ScopedMemory(this CachingConfigurator _) => new(); -} From c452f159d2e6dbb6f15dfea14c22e104b1102217 Mon Sep 17 00:00:00 2001 From: dncsvr Date: Mon, 11 Dec 2023 14:50:40 +0300 Subject: [PATCH 06/16] add caching feature documentation --- docs/blueprints/service.md | 23 ++++++++++++----------- docs/features/caching.md | 19 +++++++++++++++++++ 2 files changed, 31 insertions(+), 11 deletions(-) create mode 100644 docs/features/caching.md diff --git a/docs/blueprints/service.md b/docs/blueprints/service.md index 47c8c2e43..77de5a1a5 100644 --- a/docs/blueprints/service.md +++ b/docs/blueprints/service.md @@ -17,14 +17,15 @@ Layers in this blueprint are; Features with default options are; -| Features | Run | Test | Required | -| ------------------ | ----------- | --------------- | -------- | -| Business | | | Yes | -| Core | Dotnet | Mock | | -| Database | Sqlite | InMemory | Yes | -| Documentation | Default | | | -| Exception Handling | Default | | | -| Greeting | Hello World | | | -| Logging | Request | | | -| Mocking Overrider | | First Interface | | -| Orm | Default | Default | | +| Features | Run | Test | Required | +| ------------------ | ----------- | --------------- | -------- | +| Business | | | Yes | +| Caching | Scoped Memory | Scoped Memory | | +| Core | Dotnet | Mock | | +| Database | Sqlite | InMemory | Yes | +| Documentation | Default | | | +| Exception Handling | Default | | | +| Greeting | Hello World | | | +| Logging | Request | | | +| Mocking Overrider | | First Interface | | +| Orm | Default | Default | | diff --git a/docs/features/caching.md b/docs/features/caching.md new file mode 100644 index 000000000..5f53699da --- /dev/null +++ b/docs/features/caching.md @@ -0,0 +1,19 @@ +# Caching + +Implementations of this feature provides predefined caching behaviour. + +Add this feature using `AddCaching()` extension; + +```csharp +app.Features.AddCaching(...); +``` + +## Scoped Memory + +This feature implementation registers `Func` factory with scoped +`MemoryCache` implementation to provide request in-memory caching. + +```csharp +c => c.ScopedMemory() +``` + From 5c9e4a944f46a315e5b8e634a911f72f8efb66f4 Mon Sep 17 00:00:00 2001 From: dncsvr Date: Mon, 11 Dec 2023 15:34:43 +0300 Subject: [PATCH 07/16] add custom dialect for increasing `varchar` capacity --- .../Database/MySql/DoMySQLDialect.cs | 12 ++++++++++++ .../Database/MySql/MySqlDatabaseFeature.cs | 2 +- .../ConfigurationOverriderFeature.cs | 2 +- 3 files changed, 14 insertions(+), 2 deletions(-) create mode 100644 src/blueprints/Do.Blueprints.Service.Application/Database/MySql/DoMySQLDialect.cs diff --git a/src/blueprints/Do.Blueprints.Service.Application/Database/MySql/DoMySQLDialect.cs b/src/blueprints/Do.Blueprints.Service.Application/Database/MySql/DoMySQLDialect.cs new file mode 100644 index 000000000..34f5f452f --- /dev/null +++ b/src/blueprints/Do.Blueprints.Service.Application/Database/MySql/DoMySQLDialect.cs @@ -0,0 +1,12 @@ +using NHibernate.Dialect; +using System.Data; + +namespace Do.Database.MySql; + +public class DoMySQLDialect : MySQL57Dialect +{ + public DoMySQLDialect() + { + RegisterColumnType(DbType.String, 65535, "VARCHAR($l)"); + } +} diff --git a/src/blueprints/Do.Blueprints.Service.Application/Database/MySql/MySqlDatabaseFeature.cs b/src/blueprints/Do.Blueprints.Service.Application/Database/MySql/MySqlDatabaseFeature.cs index a06ca4502..88f281810 100644 --- a/src/blueprints/Do.Blueprints.Service.Application/Database/MySql/MySqlDatabaseFeature.cs +++ b/src/blueprints/Do.Blueprints.Service.Application/Database/MySql/MySqlDatabaseFeature.cs @@ -26,7 +26,7 @@ public void Configure(LayerConfigurator configurator) { var mysql = MySQLConfiguration.Standard .ConnectionString(_connectionString) - .Dialect(); + .Dialect(); // this should be in logging if (_showSql) { mysql.ShowSql(); } diff --git a/test/blueprints/Do.Test.Blueprints.Service.Application/ConfigurationOverrider/ConfigurationOverriderFeature.cs b/test/blueprints/Do.Test.Blueprints.Service.Application/ConfigurationOverrider/ConfigurationOverriderFeature.cs index bba2b40e1..3fd54806a 100644 --- a/test/blueprints/Do.Test.Blueprints.Service.Application/ConfigurationOverrider/ConfigurationOverriderFeature.cs +++ b/test/blueprints/Do.Test.Blueprints.Service.Application/ConfigurationOverrider/ConfigurationOverriderFeature.cs @@ -8,7 +8,7 @@ public void Configure(LayerConfigurator configurator) { configurator.ConfigureAutoPersistenceModel(model => { - model.Override(x => x.Map(e => e.String).Length(200)); + model.Override(x => x.Map(e => e.String).Length(500)); }); } } From 443b68bab0149bc56a6495f660154ea9ee57bd39 Mon Sep 17 00:00:00 2001 From: dncsvr Date: Mon, 11 Dec 2023 15:54:12 +0300 Subject: [PATCH 08/16] add `AnInteger` helper to stubber - minor edits to business extensions --- .../Business/BusinessExtensions.cs | 8 ++++---- .../ServiceSpecExtensions.cs | 6 ++++++ 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/blueprints/Do.Blueprints.Service.Application/Business/BusinessExtensions.cs b/src/blueprints/Do.Blueprints.Service.Application/Business/BusinessExtensions.cs index 5685a8ad2..66e49a5a0 100644 --- a/src/blueprints/Do.Blueprints.Service.Application/Business/BusinessExtensions.cs +++ b/src/blueprints/Do.Blueprints.Service.Application/Business/BusinessExtensions.cs @@ -15,8 +15,8 @@ public static void AddTransientWithFactory(this IServ where TService : class where TImplementation : class, TService { - source.AddSingleton>(sp => () => sp.GetRequiredServiceUsingRequestServices()); - source.AddTransient(); + source.AddSingleton>(sp => () => sp.GetRequiredServiceUsingRequestServices()); + source.AddTransient(); } public static void AddScopedWithFactory(this IServiceCollection source) where TService : class => @@ -26,7 +26,7 @@ public static void AddScopedWithFactory(this IService where TService : class where TImplementation : class, TService { - source.AddSingleton>(sp => () => sp.GetRequiredServiceUsingRequestServices()); - source.AddScoped(); + source.AddSingleton>(sp => () => sp.GetRequiredServiceUsingRequestServices()); + source.AddScoped(); } } diff --git a/src/blueprints/Do.Blueprints.Service.Application/ServiceSpecExtensions.cs b/src/blueprints/Do.Blueprints.Service.Application/ServiceSpecExtensions.cs index e3ef3218f..8b5091870 100644 --- a/src/blueprints/Do.Blueprints.Service.Application/ServiceSpecExtensions.cs +++ b/src/blueprints/Do.Blueprints.Service.Application/ServiceSpecExtensions.cs @@ -53,6 +53,12 @@ public static Guid AGuid(this Stubber _, #endregion + #region Integer + + public static int AnInteger(this Stubber _) => 42; + + #endregion + #region MemoryCache public static IMemoryCache AMemoryCache(this Stubber giveMe, From 5b1ec7e5b779029bc03b4290034f85c1be534913 Mon Sep 17 00:00:00 2001 From: dncsvr Date: Mon, 11 Dec 2023 15:56:38 +0300 Subject: [PATCH 09/16] review changes - remove memory cache field and setup --- .../Caching/ScopedMemory.cs | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/test/blueprints/Do.Test.Blueprints.Service.Test/Caching/ScopedMemory.cs b/test/blueprints/Do.Test.Blueprints.Service.Test/Caching/ScopedMemory.cs index 9497a0591..0c0d8a3ea 100644 --- a/test/blueprints/Do.Test.Blueprints.Service.Test/Caching/ScopedMemory.cs +++ b/test/blueprints/Do.Test.Blueprints.Service.Test/Caching/ScopedMemory.cs @@ -4,30 +4,23 @@ namespace Do.Test.Caching; public class ScopedMemory : TestServiceSpec { - IMemoryCache _cache = default!; - - public override void SetUp() - { - base.SetUp(); - - _cache = GiveMe.AMemoryCache(); - } - public override void TearDown() { base.TearDown(); - _cache.VerifyCount(c => c is 0); + var cache = GiveMe.AMemoryCache(); + cache.VerifyCount(c => c is 0); } [Test] public void Objects_can_be_cached_in_memory() { - var expected = _cache.GetOrCreate("key", _ => new()); + var cache = GiveMe.AMemoryCache(); + var expected = GiveMe.AMemoryCache().GetOrCreate("key", _ => new()); - var actual = _cache.GetOrCreate("key", _ => new()); + var actual = cache.GetOrCreate("key", _ => new()); actual.ShouldBeSameAs(expected); - _cache.VerifyCount(c => c is 1); + cache.VerifyCount(c => c is 1); } } From f23279b530e1e7c9d9467e9f07ab0d2a41acdca6 Mon Sep 17 00:00:00 2001 From: dncsvr Date: Mon, 11 Dec 2023 17:00:54 +0300 Subject: [PATCH 10/16] minor edit --- .../MySql/{DoMySQLDialect.cs => DefaultMySQLDialect.cs} | 4 ++-- .../Database/MySql/MySqlDatabaseFeature.cs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) rename src/blueprints/Do.Blueprints.Service.Application/Database/MySql/{DoMySQLDialect.cs => DefaultMySQLDialect.cs} (65%) diff --git a/src/blueprints/Do.Blueprints.Service.Application/Database/MySql/DoMySQLDialect.cs b/src/blueprints/Do.Blueprints.Service.Application/Database/MySql/DefaultMySQLDialect.cs similarity index 65% rename from src/blueprints/Do.Blueprints.Service.Application/Database/MySql/DoMySQLDialect.cs rename to src/blueprints/Do.Blueprints.Service.Application/Database/MySql/DefaultMySQLDialect.cs index 34f5f452f..329c08f85 100644 --- a/src/blueprints/Do.Blueprints.Service.Application/Database/MySql/DoMySQLDialect.cs +++ b/src/blueprints/Do.Blueprints.Service.Application/Database/MySql/DefaultMySQLDialect.cs @@ -3,9 +3,9 @@ namespace Do.Database.MySql; -public class DoMySQLDialect : MySQL57Dialect +public class DefaultMySQLDialect : MySQL57Dialect { - public DoMySQLDialect() + public DefaultMySQLDialect() { RegisterColumnType(DbType.String, 65535, "VARCHAR($l)"); } diff --git a/src/blueprints/Do.Blueprints.Service.Application/Database/MySql/MySqlDatabaseFeature.cs b/src/blueprints/Do.Blueprints.Service.Application/Database/MySql/MySqlDatabaseFeature.cs index 88f281810..92f80e98b 100644 --- a/src/blueprints/Do.Blueprints.Service.Application/Database/MySql/MySqlDatabaseFeature.cs +++ b/src/blueprints/Do.Blueprints.Service.Application/Database/MySql/MySqlDatabaseFeature.cs @@ -26,7 +26,7 @@ public void Configure(LayerConfigurator configurator) { var mysql = MySQLConfiguration.Standard .ConnectionString(_connectionString) - .Dialect(); + .Dialect(); // this should be in logging if (_showSql) { mysql.ShowSql(); } From aa8dbebf5f33fe677936bdbdb9b93b78545f2237 Mon Sep 17 00:00:00 2001 From: dncsvr Date: Mon, 11 Dec 2023 17:51:28 +0300 Subject: [PATCH 11/16] review changes - rename test fixture `ScopedMemory` => `UsingScopedMemory` - edit test setup - remove extra line from `Directory.Packages.props` - edit unreleased.md `MemoryCache` => `ScopedMemory` - rename `DefaultMySQLDialect` => `CustomMySQL57Dialect` - reduce varchar max size to 1023 --- Directory.Packages.props | 1 - .../Database/MySql/CustomMySQL57Dialect.cs | 12 ++++++++++++ .../Database/MySql/DefaultMySQLDialect.cs | 12 ------------ .../Database/MySql/MySqlDatabaseFeature.cs | 2 +- .../ServiceSpecExtensions.cs | 4 ++-- .../{ScopedMemory.cs => UsingScopedMemory.cs} | 9 ++++----- unreleased.md | 2 +- 7 files changed, 20 insertions(+), 22 deletions(-) create mode 100644 src/blueprints/Do.Blueprints.Service.Application/Database/MySql/CustomMySQL57Dialect.cs delete mode 100644 src/blueprints/Do.Blueprints.Service.Application/Database/MySql/DefaultMySQLDialect.cs rename test/blueprints/Do.Test.Blueprints.Service.Test/Caching/{ScopedMemory.cs => UsingScopedMemory.cs} (60%) diff --git a/Directory.Packages.props b/Directory.Packages.props index 27883a993..1f697159b 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -23,5 +23,4 @@ - \ No newline at end of file diff --git a/src/blueprints/Do.Blueprints.Service.Application/Database/MySql/CustomMySQL57Dialect.cs b/src/blueprints/Do.Blueprints.Service.Application/Database/MySql/CustomMySQL57Dialect.cs new file mode 100644 index 000000000..db19bbf69 --- /dev/null +++ b/src/blueprints/Do.Blueprints.Service.Application/Database/MySql/CustomMySQL57Dialect.cs @@ -0,0 +1,12 @@ +using NHibernate.Dialect; +using System.Data; + +namespace Do.Database.MySql; + +public class CustomMySQL57Dialect : MySQL57Dialect +{ + public CustomMySQL57Dialect() + { + RegisterColumnType(DbType.String, 1023, "VARCHAR($l)"); + } +} diff --git a/src/blueprints/Do.Blueprints.Service.Application/Database/MySql/DefaultMySQLDialect.cs b/src/blueprints/Do.Blueprints.Service.Application/Database/MySql/DefaultMySQLDialect.cs deleted file mode 100644 index 329c08f85..000000000 --- a/src/blueprints/Do.Blueprints.Service.Application/Database/MySql/DefaultMySQLDialect.cs +++ /dev/null @@ -1,12 +0,0 @@ -using NHibernate.Dialect; -using System.Data; - -namespace Do.Database.MySql; - -public class DefaultMySQLDialect : MySQL57Dialect -{ - public DefaultMySQLDialect() - { - RegisterColumnType(DbType.String, 65535, "VARCHAR($l)"); - } -} diff --git a/src/blueprints/Do.Blueprints.Service.Application/Database/MySql/MySqlDatabaseFeature.cs b/src/blueprints/Do.Blueprints.Service.Application/Database/MySql/MySqlDatabaseFeature.cs index 92f80e98b..17b44d0b2 100644 --- a/src/blueprints/Do.Blueprints.Service.Application/Database/MySql/MySqlDatabaseFeature.cs +++ b/src/blueprints/Do.Blueprints.Service.Application/Database/MySql/MySqlDatabaseFeature.cs @@ -26,7 +26,7 @@ public void Configure(LayerConfigurator configurator) { var mysql = MySQLConfiguration.Standard .ConnectionString(_connectionString) - .Dialect(); + .Dialect(); // this should be in logging if (_showSql) { mysql.ShowSql(); } diff --git a/src/blueprints/Do.Blueprints.Service.Application/ServiceSpecExtensions.cs b/src/blueprints/Do.Blueprints.Service.Application/ServiceSpecExtensions.cs index 8b5091870..23f3b888b 100644 --- a/src/blueprints/Do.Blueprints.Service.Application/ServiceSpecExtensions.cs +++ b/src/blueprints/Do.Blueprints.Service.Application/ServiceSpecExtensions.cs @@ -76,8 +76,8 @@ public static IMemoryCache AMemoryCache(this Stubber giveMe, return memoryCache; } - public static void VerifyCount(this IMemoryCache memoryCache, Func condition) => - condition((memoryCache as MemoryCache)?.Count ?? throw new("`MemoryCache` should have existed")).ShouldBe(true); + public static void ShouldHaveCount(this IMemoryCache memoryCache, int count) => + ((memoryCache as MemoryCache) ?? throw new("`MemoryCache` should have existed")).Count.ShouldBe(count); #endregion diff --git a/test/blueprints/Do.Test.Blueprints.Service.Test/Caching/ScopedMemory.cs b/test/blueprints/Do.Test.Blueprints.Service.Test/Caching/UsingScopedMemory.cs similarity index 60% rename from test/blueprints/Do.Test.Blueprints.Service.Test/Caching/ScopedMemory.cs rename to test/blueprints/Do.Test.Blueprints.Service.Test/Caching/UsingScopedMemory.cs index 0c0d8a3ea..399f36cd2 100644 --- a/test/blueprints/Do.Test.Blueprints.Service.Test/Caching/ScopedMemory.cs +++ b/test/blueprints/Do.Test.Blueprints.Service.Test/Caching/UsingScopedMemory.cs @@ -2,25 +2,24 @@ namespace Do.Test.Caching; -public class ScopedMemory : TestServiceSpec +public class UsingScopedMemory : TestServiceSpec { public override void TearDown() { base.TearDown(); - var cache = GiveMe.AMemoryCache(); - cache.VerifyCount(c => c is 0); + GiveMe.AMemoryCache().ShouldHaveCount(0); } [Test] public void Objects_can_be_cached_in_memory() { var cache = GiveMe.AMemoryCache(); - var expected = GiveMe.AMemoryCache().GetOrCreate("key", _ => new()); + var expected = cache.GetOrCreate("key", _ => new()); var actual = cache.GetOrCreate("key", _ => new()); actual.ShouldBeSameAs(expected); - cache.VerifyCount(c => c is 1); + cache.ShouldHaveCount(1); } } diff --git a/unreleased.md b/unreleased.md index 5bc72c7a9..0c209c685 100644 --- a/unreleased.md +++ b/unreleased.md @@ -3,4 +3,4 @@ ## Features - Beta features are available in do-blueprints-service package; - - `Caching` feature is now added with `MemoryCache` implementation + - `Caching` feature is now added with `ScopedMemory` implementation From ab87a1ce695d4bfc76c661455d03742f570a06e4 Mon Sep 17 00:00:00 2001 From: dncsvr Date: Mon, 11 Dec 2023 17:54:03 +0300 Subject: [PATCH 12/16] minor edits --- Directory.Packages.props | 1 + .../Caching/UsingScopedMemory.cs | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/Directory.Packages.props b/Directory.Packages.props index 1f697159b..a3a769cd4 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -23,4 +23,5 @@ + \ No newline at end of file diff --git a/test/blueprints/Do.Test.Blueprints.Service.Test/Caching/UsingScopedMemory.cs b/test/blueprints/Do.Test.Blueprints.Service.Test/Caching/UsingScopedMemory.cs index 399f36cd2..b5a3d1006 100644 --- a/test/blueprints/Do.Test.Blueprints.Service.Test/Caching/UsingScopedMemory.cs +++ b/test/blueprints/Do.Test.Blueprints.Service.Test/Caching/UsingScopedMemory.cs @@ -12,7 +12,7 @@ public override void TearDown() } [Test] - public void Objects_can_be_cached_in_memory() + public void Objects_can_be_cached_in__scoped_memory() { var cache = GiveMe.AMemoryCache(); var expected = cache.GetOrCreate("key", _ => new()); From c811bbc96aa4f15cd0c7ab44dbc3d4664ed1f4c0 Mon Sep 17 00:00:00 2001 From: dncsvr Date: Mon, 11 Dec 2023 17:54:29 +0300 Subject: [PATCH 13/16] remove extra spaces --- Directory.Packages.props | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Directory.Packages.props b/Directory.Packages.props index a3a769cd4..1e829561c 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -23,5 +23,5 @@ - + \ No newline at end of file From 94a6c563a011d23c8ec3cb26b795a06322241212 Mon Sep 17 00:00:00 2001 From: dncsvr Date: Mon, 11 Dec 2023 17:56:45 +0300 Subject: [PATCH 14/16] minor edit --- Directory.Packages.props | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Directory.Packages.props b/Directory.Packages.props index 1e829561c..06f775f56 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -24,4 +24,4 @@ - \ No newline at end of file + From 7f8ce8efb9e42335a43605d9670ba808455a9872 Mon Sep 17 00:00:00 2001 From: dncsvr Date: Mon, 11 Dec 2023 18:08:04 +0300 Subject: [PATCH 15/16] minor edit --- .../Do.Blueprints.Service.Application/ServiceSpecExtensions.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/blueprints/Do.Blueprints.Service.Application/ServiceSpecExtensions.cs b/src/blueprints/Do.Blueprints.Service.Application/ServiceSpecExtensions.cs index 23f3b888b..1a42d99c9 100644 --- a/src/blueprints/Do.Blueprints.Service.Application/ServiceSpecExtensions.cs +++ b/src/blueprints/Do.Blueprints.Service.Application/ServiceSpecExtensions.cs @@ -77,7 +77,7 @@ public static IMemoryCache AMemoryCache(this Stubber giveMe, } public static void ShouldHaveCount(this IMemoryCache memoryCache, int count) => - ((memoryCache as MemoryCache) ?? throw new("`MemoryCache` should have existed")).Count.ShouldBe(count); + ((MemoryCache)memoryCache).Count.ShouldBe(count); #endregion From db869f6d8ce166aae0a8f39b2a8b105ce836bd54 Mon Sep 17 00:00:00 2001 From: dncsvr Date: Mon, 11 Dec 2023 18:16:57 +0300 Subject: [PATCH 16/16] add improvements information to unreleased.md --- unreleased.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/unreleased.md b/unreleased.md index 0c209c685..14a2d1298 100644 --- a/unreleased.md +++ b/unreleased.md @@ -4,3 +4,15 @@ - Beta features are available in do-blueprints-service package; - `Caching` feature is now added with `ScopedMemory` implementation + +## Improvements + +- Added `CustomMySQL57Dialect` for enabling `varchar` column type with _1023_ + max capacity +- Added `TransientWithFactory` and + `ScopedWithFactory` extensions for registering + services with implementations +- Added new ServiceSpec extensions: + - `AnInteger` extension for `Stubber` + - `AMemoryCache` extension for `Stubber` + - `ShouldHaveCount` extension for `IMemoryCache` \ No newline at end of file