/// <summary> /// 启动分片(默认启用)。 /// </summary> /// <param name="sharded">给定的 <see cref="ShardedDescriptor"/>。</param> /// <param name="value">输出 <typeparamref name="TValue"/>。</param> /// <returns>返回布尔值。</returns> protected virtual bool Enabling(ShardedDescriptor sharded, [MaybeNullWhen(false)] out TValue value) { // 如果引用对象是值对象 if (sharded.ReferenceValue is TValue referenceValue) { value = referenceValue; return(true); } // 如果引用对象是属性值对象 if (sharded.ReferenceValue is not null && sharded.Entity is not null && sharded.Entity.EntityType == (sharded.ReferenceType ?? sharded.ReferenceValue.GetType())) { var property = GetEntityProperty(sharded.Entity); if (property is not null) { var propertyValue = property.GetValue(sharded.ReferenceValue); ArgumentNullException.ThrowIfNull(propertyValue); value = (TValue)propertyValue !; return(true); } } value = default; return(false); }
/// <summary> /// 对存取器的数据库分片。 /// </summary> /// <param name="shardingManager">给定的 <see cref="IShardingManager"/>。</param> /// <param name="accessor">给定的 <see cref="IAccessor"/>。</param> /// <param name="descriptor">输出 <see cref="ShardedDescriptor"/>。</param> /// <returns>返回 <see cref="IAccessor"/>。</returns> public static IAccessor ShardDatabase(this IShardingManager shardingManager, IAccessor accessor, [MaybeNullWhen(false)] out ShardedDescriptor descriptor) { var attribute = accessor.AccessorDescriptor?.Sharded; if (attribute is null) { descriptor = null; return(accessor); } descriptor = shardingManager.CreateDescriptor(attribute); descriptor.DefaultStrategy?.FormatSuffix(descriptor); var shardedName = descriptor.ToString(); if (!shardedName.Equals(descriptor.BaseName, StringComparison.Ordinal)) { var newConnectionString = accessor.CurrentConnectionString ! .Replace(attribute.BaseName !, shardedName); accessor.ChangeConnection(newConnectionString); } return(accessor); }
/// <summary> /// 格式化分片后缀。 /// </summary> /// <param name="sharded">给定的 <see cref="ShardedDescriptor"/>。</param> /// <returns>返回字符串。</returns> public virtual string FormatSuffix(ShardedDescriptor sharded) { var suffix = sharded.Suffix; if (Enabling(sharded, out TValue? value)) { foreach (var p in _parameters) { suffix = suffix.Replace(p.Key, p.Value(value), ParameterComparison); } } return(sharded.Suffix = suffix); }
private static ShardedDescriptor CreateDescriptor(this IShardingManager manager, ShardedAttribute attribute) { if (string.IsNullOrEmpty(attribute.BaseName)) { throw new ArgumentException($"The {nameof(attribute)}.{nameof(attribute.BaseName)} is null or empty."); } var descriptor = new ShardedDescriptor(attribute.BaseName, attribute.Suffix); if (attribute.DefaultStrategyType is not null) { descriptor.DefaultStrategy = manager.GetStrategy(attribute.DefaultStrategyType); } return(descriptor); }
public void AllTest() { var now = DateTime.Now; var sharding = new ShardedDescriptor("Shard", now.Year.ToString()); var expected = $"{sharding.BaseName}{sharding.SuffixConnector}{now.Year}"; Assert.Equal(expected, sharding); var copyShard = ShardedDescriptor.Parse(expected); Assert.Equal(expected, copyShard); var newSharding = sharding.WithSuffix(now.Month.ToString()); expected = $"{sharding.BaseName}{sharding.SuffixConnector}{now.Month}"; Assert.Equal(expected, newSharding); }