|
| 1 | +using Mapster.Enums; |
| 2 | +using Mapster.Models; |
| 3 | +using Mapster.Utils; |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.Diagnostics.CodeAnalysis; |
| 6 | +using System.Linq; |
| 7 | +using System.Linq.Expressions; |
| 8 | + |
| 9 | +namespace Mapster.EFCore |
| 10 | +{ |
| 11 | + public static class EFCoreExtensions |
| 12 | + { |
| 13 | + public static IQueryable<TDestination> EFCoreProjectToType<TDestination>(this IQueryable source, |
| 14 | + TypeAdapterConfig? config = null, ProjectToTypeAutoMapping autoMapConfig = ProjectToTypeAutoMapping.WithoutCollections) |
| 15 | + { |
| 16 | + var allInclude = new IncludeVisitor(); |
| 17 | + allInclude.Visit(source.Expression); |
| 18 | + |
| 19 | + if (config == null) |
| 20 | + { |
| 21 | + config = TypeAdapterConfig.GlobalSettings |
| 22 | + .Clone() |
| 23 | + .ForType(source.ElementType, typeof(TDestination)) |
| 24 | + .Config; |
| 25 | + |
| 26 | + var mapTuple = new TypeTuple(source.ElementType, typeof(TDestination)); |
| 27 | + |
| 28 | + TypeAdapterRule rule; |
| 29 | + config.RuleMap.TryGetValue(mapTuple, out rule); |
| 30 | + |
| 31 | + if(rule != null) |
| 32 | + { |
| 33 | + rule.Settings.ProjectToTypeMapConfig = autoMapConfig; |
| 34 | + |
| 35 | + foreach (var item in allInclude.IncludeExpression) |
| 36 | + { |
| 37 | + var find = rule.Settings.Resolvers.Find(x => x.SourceMemberName == item.Key); |
| 38 | + if (find != null) |
| 39 | + { |
| 40 | + find.Invoker = (LambdaExpression)item.Value.Operand; |
| 41 | + find.SourceMemberName = null; |
| 42 | + } |
| 43 | + else |
| 44 | + rule.Settings.ProjectToTypeResolvers.TryAdd(item.Key, item.Value); |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + else |
| 49 | + { |
| 50 | + config = config.Clone() |
| 51 | + .ForType(source.ElementType, typeof(TDestination)) |
| 52 | + .Config; |
| 53 | + } |
| 54 | + |
| 55 | + return source.ProjectToType<TDestination>(config); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + |
| 60 | + internal class IncludeVisitor : ExpressionVisitor |
| 61 | + { |
| 62 | + public Dictionary<string, UnaryExpression> IncludeExpression { get; protected set; } = new(); |
| 63 | + private bool IsInclude(Expression node) => node.Type.Name.StartsWith("IIncludableQueryable"); |
| 64 | + |
| 65 | + [return: NotNullIfNotNull("node")] |
| 66 | + public override Expression Visit(Expression node) |
| 67 | + { |
| 68 | + if (node == null) |
| 69 | + return null; |
| 70 | + |
| 71 | + switch (node.NodeType) |
| 72 | + { |
| 73 | + case ExpressionType.Call: |
| 74 | + { |
| 75 | + if (IsInclude(node)) |
| 76 | + { |
| 77 | + var QuoteVisiter = new QuoteVisitor(); |
| 78 | + QuoteVisiter.Visit(node); |
| 79 | + |
| 80 | + foreach (var item in QuoteVisiter.Quotes) |
| 81 | + { |
| 82 | + var memberv = new TopLevelMemberNameVisitor(); |
| 83 | + memberv.Visit(item); |
| 84 | + |
| 85 | + IncludeExpression.TryAdd(memberv.MemeberName, item); |
| 86 | + } |
| 87 | + } |
| 88 | + return base.Visit(node); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + return base.Visit(node); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | +} |
0 commit comments