/// <summary>
        /// Converts IStoreShardMap to ShardMap.
        /// </summary>
        /// <param name="manager">Reference to shard map manager.</param>
        /// <param name="ssm">Storage representation for ShardMap.</param>
        /// <returns>ShardMap object corresponding to storange representation.</returns>
        internal static ShardMap CreateShardMapFromStoreShardMap(
            ShardMapManager manager,
            IStoreShardMap ssm)
        {
            switch (ssm.MapType)
            {
            case ShardMapType.List:
                // Create ListShardMap<TKey>
                return((ShardMap)Activator.CreateInstance(
                           typeof(ListShardMap <>).MakeGenericType(
                               ShardKey.TypeFromShardKeyType(ssm.KeyType)),
                           BindingFlags.NonPublic | BindingFlags.Instance,
                           null,
                           new object[] { manager, ssm },
                           CultureInfo.InvariantCulture));

            default:
                Debug.Assert(ssm.MapType == ShardMapType.Range);
                // Create RangeShardMap<TKey>
                return((ShardMap)Activator.CreateInstance(
                           typeof(RangeShardMap <>).MakeGenericType(
                               ShardKey.TypeFromShardKeyType(ssm.KeyType)),
                           BindingFlags.NonPublic | BindingFlags.Instance,
                           null,
                           new object[] { manager, ssm },
                           CultureInfo.InvariantCulture));
            }
        }
Beispiel #2
0
        /// <summary>
        /// Asynchronously opens a regular <see cref="SqlConnection"/> to the shard
        /// to which the specified key value is mapped.
        /// </summary>
        /// <typeparam name="TKey">Type of the key.</typeparam>
        /// <param name="key">Input key value.</param>
        /// <param name="connectionString">
        /// Connection string with credential information such as SQL Server credentials or Integrated Security settings.
        /// The hostname of the server and the database name for the shard are obtained from the lookup operation for key.
        /// </param>
        /// <param name="options">Options for validation operations to perform on opened connection.</param>
        /// <returns>A Task encapsulating an opened SqlConnection.</returns>
        /// <remarks>
        /// Note that the <see cref="SqlConnection"/> object returned by this call is not protected against transient faults.
        /// Callers should follow best practices to protect the connection against transient faults
        /// in their application code, e.g., by using the transient fault handling
        /// functionality in the Enterprise Library from Microsoft Patterns and Practices team.
        /// This call only works if there is a single default mapping.
        /// </remarks>
        public Task <SqlConnection> OpenConnectionForKeyAsync <TKey>(
            TKey key,
            string connectionString,
            ConnectionOptions options)
        {
            ExceptionUtils.DisallowNullArgument(connectionString, "connectionString");

            Debug.Assert(this.StoreShardMap.KeyType != ShardKeyType.None);

            using (ActivityIdScope activityIdScope = new ActivityIdScope(Guid.NewGuid()))
            {
                IShardMapper <TKey> mapper = this.GetMapper <TKey>();

                if (mapper == null)
                {
                    throw new ArgumentException(
                              StringUtils.FormatInvariant(
                                  Errors._ShardMap_OpenConnectionForKey_KeyTypeNotSupported,
                                  typeof(TKey),
                                  this.StoreShardMap.Name,
                                  ShardKey.TypeFromShardKeyType(this.StoreShardMap.KeyType)),
                              "key");
                }

                Debug.Assert(mapper != null);

                return(mapper.OpenConnectionForKeyAsync(key, connectionString, options));
            }
        }
 /// <summary>
 /// Raise conversion exception.
 /// </summary>
 /// <typeparam name="TKey">Key type.</typeparam>
 /// <param name="ssm">Shard map whose conversion failed.</param>
 /// <param name="targetKind">Requested type of shard map.</param>
 private static ShardManagementException GetConversionException <TKey>(IStoreShardMap ssm, string targetKind)
 {
     return(new ShardManagementException(
                ShardManagementErrorCategory.ShardMapManager,
                ShardManagementErrorCode.ShardMapTypeConversionError,
                Errors._ShardMapExtensions_AsTypedShardMap_ConversionFailure,
                ssm.Name,
                targetKind,
                typeof(TKey).Name,
                ssm.MapType.ToString(),
                ssm.KeyType == ShardKeyType.None ? string.Empty : ShardKey.TypeFromShardKeyType(ssm.KeyType).Name));
 }