11using Mapster . Enums ;
22using Mapster . Models ;
3+ using Mapster . Utils ;
34using System . Collections . Generic ;
45using System . Diagnostics . CodeAnalysis ;
56using System . Linq ;
67using System . Linq . Expressions ;
7- using Mapster ;
88
99namespace Mapster . EFCore
1010{
@@ -13,9 +13,7 @@ public static class EFCoreExtensions
1313 public static IQueryable < TDestination > EFCoreProjectToType < TDestination > ( this IQueryable source ,
1414 TypeAdapterConfig ? config = null , ProjectToTypeAutoMapping autoMapConfig = ProjectToTypeAutoMapping . WithoutCollections )
1515 {
16-
17- var allInclude = new IncludeVisiter ( ) ;
18-
16+ var allInclude = new IncludeVisitor ( ) ;
1917 allInclude . Visit ( source . Expression ) ;
2018
2119 if ( config == null )
@@ -27,13 +25,9 @@ public static IQueryable<TDestination> EFCoreProjectToType<TDestination>(this IQ
2725
2826 var mapTuple = new TypeTuple ( source . ElementType , typeof ( TDestination ) ) ;
2927
30-
3128 TypeAdapterRule rule ;
32-
3329 config . RuleMap . TryGetValue ( mapTuple , out rule ) ;
3430
35-
36-
3731 if ( rule != null )
3832 {
3933 rule . Settings . ProjectToTypeMapConfig = autoMapConfig ;
@@ -48,113 +42,55 @@ public static IQueryable<TDestination> EFCoreProjectToType<TDestination>(this IQ
4842 }
4943 else
5044 rule . Settings . ProjectToTypeResolvers . TryAdd ( item . Key , item . Value ) ;
51-
52-
5345 }
54-
5546 }
56-
57-
5847 }
5948 else
6049 {
6150 config = config . Clone ( )
6251 . ForType ( source . ElementType , typeof ( TDestination ) )
6352 . Config ;
6453 }
65-
66-
54+
6755 return source . ProjectToType < TDestination > ( config ) ;
68-
6956 }
70-
7157 }
7258
73- internal class IncludeVisiter : ExpressionVisitor
59+
60+ internal class IncludeVisitor : ExpressionVisitor
7461 {
62+ public Dictionary < string , UnaryExpression > IncludeExpression { get ; protected set ; } = new ( ) ;
63+ private bool IsInclude ( Expression node ) => node . Type . Name . StartsWith ( "IIncludableQueryable" ) ;
64+
7565 [ return : NotNullIfNotNull ( "node" ) ]
7666 public override Expression Visit ( Expression node )
7767 {
7868 if ( node == null )
7969 return null ;
70+
8071 switch ( node . NodeType )
8172 {
8273 case ExpressionType . Call :
8374 {
84- if ( IsInclude ( node ) )
75+ if ( IsInclude ( node ) )
8576 {
8677 var QuoteVisiter = new QuoteVisiter ( ) ;
8778 QuoteVisiter . Visit ( node ) ;
8879
8980 foreach ( var item in QuoteVisiter . Quotes )
9081 {
91- var memberv = new TopLevelMemeberNameVisiter ( ) ;
82+ var memberv = new TopLevelMemberNameVisitor ( ) ;
9283 memberv . Visit ( item ) ;
9384
94- IncludeExpression . TryAdd ( memberv . Key , item ) ;
95-
85+ IncludeExpression . TryAdd ( memberv . MemeberName , item ) ;
9686 }
97-
98-
9987 }
10088 return base . Visit ( node ) ;
10189 }
10290 }
10391
10492 return base . Visit ( node ) ;
10593 }
106-
107- public Dictionary < string , UnaryExpression > IncludeExpression { get ; protected set ; } = new ( ) ;
108- private bool IsInclude ( Expression node ) => node . Type . Name . StartsWith ( "IIncludableQueryable" ) ;
109-
11094 }
11195
112- internal class QuoteVisiter : ExpressionVisitor
113- {
114- public List < UnaryExpression > Quotes { get ; private set ; } = new ( ) ;
115-
116- public override Expression Visit ( Expression node )
117- {
118- if ( node == null )
119- return null ;
120- switch ( node . NodeType )
121- {
122- case ExpressionType . Quote :
123- {
124- Quotes . Add ( ( UnaryExpression ) node ) ;
125- return base . Visit ( node ) ;
126- }
127- }
128-
129- return base . Visit ( node ) ;
130-
131- }
132- }
133-
134- internal class TopLevelMemeberNameVisiter : ExpressionVisitor
135- {
136- public string ? Key { get ; private set ; }
137-
138- public override Expression Visit ( Expression node )
139- {
140-
141-
142- if ( node == null )
143- return null ;
144- switch ( node . NodeType )
145- {
146- case ExpressionType . MemberAccess :
147- {
148- if ( string . IsNullOrEmpty ( Key ) )
149- Key = ( ( MemberExpression ) node ) . Member . Name ;
150-
151- return base . Visit ( node ) ;
152- }
153- }
154-
155- return base . Visit ( node ) ;
156-
157-
158- }
159- }
16096}
0 commit comments