-
Notifications
You must be signed in to change notification settings - Fork 527
Replacing AutoMapper with Grand.Mapping #664
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
Changes from all commits
c125792
6a15258
d32918e
5a1e007
8c9bd30
8a6f8c2
5f5d8bd
64d181c
e82c9f8
40ef74a
6e2f7d7
1bd140e
43fc9ef
7ec346e
52159fc
5e2cde0
3a4e8c6
3aff781
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| using AutoMapper; | ||
| using Grand.Mapping; | ||
|
|
||
| namespace Grand.Infrastructure.Mapper; | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
| <Import Project="..\..\Build\Grand.Common.props" /> | ||
| </Project> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| using System.Collections.Frozen; | ||
|
|
||
| namespace Grand.Mapping; | ||
|
|
||
| internal sealed class GrandMapper : IMapper | ||
| { | ||
| private readonly FrozenDictionary<(Type, Type), Delegate> _mappings; | ||
|
|
||
| internal GrandMapper(Dictionary<(Type, Type), Delegate> mappings) | ||
| => _mappings = mappings.ToFrozenDictionary(); | ||
|
|
||
| public TDest Map<TSource, TDest>(TSource source) where TDest : new() | ||
| { | ||
| return Map(source, new TDest()); | ||
| } | ||
|
|
||
| public TDest Map<TSource, TDest>(TSource source, TDest destination) | ||
| { | ||
| ArgumentNullException.ThrowIfNull(source); | ||
| ArgumentNullException.ThrowIfNull(destination); | ||
| if (_mappings.TryGetValue((typeof(TSource), typeof(TDest)), out var del)) | ||
| ((Action<TSource, TDest>)del)(source, destination); | ||
| return destination; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| namespace Grand.Mapping; | ||
|
|
||
| public interface IMapper | ||
| { | ||
| TDest Map<TSource, TDest>(TSource source) where TDest : new(); | ||
| TDest Map<TSource, TDest>(TSource source, TDest destination); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| namespace Grand.Mapping; | ||
|
|
||
| public interface IMapperConfigurationExpression | ||
| { | ||
| void AddProfile(Profile profile); | ||
| void AddProfile<T>() where T : Profile, new() => AddProfile(new T()); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| using System.Linq.Expressions; | ||
|
|
||
| namespace Grand.Mapping; | ||
|
|
||
| public interface IMappingExpression<TSource, TDest> | ||
| { | ||
| IMappingExpression<TSource, TDest> ForMember<TMember>( | ||
| Expression<Func<TDest, TMember>> destMember, | ||
| Action<IMemberConfigurationExpression<TSource, TDest, TMember>> opts); | ||
|
|
||
| IMappingExpression<TSource, TDest> ForPath<TMember>( | ||
| Expression<Func<TDest, TMember>> destPath, | ||
| Action<IMemberConfigurationExpression<TSource, TDest, TMember>> opts); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| using System.Linq.Expressions; | ||
|
|
||
| namespace Grand.Mapping; | ||
|
|
||
| public interface IMemberConfigurationExpression<TSource, TDest, TMember> | ||
|
Check warning on line 5 in src/Core/Grand.Mapping/IMemberConfigurationExpression.cs
|
||
| { | ||
| void Ignore(); | ||
| void MapFrom<TResult>(Expression<Func<TSource, TResult>> mapExpression); | ||
| void Condition(Expression<Func<TSource, bool>> condition); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| namespace Grand.Mapping.Internal; | ||
|
|
||
| internal interface IMappingConfiguration | ||
| { | ||
| (Type Source, Type Dest) GetTypes(); | ||
| Delegate CompileDelegate(HashSet<(Type, Type)> registeredTypes, Dictionary<(Type, Type), Delegate> mappings); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| namespace Grand.Mapping.Internal; | ||
|
|
||
| internal sealed class MapperConfigurationExpressionImpl : IMapperConfigurationExpression | ||
| { | ||
| private readonly List<IMappingConfiguration> _configurations = new(); | ||
|
|
||
| public void AddProfile(Profile profile) | ||
| => _configurations.AddRange(profile.GetConfigurations()); | ||
|
|
||
| internal IEnumerable<IMappingConfiguration> GetConfigurations() => _configurations; | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.