Skip to content

Commit

Permalink
model integration + starting what's new!
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardoporro committed Dec 9, 2024
1 parent 65ef013 commit 2dd82e7
Show file tree
Hide file tree
Showing 11 changed files with 240 additions and 15 deletions.
51 changes: 44 additions & 7 deletions Detached.Mappers.sln → Detached.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down
3 changes: 1 addition & 2 deletions src/Detached.Mappers.EntityFramework/Package.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net6.0;net8.0;net9.0</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<LangVersion>13</LangVersion>
</PropertyGroup>

<PropertyGroup>
<Version>$(Version)</Version>
<Authors>Leonardo Porro</Authors>
<Company />
<Product>Detached</Product>
<Description>A general purpose object-oriented mapper.</Description>
<Copyright>2017</Copyright>
<PackageProjectUrl>https://github.com/leonardoporro/Detached</PackageProjectUrl>
<PackageLicenseExpression>(LGPL-2.0-only WITH FLTK-exception OR Apache-2.0+)</PackageLicenseExpression>
<NeutralLanguage />
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageIcon>logo.png</PackageIcon>
<PackageId>Detached.Mappers.EntityFramework</PackageId>
<PackageReadmeFile>Readme.MD</PackageReadmeFile>
</PropertyGroup>

<PropertyGroup>
<NoWarn>IDE0059,EF1001</NoWarn>
</PropertyGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net6.0'">
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.*" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net8.0'">
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.*" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net9.0'">
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.*" />
</ItemGroup>

<ItemGroup>
<None Include="..\..\logo.png">
<Pack>True</Pack>
<PackagePath></PackagePath>
</None>
<None Include="..\..\Readme.MD">
<Pack>True</Pack>
<PackagePath>\</PackagePath>
</None>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Microsoft.EntityFrameworkCore;

namespace Detached.Model.EntityFramework.Options
{
public class DelegateModelConfiguration<TDbContext>(Action<ModelBuilder, TDbContext> configure) : IModelConfiguration<TDbContext>
where TDbContext : DbContext
{
public void ConfigureModel(ModelBuilder model, TDbContext dbContext)
{
configure(model, dbContext);
}
}
}
10 changes: 10 additions & 0 deletions src/Detached.Model.EntityFramework/Options/IModelConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Microsoft.EntityFrameworkCore;

namespace Detached.Model.EntityFramework.Options
{
public interface IModelConfiguration<TDbContext>
where TDbContext : DbContext
{
void ConfigureModel(ModelBuilder model, TDbContext dbContext);
}
}
27 changes: 27 additions & 0 deletions src/Detached.Model.EntityFramework/Options/ModelCustomizer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;

namespace Detached.Model.EntityFramework.Options
{
public class ModelCustomizer<TDbContext> : IModelCustomizer
where TDbContext : DbContext
{
readonly IEnumerable<IModelConfiguration<TDbContext>> _setups;

public ModelCustomizer(IEnumerable<IModelConfiguration<TDbContext>> modelSetup)
{
_setups = modelSetup;
}

public void Customize(ModelBuilder modelBuilder, DbContext context)
{
if (_setups != null)
{
foreach (var modelSetup in _setups)
{
modelSetup.ConfigureModel(modelBuilder, (TDbContext)context);
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -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)
{
}
}
}
Original file line number Diff line number Diff line change
@@ -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<string, string> debugInfo)
{

}

public override bool ShouldUseSameServiceProvider(DbContextOptionsExtensionInfo other) => true;
}
}
32 changes: 32 additions & 0 deletions src/Detached.Model.EntityFramework/Package.cs
Original file line number Diff line number Diff line change
@@ -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<TDbContext>(this IServiceCollection services, Action<ModelBuilder, TDbContext> configure)
where TDbContext : DbContext
{
services.AddTransient(sp => new DelegateModelConfiguration<TDbContext>(configure));

return services;
}
}
}

0 comments on commit 2dd82e7

Please sign in to comment.