-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLLBLGenProODataServiceExpandProvider.cs
151 lines (141 loc) · 6.63 KB
/
LLBLGenProODataServiceExpandProvider.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
//////////////////////////////////////////////////////////////////////
// Part of the OData Support Classes for LLBLGen Pro.
// LLBLGen Pro is (c) 2002-2020 Solutions Design. All rights reserved.
// http://www.llblgen.com
//////////////////////////////////////////////////////////////////////
// The OData Support Classes sourcecode is released under the following license:
// --------------------------------------------------------------------------------------------
//
// The MIT License(MIT)
//
// Copyright (c)2002-2020 Solutions Design. All rights reserved.
// https://www.llblgen.com
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//////////////////////////////////////////////////////////////////////
// Contributers to the code:
// - Brian Chance
// - Frans Bouma [FB]
//////////////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Services;
using System.Collections;
using SD.LLBLGen.Pro.ORMSupportClasses;
using System.Reflection;
using SD.LLBLGen.Pro.LinqSupportClasses;
using System.Linq.Expressions;
namespace SD.LLBLGen.Pro.ODataSupportClasses
{
/// <summary>
/// Class which produces the prefetch paths for a query if the OData query contains Expand directives.
/// </summary>
/// <remarks>Code based on original templates made by Brian Chance</remarks>
[Obsolete("Starting with v4, this class is obsolete and isn't used in the service. Using it will cause v5.x WCF data services to return only the first level. ", false)]
public class LLBLGenProODataServiceExpandProvider : IExpandProvider
{
/// <summary>
/// Applies expansions to the specified <paramref name="query"/> parameter.
/// </summary>
/// <param name="query">The <see cref="T:System.Linq.IQueryable`1"/> object to expand.</param>
/// <param name="expandPaths">A collection of <see cref="T:System.Data.Services.ExpandSegmentCollection"/> paths to expand.</param>
/// <returns></returns>
public IEnumerable ApplyExpansions(IQueryable query, ICollection<ExpandSegmentCollection> expandPaths)
{
if((query == null) || !typeof(IEntityCore).IsAssignableFrom(query.ElementType))
{
// not an entity query, expansions are not doable.
return query;
}
var rootNode = new PathEdgeNode();
foreach(ExpandSegmentCollection expandSegments in expandPaths)
{
DecodeSegment(rootNode, expandSegments, 0, query.ElementType);
}
IPathEdge[] rootPathEdges = rootNode.GetChildPathEdges();
return typeof(LLBLGenProODataServiceExpandProvider).GetMethod("AppendWithPathCall", BindingFlags.Public | BindingFlags.Static)
.MakeGenericMethod(query.ElementType)
.Invoke(null, new object[] { query, rootPathEdges}) as IEnumerable;
}
/// <summary>
/// Appends the WithPath call to the queryable specified
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="query">The query to append WithPath to.</param>
/// <param name="pathEdges">The path edges.</param>
/// <returns></returns>
/// <remarks>Declared public to avoid medium trust issues as the method is called through reflection</remarks>
public static IEnumerable AppendWithPathCall<T>(IQueryable query, IPathEdge[] pathEdges)
where T:class, IEntityCore
{
return (query as IQueryable<T>).WithPath(pathEdges);
}
/// <summary>
/// Adds the segment as pathedgenode
/// </summary>
/// <param name="parentNode">The parent node.</param>
/// <param name="segments">The segments.</param>
/// <param name="currentNodeIndex">The current node index in the segments list.</param>
/// <param name="pathElement">The path element.</param>
/// <param name="entityType">Type of the entity.</param>
/// <remarks>Declared public to avoid medium trust reflection issues.</remarks>
public void AddSegment(PathEdgeNode parentNode, IList<ExpandSegment> segments, int currentNodeIndex, IPrefetchPathElementCore pathElement, Type entityType)
{
if(currentNodeIndex >= segments.Count)
{
return;
}
ExpandSegment segment = segments[currentNodeIndex];
PathEdgeNode childNode = null;
if(!parentNode.ChildPathEdgeNodes.TryGetValue(segment.Name, out childNode))
{
childNode = new PathEdgeNode(entityType, pathElement, segment);
parentNode.ChildPathEdgeNodes.Add(segment.Name, childNode);
}
DecodeSegment(childNode, segments, ++currentNodeIndex, entityType);
}
/// <summary>
/// Decodes an expansion segment.
/// </summary>
/// <param name="parentNode">The parent node.</param>
/// <param name="segments">The segments.</param>
/// <param name="currentNodeIndex">The current node index in the segments list.</param>
/// <param name="entityType">Type of the entity.</param>
private void DecodeSegment(PathEdgeNode parentNode, IList<ExpandSegment> segments, int currentNodeIndex, Type entityType)
{
if(currentNodeIndex >= segments.Count)
{
return;
}
var navigatorProperty = entityType.GetProperty(segments[currentNodeIndex].Name);
if(navigatorProperty == null)
{
return;
}
Type navigatorEntityType = typeof(IEntityCore).IsAssignableFrom(navigatorProperty.PropertyType)
? navigatorProperty.PropertyType
: navigatorProperty.PropertyType.BaseType.GetGenericArguments()[0];
PropertyInfo prefetchPathProperty = entityType.GetProperty("PrefetchPath" + segments[currentNodeIndex].Name, BindingFlags.Static | BindingFlags.Public);
IPrefetchPathElementCore prefetchPathElement = prefetchPathProperty.GetValue(null, null) as IPrefetchPathElementCore;
AddSegment(parentNode, segments, currentNodeIndex, prefetchPathElement, navigatorEntityType);
}
}
}