From 2dd82e752efbf295c27aeee480482c11033ff1ad Mon Sep 17 00:00:00 2001 From: Leonardo Porro Date: Mon, 9 Dec 2024 17:31:38 -0300 Subject: [PATCH] model integration + starting what's new! --- Detached.Mappers.sln => Detached.sln | 51 +++++++++++++++--- .../EntityMapperDbContextOptionsExtension.cs | 6 +-- ...tityMapperDbContextOptionsExtensionInfo.cs | 3 +- .../Package.cs | 3 +- .../Detached.Model.EntityFramework.csproj | 53 +++++++++++++++++++ .../Options/DelegateModelConfiguration.cs | 13 +++++ .../Options/IModelConfiguration.cs | 10 ++++ .../Options/ModelCustomizer.cs | 27 ++++++++++ .../ModelDbContextOptionsExtensions.cs | 29 ++++++++++ .../ModelDbContextOptionsExtensionsInfo.cs | 28 ++++++++++ src/Detached.Model.EntityFramework/Package.cs | 32 +++++++++++ 11 files changed, 240 insertions(+), 15 deletions(-) rename Detached.Mappers.sln => Detached.sln (65%) rename src/Detached.Mappers.EntityFramework/{Integration => Options}/EntityMapperDbContextOptionsExtension.cs (81%) rename src/Detached.Mappers.EntityFramework/{Integration => Options}/EntityMapperDbContextOptionsExtensionInfo.cs (90%) create mode 100644 src/Detached.Model.EntityFramework/Detached.Model.EntityFramework.csproj create mode 100644 src/Detached.Model.EntityFramework/Options/DelegateModelConfiguration.cs create mode 100644 src/Detached.Model.EntityFramework/Options/IModelConfiguration.cs create mode 100644 src/Detached.Model.EntityFramework/Options/ModelCustomizer.cs create mode 100644 src/Detached.Model.EntityFramework/Options/ModelDbContextOptionsExtensions.cs create mode 100644 src/Detached.Model.EntityFramework/Options/ModelDbContextOptionsExtensionsInfo.cs create mode 100644 src/Detached.Model.EntityFramework/Package.cs diff --git a/Detached.Mappers.sln b/Detached.sln similarity index 65% rename from Detached.Mappers.sln rename to Detached.sln index cc44cca..3b6b482 100644 --- a/Detached.Mappers.sln +++ b/Detached.sln @@ -32,6 +32,28 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Detached.Mappers.HotChocola EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Detached.Mappers.Annotations", "src\Detached.Mappers.Annotations\Detached.Mappers.Annotations.csproj", "{8ABB6D6F-4BFD-4196-8352-67A767C5574F}" EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Detached.Model.EntityFramework", "src\Detached.Model.EntityFramework\Detached.Model.EntityFramework.csproj", "{7FBA946F-1507-4847-B584-D0EDA51F1A12}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Mappers", "Mappers", "{ED84E94C-4D3D-4897-BA00-734612B01730}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Model", "Model", "{FEB397C1-A23C-47E5-9615-1E316EDF122E}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Actions", "Actions", "{7852C9A8-FFE3-48C9-A78C-FBC8AE17F32F}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Types", "Types", "{5D512CB3-5187-435F-AE9A-18B646F01580}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Actions", "Actions", "{579776BD-1E44-4BC5-9514-182C6E9578E3}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Mappers", "Mappers", "{F9A749C7-FE61-4C39-A4DB-6FFFD6BC50B6}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Model", "Model", "{96D60403-21AD-453A-922C-913D37BD0853}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Types", "Types", "{E41D9456-7D15-4767-AFE0-BB13D196B6CC}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Sourcing", "Sourcing", "{F2A88034-CA33-4EE7-9AB7-CF7D8422FAC8}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Sourcing", "Sourcing", "{561145D5-923A-4849-8BB7-0C133E386445}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -70,19 +92,34 @@ Global {8ABB6D6F-4BFD-4196-8352-67A767C5574F}.Debug|Any CPU.Build.0 = Debug|Any CPU {8ABB6D6F-4BFD-4196-8352-67A767C5574F}.Release|Any CPU.ActiveCfg = Release|Any CPU {8ABB6D6F-4BFD-4196-8352-67A767C5574F}.Release|Any CPU.Build.0 = Release|Any CPU + {7FBA946F-1507-4847-B584-D0EDA51F1A12}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {7FBA946F-1507-4847-B584-D0EDA51F1A12}.Debug|Any CPU.Build.0 = Debug|Any CPU + {7FBA946F-1507-4847-B584-D0EDA51F1A12}.Release|Any CPU.ActiveCfg = Release|Any CPU + {7FBA946F-1507-4847-B584-D0EDA51F1A12}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection GlobalSection(NestedProjects) = preSolution {6310ED0B-E8C2-4369-9147-34895A561ABB} = {6DF44ED2-7BB6-46F9-90EE-7A0D90874061} - {6E0CD895-9653-4BD5-B7B4-A42883F94826} = {745CB1E1-F50B-4307-8BD6-1ADDC837102B} - {DA563FFB-7793-47D6-8CEC-7E9206F4D0B2} = {745CB1E1-F50B-4307-8BD6-1ADDC837102B} - {AE2CCD23-0DD6-4B16-960C-BC8B233206BF} = {C9521C36-FBA4-4B3E-AE14-A1735E9D1E71} - {15A220B4-B286-4BD4-B9A9-CC2B7A0EB897} = {C9521C36-FBA4-4B3E-AE14-A1735E9D1E71} - {DF4B4AFB-C8AE-4B17-AAAB-AD2693639632} = {745CB1E1-F50B-4307-8BD6-1ADDC837102B} - {B6162ECF-00B0-4506-B3E8-2A4B46517ECE} = {C9521C36-FBA4-4B3E-AE14-A1735E9D1E71} - {8ABB6D6F-4BFD-4196-8352-67A767C5574F} = {745CB1E1-F50B-4307-8BD6-1ADDC837102B} + {6E0CD895-9653-4BD5-B7B4-A42883F94826} = {ED84E94C-4D3D-4897-BA00-734612B01730} + {DA563FFB-7793-47D6-8CEC-7E9206F4D0B2} = {ED84E94C-4D3D-4897-BA00-734612B01730} + {AE2CCD23-0DD6-4B16-960C-BC8B233206BF} = {F9A749C7-FE61-4C39-A4DB-6FFFD6BC50B6} + {15A220B4-B286-4BD4-B9A9-CC2B7A0EB897} = {F9A749C7-FE61-4C39-A4DB-6FFFD6BC50B6} + {DF4B4AFB-C8AE-4B17-AAAB-AD2693639632} = {ED84E94C-4D3D-4897-BA00-734612B01730} + {B6162ECF-00B0-4506-B3E8-2A4B46517ECE} = {F9A749C7-FE61-4C39-A4DB-6FFFD6BC50B6} + {8ABB6D6F-4BFD-4196-8352-67A767C5574F} = {ED84E94C-4D3D-4897-BA00-734612B01730} + {7FBA946F-1507-4847-B584-D0EDA51F1A12} = {FEB397C1-A23C-47E5-9615-1E316EDF122E} + {ED84E94C-4D3D-4897-BA00-734612B01730} = {745CB1E1-F50B-4307-8BD6-1ADDC837102B} + {FEB397C1-A23C-47E5-9615-1E316EDF122E} = {745CB1E1-F50B-4307-8BD6-1ADDC837102B} + {7852C9A8-FFE3-48C9-A78C-FBC8AE17F32F} = {745CB1E1-F50B-4307-8BD6-1ADDC837102B} + {5D512CB3-5187-435F-AE9A-18B646F01580} = {745CB1E1-F50B-4307-8BD6-1ADDC837102B} + {579776BD-1E44-4BC5-9514-182C6E9578E3} = {C9521C36-FBA4-4B3E-AE14-A1735E9D1E71} + {F9A749C7-FE61-4C39-A4DB-6FFFD6BC50B6} = {C9521C36-FBA4-4B3E-AE14-A1735E9D1E71} + {96D60403-21AD-453A-922C-913D37BD0853} = {C9521C36-FBA4-4B3E-AE14-A1735E9D1E71} + {E41D9456-7D15-4767-AFE0-BB13D196B6CC} = {C9521C36-FBA4-4B3E-AE14-A1735E9D1E71} + {F2A88034-CA33-4EE7-9AB7-CF7D8422FAC8} = {745CB1E1-F50B-4307-8BD6-1ADDC837102B} + {561145D5-923A-4849-8BB7-0C133E386445} = {C9521C36-FBA4-4B3E-AE14-A1735E9D1E71} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {7FC47439-B36B-45BB-BEEC-A6D595358C63} diff --git a/src/Detached.Mappers.EntityFramework/Integration/EntityMapperDbContextOptionsExtension.cs b/src/Detached.Mappers.EntityFramework/Options/EntityMapperDbContextOptionsExtension.cs similarity index 81% rename from src/Detached.Mappers.EntityFramework/Integration/EntityMapperDbContextOptionsExtension.cs rename to src/Detached.Mappers.EntityFramework/Options/EntityMapperDbContextOptionsExtension.cs index a7d8539..c2c58ce 100644 --- a/src/Detached.Mappers.EntityFramework/Integration/EntityMapperDbContextOptionsExtension.cs +++ b/src/Detached.Mappers.EntityFramework/Options/EntityMapperDbContextOptionsExtension.cs @@ -1,9 +1,7 @@ -using Detached.Mappers.EntityFramework.Options; -using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.Extensions.DependencyInjection; -using System; -namespace Detached.Mappers.EntityFramework.Integration +namespace Detached.Mappers.EntityFramework.Options { public class EntityMapperDbContextOptionsExtension : IDbContextOptionsExtension { diff --git a/src/Detached.Mappers.EntityFramework/Integration/EntityMapperDbContextOptionsExtensionInfo.cs b/src/Detached.Mappers.EntityFramework/Options/EntityMapperDbContextOptionsExtensionInfo.cs similarity index 90% rename from src/Detached.Mappers.EntityFramework/Integration/EntityMapperDbContextOptionsExtensionInfo.cs rename to src/Detached.Mappers.EntityFramework/Options/EntityMapperDbContextOptionsExtensionInfo.cs index 240d932..358190c 100644 --- a/src/Detached.Mappers.EntityFramework/Integration/EntityMapperDbContextOptionsExtensionInfo.cs +++ b/src/Detached.Mappers.EntityFramework/Options/EntityMapperDbContextOptionsExtensionInfo.cs @@ -1,7 +1,6 @@ using Microsoft.EntityFrameworkCore.Infrastructure; -using System.Collections.Generic; -namespace Detached.Mappers.EntityFramework.Integration +namespace Detached.Mappers.EntityFramework.Options { public class EntityMapperDbContextOptionsExtensionInfo : DbContextOptionsExtensionInfo { diff --git a/src/Detached.Mappers.EntityFramework/Package.cs b/src/Detached.Mappers.EntityFramework/Package.cs index 560d287..6cc23ad 100644 --- a/src/Detached.Mappers.EntityFramework/Package.cs +++ b/src/Detached.Mappers.EntityFramework/Package.cs @@ -1,5 +1,4 @@ -using Detached.Mappers.EntityFramework.Integration; -using Detached.Mappers.EntityFramework.Options; +using Detached.Mappers.EntityFramework.Options; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.Extensions.DependencyInjection; diff --git a/src/Detached.Model.EntityFramework/Detached.Model.EntityFramework.csproj b/src/Detached.Model.EntityFramework/Detached.Model.EntityFramework.csproj new file mode 100644 index 0000000..c85954f --- /dev/null +++ b/src/Detached.Model.EntityFramework/Detached.Model.EntityFramework.csproj @@ -0,0 +1,53 @@ + + + + net6.0;net8.0;net9.0 + enable + enable + 13 + + + + $(Version) + Leonardo Porro + + Detached + A general purpose object-oriented mapper. + 2017 + https://github.com/leonardoporro/Detached + (LGPL-2.0-only WITH FLTK-exception OR Apache-2.0+) + + true + logo.png + Detached.Mappers.EntityFramework + Readme.MD + + + + IDE0059,EF1001 + + + + + + + + + + + + + + + + + True + + + + True + \ + + + + diff --git a/src/Detached.Model.EntityFramework/Options/DelegateModelConfiguration.cs b/src/Detached.Model.EntityFramework/Options/DelegateModelConfiguration.cs new file mode 100644 index 0000000..185e563 --- /dev/null +++ b/src/Detached.Model.EntityFramework/Options/DelegateModelConfiguration.cs @@ -0,0 +1,13 @@ +using Microsoft.EntityFrameworkCore; + +namespace Detached.Model.EntityFramework.Options +{ + public class DelegateModelConfiguration(Action configure) : IModelConfiguration + where TDbContext : DbContext + { + public void ConfigureModel(ModelBuilder model, TDbContext dbContext) + { + configure(model, dbContext); + } + } +} \ No newline at end of file diff --git a/src/Detached.Model.EntityFramework/Options/IModelConfiguration.cs b/src/Detached.Model.EntityFramework/Options/IModelConfiguration.cs new file mode 100644 index 0000000..2316580 --- /dev/null +++ b/src/Detached.Model.EntityFramework/Options/IModelConfiguration.cs @@ -0,0 +1,10 @@ +using Microsoft.EntityFrameworkCore; + +namespace Detached.Model.EntityFramework.Options +{ + public interface IModelConfiguration + where TDbContext : DbContext + { + void ConfigureModel(ModelBuilder model, TDbContext dbContext); + } +} diff --git a/src/Detached.Model.EntityFramework/Options/ModelCustomizer.cs b/src/Detached.Model.EntityFramework/Options/ModelCustomizer.cs new file mode 100644 index 0000000..c732ade --- /dev/null +++ b/src/Detached.Model.EntityFramework/Options/ModelCustomizer.cs @@ -0,0 +1,27 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; + +namespace Detached.Model.EntityFramework.Options +{ + public class ModelCustomizer : IModelCustomizer + where TDbContext : DbContext + { + readonly IEnumerable> _setups; + + public ModelCustomizer(IEnumerable> modelSetup) + { + _setups = modelSetup; + } + + public void Customize(ModelBuilder modelBuilder, DbContext context) + { + if (_setups != null) + { + foreach (var modelSetup in _setups) + { + modelSetup.ConfigureModel(modelBuilder, (TDbContext)context); + } + } + } + } +} \ No newline at end of file diff --git a/src/Detached.Model.EntityFramework/Options/ModelDbContextOptionsExtensions.cs b/src/Detached.Model.EntityFramework/Options/ModelDbContextOptionsExtensions.cs new file mode 100644 index 0000000..b069db4 --- /dev/null +++ b/src/Detached.Model.EntityFramework/Options/ModelDbContextOptionsExtensions.cs @@ -0,0 +1,29 @@ +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.Extensions.DependencyInjection; + +namespace Detached.Model.EntityFramework.Options +{ + public class ModelDbContextOptionsExtension : IDbContextOptionsExtension + { + readonly IModelCustomizer _modelCustomizer; + + public ModelDbContextOptionsExtension(Type dbContextType, IModelCustomizer modelCustomizer) + { + Info = new ModelDbContextOptionsExtensionInfo(this); + + _modelCustomizer = modelCustomizer; + } + + public DbContextOptionsExtensionInfo Info { get; } + + + public void ApplyServices(IServiceCollection services) + { + services.AddSingleton(_modelCustomizer); + } + + public void Validate(IDbContextOptions options) + { + } + } +} \ No newline at end of file diff --git a/src/Detached.Model.EntityFramework/Options/ModelDbContextOptionsExtensionsInfo.cs b/src/Detached.Model.EntityFramework/Options/ModelDbContextOptionsExtensionsInfo.cs new file mode 100644 index 0000000..718611f --- /dev/null +++ b/src/Detached.Model.EntityFramework/Options/ModelDbContextOptionsExtensionsInfo.cs @@ -0,0 +1,28 @@ +using Microsoft.EntityFrameworkCore.Infrastructure; + +namespace Detached.Model.EntityFramework.Options +{ + public class ModelDbContextOptionsExtensionInfo : DbContextOptionsExtensionInfo + { + public ModelDbContextOptionsExtensionInfo(ModelDbContextOptionsExtension extension) + : base(extension) + { + TypedExtension = extension; + } + + public override bool IsDatabaseProvider => false; + + public override string LogFragment => "SailMapper"; + + public ModelDbContextOptionsExtension TypedExtension { get; } + + public override int GetServiceProviderHashCode() => 0; + + public override void PopulateDebugInfo(IDictionary debugInfo) + { + + } + + public override bool ShouldUseSameServiceProvider(DbContextOptionsExtensionInfo other) => true; + } +} \ No newline at end of file diff --git a/src/Detached.Model.EntityFramework/Package.cs b/src/Detached.Model.EntityFramework/Package.cs new file mode 100644 index 0000000..6a0f684 --- /dev/null +++ b/src/Detached.Model.EntityFramework/Package.cs @@ -0,0 +1,32 @@ +using Detached.Model.EntityFramework.Options; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.Extensions.DependencyInjection; + +namespace Detached.Model.EntityFramework +{ + public static class Package + { + public static DbContextOptionsBuilder UseModel(this DbContextOptionsBuilder dbContextBuilder, IServiceProvider serviceProvider) + { + var builder = (IDbContextOptionsBuilderInfrastructure)dbContextBuilder; + var dbContextType = dbContextBuilder.Options.ContextType; + var setupType = typeof(IModelConfiguration<>).MakeGenericType(dbContextType); + var setups = serviceProvider.GetServices(setupType); + var customizerType = typeof(ModelCustomizer<>).MakeGenericType(dbContextType); + var customizer = (IModelCustomizer)Activator.CreateInstance(customizerType, setups)!; + + builder.AddOrUpdateExtension(new ModelDbContextOptionsExtension(dbContextType, customizer)); + + return dbContextBuilder; + } + + public static IServiceCollection ConfigureModel(this IServiceCollection services, Action configure) + where TDbContext : DbContext + { + services.AddTransient(sp => new DelegateModelConfiguration(configure)); + + return services; + } + } +}