-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
model integration + starting what's new!
- Loading branch information
1 parent
65ef013
commit 2dd82e7
Showing
11 changed files
with
240 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 2 additions & 4 deletions
6
.../EntityMapperDbContextOptionsExtension.cs → .../EntityMapperDbContextOptionsExtension.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 1 addition & 2 deletions
3
...ityMapperDbContextOptionsExtensionInfo.cs → ...ityMapperDbContextOptionsExtensionInfo.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
src/Detached.Model.EntityFramework/Detached.Model.EntityFramework.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
13 changes: 13 additions & 0 deletions
13
src/Detached.Model.EntityFramework/Options/DelegateModelConfiguration.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
10
src/Detached.Model.EntityFramework/Options/IModelConfiguration.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
27
src/Detached.Model.EntityFramework/Options/ModelCustomizer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/Detached.Model.EntityFramework/Options/ModelDbContextOptionsExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
{ | ||
} | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/Detached.Model.EntityFramework/Options/ModelDbContextOptionsExtensionsInfo.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |