Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,22 @@ class MockEntity
return new Foo() { Id = data.Span[0], Name = name };
}
}</Pre>

<p class="code-label">针对第三方程序集的数据类型解决方案如下:</p>
<p>使用 <code></code></p>

<Pre>builder.Services.ConfigureSocketDataConverters(options =>
{
options.AddTypeConverter&lt;MockEntity&gt;();
options.AddPropertyConverter&lt;MockEntity&gt;(entity =&gt; entity.Header, new SocketDataPropertyConverterAttribute()
{
Offset = 0,
Length = 5
});
options.AddPropertyConverter&lt;MockEntity&gt;(entity =&gt; entity.Body, new SocketDataPropertyConverterAttribute()
{
Offset = 5,
Length = 2
});
});
</Pre>
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,41 @@
namespace BootstrapBlazor.Components;

/// <summary>
///
/// 数据转换器集合类
/// </summary>
public class SocketDataConverterCollections
public sealed class SocketDataConverterCollections
{
readonly ConcurrentDictionary<Type, ISocketDataConverter> _converters = new();
readonly ConcurrentDictionary<MemberInfo, SocketDataPropertyConverterAttribute> _propertyConverters = new();

/// <summary>
/// 增加数据类型转换器方法
/// 增加指定 <see cref="ISocketDataConverter{TEntity}"/> 数据类型转换器方法
/// </summary>
/// <typeparam name="TEntity"></typeparam>
/// <param name="converter"></param>
public void AddOrUpdateTypeConverter<TEntity>(ISocketDataConverter<TEntity> converter)
public void AddTypeConverter<TEntity>(ISocketDataConverter<TEntity> converter)
{
var type = typeof(TEntity);
_converters.AddOrUpdate(type, t => converter, (t, v) => converter);
}

/// <summary>
/// 增加默认数据类型转换器方法 转换器使用 <see cref="SocketDataConverter{TEntity}"/>
/// </summary>
/// <typeparam name="TEntity"></typeparam>
public void AddTypeConverter<TEntity>() => AddTypeConverter(new SocketDataConverter<TEntity>(this));

/// <summary>
/// 添加属性类型转化器方法
/// </summary>
/// <typeparam name="TEntity"></typeparam>
/// <param name="propertyExpression"></param>
/// <param name="attribute"></param>
public void AddOrUpdatePropertyConverter<TEntity>(Expression<Func<TEntity, object?>> propertyExpression, SocketDataPropertyConverterAttribute attribute)
public void AddPropertyConverter<TEntity>(Expression<Func<TEntity, object?>> propertyExpression, SocketDataPropertyConverterAttribute attribute)
{
if (propertyExpression.Body is MemberExpression memberExpression)
{
if(attribute.Type == null)
if (attribute.Type == null)
{
attribute.Type = memberExpression.Type;
}
Expand Down
10 changes: 5 additions & 5 deletions test/UnitTest/Services/SocketDataConverterCollectionsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,21 @@ protected override void ConfigureConfiguration(IServiceCollection services)

services.ConfigureSocketDataConverters(options =>
{
options.AddOrUpdateTypeConverter(new SocketDataConverter<MockEntity>());
options.AddOrUpdatePropertyConverter<MockEntity>(entity => entity.Header, new SocketDataPropertyConverterAttribute()
options.AddTypeConverter<MockEntity>();
options.AddPropertyConverter<MockEntity>(entity => entity.Header, new SocketDataPropertyConverterAttribute()
{
Offset = 0,
Length = 5
});
options.AddOrUpdatePropertyConverter<MockEntity>(entity => entity.Body, new SocketDataPropertyConverterAttribute()
options.AddPropertyConverter<MockEntity>(entity => entity.Body, new SocketDataPropertyConverterAttribute()
{
Offset = 5,
Length = 2
});

// 为提高代码覆盖率 重复添加转换器以后面的为准
options.AddOrUpdateTypeConverter(new SocketDataConverter<MockEntity>());
options.AddOrUpdatePropertyConverter<MockEntity>(entity => entity.Header, new SocketDataPropertyConverterAttribute()
options.AddTypeConverter<MockEntity>();
options.AddPropertyConverter<MockEntity>(entity => entity.Header, new SocketDataPropertyConverterAttribute()
{
Offset = 0,
Length = 5
Expand Down
6 changes: 3 additions & 3 deletions test/UnitTest/Services/TcpSocketFactoryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -770,13 +770,13 @@ public async Task TryGetTypeConverter_Ok()
{
builder.ConfigureSocketDataConverters(options =>
{
options.AddOrUpdateTypeConverter(new SocketDataConverter<OptionConvertEntity>(options));
options.AddOrUpdatePropertyConverter<OptionConvertEntity>(entity => entity.Header, new SocketDataPropertyConverterAttribute()
options.AddTypeConverter<OptionConvertEntity>();
options.AddPropertyConverter<OptionConvertEntity>(entity => entity.Header, new SocketDataPropertyConverterAttribute()
{
Offset = 0,
Length = 5
});
options.AddOrUpdatePropertyConverter<OptionConvertEntity>(entity => entity.Body, new SocketDataPropertyConverterAttribute()
options.AddPropertyConverter<OptionConvertEntity>(entity => entity.Body, new SocketDataPropertyConverterAttribute()
{
Offset = 5,
Length = 2
Expand Down
Loading