-
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.
- Loading branch information
1 parent
5017590
commit 65ef013
Showing
23 changed files
with
143 additions
and
23 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
13 changes: 13 additions & 0 deletions
13
src/Detached.Mappers.EntityFramework/Options/DelegateMapperConfiguration.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.Mappers.EntityFramework.Options | ||
{ | ||
public class DelegateMapperConfiguration<TDbContext>(Action<EntityMapperOptionsBuilder> config) : IMapperConfiguration<TDbContext> | ||
where TDbContext : DbContext | ||
{ | ||
public void ConfigureMapper(EntityMapperOptionsBuilder mapper) | ||
{ | ||
config(mapper); | ||
} | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/Detached.Mappers.EntityFramework/Options/IMapperConfiguration.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,14 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace Detached.Mappers.EntityFramework.Options | ||
{ | ||
public interface IMapperConfiguration | ||
{ | ||
void ConfigureMapper(EntityMapperOptionsBuilder mapper); | ||
} | ||
|
||
public interface IMapperConfiguration<TDbContext> : IMapperConfiguration | ||
where TDbContext : DbContext | ||
{ | ||
} | ||
} |
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
69 changes: 69 additions & 0 deletions
69
test/Detached.Mappers.EntityFramework.Tests/Configuration/ConfigureFromServicesTests.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,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 }); | ||
} | ||
} | ||
} |
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
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
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
File renamed without changes.
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
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
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
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
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