From 29853e5fe7779d4bc7b82fa3a06710ffd01525d8 Mon Sep 17 00:00:00 2001 From: Ali Yousefi Date: Tue, 13 Feb 2024 21:51:27 +0330 Subject: [PATCH 1/4] Add support for EasyMicroservices.Cores.Infrastructure --- ...yMicroservices.Cores.Infrastructure.csproj | 21 +++++++++ .../Interfaces/IWidget.cs | 14 ++++++ .../Interfaces/IWidgetManager.cs | 13 ++++++ .../Widgets/WidgetManager.cs | 44 +++++++++++++++++++ 4 files changed, 92 insertions(+) create mode 100644 src/CSharp/EasyMicroservices.Cores.Infrastructure/EasyMicroservices.Cores.Infrastructure.csproj create mode 100644 src/CSharp/EasyMicroservices.Cores.Infrastructure/Interfaces/IWidget.cs create mode 100644 src/CSharp/EasyMicroservices.Cores.Infrastructure/Interfaces/IWidgetManager.cs create mode 100644 src/CSharp/EasyMicroservices.Cores.Infrastructure/Widgets/WidgetManager.cs diff --git a/src/CSharp/EasyMicroservices.Cores.Infrastructure/EasyMicroservices.Cores.Infrastructure.csproj b/src/CSharp/EasyMicroservices.Cores.Infrastructure/EasyMicroservices.Cores.Infrastructure.csproj new file mode 100644 index 0000000..41c0b92 --- /dev/null +++ b/src/CSharp/EasyMicroservices.Cores.Infrastructure/EasyMicroservices.Cores.Infrastructure.csproj @@ -0,0 +1,21 @@ + + + + netstandard2.0;netstandard2.1;net45;net6.0;net8.0 + AnyCPU;x64;x86 + EasyMicroservices + 0.0.1.50 + core of infrastructure. + EasyMicroservices@gmail.com + core,cores,base,infrastructure + https://github.com/EasyMicroservices/Cores + latest + true + EasyMicroservices.Cores + + + + + + + diff --git a/src/CSharp/EasyMicroservices.Cores.Infrastructure/Interfaces/IWidget.cs b/src/CSharp/EasyMicroservices.Cores.Infrastructure/Interfaces/IWidget.cs new file mode 100644 index 0000000..4e83921 --- /dev/null +++ b/src/CSharp/EasyMicroservices.Cores.Infrastructure/Interfaces/IWidget.cs @@ -0,0 +1,14 @@ +using System; +using System.Threading.Tasks; + +namespace EasyMicroservices.Cores.Interfaces; +public interface IWidget +{ + void Build(); + Type GetObjectType(); +} + +public interface IWidget: IWidget +{ + Task Initialize(params T[] parameters); +} \ No newline at end of file diff --git a/src/CSharp/EasyMicroservices.Cores.Infrastructure/Interfaces/IWidgetManager.cs b/src/CSharp/EasyMicroservices.Cores.Infrastructure/Interfaces/IWidgetManager.cs new file mode 100644 index 0000000..fab2ef2 --- /dev/null +++ b/src/CSharp/EasyMicroservices.Cores.Infrastructure/Interfaces/IWidgetManager.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; + +namespace EasyMicroservices.Cores.Interfaces; + +public interface IWidgetManager +{ + void Register(IWidget widget); + void UnRegister(IWidget widget); + IEnumerable GetWidgetsByType(Type type); + IEnumerable GetWidgets() + where T : IWidget; +} diff --git a/src/CSharp/EasyMicroservices.Cores.Infrastructure/Widgets/WidgetManager.cs b/src/CSharp/EasyMicroservices.Cores.Infrastructure/Widgets/WidgetManager.cs new file mode 100644 index 0000000..52c3c9c --- /dev/null +++ b/src/CSharp/EasyMicroservices.Cores.Infrastructure/Widgets/WidgetManager.cs @@ -0,0 +1,44 @@ +using EasyMicroservices.Cores.Interfaces; +using System; +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Linq; + +namespace EasyMicroservices.Cores.Widgets; +public class WidgetManager : IWidgetManager +{ + readonly ConcurrentDictionary> Widgets = new ConcurrentDictionary>(); + + public void Register(IWidget widget) + { + var type = widget.GetObjectType(); + if (Widgets.TryGetValue(type, out List widgets)) + widgets.Add(widget); + else + { + Widgets.TryAdd(widget.GetObjectType(), new List() + { + widget + }); + } + } + + public void UnRegister(IWidget widget) + { + Widgets.TryRemove(widget.GetObjectType(), out _); + } + + public IEnumerable GetWidgetsByType(Type type) + { + if (Widgets.TryGetValue(type, out List widgets)) + return widgets.ToList(); + return Enumerable.Empty(); + } + + public IEnumerable GetWidgets() where T : IWidget + { + if (Widgets.TryGetValue(typeof(T), out List widgets)) + return widgets.Cast().ToList(); + return Enumerable.Empty(); + } +} From 81e297ef0ee10f3c50fc1f606754ecb3dc2a34a5 Mon Sep 17 00:00:00 2001 From: Ali Yousefi Date: Tue, 13 Feb 2024 21:54:15 +0330 Subject: [PATCH 2/4] Add support for Widget, Reporting Widget only for add #157 --- .../Fixtures/BaseFixture.cs | 2 + .../StartUpExtensions.cs | 7 ++ .../UnitOfWork.cs | 21 ++++- .../Models/LogicOptions.cs | 4 + .../Database/Interfaces/IDatabaseWidget.cs | 22 ++++++ .../Interfaces/IDatabaseWidgetManager.cs | 19 +++++ .../Database/Interfaces/IWritableLogic.cs | 1 + .../Logics/DatabaseLogicInfrastructure.cs | 10 ++- .../Logics/DatabaseMappedLogicBase.cs | 3 +- .../Logics/IdSchemaDatabaseMappedLogicBase.cs | 3 +- .../Managers/DatabaseWidgetManager.cs | 31 ++++++++ .../Database/Widgets/DatabaseBuilderWidget.cs | 34 +++++++++ .../Widgets/ReportingBuilderWidget.cs | 22 ++++++ .../Widgets/SimpleReportingEntityWidget.cs | 76 +++++++++++++++++++ .../EasyMicroservices.Cores.Database.csproj | 1 + .../Interfaces/IBaseUnitOfWork.cs | 9 ++- .../EntityFrameworkCoreDatabaseBuilder.cs | 34 ++++++++- .../IEntityFrameworkCoreDatabaseBuilder.cs | 5 ++ .../RelationalCoreContext.cs | 9 ++- .../LongIdMappedDatabaseLogicBaseTest.cs | 5 ++ .../Fixtures/ServiceProviderFixture.cs | 2 + src/CSharp/EasyMicroservices.Cores.sln | 21 ++++- 22 files changed, 325 insertions(+), 16 deletions(-) create mode 100644 src/CSharp/EasyMicroservices.Cores.Database/Database/Interfaces/IDatabaseWidget.cs create mode 100644 src/CSharp/EasyMicroservices.Cores.Database/Database/Interfaces/IDatabaseWidgetManager.cs create mode 100644 src/CSharp/EasyMicroservices.Cores.Database/Database/Managers/DatabaseWidgetManager.cs create mode 100644 src/CSharp/EasyMicroservices.Cores.Database/Database/Widgets/DatabaseBuilderWidget.cs create mode 100644 src/CSharp/EasyMicroservices.Cores.Database/Database/Widgets/ReportingBuilderWidget.cs create mode 100644 src/CSharp/EasyMicroservices.Cores.Database/Database/Widgets/SimpleReportingEntityWidget.cs diff --git a/src/CSharp/EasyMicroservices.Cores.AspCore.Tests/Fixtures/BaseFixture.cs b/src/CSharp/EasyMicroservices.Cores.AspCore.Tests/Fixtures/BaseFixture.cs index 6588b9e..ad1dd20 100644 --- a/src/CSharp/EasyMicroservices.Cores.AspCore.Tests/Fixtures/BaseFixture.cs +++ b/src/CSharp/EasyMicroservices.Cores.AspCore.Tests/Fixtures/BaseFixture.cs @@ -2,6 +2,7 @@ using EasyMicroservices.Cores.AspCoreApi.Managers; using EasyMicroservices.Cores.AspEntityFrameworkCoreApi; using EasyMicroservices.Cores.Database.Interfaces; +using EasyMicroservices.Cores.Database.Logics; using EasyMicroservices.Cores.Database.Managers; using EasyMicroservices.Cores.Models; using EasyMicroservices.Cores.Relational.EntityFrameworkCore.Intrerfaces; @@ -20,6 +21,7 @@ public static async Task Init(int port, int? whiteLabelPort, A app.Services.AddTransient(serviceProvider => new MyTestContext(serviceProvider.GetService())); app.Services.AddTransient(); app.Services.AddSingleton(service => new WhiteLabelManager(service)); + app.Services.AddSingleton(); app.Services.AddSingleton((provider) => { return new DefaultUniqueIdentityManager(provider.GetService().CurrentWhiteLabel); diff --git a/src/CSharp/EasyMicroservices.Cores.AspEntityFrameworkCoreApi/StartUpExtensions.cs b/src/CSharp/EasyMicroservices.Cores.AspEntityFrameworkCoreApi/StartUpExtensions.cs index 37f8bad..9f07ab9 100644 --- a/src/CSharp/EasyMicroservices.Cores.AspEntityFrameworkCoreApi/StartUpExtensions.cs +++ b/src/CSharp/EasyMicroservices.Cores.AspEntityFrameworkCoreApi/StartUpExtensions.cs @@ -9,10 +9,12 @@ using EasyMicroservices.Cores.Database.Interfaces; using EasyMicroservices.Cores.Database.Logics; using EasyMicroservices.Cores.Database.Managers; +using EasyMicroservices.Cores.Database.Widgets; using EasyMicroservices.Cores.Interfaces; using EasyMicroservices.Cores.Models; using EasyMicroservices.Cores.Relational.EntityFrameworkCore; using EasyMicroservices.Cores.Relational.EntityFrameworkCore.Builders; +using EasyMicroservices.Cores.Widgets; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Cors.Infrastructure; @@ -77,6 +79,8 @@ public static IServiceCollection Builder(this IServiceCollection servi services.AddHttpContextAccessor(); services.AddScoped(service => new UnitOfWork(service)); services.AddScoped(); + services.AddSingleton(); + services.AddSingleton(); services.AddSingleton(service => new WhiteLabelManager(service)); services.AddSingleton((provider) => { @@ -369,6 +373,9 @@ public static async Task Use(this WebApplication webAp using (var scope = webApplication.Services.CreateAsyncScope()) { + var init = scope.ServiceProvider.GetService(); + if (init != null) + init.Build(); var dbbuilder = new DatabaseCreator(); using var context = scope.ServiceProvider.GetRequiredService(); dbbuilder.Initialize(context); diff --git a/src/CSharp/EasyMicroservices.Cores.AspEntityFrameworkCoreApi/UnitOfWork.cs b/src/CSharp/EasyMicroservices.Cores.AspEntityFrameworkCoreApi/UnitOfWork.cs index c52e97d..ee460f8 100644 --- a/src/CSharp/EasyMicroservices.Cores.AspEntityFrameworkCoreApi/UnitOfWork.cs +++ b/src/CSharp/EasyMicroservices.Cores.AspEntityFrameworkCoreApi/UnitOfWork.cs @@ -109,13 +109,21 @@ public virtual IDatabase GetDatabase() } #region ContractLogic + LogicOptions? _defaultLogicOptions; /// /// /// - public virtual void SetDefaultLogicOptions(LogicOptions logicOptions) + public LogicOptions? LogicOptions { - _defaultLogicOptions = logicOptions; + get + { + return _defaultLogicOptions; + } + set + { + _defaultLogicOptions = value; + } } LogicOptions GetLogicOptions(LogicOptions logicOptions) @@ -596,5 +604,14 @@ public virtual ServiceAddressInfo GetServiceAddress(string name) ?.Where(x => x.Name.HasValue()) .FirstOrDefault(x => x.Name.Equals(name, StringComparison.OrdinalIgnoreCase)); } + + /// + /// + /// + /// + public IDatabaseWidgetManager GetDatabaseWidgetManager() + { + return GetService(); + } } } diff --git a/src/CSharp/EasyMicroservices.Cores.Contracts/Models/LogicOptions.cs b/src/CSharp/EasyMicroservices.Cores.Contracts/Models/LogicOptions.cs index 84693d7..7030420 100644 --- a/src/CSharp/EasyMicroservices.Cores.Contracts/Models/LogicOptions.cs +++ b/src/CSharp/EasyMicroservices.Cores.Contracts/Models/LogicOptions.cs @@ -20,6 +20,10 @@ public LogicOptions() /// /// /// + public bool DoStopReporting { get; set; } + /// + /// + /// public UniqueIdentityStrategy UniqueIdentityStrategy { get diff --git a/src/CSharp/EasyMicroservices.Cores.Database/Database/Interfaces/IDatabaseWidget.cs b/src/CSharp/EasyMicroservices.Cores.Database/Database/Interfaces/IDatabaseWidget.cs new file mode 100644 index 0000000..bf9ede7 --- /dev/null +++ b/src/CSharp/EasyMicroservices.Cores.Database/Database/Interfaces/IDatabaseWidget.cs @@ -0,0 +1,22 @@ +using EasyMicroservices.Cores.Interfaces; +using System.Threading.Tasks; + +namespace EasyMicroservices.Cores.Database.Interfaces; +/// +/// +/// +public interface IDatabaseWidget : IWidget +{ + /// + /// + /// + bool CanProcess(IBaseUnitOfWork baseUnitOfWork); + /// + /// + /// + /// + /// + /// + /// + Task Process(IDatabaseWidgetManager databaseWidgetManager, IBaseUnitOfWork baseUnitOfWork, T contract); +} diff --git a/src/CSharp/EasyMicroservices.Cores.Database/Database/Interfaces/IDatabaseWidgetManager.cs b/src/CSharp/EasyMicroservices.Cores.Database/Database/Interfaces/IDatabaseWidgetManager.cs new file mode 100644 index 0000000..f411417 --- /dev/null +++ b/src/CSharp/EasyMicroservices.Cores.Database/Database/Interfaces/IDatabaseWidgetManager.cs @@ -0,0 +1,19 @@ +using EasyMicroservices.Cores.Interfaces; +using EasyMicroservices.Database.Interfaces; +using System.Threading.Tasks; + +namespace EasyMicroservices.Cores.Database.Interfaces; +/// +/// +/// +public interface IDatabaseWidgetManager : IWidgetManager +{ + /// + /// + /// + /// + /// + /// + /// + Task Add(IBaseUnitOfWork baseUnitOfWork, T contract); +} diff --git a/src/CSharp/EasyMicroservices.Cores.Database/Database/Interfaces/IWritableLogic.cs b/src/CSharp/EasyMicroservices.Cores.Database/Database/Interfaces/IWritableLogic.cs index e9dde22..262d549 100644 --- a/src/CSharp/EasyMicroservices.Cores.Database/Database/Interfaces/IWritableLogic.cs +++ b/src/CSharp/EasyMicroservices.Cores.Database/Database/Interfaces/IWritableLogic.cs @@ -101,6 +101,7 @@ public interface IContractWritableLogic /// /// + [Obsolete] Task> AddEntity(TEntity entity, CancellationToken cancellationToken = default); /// diff --git a/src/CSharp/EasyMicroservices.Cores.Database/Database/Logics/DatabaseLogicInfrastructure.cs b/src/CSharp/EasyMicroservices.Cores.Database/Database/Logics/DatabaseLogicInfrastructure.cs index c51176d..1690aa9 100644 --- a/src/CSharp/EasyMicroservices.Cores.Database/Database/Logics/DatabaseLogicInfrastructure.cs +++ b/src/CSharp/EasyMicroservices.Cores.Database/Database/Logics/DatabaseLogicInfrastructure.cs @@ -598,7 +598,7 @@ private async Task> InternalUpdateBulk(IEa { if (uniqueIdentityManager.UpdateUniqueIdentity(currentUserUniqueIdentity, easyWritableQueryable.Context, entityEntry.Entity)) { - FixUniqueIdentity(easyWritableQueryable.Context,typeof(TEntity), [entityEntry]); + FixUniqueIdentity(easyWritableQueryable.Context, typeof(TEntity), [entityEntry]); } } } @@ -938,11 +938,13 @@ void FixUniqueIdentity(IContext context, Type entityType, IEntityEntry[] entityE /// /// /// + /// /// /// + /// /// /// - public async Task> Add(IEasyWritableQueryableAsync easyWritableQueryable, TEntity entity, CancellationToken cancellationToken = default) + internal async Task> Add(IEasyWritableQueryableAsync easyWritableQueryable, TEntity entity, TContract contract, CancellationToken cancellationToken = default) where TEntity : class { var result = await easyWritableQueryable.AddAsync(entity, cancellationToken); @@ -971,6 +973,8 @@ await InternalUpdate(easyWritableQueryable, result.Entity, false, true, true, tr await easyWritableQueryable.SaveChangesAsync(); } } + var widgetManager = _baseUnitOfWork.GetDatabaseWidgetManager(); + await widgetManager.Add(_baseUnitOfWork, contract); await ActivityChangeLogLogic.AddAsync(result.Entity, _baseUnitOfWork); return result.Entity; } @@ -1038,7 +1042,7 @@ public async Task> Add(IEasyWritabl where TEntity : class { var entity = await MapAsync(contract); - var result = await Add(easyWritableQueryable, entity, cancellationToken); + var result = await Add(easyWritableQueryable, entity, contract, cancellationToken); return result; } diff --git a/src/CSharp/EasyMicroservices.Cores.Database/Database/Logics/DatabaseMappedLogicBase.cs b/src/CSharp/EasyMicroservices.Cores.Database/Database/Logics/DatabaseMappedLogicBase.cs index 26d5ac2..1f7e7a2 100644 --- a/src/CSharp/EasyMicroservices.Cores.Database/Database/Logics/DatabaseMappedLogicBase.cs +++ b/src/CSharp/EasyMicroservices.Cores.Database/Database/Logics/DatabaseMappedLogicBase.cs @@ -149,9 +149,10 @@ public async Task> AddBulk(CreateBulkRequ /// /// /// + [Obsolete] public Task> AddEntity(TEntity entity, CancellationToken cancellationToken = default) { - return Add(_easyWriteableQueryable, entity, cancellationToken); + return Add(_easyWriteableQueryable, entity, entity, cancellationToken); } /// diff --git a/src/CSharp/EasyMicroservices.Cores.Database/Database/Logics/IdSchemaDatabaseMappedLogicBase.cs b/src/CSharp/EasyMicroservices.Cores.Database/Database/Logics/IdSchemaDatabaseMappedLogicBase.cs index 772c0b6..712828b 100644 --- a/src/CSharp/EasyMicroservices.Cores.Database/Database/Logics/IdSchemaDatabaseMappedLogicBase.cs +++ b/src/CSharp/EasyMicroservices.Cores.Database/Database/Logics/IdSchemaDatabaseMappedLogicBase.cs @@ -244,9 +244,10 @@ public async Task> AddBulk(CreateBulkRequestContract /// /// + [Obsolete] public Task> AddEntity(TEntity entity, CancellationToken cancellationToken = default) { - return Add(_easyWriteableQueryable, entity, cancellationToken); + return Add(_easyWriteableQueryable, entity, entity, cancellationToken); } /// diff --git a/src/CSharp/EasyMicroservices.Cores.Database/Database/Managers/DatabaseWidgetManager.cs b/src/CSharp/EasyMicroservices.Cores.Database/Database/Managers/DatabaseWidgetManager.cs new file mode 100644 index 0000000..4527305 --- /dev/null +++ b/src/CSharp/EasyMicroservices.Cores.Database/Database/Managers/DatabaseWidgetManager.cs @@ -0,0 +1,31 @@ +using EasyMicroservices.Cores.Database.Interfaces; +using EasyMicroservices.Cores.Interfaces; +using EasyMicroservices.Cores.Widgets; +using System.Threading.Tasks; + +namespace EasyMicroservices.Cores.Database.Managers; + +/// +/// +/// +public class DatabaseWidgetManager : WidgetManager, IDatabaseWidgetManager +{ + /// + /// + /// + /// + /// + /// + /// + public async Task Add(IBaseUnitOfWork baseUnitOfWork, T contract) + { + var widgets = GetWidgetsByType(typeof(T)); + foreach (var widget in widgets) + { + if (widget is IDatabaseWidget databaseWidget && databaseWidget.CanProcess(baseUnitOfWork)) + { + await databaseWidget.Process(this, baseUnitOfWork, contract); + } + } + } +} \ No newline at end of file diff --git a/src/CSharp/EasyMicroservices.Cores.Database/Database/Widgets/DatabaseBuilderWidget.cs b/src/CSharp/EasyMicroservices.Cores.Database/Database/Widgets/DatabaseBuilderWidget.cs new file mode 100644 index 0000000..4ef4159 --- /dev/null +++ b/src/CSharp/EasyMicroservices.Cores.Database/Database/Widgets/DatabaseBuilderWidget.cs @@ -0,0 +1,34 @@ +using EasyMicroservices.Cores.Interfaces; +using System; +using System.Reflection.Emit; + +namespace EasyMicroservices.Cores.Database.Widgets; +/// +/// +/// +public abstract class DatabaseBuilderWidget : IWidget +{ + /// + /// + /// + public void Build() + { + + } + + /// + /// + /// + /// + /// + /// + public abstract void OnModelCreating(TModelBuilder modelBuilder, string suffix = "", string prefix = ""); + /// + /// + /// + /// + public Type GetObjectType() + { + return typeof(DatabaseBuilderWidget); + } +} diff --git a/src/CSharp/EasyMicroservices.Cores.Database/Database/Widgets/ReportingBuilderWidget.cs b/src/CSharp/EasyMicroservices.Cores.Database/Database/Widgets/ReportingBuilderWidget.cs new file mode 100644 index 0000000..bb66eed --- /dev/null +++ b/src/CSharp/EasyMicroservices.Cores.Database/Database/Widgets/ReportingBuilderWidget.cs @@ -0,0 +1,22 @@ +using EasyMicroservices.Cores.Interfaces; +using System; + +namespace EasyMicroservices.Cores.Database.Widgets; +/// +/// +/// +public abstract class ReportingBuilderWidget : IWidget +{ + /// + /// + /// + public abstract void Build(); + /// + /// + /// + /// + public Type GetObjectType() + { + return typeof(ReportingBuilderWidget); + } +} diff --git a/src/CSharp/EasyMicroservices.Cores.Database/Database/Widgets/SimpleReportingEntityWidget.cs b/src/CSharp/EasyMicroservices.Cores.Database/Database/Widgets/SimpleReportingEntityWidget.cs new file mode 100644 index 0000000..d218051 --- /dev/null +++ b/src/CSharp/EasyMicroservices.Cores.Database/Database/Widgets/SimpleReportingEntityWidget.cs @@ -0,0 +1,76 @@ +using EasyMicroservices.Cores.Database.Interfaces; +using EasyMicroservices.Cores.Interfaces; +using EasyMicroservices.ServiceContracts; +using System; +using System.Threading.Tasks; + +namespace EasyMicroservices.Cores.Widgets; + +/// +/// +/// +/// +/// +public class SimpleReportingEntityWidget : IDatabaseWidget + where TReportEntity : class + where TObjectContract : class +{ + /// + /// + /// + public void Build() + { + + } + + /// + /// + /// + public bool CanProcess(IBaseUnitOfWork baseUnitOfWork) + { + if (baseUnitOfWork.LogicOptions.HasValue) + return !baseUnitOfWork.LogicOptions.Value.DoStopReporting; + return true; + } + + /// + /// + /// + /// + public Type GetObjectType() + { + return typeof(TObjectContract); + } + + /// + /// + /// + /// + /// + public Task Initialize(params TObjectContract[] parameters) + { + return TaskHelper.GetCompletedTask(); + } + + /// + /// + /// + /// + /// + /// + /// + /// + public async Task Process(IDatabaseWidgetManager databaseWidgetManager, IBaseUnitOfWork baseUnitOfWork, TObjectContract contract) + { + var reportEntity = await baseUnitOfWork + .GetMapper() + .MapAsync(contract); + + await baseUnitOfWork.GetLogic(new Models.LogicOptions() + { + DoStopReporting = true + }) + .Add(reportEntity) + .AsCheckedResult(); + } +} diff --git a/src/CSharp/EasyMicroservices.Cores.Database/EasyMicroservices.Cores.Database.csproj b/src/CSharp/EasyMicroservices.Cores.Database/EasyMicroservices.Cores.Database.csproj index 1cf5042..ff610f9 100644 --- a/src/CSharp/EasyMicroservices.Cores.Database/EasyMicroservices.Cores.Database.csproj +++ b/src/CSharp/EasyMicroservices.Cores.Database/EasyMicroservices.Cores.Database.csproj @@ -31,6 +31,7 @@ + diff --git a/src/CSharp/EasyMicroservices.Cores.Database/Interfaces/IBaseUnitOfWork.cs b/src/CSharp/EasyMicroservices.Cores.Database/Interfaces/IBaseUnitOfWork.cs index cd97c4e..59ed533 100644 --- a/src/CSharp/EasyMicroservices.Cores.Database/Interfaces/IBaseUnitOfWork.cs +++ b/src/CSharp/EasyMicroservices.Cores.Database/Interfaces/IBaseUnitOfWork.cs @@ -23,11 +23,11 @@ public interface IBaseUnitOfWork : IDisposable /// /// ServiceAddressInfo GetServiceAddress(string name); + /// /// /// - /// - void SetDefaultLogicOptions(LogicOptions logicOptions); + LogicOptions? LogicOptions { get; set; } /// /// @@ -60,6 +60,11 @@ public interface IBaseUnitOfWork : IDisposable /// /// T GetService(); + /// + /// + /// + /// + IDatabaseWidgetManager GetDatabaseWidgetManager(); /// /// diff --git a/src/CSharp/EasyMicroservices.Cores.Relational.EntityFrameworkCore/EntityFrameworkCoreDatabaseBuilder.cs b/src/CSharp/EasyMicroservices.Cores.Relational.EntityFrameworkCore/EntityFrameworkCoreDatabaseBuilder.cs index 39b7b99..00bcefa 100644 --- a/src/CSharp/EasyMicroservices.Cores.Relational.EntityFrameworkCore/EntityFrameworkCoreDatabaseBuilder.cs +++ b/src/CSharp/EasyMicroservices.Cores.Relational.EntityFrameworkCore/EntityFrameworkCoreDatabaseBuilder.cs @@ -1,4 +1,7 @@ -using EasyMicroservices.Cores.Models; +using EasyMicroservices.Cores.Database.Interfaces; +using EasyMicroservices.Cores.Database.Widgets; +using EasyMicroservices.Cores.Interfaces; +using EasyMicroservices.Cores.Models; using EasyMicroservices.Cores.Relational.EntityFrameworkCore.Intrerfaces; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; @@ -16,6 +19,21 @@ public abstract class EntityFrameworkCoreDatabaseBuilder : IEntityFrameworkCoreD /// /// protected readonly IConfiguration Configuration; + /// + /// + /// + protected readonly IDatabaseWidgetManager WidgetManager; + /// + /// + /// + /// + /// + public EntityFrameworkCoreDatabaseBuilder(IConfiguration configuration, IDatabaseWidgetManager widgetManager) + { + Configuration = configuration; + WidgetManager = widgetManager; + } + /// /// /// @@ -50,4 +68,18 @@ public virtual List GetDatabases() { return Configuration?.GetSection("Databases")?.Get>(); } + + /// + /// + /// + /// + public virtual void OnWidgetBuilder(ModelBuilder modelBuilder) + { + if (WidgetManager is null) + return; + foreach (var widget in WidgetManager.GetWidgets>()) + { + widget.OnModelCreating(modelBuilder); + } + } } \ No newline at end of file diff --git a/src/CSharp/EasyMicroservices.Cores.Relational.EntityFrameworkCore/Intrerfaces/IEntityFrameworkCoreDatabaseBuilder.cs b/src/CSharp/EasyMicroservices.Cores.Relational.EntityFrameworkCore/Intrerfaces/IEntityFrameworkCoreDatabaseBuilder.cs index 6b62ebb..78e1ffb 100644 --- a/src/CSharp/EasyMicroservices.Cores.Relational.EntityFrameworkCore/Intrerfaces/IEntityFrameworkCoreDatabaseBuilder.cs +++ b/src/CSharp/EasyMicroservices.Cores.Relational.EntityFrameworkCore/Intrerfaces/IEntityFrameworkCoreDatabaseBuilder.cs @@ -14,5 +14,10 @@ public interface IEntityFrameworkCoreDatabaseBuilder /// /// void OnConfiguring(DbContextOptionsBuilder optionsBuilder); + /// + /// + /// + /// + void OnWidgetBuilder(ModelBuilder modelBuilder); } } diff --git a/src/CSharp/EasyMicroservices.Cores.Relational.EntityFrameworkCore/RelationalCoreContext.cs b/src/CSharp/EasyMicroservices.Cores.Relational.EntityFrameworkCore/RelationalCoreContext.cs index 680dfbd..f3fcee8 100644 --- a/src/CSharp/EasyMicroservices.Cores.Relational.EntityFrameworkCore/RelationalCoreContext.cs +++ b/src/CSharp/EasyMicroservices.Cores.Relational.EntityFrameworkCore/RelationalCoreContext.cs @@ -11,7 +11,8 @@ namespace EasyMicroservices.Cores.Relational.EntityFrameworkCore /// public class RelationalCoreContext : CoreContext { - IEntityFrameworkCoreDatabaseBuilder _builder; + readonly IEntityFrameworkCoreDatabaseBuilder _builder; + /// /// /// @@ -20,6 +21,7 @@ public RelationalCoreContext(IEntityFrameworkCoreDatabaseBuilder builder) { _builder = builder; } + /// /// /// @@ -34,8 +36,7 @@ public RelationalCoreContext() /// protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { - if (_builder != null) - _builder.OnConfiguring(optionsBuilder); + _builder?.OnConfiguring(optionsBuilder); base.OnConfiguring(optionsBuilder); } @@ -46,6 +47,7 @@ protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); + _builder?.OnWidgetBuilder(modelBuilder); new RelationalCoreModelBuilder().OnModelCreating(modelBuilder); } @@ -55,6 +57,7 @@ protected override void OnModelCreating(ModelBuilder modelBuilder) protected virtual StringBuilder AutoModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); + _builder?.OnWidgetBuilder(modelBuilder); return new RelationalCoreModelBuilder().AutoModelCreating(modelBuilder); } } diff --git a/src/CSharp/EasyMicroservices.Cores.Tests/DatabaseLogics/LongIdMappedDatabaseLogicBaseTest.cs b/src/CSharp/EasyMicroservices.Cores.Tests/DatabaseLogics/LongIdMappedDatabaseLogicBaseTest.cs index a0d7d02..8b8071d 100644 --- a/src/CSharp/EasyMicroservices.Cores.Tests/DatabaseLogics/LongIdMappedDatabaseLogicBaseTest.cs +++ b/src/CSharp/EasyMicroservices.Cores.Tests/DatabaseLogics/LongIdMappedDatabaseLogicBaseTest.cs @@ -24,6 +24,11 @@ public void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseInMemoryDatabase("Test DB"); } + + public void OnWidgetBuilder(ModelBuilder modelBuilder) + { + + } } public class LongIdMappedDatabaseLogicBaseTest : IClassFixture, IClassFixture diff --git a/src/CSharp/EasyMicroservices.Cores.Tests/Fixtures/ServiceProviderFixture.cs b/src/CSharp/EasyMicroservices.Cores.Tests/Fixtures/ServiceProviderFixture.cs index 6b57dca..ec41e1c 100644 --- a/src/CSharp/EasyMicroservices.Cores.Tests/Fixtures/ServiceProviderFixture.cs +++ b/src/CSharp/EasyMicroservices.Cores.Tests/Fixtures/ServiceProviderFixture.cs @@ -2,6 +2,7 @@ using EasyMicroservices.Cores.AspEntityFrameworkCoreApi; using EasyMicroservices.Cores.AspEntityFrameworkCoreApi.Interfaces; using EasyMicroservices.Cores.Database.Interfaces; +using EasyMicroservices.Cores.Database.Logics; using EasyMicroservices.Cores.Database.Managers; using EasyMicroservices.Cores.Interfaces; using EasyMicroservices.Cores.Relational.EntityFrameworkCore; @@ -31,6 +32,7 @@ public async Task InitializeAsync() serviceCollection.AddTransient(serviceProvider => new MyTestContext(serviceProvider.GetService())); serviceCollection.AddTransient(); serviceCollection.AddSingleton(service => new WhiteLabelManager(service)); + serviceCollection.AddSingleton(); serviceCollection.AddSingleton((provider) => { return new DefaultUniqueIdentityManager(provider.GetService().CurrentWhiteLabel); diff --git a/src/CSharp/EasyMicroservices.Cores.sln b/src/CSharp/EasyMicroservices.Cores.sln index f4ff4f4..026aca6 100644 --- a/src/CSharp/EasyMicroservices.Cores.sln +++ b/src/CSharp/EasyMicroservices.Cores.sln @@ -15,9 +15,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EasyMicroservices.Cores.Asp EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EasyMicroservices.Cores.Tests", "EasyMicroservices.Cores.Tests\EasyMicroservices.Cores.Tests.csproj", "{0EA879CC-3350-4B97-8517-DCE7DBD2C3CC}" EndProject -Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "DataAccess-Layer", "DataAccess-Layer", "{B9CFFFCB-64BC-4E3A-982B-C50FEFE6EA9B}" +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Infrastructure-Layer", "Infrastructure-Layer", "{B9CFFFCB-64BC-4E3A-982B-C50FEFE6EA9B}" EndProject -Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "BusinessLogic-Layer", "BusinessLogic-Layer", "{5F639C59-EF1C-464B-85F1-FE01E4A9F81A}" +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Business-Layer", "Business-Layer", "{5F639C59-EF1C-464B-85F1-FE01E4A9F81A}" EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Test-Layer", "Test-Layer", "{C2D086CC-9E2B-4152-B8F7-81E91032C17C}" EndProject @@ -27,7 +27,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EasyMicroservices.Cores.Asp EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EasyMicroservices.Cores.AspCore.Tests", "EasyMicroservices.Cores.AspCore.Tests\EasyMicroservices.Cores.AspCore.Tests.csproj", "{243E15A6-5AD6-4304-8F91-D12666C59A0E}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EasyMicroservices.Cores.Clients", "EasyMicroservices.Cores.Clients\EasyMicroservices.Cores.Clients.csproj", "{1F65B13F-F101-437C-80E7-BA4B54574D5F}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EasyMicroservices.Cores.Clients", "EasyMicroservices.Cores.Clients\EasyMicroservices.Cores.Clients.csproj", "{1F65B13F-F101-437C-80E7-BA4B54574D5F}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EasyMicroservices.Cores.Infrastructure", "EasyMicroservices.Cores.Infrastructure\EasyMicroservices.Cores.Infrastructure.csproj", "{37928D9D-11FD-40AB-9C4E-42C9B506D386}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -159,6 +161,18 @@ Global {1F65B13F-F101-437C-80E7-BA4B54574D5F}.Release|x64.Build.0 = Release|Any CPU {1F65B13F-F101-437C-80E7-BA4B54574D5F}.Release|x86.ActiveCfg = Release|Any CPU {1F65B13F-F101-437C-80E7-BA4B54574D5F}.Release|x86.Build.0 = Release|Any CPU + {37928D9D-11FD-40AB-9C4E-42C9B506D386}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {37928D9D-11FD-40AB-9C4E-42C9B506D386}.Debug|Any CPU.Build.0 = Debug|Any CPU + {37928D9D-11FD-40AB-9C4E-42C9B506D386}.Debug|x64.ActiveCfg = Debug|Any CPU + {37928D9D-11FD-40AB-9C4E-42C9B506D386}.Debug|x64.Build.0 = Debug|Any CPU + {37928D9D-11FD-40AB-9C4E-42C9B506D386}.Debug|x86.ActiveCfg = Debug|Any CPU + {37928D9D-11FD-40AB-9C4E-42C9B506D386}.Debug|x86.Build.0 = Debug|Any CPU + {37928D9D-11FD-40AB-9C4E-42C9B506D386}.Release|Any CPU.ActiveCfg = Release|Any CPU + {37928D9D-11FD-40AB-9C4E-42C9B506D386}.Release|Any CPU.Build.0 = Release|Any CPU + {37928D9D-11FD-40AB-9C4E-42C9B506D386}.Release|x64.ActiveCfg = Release|Any CPU + {37928D9D-11FD-40AB-9C4E-42C9B506D386}.Release|x64.Build.0 = Release|Any CPU + {37928D9D-11FD-40AB-9C4E-42C9B506D386}.Release|x86.ActiveCfg = Release|Any CPU + {37928D9D-11FD-40AB-9C4E-42C9B506D386}.Release|x86.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -174,6 +188,7 @@ Global {2A012C31-966A-427B-B685-DCB95AC299E3} = {5F639C59-EF1C-464B-85F1-FE01E4A9F81A} {243E15A6-5AD6-4304-8F91-D12666C59A0E} = {C2D086CC-9E2B-4152-B8F7-81E91032C17C} {1F65B13F-F101-437C-80E7-BA4B54574D5F} = {B9CFFFCB-64BC-4E3A-982B-C50FEFE6EA9B} + {37928D9D-11FD-40AB-9C4E-42C9B506D386} = {B9CFFFCB-64BC-4E3A-982B-C50FEFE6EA9B} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {0E8382C1-722C-4F92-B66F-0354487C7F02} From c65867d3488d574b47db89c9555bd5b95764ade0 Mon Sep 17 00:00:00 2001 From: Ali Yousefi Date: Tue, 13 Feb 2024 21:54:39 +0330 Subject: [PATCH 3/4] add version --- .../EasyMicroservices.Cores.AspCoreApi.csproj | 2 +- .../EasyMicroservices.Cores.AspEntityFrameworkCoreApi.csproj | 2 +- .../EasyMicroservices.Cores.Clients.csproj | 2 +- .../EasyMicroservices.Cores.Contracts.csproj | 2 +- .../EasyMicroservices.Cores.Database.csproj | 2 +- .../EasyMicroservices.Cores.EntityFrameworkCore.csproj | 2 +- .../EasyMicroservices.Cores.Infrastructure.csproj | 2 +- ...asyMicroservices.Cores.Relational.EntityFrameworkCore.csproj | 2 +- 8 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/CSharp/EasyMicroservices.Cores.AspCoreApi/EasyMicroservices.Cores.AspCoreApi.csproj b/src/CSharp/EasyMicroservices.Cores.AspCoreApi/EasyMicroservices.Cores.AspCoreApi.csproj index e3d4ed3..5b9abea 100644 --- a/src/CSharp/EasyMicroservices.Cores.AspCoreApi/EasyMicroservices.Cores.AspCoreApi.csproj +++ b/src/CSharp/EasyMicroservices.Cores.AspCoreApi/EasyMicroservices.Cores.AspCoreApi.csproj @@ -4,7 +4,7 @@ netstandard2.1;net6.0;net7.0;net8.0 AnyCPU;x64;x86 EasyMicroservices - 0.0.1.50 + 0.0.1.51 asp core servces. EasyMicroservices@gmail.com core,cores,base,database,services,asp,aspnet diff --git a/src/CSharp/EasyMicroservices.Cores.AspEntityFrameworkCoreApi/EasyMicroservices.Cores.AspEntityFrameworkCoreApi.csproj b/src/CSharp/EasyMicroservices.Cores.AspEntityFrameworkCoreApi/EasyMicroservices.Cores.AspEntityFrameworkCoreApi.csproj index 8d4bc30..c057c57 100644 --- a/src/CSharp/EasyMicroservices.Cores.AspEntityFrameworkCoreApi/EasyMicroservices.Cores.AspEntityFrameworkCoreApi.csproj +++ b/src/CSharp/EasyMicroservices.Cores.AspEntityFrameworkCoreApi/EasyMicroservices.Cores.AspEntityFrameworkCoreApi.csproj @@ -4,7 +4,7 @@ net6.0;net7.0;net8.0 AnyCPU;x64;x86 EasyMicroservices - 0.0.1.50 + 0.0.1.51 asp core servces. EasyMicroservices@gmail.com core,cores,base,database,services,asp,aspnet,aspcore,efcore diff --git a/src/CSharp/EasyMicroservices.Cores.Clients/EasyMicroservices.Cores.Clients.csproj b/src/CSharp/EasyMicroservices.Cores.Clients/EasyMicroservices.Cores.Clients.csproj index d9dd417..4528311 100644 --- a/src/CSharp/EasyMicroservices.Cores.Clients/EasyMicroservices.Cores.Clients.csproj +++ b/src/CSharp/EasyMicroservices.Cores.Clients/EasyMicroservices.Cores.Clients.csproj @@ -4,7 +4,7 @@ netstandard2.0;netstandard2.1;net6.0;net8.0 AnyCPU;x64;x86 EasyMicroservices - 0.0.1.50 + 0.0.1.51 core of database. EasyMicroservices@gmail.com core,cores,base,client,clients diff --git a/src/CSharp/EasyMicroservices.Cores.Contracts/EasyMicroservices.Cores.Contracts.csproj b/src/CSharp/EasyMicroservices.Cores.Contracts/EasyMicroservices.Cores.Contracts.csproj index d018c8f..44f21c8 100644 --- a/src/CSharp/EasyMicroservices.Cores.Contracts/EasyMicroservices.Cores.Contracts.csproj +++ b/src/CSharp/EasyMicroservices.Cores.Contracts/EasyMicroservices.Cores.Contracts.csproj @@ -4,7 +4,7 @@ netstandard2.0;netstandard2.1;net45;net6.0;net8.0 AnyCPU;x64;x86 EasyMicroservices - 0.0.1.50 + 0.0.1.51 core contracts. EasyMicroservices@gmail.com core,cores,base,contract,contracts,dto,dtos diff --git a/src/CSharp/EasyMicroservices.Cores.Database/EasyMicroservices.Cores.Database.csproj b/src/CSharp/EasyMicroservices.Cores.Database/EasyMicroservices.Cores.Database.csproj index ff610f9..379cb96 100644 --- a/src/CSharp/EasyMicroservices.Cores.Database/EasyMicroservices.Cores.Database.csproj +++ b/src/CSharp/EasyMicroservices.Cores.Database/EasyMicroservices.Cores.Database.csproj @@ -4,7 +4,7 @@ netstandard2.0;netstandard2.1;net45;net6.0;net8.0 AnyCPU;x64;x86 EasyMicroservices - 0.0.1.50 + 0.0.1.51 core of database. EasyMicroservices@gmail.com core,cores,base,database diff --git a/src/CSharp/EasyMicroservices.Cores.EntityFrameworkCore/EasyMicroservices.Cores.EntityFrameworkCore.csproj b/src/CSharp/EasyMicroservices.Cores.EntityFrameworkCore/EasyMicroservices.Cores.EntityFrameworkCore.csproj index 917394b..07f2446 100644 --- a/src/CSharp/EasyMicroservices.Cores.EntityFrameworkCore/EasyMicroservices.Cores.EntityFrameworkCore.csproj +++ b/src/CSharp/EasyMicroservices.Cores.EntityFrameworkCore/EasyMicroservices.Cores.EntityFrameworkCore.csproj @@ -4,7 +4,7 @@ netstandard2.1;net6.0;net8.0 AnyCPU;x64;x86 EasyMicroservices - 0.0.1.50 + 0.0.1.51 ef core of database. EasyMicroservices@gmail.com core,cores,base,database,ef,efcore diff --git a/src/CSharp/EasyMicroservices.Cores.Infrastructure/EasyMicroservices.Cores.Infrastructure.csproj b/src/CSharp/EasyMicroservices.Cores.Infrastructure/EasyMicroservices.Cores.Infrastructure.csproj index 41c0b92..f31d4e9 100644 --- a/src/CSharp/EasyMicroservices.Cores.Infrastructure/EasyMicroservices.Cores.Infrastructure.csproj +++ b/src/CSharp/EasyMicroservices.Cores.Infrastructure/EasyMicroservices.Cores.Infrastructure.csproj @@ -4,7 +4,7 @@ netstandard2.0;netstandard2.1;net45;net6.0;net8.0 AnyCPU;x64;x86 EasyMicroservices - 0.0.1.50 + 0.0.1.51 core of infrastructure. EasyMicroservices@gmail.com core,cores,base,infrastructure diff --git a/src/CSharp/EasyMicroservices.Cores.Relational.EntityFrameworkCore/EasyMicroservices.Cores.Relational.EntityFrameworkCore.csproj b/src/CSharp/EasyMicroservices.Cores.Relational.EntityFrameworkCore/EasyMicroservices.Cores.Relational.EntityFrameworkCore.csproj index 688ba24..d568237 100644 --- a/src/CSharp/EasyMicroservices.Cores.Relational.EntityFrameworkCore/EasyMicroservices.Cores.Relational.EntityFrameworkCore.csproj +++ b/src/CSharp/EasyMicroservices.Cores.Relational.EntityFrameworkCore/EasyMicroservices.Cores.Relational.EntityFrameworkCore.csproj @@ -4,7 +4,7 @@ netstandard2.1;net6.0;net7.0;net8.0 AnyCPU;x64;x86 EasyMicroservices - 0.0.1.50 + 0.0.1.51 ef core of Relational database. EasyMicroservices@gmail.com core,cores,base,database,ef,efcore,Relational From 3a16d09aa5313d5ef7bf77cb367a0e51659d288c Mon Sep 17 00:00:00 2001 From: Ali Yousefi Date: Wed, 14 Feb 2024 11:24:21 +0330 Subject: [PATCH 4/4] Add support for RecordId for report mapping --- .../Database/Helpers/DatabaseExtensions.cs | 25 +++++++++++++++++++ .../Database/Interfaces/IDatabaseWidget.cs | 6 +++-- .../Interfaces/IDatabaseWidgetManager.cs | 6 +++-- .../Database/Interfaces/IReadableLogic.cs | 6 +++++ .../Database/Interfaces/IRecordIdSchema.cs | 12 +++++++++ .../Database/Interfaces/IWritableLogic.cs | 6 +++++ .../Database/Logics/DatabaseLogicBase.cs | 20 +++++++++++++++ .../Logics/DatabaseLogicInfrastructure.cs | 2 +- .../Logics/DatabaseMappedLogicBase.cs | 18 +++++++++++++ .../Logics/IdSchemaDatabaseMappedLogicBase.cs | 18 +++++++++++++ .../Managers/DatabaseWidgetManager.cs | 9 ++++--- .../Widgets/SimpleReportingEntityWidget.cs | 19 ++++++++------ 12 files changed, 132 insertions(+), 15 deletions(-) create mode 100644 src/CSharp/EasyMicroservices.Cores.Database/Database/Helpers/DatabaseExtensions.cs create mode 100644 src/CSharp/EasyMicroservices.Cores.Database/Database/Interfaces/IRecordIdSchema.cs diff --git a/src/CSharp/EasyMicroservices.Cores.Database/Database/Helpers/DatabaseExtensions.cs b/src/CSharp/EasyMicroservices.Cores.Database/Database/Helpers/DatabaseExtensions.cs new file mode 100644 index 0000000..d74dad8 --- /dev/null +++ b/src/CSharp/EasyMicroservices.Cores.Database/Database/Helpers/DatabaseExtensions.cs @@ -0,0 +1,25 @@ +using EasyMicroservices.Cores.Database.Interfaces; +using EasyMicroservices.Database.Interfaces; +using System; +using System.Linq; +using System.Reflection; + +namespace EasyMicroservices.Cores.Database.Helpers; +/// +/// +/// +internal static class DatabaseExtensions +{ + public static void SetIdToRecordId(IContext context, TEntity entity, TRecordEntity recordEntity) + { + var idProperty = typeof(TRecordEntity) + .GetProperty(nameof(IRecordIdSchema.RecordId), BindingFlags.Public | BindingFlags.Instance); + if (idProperty == null) + throw new Exception($"I cannot find RecordId in your {typeof(TRecordEntity).Name}, Did you inherit from IRecordIdSchema?"); + + var ids = context.GetPrimaryKeyValues(entity); + if (!ids.HasAny()) + throw new Exception($"I cannot find any primary key in your {typeof(TEntity).Name}!"); + idProperty.SetValue(recordEntity, ids.First()); + } +} diff --git a/src/CSharp/EasyMicroservices.Cores.Database/Database/Interfaces/IDatabaseWidget.cs b/src/CSharp/EasyMicroservices.Cores.Database/Database/Interfaces/IDatabaseWidget.cs index bf9ede7..3cccec7 100644 --- a/src/CSharp/EasyMicroservices.Cores.Database/Database/Interfaces/IDatabaseWidget.cs +++ b/src/CSharp/EasyMicroservices.Cores.Database/Database/Interfaces/IDatabaseWidget.cs @@ -5,7 +5,8 @@ namespace EasyMicroservices.Cores.Database.Interfaces; /// /// /// -public interface IDatabaseWidget : IWidget +public interface IDatabaseWidget : IWidget + where TEntity : class { /// /// @@ -17,6 +18,7 @@ public interface IDatabaseWidget : IWidget /// /// /// + /// /// - Task Process(IDatabaseWidgetManager databaseWidgetManager, IBaseUnitOfWork baseUnitOfWork, T contract); + Task Process(IDatabaseWidgetManager databaseWidgetManager, IBaseUnitOfWork baseUnitOfWork, T contract, TEntity entity); } diff --git a/src/CSharp/EasyMicroservices.Cores.Database/Database/Interfaces/IDatabaseWidgetManager.cs b/src/CSharp/EasyMicroservices.Cores.Database/Database/Interfaces/IDatabaseWidgetManager.cs index f411417..ed7d7db 100644 --- a/src/CSharp/EasyMicroservices.Cores.Database/Database/Interfaces/IDatabaseWidgetManager.cs +++ b/src/CSharp/EasyMicroservices.Cores.Database/Database/Interfaces/IDatabaseWidgetManager.cs @@ -1,5 +1,4 @@ using EasyMicroservices.Cores.Interfaces; -using EasyMicroservices.Database.Interfaces; using System.Threading.Tasks; namespace EasyMicroservices.Cores.Database.Interfaces; @@ -12,8 +11,11 @@ public interface IDatabaseWidgetManager : IWidgetManager /// /// /// + /// /// /// + /// /// - Task Add(IBaseUnitOfWork baseUnitOfWork, T contract); + Task Add(IBaseUnitOfWork baseUnitOfWork, T contract, TEntity entity) + where TEntity : class; } diff --git a/src/CSharp/EasyMicroservices.Cores.Database/Database/Interfaces/IReadableLogic.cs b/src/CSharp/EasyMicroservices.Cores.Database/Database/Interfaces/IReadableLogic.cs index 6fbe74d..0ec2e3c 100644 --- a/src/CSharp/EasyMicroservices.Cores.Database/Database/Interfaces/IReadableLogic.cs +++ b/src/CSharp/EasyMicroservices.Cores.Database/Database/Interfaces/IReadableLogic.cs @@ -1,6 +1,7 @@ using EasyMicroservices.Cores.Contracts.Requests; using EasyMicroservices.Cores.DataTypes; using EasyMicroservices.Cores.Interfaces; +using EasyMicroservices.Database.Interfaces; using EasyMicroservices.ServiceContracts; using System; using System.Collections.Generic; @@ -18,6 +19,11 @@ namespace EasyMicroservices.Cores.Database.Interfaces /// public interface IReadableLogic { + /// + /// + /// + /// + IContext GetReadableContext(); /// /// /// diff --git a/src/CSharp/EasyMicroservices.Cores.Database/Database/Interfaces/IRecordIdSchema.cs b/src/CSharp/EasyMicroservices.Cores.Database/Database/Interfaces/IRecordIdSchema.cs new file mode 100644 index 0000000..23aad09 --- /dev/null +++ b/src/CSharp/EasyMicroservices.Cores.Database/Database/Interfaces/IRecordIdSchema.cs @@ -0,0 +1,12 @@ +namespace EasyMicroservices.Cores.Database.Interfaces; +/// +/// +/// +/// +public interface IRecordIdSchema +{ + /// + /// + /// + public TId RecordId { get; set; } +} \ No newline at end of file diff --git a/src/CSharp/EasyMicroservices.Cores.Database/Database/Interfaces/IWritableLogic.cs b/src/CSharp/EasyMicroservices.Cores.Database/Database/Interfaces/IWritableLogic.cs index 262d549..41d56eb 100644 --- a/src/CSharp/EasyMicroservices.Cores.Database/Database/Interfaces/IWritableLogic.cs +++ b/src/CSharp/EasyMicroservices.Cores.Database/Database/Interfaces/IWritableLogic.cs @@ -1,4 +1,5 @@ using EasyMicroservices.Cores.Contracts.Requests; +using EasyMicroservices.Database.Interfaces; using EasyMicroservices.ServiceContracts; using System; using System.Linq.Expressions; @@ -12,6 +13,11 @@ namespace EasyMicroservices.Cores.Database.Interfaces /// public interface IWritableLogic { + /// + /// + /// + /// + IContext GetWritableContext(); /// /// /// diff --git a/src/CSharp/EasyMicroservices.Cores.Database/Database/Logics/DatabaseLogicBase.cs b/src/CSharp/EasyMicroservices.Cores.Database/Database/Logics/DatabaseLogicBase.cs index 48c8ea9..d318fc1 100644 --- a/src/CSharp/EasyMicroservices.Cores.Database/Database/Logics/DatabaseLogicBase.cs +++ b/src/CSharp/EasyMicroservices.Cores.Database/Database/Logics/DatabaseLogicBase.cs @@ -257,5 +257,25 @@ public Task SaveChangesAsync(CancellationToken cancellationToken = default) { return _easyWriteableQueryable.SaveChangesAsync(cancellationToken); } + + /// + /// + /// + /// + /// + public IContext GetReadableContext() + { + return _easyReadableQueryable.Context; + } + + /// + /// + /// + /// + /// + public IContext GetWritableContext() + { + return _easyWriteableQueryable.Context; + } } } diff --git a/src/CSharp/EasyMicroservices.Cores.Database/Database/Logics/DatabaseLogicInfrastructure.cs b/src/CSharp/EasyMicroservices.Cores.Database/Database/Logics/DatabaseLogicInfrastructure.cs index 1690aa9..c6de48e 100644 --- a/src/CSharp/EasyMicroservices.Cores.Database/Database/Logics/DatabaseLogicInfrastructure.cs +++ b/src/CSharp/EasyMicroservices.Cores.Database/Database/Logics/DatabaseLogicInfrastructure.cs @@ -974,7 +974,7 @@ await InternalUpdate(easyWritableQueryable, result.Entity, false, true, true, tr } } var widgetManager = _baseUnitOfWork.GetDatabaseWidgetManager(); - await widgetManager.Add(_baseUnitOfWork, contract); + await widgetManager.Add(_baseUnitOfWork, contract, result.Entity); await ActivityChangeLogLogic.AddAsync(result.Entity, _baseUnitOfWork); return result.Entity; } diff --git a/src/CSharp/EasyMicroservices.Cores.Database/Database/Logics/DatabaseMappedLogicBase.cs b/src/CSharp/EasyMicroservices.Cores.Database/Database/Logics/DatabaseMappedLogicBase.cs index 1f7e7a2..c344833 100644 --- a/src/CSharp/EasyMicroservices.Cores.Database/Database/Logics/DatabaseMappedLogicBase.cs +++ b/src/CSharp/EasyMicroservices.Cores.Database/Database/Logics/DatabaseMappedLogicBase.cs @@ -365,5 +365,23 @@ public Task SoftDeleteBulkByIds(SoftDeleteBulkRequestContract + /// + /// + /// + public IContext GetReadableContext() + { + return _easyReadableQueryable.Context; + } + + /// + /// + /// + /// + public IContext GetWritableContext() + { + return _easyWriteableQueryable.Context; + } } } diff --git a/src/CSharp/EasyMicroservices.Cores.Database/Database/Logics/IdSchemaDatabaseMappedLogicBase.cs b/src/CSharp/EasyMicroservices.Cores.Database/Database/Logics/IdSchemaDatabaseMappedLogicBase.cs index 712828b..b4cd316 100644 --- a/src/CSharp/EasyMicroservices.Cores.Database/Database/Logics/IdSchemaDatabaseMappedLogicBase.cs +++ b/src/CSharp/EasyMicroservices.Cores.Database/Database/Logics/IdSchemaDatabaseMappedLogicBase.cs @@ -388,5 +388,23 @@ protected Func, IEasyReadableQueryableAsync return (q) => _easyReadableQueryable.ConvertToReadable(query(q)); return null; } + + /// + /// + /// + /// + public IContext GetReadableContext() + { + return _easyReadableQueryable.Context; + } + + /// + /// + /// + /// + public IContext GetWritableContext() + { + return _easyWriteableQueryable.Context; + } } } diff --git a/src/CSharp/EasyMicroservices.Cores.Database/Database/Managers/DatabaseWidgetManager.cs b/src/CSharp/EasyMicroservices.Cores.Database/Database/Managers/DatabaseWidgetManager.cs index 4527305..539d5bd 100644 --- a/src/CSharp/EasyMicroservices.Cores.Database/Database/Managers/DatabaseWidgetManager.cs +++ b/src/CSharp/EasyMicroservices.Cores.Database/Database/Managers/DatabaseWidgetManager.cs @@ -14,17 +14,20 @@ public class DatabaseWidgetManager : WidgetManager, IDatabaseWidgetManager /// /// /// + /// /// /// + /// /// - public async Task Add(IBaseUnitOfWork baseUnitOfWork, T contract) + public async Task Add(IBaseUnitOfWork baseUnitOfWork, T contract, TEntity entity) + where TEntity : class { var widgets = GetWidgetsByType(typeof(T)); foreach (var widget in widgets) { - if (widget is IDatabaseWidget databaseWidget && databaseWidget.CanProcess(baseUnitOfWork)) + if (widget is IDatabaseWidget databaseWidget && databaseWidget.CanProcess(baseUnitOfWork)) { - await databaseWidget.Process(this, baseUnitOfWork, contract); + await databaseWidget.Process(this, baseUnitOfWork, contract, entity); } } } diff --git a/src/CSharp/EasyMicroservices.Cores.Database/Database/Widgets/SimpleReportingEntityWidget.cs b/src/CSharp/EasyMicroservices.Cores.Database/Database/Widgets/SimpleReportingEntityWidget.cs index d218051..c6970ce 100644 --- a/src/CSharp/EasyMicroservices.Cores.Database/Database/Widgets/SimpleReportingEntityWidget.cs +++ b/src/CSharp/EasyMicroservices.Cores.Database/Database/Widgets/SimpleReportingEntityWidget.cs @@ -1,4 +1,5 @@ -using EasyMicroservices.Cores.Database.Interfaces; +using EasyMicroservices.Cores.Database.Helpers; +using EasyMicroservices.Cores.Database.Interfaces; using EasyMicroservices.Cores.Interfaces; using EasyMicroservices.ServiceContracts; using System; @@ -11,8 +12,10 @@ namespace EasyMicroservices.Cores.Widgets; /// /// /// -public class SimpleReportingEntityWidget : IDatabaseWidget +/// +public class SimpleReportingEntityWidget : IDatabaseWidget where TReportEntity : class + where TEntity : class where TObjectContract : class { /// @@ -29,7 +32,7 @@ public void Build() public bool CanProcess(IBaseUnitOfWork baseUnitOfWork) { if (baseUnitOfWork.LogicOptions.HasValue) - return !baseUnitOfWork.LogicOptions.Value.DoStopReporting; + return !baseUnitOfWork.LogicOptions.Value.DoStopReporting; return true; } @@ -58,18 +61,20 @@ public Task Initialize(params TObjectContract[] parameters) /// /// /// + /// /// /// - public async Task Process(IDatabaseWidgetManager databaseWidgetManager, IBaseUnitOfWork baseUnitOfWork, TObjectContract contract) + public async Task Process(IDatabaseWidgetManager databaseWidgetManager, IBaseUnitOfWork baseUnitOfWork, TObjectContract contract, TEntity entity) { var reportEntity = await baseUnitOfWork .GetMapper() .MapAsync(contract); - - await baseUnitOfWork.GetLogic(new Models.LogicOptions() + var logic = baseUnitOfWork.GetLogic(new Models.LogicOptions() { DoStopReporting = true - }) + }); + DatabaseExtensions.SetIdToRecordId(logic.GetReadableContext(), entity, reportEntity); + await logic .Add(reportEntity) .AsCheckedResult(); }