Skip to content

Commit

Permalink
modularized mapper configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardoporro committed Dec 6, 2024
1 parent 5017590 commit 65ef013
Show file tree
Hide file tree
Showing 23 changed files with 143 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<TargetFrameworks>net6.0;net8.0;net9.0</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<LangVersion>13</LangVersion>
</PropertyGroup>

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

namespace Detached.Mappers.EntityFramework.Options
{
public class DelegateMapperConfiguration<TDbContext>(Action<EntityMapperOptionsBuilder> config) : IMapperConfiguration<TDbContext>
where TDbContext : DbContext
{
public void ConfigureMapper(EntityMapperOptionsBuilder mapper)
{
config(mapper);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Microsoft.EntityFrameworkCore;

namespace Detached.Mappers.EntityFramework.Options
{
public interface IMapperConfiguration
{
void ConfigureMapper(EntityMapperOptionsBuilder mapper);
}

public interface IMapperConfiguration<TDbContext> : IMapperConfiguration
where TDbContext : DbContext
{
}
}
29 changes: 26 additions & 3 deletions src/Detached.Mappers.EntityFramework/Package.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@
using Detached.Mappers.EntityFramework.Options;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using System;
using Microsoft.Extensions.DependencyInjection;

namespace Detached.Mappers.EntityFramework
{
public static class Package
{
public static DbContextOptionsBuilder UseMapping(this DbContextOptionsBuilder dbContextBuilder, Action<EntityMapperOptions> configure = null)
public static DbContextOptionsBuilder UseMapping(this DbContextOptionsBuilder dbContextBuilder, Action<EntityMapperOptionsBuilder>? configure = null)
{
var options = new EntityMapperOptions();

configure?.Invoke(options);
configure?.Invoke(new EntityMapperOptionsBuilder());

UseMapping(dbContextBuilder, options);

Expand All @@ -27,5 +27,28 @@ public static DbContextOptionsBuilder UseMapping(this DbContextOptionsBuilder db

return dbContextBuilder;
}

public static DbContextOptionsBuilder UseMapping(this DbContextOptionsBuilder dbContextBuilder, IServiceProvider serviceProvider)
{
var builder = new EntityMapperOptionsBuilder();

var configType = typeof(IMapperConfiguration<>).MakeGenericType(dbContextBuilder.Options.ContextType);

foreach (IMapperConfiguration? configService in serviceProvider.GetServices(configType))
{
configService?.ConfigureMapper(builder);
}

UseMapping(dbContextBuilder, builder.Options);

return dbContextBuilder;
}

public static IServiceCollection ConfigureMapper<TDbContext>(this IServiceCollection services, Action<EntityMapperOptionsBuilder> config)
where TDbContext : DbContext
{
services.AddTransient<IMapperConfiguration<TDbContext>>(sp => new DelegateMapperConfiguration<TDbContext>(config));
return services;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
using System.Threading.Tasks;
using Xunit;

namespace Detached.Mappers.EntityFramework.Tests.Features
namespace Detached.Mappers.EntityFramework.Tests.Configuration
{
public class ConfigureCustomMapper
public class ConfigureCustomMapperTests
{
[Fact]
public async Task configure_custom_mapper()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using Detached.Mappers.Annotations.Extensions;
using Detached.Mappers.EntityFramework.Extensions;
using Detached.Mappers.EntityFramework.Tests.Fixture;
using Detached.Mappers.Types;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Xunit;

namespace Detached.Mappers.EntityFramework.Tests.Configuration
{
public class ConfigureFromServicesTests
{
[Fact]
public void configure_mapper_from_services()
{
var services = new ServiceCollection();

services.AddDbContext<ModuleTestDbContext>((services, db) =>
{
db.UseSqlite($"DataSource=file:{nameof(configure_mapper_from_services)}?mode=memory&cache=shared");
db.UseMapping(services);
});


services.ConfigureMapper<ModuleTestDbContext>(builder =>
{
builder.Type<ModuleTestClass>().Entity(true);
builder.Type<ModuleTestClass>().Member(c => c.CustomizedKey1).Key(true);
builder.Type<ModuleTestClass>().Member(c => c.CustomizedKey2).Key(true);
builder.Type<ModuleTestClass>().Key(c => c.CustomizedKey1, c => c.CustomizedKey2);
});

var serviceProvider = services.BuildServiceProvider();

var dbContext = serviceProvider.GetRequiredService<ModuleTestDbContext>();

IType typeOptions = dbContext.GetMapper().Options.GetType(typeof(ModuleTestClass));

Assert.True(typeOptions.IsEntity());
Assert.True(typeOptions.GetMember(nameof(ModuleTestClass.CustomizedKey1)).IsKey());
Assert.True(typeOptions.GetMember(nameof(ModuleTestClass.CustomizedKey2)).IsKey());
Assert.False(typeOptions.GetMember(nameof(ModuleTestClass.Id)).IsKey());
}
}

public class ModuleTestClass
{
public int Id { get; set; }

public int CustomizedKey1 { get; set; }

public int CustomizedKey2 { get; set; }

public string Name { get; set; }
}

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

protected override void OnModelCreating(ModelBuilder mb)
{
mb.Entity<ModuleTestClass>().HasKey(c => new { c.CustomizedKey1, c.CustomizedKey2 });
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
using System.Threading.Tasks;
using Xunit;

namespace Detached.Mappers.EntityFramework.Tests.Features
namespace Detached.Mappers.EntityFramework.Tests.Configuration
{
public class ConfigureProfiles
public class ConfigureProfilesTests
{
[Fact]
public async Task configure_profiles_profile1()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
using System.Threading.Tasks;
using Xunit;

namespace Detached.Mappers.EntityFramework.Tests.Features
namespace Detached.Mappers.EntityFramework.Tests.Configuration
{
public class ConfigureTypeFluent
public class ConfigureTypeFluentTests
{
[Fact]
public async Task apply_conventions_to_fluent()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace Detached.Mappers.EntityFramework.Tests.Features
{
public class MapAssociationIList
public class AssociationIListTests
{
[Fact]
public async Task map_association_ilist()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace Detached.Mappers.EntityFramework.Tests.Features
{
public class MapAssociationNoSetter
public class AssociationNoSetterTests
{
[Fact]
public async Task map_association_nosetter()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

namespace Detached.Mappers.EntityFramework.Tests.Features
{
public class MapAssociation
public class AssociationTests
{
[Fact]
public async Task map_association()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

namespace Detached.Mappers.EntityFramework.Tests.Features
{
public class MapMany
public class BatchTests
{
[Fact]
public async Task map_many()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

namespace Detached.Mappers.EntityFramework.Tests.Features
{
public class MapDtoToEntity
public class DtoToEntityTests
{
[Fact]
public async Task map_dto_to_entity()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace Detached.Mappers.EntityFramework.Tests.Features
{
public class MapEntityNoKey
public class EntityNoKeyTests
{
[Fact]
public async Task map_entity_nokey_dto()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace Detached.Mappers.EntityFramework.Tests.Features
{
public class MapEntityNullableKey
public class EntityNullableKeyTests
{
[Fact]
public async Task map_entity_nullable_key()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace Detached.Mappers.EntityFramework.Tests.Features
{
public class MapForeignKeyLongNameToEntity
public class ForeignKeyLongNameToEntityTests
{
[Fact]
public async Task map_fk_longname_to_entity()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace Detached.Mappers.EntityFramework.Tests.Features
{
public class MapForeignKeyToEntity
public class ForeignKeyToEntityTests
{
[Fact]
public async Task map_fk_to_entity()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

namespace Detached.Mappers.EntityFramework.Tests.Features
{
public class InputTests
public class FullGraphTests
{
[Fact]
public async Task map_input()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

namespace Detached.Mappers.EntityFramework.Tests.Features
{
public class ImportJson
public class ImportJsonTests
{
[Fact]
public async Task map_json_string()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace Detached.Mappers.EntityFramework.Tests.Features
{
public class MapInheritedEntity
public class InheritedEntityTests
{
[Fact]
public async Task map_inherited_entity()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

namespace Detached.Mappers.EntityFramework.Tests.Features
{
public class MapMemberWithValueConverter
public class MemberWithValueConverterTests
{
[Fact]
public async Task map_meber_with_value_converter()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace Detached.Mappers.EntityFramework.Tests.Features
{
public class MapOwnedEntity
public class OwnedEntityTests
{
[Fact]
public async Task map_owned_entity()
Expand Down

0 comments on commit 65ef013

Please sign in to comment.