Skip to content

Commit e862e75

Browse files
authored
feat(ISocketDataConverter): add ConfigureSocketDataConverters method (#6463)
* refactor: 更改文件目录结构 * refactor: 代码格式化 * refactor: 增加重构方法精简代码 * refactor: 精简方法名称 * doc: 更新配置转换器集合文档
1 parent bbae67e commit e862e75

File tree

5 files changed

+39
-14
lines changed

5 files changed

+39
-14
lines changed

src/BootstrapBlazor.Server/Components/Samples/SocketFactories.razor

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,22 @@ class MockEntity
110110
return new Foo() { Id = data.Span[0], Name = name };
111111
}
112112
}</Pre>
113+
114+
<p class="code-label">针对第三方程序集的数据类型解决方案如下:</p>
115+
<p>使用 <code></code></p>
116+
117+
<Pre>builder.Services.ConfigureSocketDataConverters(options =>
118+
{
119+
options.AddTypeConverter&lt;MockEntity&gt;();
120+
options.AddPropertyConverter&lt;MockEntity&gt;(entity =&gt; entity.Header, new SocketDataPropertyConverterAttribute()
121+
{
122+
Offset = 0,
123+
Length = 5
124+
});
125+
options.AddPropertyConverter&lt;MockEntity&gt;(entity =&gt; entity.Body, new SocketDataPropertyConverterAttribute()
126+
{
127+
Offset = 5,
128+
Length = 2
129+
});
130+
});
131+
</Pre>

src/BootstrapBlazor/Services/TcpSocket/DataConverter/SocketDataConverterCollections.cs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,35 +10,41 @@
1010
namespace BootstrapBlazor.Components;
1111

1212
/// <summary>
13-
///
13+
/// 数据转换器集合类
1414
/// </summary>
15-
public class SocketDataConverterCollections
15+
public sealed class SocketDataConverterCollections
1616
{
1717
readonly ConcurrentDictionary<Type, ISocketDataConverter> _converters = new();
1818
readonly ConcurrentDictionary<MemberInfo, SocketDataPropertyConverterAttribute> _propertyConverters = new();
1919

2020
/// <summary>
21-
/// 增加数据类型转换器方法
21+
/// 增加指定 <see cref="ISocketDataConverter{TEntity}"/> 数据类型转换器方法
2222
/// </summary>
2323
/// <typeparam name="TEntity"></typeparam>
2424
/// <param name="converter"></param>
25-
public void AddOrUpdateTypeConverter<TEntity>(ISocketDataConverter<TEntity> converter)
25+
public void AddTypeConverter<TEntity>(ISocketDataConverter<TEntity> converter)
2626
{
2727
var type = typeof(TEntity);
2828
_converters.AddOrUpdate(type, t => converter, (t, v) => converter);
2929
}
3030

31+
/// <summary>
32+
/// 增加默认数据类型转换器方法 转换器使用 <see cref="SocketDataConverter{TEntity}"/>
33+
/// </summary>
34+
/// <typeparam name="TEntity"></typeparam>
35+
public void AddTypeConverter<TEntity>() => AddTypeConverter(new SocketDataConverter<TEntity>(this));
36+
3137
/// <summary>
3238
/// 添加属性类型转化器方法
3339
/// </summary>
3440
/// <typeparam name="TEntity"></typeparam>
3541
/// <param name="propertyExpression"></param>
3642
/// <param name="attribute"></param>
37-
public void AddOrUpdatePropertyConverter<TEntity>(Expression<Func<TEntity, object?>> propertyExpression, SocketDataPropertyConverterAttribute attribute)
43+
public void AddPropertyConverter<TEntity>(Expression<Func<TEntity, object?>> propertyExpression, SocketDataPropertyConverterAttribute attribute)
3844
{
3945
if (propertyExpression.Body is MemberExpression memberExpression)
4046
{
41-
if(attribute.Type == null)
47+
if (attribute.Type == null)
4248
{
4349
attribute.Type = memberExpression.Type;
4450
}

test/UnitTest/Services/SocketDataConverterCollectionsTest.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,21 @@ protected override void ConfigureConfiguration(IServiceCollection services)
1616

1717
services.ConfigureSocketDataConverters(options =>
1818
{
19-
options.AddOrUpdateTypeConverter(new SocketDataConverter<MockEntity>());
20-
options.AddOrUpdatePropertyConverter<MockEntity>(entity => entity.Header, new SocketDataPropertyConverterAttribute()
19+
options.AddTypeConverter<MockEntity>();
20+
options.AddPropertyConverter<MockEntity>(entity => entity.Header, new SocketDataPropertyConverterAttribute()
2121
{
2222
Offset = 0,
2323
Length = 5
2424
});
25-
options.AddOrUpdatePropertyConverter<MockEntity>(entity => entity.Body, new SocketDataPropertyConverterAttribute()
25+
options.AddPropertyConverter<MockEntity>(entity => entity.Body, new SocketDataPropertyConverterAttribute()
2626
{
2727
Offset = 5,
2828
Length = 2
2929
});
3030

3131
// 为提高代码覆盖率 重复添加转换器以后面的为准
32-
options.AddOrUpdateTypeConverter(new SocketDataConverter<MockEntity>());
33-
options.AddOrUpdatePropertyConverter<MockEntity>(entity => entity.Header, new SocketDataPropertyConverterAttribute()
32+
options.AddTypeConverter<MockEntity>();
33+
options.AddPropertyConverter<MockEntity>(entity => entity.Header, new SocketDataPropertyConverterAttribute()
3434
{
3535
Offset = 0,
3636
Length = 5

test/UnitTest/Services/TcpSocketFactoryTest.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -770,13 +770,13 @@ public async Task TryGetTypeConverter_Ok()
770770
{
771771
builder.ConfigureSocketDataConverters(options =>
772772
{
773-
options.AddOrUpdateTypeConverter(new SocketDataConverter<OptionConvertEntity>(options));
774-
options.AddOrUpdatePropertyConverter<OptionConvertEntity>(entity => entity.Header, new SocketDataPropertyConverterAttribute()
773+
options.AddTypeConverter<OptionConvertEntity>();
774+
options.AddPropertyConverter<OptionConvertEntity>(entity => entity.Header, new SocketDataPropertyConverterAttribute()
775775
{
776776
Offset = 0,
777777
Length = 5
778778
});
779-
options.AddOrUpdatePropertyConverter<OptionConvertEntity>(entity => entity.Body, new SocketDataPropertyConverterAttribute()
779+
options.AddPropertyConverter<OptionConvertEntity>(entity => entity.Body, new SocketDataPropertyConverterAttribute()
780780
{
781781
Offset = 5,
782782
Length = 2

0 commit comments

Comments
 (0)