Skip to content

Commit

Permalink
fix model configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardoporro committed Dec 10, 2024
1 parent 2dd82e7 commit baca5a5
Show file tree
Hide file tree
Showing 12 changed files with 173 additions and 18 deletions.
32 changes: 23 additions & 9 deletions Detached.sln
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,9 @@ 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}"
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Models", "Models", "{FEB397C1-A23C-47E5-9615-1E316EDF122E}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Actions", "Actions", "{7852C9A8-FFE3-48C9-A78C-FBC8AE17F32F}"
EndProject
Expand All @@ -46,14 +44,20 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Actions", "Actions", "{5797
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}"
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Models", "Models", "{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
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Detached.Models.EntityFramework", "src\Detached.Models.EntityFramework\Detached.Models.EntityFramework.csproj", "{DBFE470C-065E-4974-8A04-BECF3E9E7419}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Detached.Models", "src\Detached.Models\Detached.Models.csproj", "{89E57324-6977-4F8F-B47E-E443956D3706}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Detached.Models.EntityFramework.Tests", "test\Detached.Models.EntityFramework.Tests\Detached.Models.EntityFramework.Tests.csproj", "{D8973CA5-311B-45F9-BA51-9EF10C7C6AF9}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -92,10 +96,18 @@ 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
{DBFE470C-065E-4974-8A04-BECF3E9E7419}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DBFE470C-065E-4974-8A04-BECF3E9E7419}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DBFE470C-065E-4974-8A04-BECF3E9E7419}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DBFE470C-065E-4974-8A04-BECF3E9E7419}.Release|Any CPU.Build.0 = Release|Any CPU
{89E57324-6977-4F8F-B47E-E443956D3706}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{89E57324-6977-4F8F-B47E-E443956D3706}.Debug|Any CPU.Build.0 = Debug|Any CPU
{89E57324-6977-4F8F-B47E-E443956D3706}.Release|Any CPU.ActiveCfg = Release|Any CPU
{89E57324-6977-4F8F-B47E-E443956D3706}.Release|Any CPU.Build.0 = Release|Any CPU
{D8973CA5-311B-45F9-BA51-9EF10C7C6AF9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D8973CA5-311B-45F9-BA51-9EF10C7C6AF9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D8973CA5-311B-45F9-BA51-9EF10C7C6AF9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D8973CA5-311B-45F9-BA51-9EF10C7C6AF9}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -109,7 +121,6 @@ Global
{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}
Expand All @@ -120,6 +131,9 @@ Global
{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}
{DBFE470C-065E-4974-8A04-BECF3E9E7419} = {FEB397C1-A23C-47E5-9615-1E316EDF122E}
{89E57324-6977-4F8F-B47E-E443956D3706} = {FEB397C1-A23C-47E5-9615-1E316EDF122E}
{D8973CA5-311B-45F9-BA51-9EF10C7C6AF9} = {96D60403-21AD-453A-922C-913D37BD0853}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {7FC47439-B36B-45BB-BEEC-A6D595358C63}
Expand Down
2 changes: 1 addition & 1 deletion Readme.MD
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
![Detached Banner](banner.png?raw=true)
![Detached Banner](https://raw.githubusercontent.com/leonardoporro/Detached-Mapper/refs/heads/main/banner.png?raw=true)

#### Maps Dtos directly to entities already attached to a DbContext
#### Solves disconnected entities, partial updates and automapping in a single step
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Microsoft.EntityFrameworkCore;

namespace Detached.Model.EntityFramework.Options
namespace Detached.Models.EntityFramework.Options
{
public class DelegateModelConfiguration<TDbContext>(Action<ModelBuilder, TDbContext> configure) : IModelConfiguration<TDbContext>
where TDbContext : DbContext
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Microsoft.EntityFrameworkCore;

namespace Detached.Model.EntityFramework.Options
namespace Detached.Models.EntityFramework.Options
{
public interface IModelConfiguration<TDbContext>
where TDbContext : DbContext
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;

namespace Detached.Model.EntityFramework.Options
namespace Detached.Models.EntityFramework.Options
{
public class ModelCustomizer<TDbContext> : IModelCustomizer
where TDbContext : DbContext
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.Extensions.DependencyInjection;

namespace Detached.Model.EntityFramework.Options
namespace Detached.Models.EntityFramework.Options
{
public class ModelDbContextOptionsExtension : IDbContextOptionsExtension
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Microsoft.EntityFrameworkCore.Infrastructure;

namespace Detached.Model.EntityFramework.Options
namespace Detached.Models.EntityFramework.Options
{
public class ModelDbContextOptionsExtensionInfo : DbContextOptionsExtensionInfo
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using Detached.Model.EntityFramework.Options;
using Detached.Models.EntityFramework.Options;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.Extensions.DependencyInjection;

namespace Detached.Model.EntityFramework
namespace Detached.Models.EntityFramework
{
public static class Package
{
Expand All @@ -24,7 +24,7 @@ public static DbContextOptionsBuilder UseModel(this DbContextOptionsBuilder dbCo
public static IServiceCollection ConfigureModel<TDbContext>(this IServiceCollection services, Action<ModelBuilder, TDbContext> configure)
where TDbContext : DbContext
{
services.AddTransient(sp => new DelegateModelConfiguration<TDbContext>(configure));
services.AddTransient<IModelConfiguration<TDbContext>>(sp => new DelegateModelConfiguration<TDbContext>(configure));

return services;
}
Expand Down
37 changes: 37 additions & 0 deletions src/Detached.Models/Detached.Models.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<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>

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

</Project>
71 changes: 71 additions & 0 deletions test/Detached.Models.EntityFramework.Tests/ConfigureModelTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Xunit;

namespace Detached.Models.EntityFramework.Tests
{
public class ConfigureModelTests
{
[Fact]
public void DbContext_ConfigureModel_Success()
{
var services = new ServiceCollection();

services.AddDbContext<TestDbContext>((services, options) =>
{
options.UseSqlite($"DataSource=file:test_configure_model?mode=memory&cache=shared");
options.UseModel(services);
});

services.ConfigureModel<TestDbContext>((model, dbContext) =>
{
model.Entity<Entity1>().HasKey(e => e.Id1);
});

services.ConfigureModel<TestDbContext>((model, dbContext) =>
{
model.Entity<Entity2>().HasKey(e => e.Id2);
});

var serviceProvider = services.BuildServiceProvider();

var dbContext = serviceProvider.GetRequiredService<TestDbContext>();
dbContext.Database.EnsureCreated();
dbContext.Set<Entity1>().Add(new Entity1 { Id1 = 1, Name = "Entity 1" });
dbContext.Set<Entity2>().Add(new Entity2 { Id2 = 2, Name = "Entity 2" });

dbContext.SaveChanges();

var entity1 = dbContext.Find<Entity1>(1);
var entity2 = dbContext.Find<Entity2>(2);

Assert.NotNull(entity1);
Assert.Equal("Entity 1", entity1.Name);

Assert.NotNull(entity2);
Assert.Equal("Entity 2", entity2.Name);
}
}

public class TestDbContext : DbContext
{
public TestDbContext(DbContextOptions<TestDbContext> options)
: base(options)
{
}
}

public class Entity1
{
public int Id1 { get; set; }

public string Name { get; set; }
}

public class Entity2
{
public int Id2 { get; set; }

public string Name { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Detached.Models.EntityFramework\Detached.Models.EntityFramework.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="9.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="9.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageReference Include="xunit" Version="2.9.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<Folder Include="Fixture\" />
</ItemGroup>


</Project>

0 comments on commit baca5a5

Please sign in to comment.