//------------------------------------------------------------------------------ // // Copyright (c) Microsoft Corporation. All rights reserved. // // // @owner Microsoft // @backupOwner venkatja //------------------------------------------------------------------------------ namespace System.Data.Common.QueryCache { using System; using System.Data.Objects; using System.Diagnostics; /// /// Represents an ELinq-based ObjectQuery Cache key context /// internal sealed class LinqQueryCacheKey : QueryCacheKey { /// /// Aggregate hashcode based the hashcode of the properties of this cache key /// private readonly int _hashCode; /// /// DbExpression key /// private readonly string _expressionKey; /// /// Parameter collection token /// private readonly string _parametersToken; /// /// Number of parameters /// private readonly int _parameterCount; /// /// Concatenated representation of the Include span paths /// private readonly string _includePathsToken; /// /// The merge option in effect /// private readonly MergeOption _mergeOption; /// /// Result type affects assembly plan. /// private readonly Type _resultType; /// /// Flag indicating if the C# behavior should be used for null comparisons /// private readonly bool _useCSharpNullComparisonBehavior; /// /// Creates a new instance of LinqQueryCacheKey. /// /// The DbExpression key of the linq query /// The number of parameters to the query /// A string representation of the parameters to the query (may be null) /// A string representation of the Include span paths in effect (may be null) /// The merge option in effect. Required for result assembly. /// Flag indicating if the C# behavior should be used for null comparisons /// The type of each result item - for a given query as a CLR type instance internal LinqQueryCacheKey(string expressionKey, int parameterCount, string parametersToken, string includePathsToken, MergeOption mergeOption, bool useCSharpNullComparisonBehavior, Type resultType) : base() { Debug.Assert(null != expressionKey, "expressionKey must not be null"); _expressionKey = expressionKey; _parameterCount = parameterCount; _parametersToken = parametersToken; _includePathsToken = includePathsToken; _mergeOption = mergeOption; _resultType = resultType; _useCSharpNullComparisonBehavior = useCSharpNullComparisonBehavior; int combinedHash = _expressionKey.GetHashCode() ^ _mergeOption.GetHashCode(); if (_parametersToken != null) { combinedHash ^= _parametersToken.GetHashCode(); } if (_includePathsToken != null) { combinedHash ^= _includePathsToken.GetHashCode(); } combinedHash ^= _useCSharpNullComparisonBehavior.GetHashCode(); _hashCode = combinedHash; } /// /// Determines equality of two cache keys based on cache context values /// public override bool Equals(object otherObject) { Debug.Assert(null != otherObject, "otherObject must not be null"); if (typeof(LinqQueryCacheKey) != otherObject.GetType()) { return false; } LinqQueryCacheKey otherObjectQueryCacheKey = (LinqQueryCacheKey)otherObject; // also use result type... return (_parameterCount == otherObjectQueryCacheKey._parameterCount) && (_mergeOption == otherObjectQueryCacheKey._mergeOption) && Equals(otherObjectQueryCacheKey._expressionKey, _expressionKey) && Equals(otherObjectQueryCacheKey._includePathsToken, _includePathsToken) && Equals(otherObjectQueryCacheKey._parametersToken, _parametersToken) && Equals(otherObjectQueryCacheKey._resultType, _resultType) && Equals(otherObjectQueryCacheKey._useCSharpNullComparisonBehavior, _useCSharpNullComparisonBehavior); } /// /// Returns the hashcode for this cache key /// public override int GetHashCode() { return _hashCode; } /// /// Returns a string representation of the state of this cache key /// public override string ToString() { return String.Join("|", new string[] { _expressionKey, _parametersToken, _includePathsToken, Enum.GetName(typeof(MergeOption), _mergeOption), _useCSharpNullComparisonBehavior.ToString() }); } } }