Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / DataEntity / System / Data / Common / QueryCache / EntityClientCacheKey.cs / 1305376 / EntityClientCacheKey.cs
//------------------------------------------------------------------------------ //// Copyright (c) Microsoft Corporation. All rights reserved. // // // @owner [....] // @backupOwner [....] //----------------------------------------------------------------------------- namespace System.Data.Common.QueryCache { using System; using System.Collections.Generic; using System.Data.Common.Internal; using System.Data.EntityClient; using System.Data.Metadata.Edm; using System.Diagnostics; using System.Globalization; using System.Text; ////// Represents EntityCommand Cache key context /// internal sealed class EntityClientCacheKey : QueryCacheKey { ////// Stored procedure or command text? /// readonly CommandType _commandType; ////// Entity Sql statement /// readonly string _eSqlStatement; ////// parameter collection token /// readonly string _parametersToken; ////// number of parameters /// readonly int _parameterCount; ////// Combined Hashcode based on component hashcodes /// readonly int _hashCode; ////// Creates a new instance of EntityClientCacheKey given a entityCommand instance /// /// internal EntityClientCacheKey(EntityCommand entityCommand) : base() { // Command Type _commandType = entityCommand.CommandType; // Statement _eSqlStatement = entityCommand.CommandText; // Parameters _parametersToken = GetParametersToken(entityCommand); _parameterCount = entityCommand.Parameters.Count; // Hashcode _hashCode = _commandType.GetHashCode() ^ _eSqlStatement.GetHashCode() ^ _parametersToken.GetHashCode(); } ////// 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(EntityClientCacheKey) != otherObject.GetType()) { return false; } EntityClientCacheKey otherEntityClientCacheKey = (EntityClientCacheKey)otherObject; return (_commandType == otherEntityClientCacheKey._commandType && _parameterCount == otherEntityClientCacheKey._parameterCount) && Equals(otherEntityClientCacheKey._eSqlStatement, _eSqlStatement) && Equals(otherEntityClientCacheKey._parametersToken, _parametersToken); } /// /// Returns Context Hash Code /// ///public override int GetHashCode() { return _hashCode; } private static string GetTypeUsageToken(TypeUsage type) { string result = null; // Dev10#537010: EntityCommand false positive cache hits caused by insufficient parameter type information in cache key // Ensure String types are correctly differentiated. if (object.ReferenceEquals(type, DbTypeMap.AnsiString)) { result = "AnsiString"; } else if (object.ReferenceEquals(type, DbTypeMap.AnsiStringFixedLength)) { result = "AnsiStringFixedLength"; } else if (object.ReferenceEquals(type, DbTypeMap.String)) { result = "String"; } else if (object.ReferenceEquals(type, DbTypeMap.StringFixedLength)) { result = "StringFixedLength"; } else if (object.ReferenceEquals(type, DbTypeMap.Xml)) { // Xml is currently mapped to (unicode, variable-length) string, so the TypeUsage // given to the provider is actually a String TypeUsage. Debug.Assert(TypeSemantics.IsPrimitiveType(type, PrimitiveTypeKind.String), "Update GetTypeUsageToken to return 'Xml' for Xml parameters"); result = "String"; } else { // String/Xml TypeUsages are the only DbType-derived TypeUsages that carry meaningful facets. // Otherwise, the primitive type name is a sufficient token (note that full name is not required // since model types always have the 'Edm' namespace). Debug.Assert(TypeSemantics.IsPrimitiveType(type), "EntityParameter TypeUsage not a primitive type?"); Debug.Assert(!TypeSemantics.IsPrimitiveType(type, PrimitiveTypeKind.String), "String TypeUsage not derived from DbType.AnsiString, AnsiString, String, StringFixedLength or Xml?"); result = type.EdmType.Name; } return result; } /// /// Returns a string representation of the parameter list /// /// ///private static string GetParametersToken(EntityCommand entityCommand) { if (null == entityCommand.Parameters || 0 == entityCommand.Parameters.Count) { // // means no parameters // return "@@0"; } // Ensure that parameter DbTypes are valid and there are no duplicate names Dictionary paramTypeUsage = entityCommand.GetParameterTypeUsage(); Debug.Assert(paramTypeUsage.Count == entityCommand.Parameters.Count, "entityParameter collection and query parameter collection must have the same number of entries"); if (1 == paramTypeUsage.Count) { // if its one parameter only, there is no need to use stringbuilder return "@@1:" + entityCommand.Parameters[0].ParameterName + ":" + GetTypeUsageToken(paramTypeUsage[entityCommand.Parameters[0].ParameterName]); } else { StringBuilder sb = new StringBuilder(entityCommand.Parameters.Count * EstimatedParameterStringSize); Debug.Assert(paramTypeUsage.Count == entityCommand.Parameters.Count, "entityParameter collection and query parameter collection must have the same number of entries"); sb.Append("@@"); sb.Append(entityCommand.Parameters.Count); sb.Append(":"); string separator = ""; foreach (KeyValuePair param in paramTypeUsage) { sb.Append(separator); sb.Append(param.Key); sb.Append(":"); sb.Append(GetTypeUsageToken(param.Value)); separator = ";"; } return sb.ToString(); } } /// /// returns the composed cache key /// ///public override string ToString() { return String.Join("|", new string[] { Enum.GetName(typeof(CommandType), _commandType), _eSqlStatement, _parametersToken }); } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. //------------------------------------------------------------------------------ // // Copyright (c) Microsoft Corporation. All rights reserved. // // // @owner [....] // @backupOwner [....] //----------------------------------------------------------------------------- namespace System.Data.Common.QueryCache { using System; using System.Collections.Generic; using System.Data.Common.Internal; using System.Data.EntityClient; using System.Data.Metadata.Edm; using System.Diagnostics; using System.Globalization; using System.Text; ////// Represents EntityCommand Cache key context /// internal sealed class EntityClientCacheKey : QueryCacheKey { ////// Stored procedure or command text? /// readonly CommandType _commandType; ////// Entity Sql statement /// readonly string _eSqlStatement; ////// parameter collection token /// readonly string _parametersToken; ////// number of parameters /// readonly int _parameterCount; ////// Combined Hashcode based on component hashcodes /// readonly int _hashCode; ////// Creates a new instance of EntityClientCacheKey given a entityCommand instance /// /// internal EntityClientCacheKey(EntityCommand entityCommand) : base() { // Command Type _commandType = entityCommand.CommandType; // Statement _eSqlStatement = entityCommand.CommandText; // Parameters _parametersToken = GetParametersToken(entityCommand); _parameterCount = entityCommand.Parameters.Count; // Hashcode _hashCode = _commandType.GetHashCode() ^ _eSqlStatement.GetHashCode() ^ _parametersToken.GetHashCode(); } ////// 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(EntityClientCacheKey) != otherObject.GetType()) { return false; } EntityClientCacheKey otherEntityClientCacheKey = (EntityClientCacheKey)otherObject; return (_commandType == otherEntityClientCacheKey._commandType && _parameterCount == otherEntityClientCacheKey._parameterCount) && Equals(otherEntityClientCacheKey._eSqlStatement, _eSqlStatement) && Equals(otherEntityClientCacheKey._parametersToken, _parametersToken); } /// /// Returns Context Hash Code /// ///public override int GetHashCode() { return _hashCode; } private static string GetTypeUsageToken(TypeUsage type) { string result = null; // Dev10#537010: EntityCommand false positive cache hits caused by insufficient parameter type information in cache key // Ensure String types are correctly differentiated. if (object.ReferenceEquals(type, DbTypeMap.AnsiString)) { result = "AnsiString"; } else if (object.ReferenceEquals(type, DbTypeMap.AnsiStringFixedLength)) { result = "AnsiStringFixedLength"; } else if (object.ReferenceEquals(type, DbTypeMap.String)) { result = "String"; } else if (object.ReferenceEquals(type, DbTypeMap.StringFixedLength)) { result = "StringFixedLength"; } else if (object.ReferenceEquals(type, DbTypeMap.Xml)) { // Xml is currently mapped to (unicode, variable-length) string, so the TypeUsage // given to the provider is actually a String TypeUsage. Debug.Assert(TypeSemantics.IsPrimitiveType(type, PrimitiveTypeKind.String), "Update GetTypeUsageToken to return 'Xml' for Xml parameters"); result = "String"; } else { // String/Xml TypeUsages are the only DbType-derived TypeUsages that carry meaningful facets. // Otherwise, the primitive type name is a sufficient token (note that full name is not required // since model types always have the 'Edm' namespace). Debug.Assert(TypeSemantics.IsPrimitiveType(type), "EntityParameter TypeUsage not a primitive type?"); Debug.Assert(!TypeSemantics.IsPrimitiveType(type, PrimitiveTypeKind.String), "String TypeUsage not derived from DbType.AnsiString, AnsiString, String, StringFixedLength or Xml?"); result = type.EdmType.Name; } return result; } /// /// Returns a string representation of the parameter list /// /// ///private static string GetParametersToken(EntityCommand entityCommand) { if (null == entityCommand.Parameters || 0 == entityCommand.Parameters.Count) { // // means no parameters // return "@@0"; } // Ensure that parameter DbTypes are valid and there are no duplicate names Dictionary paramTypeUsage = entityCommand.GetParameterTypeUsage(); Debug.Assert(paramTypeUsage.Count == entityCommand.Parameters.Count, "entityParameter collection and query parameter collection must have the same number of entries"); if (1 == paramTypeUsage.Count) { // if its one parameter only, there is no need to use stringbuilder return "@@1:" + entityCommand.Parameters[0].ParameterName + ":" + GetTypeUsageToken(paramTypeUsage[entityCommand.Parameters[0].ParameterName]); } else { StringBuilder sb = new StringBuilder(entityCommand.Parameters.Count * EstimatedParameterStringSize); Debug.Assert(paramTypeUsage.Count == entityCommand.Parameters.Count, "entityParameter collection and query parameter collection must have the same number of entries"); sb.Append("@@"); sb.Append(entityCommand.Parameters.Count); sb.Append(":"); string separator = ""; foreach (KeyValuePair param in paramTypeUsage) { sb.Append(separator); sb.Append(param.Key); sb.Append(":"); sb.Append(GetTypeUsageToken(param.Value)); separator = ";"; } return sb.ToString(); } } /// /// returns the composed cache key /// ///public override string ToString() { return String.Join("|", new string[] { Enum.GetName(typeof(CommandType), _commandType), _eSqlStatement, _parametersToken }); } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- Panel.cs
- LinkedResourceCollection.cs
- ToolStripPanelRow.cs
- PrtCap_Reader.cs
- TimeoutValidationAttribute.cs
- InternalBase.cs
- Activator.cs
- ObjectStateEntryDbUpdatableDataRecord.cs
- ServiceRouteHandler.cs
- ScriptResourceInfo.cs
- ProfessionalColorTable.cs
- Ref.cs
- StrokeCollection.cs
- DataGridViewColumnDesignTimeVisibleAttribute.cs
- ProcessingInstructionAction.cs
- GPPOINTF.cs
- ObjectDataSourceDisposingEventArgs.cs
- SqlProfileProvider.cs
- TextParagraphCache.cs
- ManipulationLogic.cs
- ContextBase.cs
- BindingMAnagerBase.cs
- ItemCollection.cs
- ReadOnlyCollectionBase.cs
- XPathParser.cs
- Mutex.cs
- BindingWorker.cs
- EndpointAddressAugust2004.cs
- XmlHierarchicalEnumerable.cs
- DataGridViewCellStateChangedEventArgs.cs
- SystemSounds.cs
- EntityContainerEntitySetDefiningQuery.cs
- ObjectTag.cs
- TrustManagerPromptUI.cs
- GeometryHitTestResult.cs
- EdmError.cs
- cookiecontainer.cs
- LinqDataSourceContextEventArgs.cs
- ToolStripDesignerAvailabilityAttribute.cs
- PropertyMetadata.cs
- NullableDecimalMinMaxAggregationOperator.cs
- SmiMetaData.cs
- CursorConverter.cs
- InstallerTypeAttribute.cs
- DateTimeConstantAttribute.cs
- Propagator.Evaluator.cs
- Style.cs
- EnumMember.cs
- GridViewCancelEditEventArgs.cs
- BoundField.cs
- DataServiceHost.cs
- ResolvedKeyFrameEntry.cs
- RangeContentEnumerator.cs
- BindingCompleteEventArgs.cs
- ProfilePropertySettingsCollection.cs
- ContextItem.cs
- Item.cs
- ZipIOCentralDirectoryFileHeader.cs
- JavaScriptObjectDeserializer.cs
- RadioButtonRenderer.cs
- XmlSchemaInclude.cs
- XmlSchemaChoice.cs
- MimeTypePropertyAttribute.cs
- DataConnectionHelper.cs
- PrivilegedConfigurationManager.cs
- OrderByLifter.cs
- BaseTemplateBuildProvider.cs
- ComponentSerializationService.cs
- RemoteWebConfigurationHostStream.cs
- KeyInstance.cs
- NotCondition.cs
- BindingContext.cs
- TiffBitmapEncoder.cs
- OneWayBindingElementImporter.cs
- QuadraticBezierSegment.cs
- TemplateColumn.cs
- GlyphShapingProperties.cs
- WebControlToolBoxItem.cs
- PersianCalendar.cs
- MinimizableAttributeTypeConverter.cs
- BrowserCapabilitiesCompiler.cs
- BitmapVisualManager.cs
- WebScriptServiceHost.cs
- RenderOptions.cs
- DataSourceListEditor.cs
- FileDialogPermission.cs
- ServiceReference.cs
- PrinterResolution.cs
- LayoutEvent.cs
- CompilerScope.Storage.cs
- MobileResource.cs
- Renderer.cs
- Literal.cs
- GenericPrincipal.cs
- XPathParser.cs
- VectorAnimationUsingKeyFrames.cs
- ImageCodecInfoPrivate.cs
- Parser.cs
- InputBinding.cs
- _NestedSingleAsyncResult.cs