-
Notifications
You must be signed in to change notification settings - Fork 2.4k
add options to disable and override ExplicitExpansion #4327
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
lbargaoanu
merged 10 commits into
LuckyPennySoftware:basic_include
from
Angelinsky7:disable_explicit_expansion
Aug 21, 2023
+160
−6
Merged
Changes from 8 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
dd6b4ce
add options to disable and override ExplicitExpansion
Angelinsky7 7dfb089
rename field
Angelinsky7 339d86b
add inheritance test
Angelinsky7 bba6731
add constructor test
Angelinsky7 eed5dfb
bumb: restore compat baseline
Angelinsky7 bc835bf
remove override and corrected test
Angelinsky7 389b22b
Delete SqlLocalDB.msi
Angelinsky7 328308b
Delete SqlLocalDBInstall.log
Angelinsky7 affeed4
test: add constructor mapping with inheritance
Angelinsky7 0b9fbce
remove the ad-hoc stuff
lbargaoanu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
111 changes: 111 additions & 0 deletions
111
src/IntegrationTests/ExplicitExpansion/DisableExplicitExpansion.cs
This file contains hidden or 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,111 @@ | ||
| using System.Text.RegularExpressions; | ||
|
|
||
| namespace AutoMapper.IntegrationTests.ExplicitExpansion; | ||
|
|
||
| public class DisableExplicitExpansion : IntegrationTest<DisableExplicitExpansion.DatabaseInitializer> | ||
| { | ||
| public class Source | ||
| { [Key, DatabaseGenerated(DatabaseGeneratedOption.None)] | ||
| public Int32 Desc { get; set; } | ||
| public String Name { get; set; } | ||
| public String Code { get; set; } | ||
| } | ||
| public class Dto | ||
| { | ||
| public String Name { get; set; } | ||
| public String Code { get; set; } | ||
| public Nullable<int> Desc { get; set; } | ||
| } | ||
|
|
||
| public class Context : LocalDbContext | ||
| { | ||
| public List<string> Log = new List<string>(); | ||
|
|
||
| protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) | ||
| { | ||
| optionsBuilder.LogTo(s => Log.Add(s)); | ||
| base.OnConfiguring(optionsBuilder); | ||
| } | ||
|
|
||
| public DbSet<Source> Sources { get; set; } | ||
|
|
||
| public string GetLastSelectSqlLogEntry() => Log.LastOrDefault(_ => _.Contains("SELECT")); | ||
| } | ||
|
|
||
| private static readonly IQueryable<Source> _iq = new List<Source> { | ||
| new Source() { Name = "Name1", Code = "Code1", Desc = -12 }, | ||
| } .AsQueryable(); | ||
|
|
||
| private static readonly Source _iqf = _iq.First(); | ||
|
|
||
| public class DatabaseInitializer : DropCreateDatabaseAlways<Context> | ||
| { | ||
| protected override void Seed(Context context) | ||
| { | ||
| context.Sources.Add(_iqf); | ||
| base.Seed(context); | ||
| } | ||
| } | ||
|
|
||
| protected override MapperConfiguration CreateConfiguration() => new(cfg => | ||
| { | ||
| cfg.CreateMap<Source, Dto>() | ||
| .ForMember(dto => dto.Code, conf => conf.ExplicitExpansion(false)) | ||
| .ForAllMembers(conf => conf.ExplicitExpansion()) | ||
| ;}); | ||
|
|
||
| [Fact] | ||
| public void Should_CodeBeExpanded() { | ||
| using (var ctx = new Context()) { | ||
| var dto = ProjectTo<Dto>(ctx.Sources).ToList().First(); | ||
| var sqlSelect = ctx.GetLastSelectSqlLogEntry(); | ||
| sqlSelect.SqlFromShouldStartWith(nameof(ctx.Sources)); | ||
| sqlSelect.ShouldNotContain("JOIN"); | ||
|
|
||
| sqlSelect.SqlShouldNotSelectColumn(nameof(_iqf.Name)); dto.Name.ShouldBeNull(); | ||
| sqlSelect.SqlShouldSelectColumn(nameof(_iqf.Code)); dto.Code.ShouldBe(_iqf.Code); | ||
| sqlSelect.SqlShouldNotSelectColumn(nameof(_iqf.Desc)); dto.Desc.ShouldBeNull(); | ||
| } | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Should_NameAndCodeBeExpanded() { | ||
| using (var ctx = new Context()) { | ||
| var dto = ProjectTo<Dto>(ctx.Sources, null, _ => _.Name).First(); | ||
| var sqlSelect = ctx.GetLastSelectSqlLogEntry(); | ||
| sqlSelect.SqlFromShouldStartWith(nameof(ctx.Sources)); | ||
| sqlSelect.ShouldNotContain("JOIN"); | ||
|
|
||
| dto.Name.ShouldBe(_iqf.Name); sqlSelect.SqlShouldSelectColumn(nameof(_iqf.Name)); | ||
| dto.Code.ShouldBe(_iqf.Code); sqlSelect.SqlShouldSelectColumn(nameof(_iqf.Code)); | ||
| dto.Desc.ShouldBeNull(); sqlSelect.SqlShouldNotSelectColumn(nameof(_iqf.Desc)); | ||
| } | ||
| } | ||
|
|
||
| } | ||
|
|
||
| public class ConstructorNoExplicitExpansion : IntegrationTest<ConstructorNoExplicitExpansion.DatabaseInitializer> { | ||
| public class Entity { | ||
| public int Id { get; set; } | ||
| public string Name { get; set; } | ||
| } | ||
| record Dto(string Name) { } | ||
| public class Context : LocalDbContext { | ||
| public DbSet<Entity> Entities { get; set; } | ||
| } | ||
| public class DatabaseInitializer : DropCreateDatabaseAlways<Context> { | ||
| protected override void Seed(Context context) { | ||
| context.Entities.Add(new() { Name = "Name" }); | ||
| base.Seed(context); | ||
| } | ||
| } | ||
| protected override MapperConfiguration CreateConfiguration() => new(c => c.CreateProjection<Entity, Dto>().ForCtorParam("Name", o => o.ExplicitExpansion(false))); | ||
| [Fact] | ||
| public void Should_work() { | ||
| using var context = new Context(); | ||
| var dto = ProjectTo<Dto>(context.Entities).Single(); | ||
| dto.Name.ShouldBe("Name"); | ||
| dto = ProjectTo<Dto>(context.Entities, null, d => d.Name).Single(); | ||
| dto.Name.ShouldBe("Name"); | ||
| } | ||
| } | ||
110 changes: 110 additions & 0 deletions
110
src/IntegrationTests/ExplicitExpansion/ExpandCollectionsWithoutExplicit.cs
This file contains hidden or 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,110 @@ | ||
| namespace AutoMapper.IntegrationTests.ExplicitExpansion; | ||
|
|
||
| public class ExpandCollectionsWithoutExplicit : IntegrationTest<ExpandCollectionsWithoutExplicit.DatabaseInitializer> | ||
| { | ||
| TrainingCourseDto _course; | ||
|
|
||
| protected override MapperConfiguration CreateConfiguration() => new(cfg => | ||
| { | ||
| cfg.CreateMap<Category, CategoryDto>(); | ||
| cfg.CreateMap<TrainingCourse, TrainingCourseDto>() | ||
| .ForMember(p => p.CourseName, opt => opt.ExplicitExpansion()); | ||
| cfg.CreateMap<TrainingContent, TrainingContentDto>().ForMember(c => c.Category, o => o.ExplicitExpansion()); | ||
| cfg.CreateMap<TrainingCourse, TrainingCourseDetailDto>() | ||
| .IncludeBase<TrainingCourse, TrainingCourseDto>() | ||
| .ForMember(c => c.CourseName, opt => opt.ExplicitExpansion(false)); | ||
| }); | ||
|
|
||
| [Fact] | ||
| public void Should_notexpand_courseName() { | ||
| using (var context = new ClientContext()) { | ||
| _course = ProjectTo<TrainingCourseDto>(context.TrainingCourses).FirstOrDefault(); | ||
| } | ||
| _course.CourseName.ShouldBeNull(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Should_expand_courseName() { | ||
| using (var context = new ClientContext()) { | ||
| _course = ProjectTo<TrainingCourseDetailDto>(context.TrainingCourses).FirstOrDefault(); | ||
| } | ||
| _course.CourseName.ShouldBe("Course 1"); | ||
| } | ||
|
|
||
| public class DatabaseInitializer : DropCreateDatabaseAlways<ClientContext> | ||
| { | ||
| protected override void Seed(ClientContext context) | ||
| { | ||
| var category = new Category { CategoryName = "Category 1" }; | ||
| var course = new TrainingCourse { CourseName = "Course 1" }; | ||
| context.TrainingCourses.Add(course); | ||
| var content = new TrainingContent { ContentName = "Content 1", Category = category }; | ||
| context.TrainingContents.Add(content); | ||
| course.Content.Add(content); | ||
| } | ||
| } | ||
|
|
||
| public class ClientContext : LocalDbContext | ||
| { | ||
| public DbSet<Category> Categories { get; set; } | ||
| public DbSet<TrainingCourse> TrainingCourses { get; set; } | ||
| public DbSet<TrainingContent> TrainingContents { get; set; } | ||
| } | ||
|
|
||
| public class TrainingCourse | ||
| { | ||
| [Key] | ||
| public int CourseId { get; set; } | ||
|
|
||
| public string CourseName { get; set; } | ||
|
|
||
| public virtual IList<TrainingContent> Content { get; set; } = new List<TrainingContent>(); | ||
| } | ||
|
|
||
| public class TrainingContent | ||
| { | ||
| [Key] | ||
| public int ContentId { get; set; } | ||
|
|
||
| public string ContentName { get; set; } | ||
| public string CaptionName { get; set; } | ||
|
|
||
| public Category Category { get; set; } | ||
| } | ||
|
|
||
| public class Category | ||
| { | ||
| public int CategoryId { get; set; } | ||
| public string CategoryName { get; set; } | ||
| } | ||
|
|
||
|
|
||
| public class TrainingCourseDto | ||
| { | ||
| public int CourseId { get; set; } | ||
|
|
||
| public string CourseName { get; set; } | ||
|
|
||
| public virtual IList<TrainingContentDto> Content { get; set; } | ||
| } | ||
|
|
||
| public class TrainingCourseDetailDto : TrainingCourseDto | ||
| { | ||
| } | ||
|
|
||
| public class CategoryDto | ||
| { | ||
| public int CategoryId { get; set; } | ||
| public string CategoryName { get; set; } | ||
| } | ||
|
|
||
| public class TrainingContentDto | ||
| { | ||
| public int ContentId { get; set; } | ||
|
|
||
| public string ContentName { get; set; } | ||
|
|
||
| public CategoryDto Category { get; set; } | ||
| } | ||
|
|
||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should use mapping inheritance, the same as the test below.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@lbargaoanu i added a new test for constructor mapping with inheritance (sorry for the long delay i needed to change my computer)