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 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
|
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.Linq;
using System.Linq.Expressions;
using System.Collections;
using System.Data.Linq.SqlClient;
using System.Diagnostics.CodeAnalysis;
namespace System.Data.Linq {
sealed public class DataLoadOptions {
bool frozen;
Dictionary<MetaPosition, MemberInfo> includes = new Dictionary<MetaPosition, MemberInfo>();
Dictionary<MetaPosition, LambdaExpression> subqueries = new Dictionary<MetaPosition, LambdaExpression>();
/// <summary>
/// Describe a property that is automatically loaded when the containing instance is loaded
/// </summary>
[SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "Microsoft: Generic types are an important part of Linq APIs and they could not exist without nested generic support.")]
[SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters", Justification = "Microsoft: Need to provide static typing.")]
public void LoadWith<T>(Expression<Func<T, object>> expression) {
if (expression == null) {
throw Error.ArgumentNull("expression");
}
MemberInfo mi = GetLoadWithMemberInfo(expression);
this.Preload(mi);
}
/// <summary>
/// Describe a property that is automatically loaded when the containing instance is loaded
/// </summary>
public void LoadWith(LambdaExpression expression) {
if (expression == null) {
throw Error.ArgumentNull("expression");
}
MemberInfo mi = GetLoadWithMemberInfo(expression);
this.Preload(mi);
}
/// <summary>
/// Place a subquery on the given association.
/// </summary>
[SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "Microsoft: Generic types are an important part of Linq APIs and they could not exist without nested generic support.")]
[SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters", Justification = "Microsoft: Need to provide static typing.")]
public void AssociateWith<T>(Expression<Func<T, object>> expression) {
if (expression == null) {
throw Error.ArgumentNull("expression");
}
this.AssociateWithInternal(expression);
}
/// <summary>
/// Place a subquery on the given association.
/// </summary>
public void AssociateWith(LambdaExpression expression) {
if (expression == null) {
throw Error.ArgumentNull("expression");
}
this.AssociateWithInternal(expression);
}
private void AssociateWithInternal(LambdaExpression expression) {
// Strip the cast-to-object.
Expression op = expression.Body;
while (op.NodeType == ExpressionType.Convert || op.NodeType == ExpressionType.ConvertChecked) {
op = ((UnaryExpression)op).Operand;
}
LambdaExpression lambda = Expression.Lambda(op, expression.Parameters.ToArray());
MemberInfo mi = Searcher.MemberInfoOf(lambda);
this.Subquery(mi, lambda);
}
/// <summary>
/// Determines if the member is automatically loaded with its containing instances.
/// </summary>
/// <param name="member">The member this is automatically loaded.</param>
/// <returns>True if the member is automatically loaded.</returns>
internal bool IsPreloaded(MemberInfo member) {
if (member == null) {
throw Error.ArgumentNull("member");
}
return includes.ContainsKey(new MetaPosition(member));
}
/// <summary>
/// Two shapes are equivalent if any of the following are true:
/// (1) They are the same object instance
/// (2) They are both null or empty
/// (3) They contain the same preloaded members
/// </summary>
internal static bool ShapesAreEquivalent(DataLoadOptions ds1, DataLoadOptions ds2) {
bool shapesAreSameOrEmpty = (ds1 == ds2) || ((ds1 == null || ds1.IsEmpty) && (ds2 == null || ds2.IsEmpty));
if (!shapesAreSameOrEmpty) {
if (ds1 == null || ds2 == null || ds1.includes.Count != ds2.includes.Count) {
return false;
}
foreach (MetaPosition metaPosition in ds2.includes.Keys) {
if (!ds1.includes.ContainsKey(metaPosition)) {
return false;
}
}
}
return true;
}
/// <summary>
/// Gets the subquery expression associated with the member.
/// </summary>
/// <param name="member">The member with the subquery.</param>
/// <returns></returns>
internal LambdaExpression GetAssociationSubquery(MemberInfo member) {
if (member == null) {
throw Error.ArgumentNull("member");
}
LambdaExpression expression = null;
subqueries.TryGetValue(new MetaPosition(member), out expression);
return expression;
}
/// <summary>
/// Freeze the shape. Any further attempts to modify the shape will result in
/// an exception.
/// </summary>
internal void Freeze() {
this.frozen = true;
}
/// <summary>
/// Describe a property that is automatically loaded when the containing instance is loaded
/// </summary>
internal void Preload(MemberInfo association) {
if (association == null) {
throw Error.ArgumentNull("association");
}
if (this.frozen) {
throw Error.IncludeNotAllowedAfterFreeze();
}
this.includes.Add(new MetaPosition(association), association);
ValidateTypeGraphAcyclic();
}
/// <summary>
/// Place a subquery on the given association.
/// </summary>
private void Subquery(MemberInfo association, LambdaExpression subquery) {
if (this.frozen) {
throw Error.SubqueryNotAllowedAfterFreeze();
}
subquery = (LambdaExpression)System.Data.Linq.SqlClient.Funcletizer.Funcletize(subquery); // Layering violation.
ValidateSubqueryMember(association);
ValidateSubqueryExpression(subquery);
this.subqueries[new MetaPosition(association)] = subquery;
}
/// <summary>
/// If the lambda specified is of the form p.A, where p is the parameter
/// and A is a member on p, the MemberInfo for A is returned. If
/// the expression is not of this form, an exception is thrown.
/// </summary>
private static MemberInfo GetLoadWithMemberInfo(LambdaExpression lambda)
{
// When the specified member is a value type, there will be a conversion
// to object that we need to strip
Expression body = lambda.Body;
if (body != null && (body.NodeType == ExpressionType.Convert || body.NodeType == ExpressionType.ConvertChecked))
{
body = ((UnaryExpression)body).Operand;
}
MemberExpression mex = body as MemberExpression;
if (mex != null && mex.Expression.NodeType == ExpressionType.Parameter)
{
return mex.Member;
}
else
{
throw Error.InvalidLoadOptionsLoadMemberSpecification();
}
}
private static class Searcher {
static internal MemberInfo MemberInfoOf(LambdaExpression lambda) {
Visitor v = new Visitor();
v.VisitLambda(lambda);
return v.MemberInfo;
}
private class Visitor : System.Data.Linq.SqlClient.ExpressionVisitor {
internal MemberInfo MemberInfo;
internal override Expression VisitMemberAccess(MemberExpression m) {
this.MemberInfo = m.Member;
return base.VisitMemberAccess(m);
}
internal override Expression VisitMethodCall(MethodCallExpression m) {
this.Visit(m.Object);
foreach (Expression arg in m.Arguments) {
this.Visit(arg);
break; // Only follow the extension method 'this'
}
return m;
}
}
}
private void ValidateTypeGraphAcyclic() {
IEnumerable<MemberInfo> edges = this.includes.Values;
int removed = 0;
for (int loop = 0; loop < this.includes.Count; ++loop) {
// Build a list of all edge targets.
HashSet<Type> edgeTargets = new HashSet<Type>();
foreach (MemberInfo edge in edges) {
edgeTargets.Add(GetIncludeTarget(edge));
}
// Remove all edges with sources matching no target.
List<MemberInfo> newEdges = new List<MemberInfo>();
bool someRemoved = false;
foreach (MemberInfo edge in edges) {
if (edgeTargets.Where(et=>et.IsAssignableFrom(edge.DeclaringType) || edge.DeclaringType.IsAssignableFrom(et)).Any()) {
newEdges.Add(edge);
}
else {
++removed;
someRemoved = true;
if (removed == this.includes.Count)
return;
}
}
if (!someRemoved) {
throw Error.IncludeCycleNotAllowed(); // No edges removed, there must be a loop.
}
edges = newEdges;
}
throw new InvalidOperationException("Bug in ValidateTypeGraphAcyclic"); // Getting here means a bug.
}
private static Type GetIncludeTarget(MemberInfo mi) {
Type mt = System.Data.Linq.SqlClient.TypeSystem.GetMemberType(mi);
if (mt.IsGenericType) {
return mt.GetGenericArguments()[0];
}
return mt;
}
private static void ValidateSubqueryMember(MemberInfo mi) {
Type memberType = System.Data.Linq.SqlClient.TypeSystem.GetMemberType(mi);
if (memberType == null) {
throw Error.SubqueryNotSupportedOn(mi);
}
if (!typeof(IEnumerable).IsAssignableFrom(memberType)) {
throw Error.SubqueryNotSupportedOnType(mi.Name, mi.DeclaringType);
}
}
private static void ValidateSubqueryExpression(LambdaExpression subquery) {
if (!typeof(IEnumerable).IsAssignableFrom(subquery.Body.Type)) {
throw Error.SubqueryMustBeSequence();
}
new SubqueryValidator().VisitLambda(subquery);
}
/// <summary>
/// Ensure that the subquery follows the rules for subqueries.
/// </summary>
private class SubqueryValidator : System.Data.Linq.SqlClient.ExpressionVisitor {
bool isTopLevel = true;
internal override Expression VisitMethodCall(MethodCallExpression m) {
bool was = isTopLevel;
try {
if (isTopLevel && !SubqueryRules.IsSupportedTopLevelMethod(m.Method))
throw Error.SubqueryDoesNotSupportOperator(m.Method.Name);
isTopLevel = false;
return base.VisitMethodCall(m);
}
finally {
isTopLevel = was;
}
}
}
/// <summary>
/// Whether there have been LoadOptions specified.
/// </summary>
internal bool IsEmpty {
get { return this.includes.Count == 0 && this.subqueries.Count == 0; }
}
}
}
|