瀏覽代碼

add DeSerialize method to IContentSerializer with type deseralize

master
Savorboard 7 年之前
父節點
當前提交
cda3f5a808
共有 3 個檔案被更改,包括 21 行新增9 行删除
  1. +4
    -1
      src/DotNetCore.CAP/Abstractions/IContentSerializer.cs
  2. +6
    -4
      src/DotNetCore.CAP/Internal/IModelBinder.ComplexType.cs
  3. +11
    -4
      src/DotNetCore.CAP/Internal/ModelBinderFactory.cs

+ 4
- 1
src/DotNetCore.CAP/Abstractions/IContentSerializer.cs 查看文件

@@ -1,4 +1,5 @@
using DotNetCore.CAP.Models;
using System;
using DotNetCore.CAP.Models;

namespace DotNetCore.CAP.Abstractions
{
@@ -7,6 +8,8 @@ namespace DotNetCore.CAP.Abstractions
string Serialize<T>(T obj);

T DeSerialize<T>(string content);

object DeSerialize(string content, Type type);
}

public interface IMessagePacker


+ 6
- 4
src/DotNetCore.CAP/Internal/IModelBinder.ComplexType.cs 查看文件

@@ -1,18 +1,20 @@
using System;
using System.Reflection;
using System.Threading.Tasks;
using DotNetCore.CAP.Abstractions;
using DotNetCore.CAP.Abstractions.ModelBinding;
using DotNetCore.CAP.Infrastructure;

namespace DotNetCore.CAP.Internal
{
public class ComplexTypeModelBinder : IModelBinder
internal class ComplexTypeModelBinder : IModelBinder
{
private readonly ParameterInfo _parameterInfo;
private readonly IContentSerializer _serializer;

public ComplexTypeModelBinder(ParameterInfo parameterInfo)
public ComplexTypeModelBinder(ParameterInfo parameterInfo, IContentSerializer contentSerializer)
{
_parameterInfo = parameterInfo;
_serializer = contentSerializer;
}

public Task<ModelBindingResult> BindModelAsync(string content)
@@ -21,7 +23,7 @@ namespace DotNetCore.CAP.Internal
{
var type = _parameterInfo.ParameterType;

var value = Helper.FromJson(content, type);
var value = _serializer.DeSerialize(content, type);

return Task.FromResult(ModelBindingResult.Success(value));
}


+ 11
- 4
src/DotNetCore.CAP/Internal/ModelBinderFactory.cs 查看文件

@@ -2,6 +2,7 @@
using System.Collections.Concurrent;
using System.Reflection;
using System.Runtime.CompilerServices;
using DotNetCore.CAP.Abstractions;
using DotNetCore.CAP.Abstractions.ModelBinding;
using DotNetCore.CAP.Infrastructure;

@@ -10,10 +11,16 @@ namespace DotNetCore.CAP.Internal
/// <summary>
/// A factory for <see cref="IModelBinder" /> instances.
/// </summary>
public class ModelBinderFactory : IModelBinderFactory
internal class ModelBinderFactory : IModelBinderFactory
{
private readonly ConcurrentDictionary<Key, IModelBinder> _cache =
new ConcurrentDictionary<Key, IModelBinder>();
private readonly IContentSerializer _serializer;
private readonly ConcurrentDictionary<Key, IModelBinder> _cache;

public ModelBinderFactory(IContentSerializer contentSerializer)
{
_serializer = contentSerializer;
_cache = new ConcurrentDictionary<Key, IModelBinder>();
}

public IModelBinder CreateBinder(ParameterInfo parameter)
{
@@ -36,7 +43,7 @@ namespace DotNetCore.CAP.Internal
if (!Helper.IsComplexType(parameterInfo.ParameterType))
binder = new SimpleTypeModelBinder(parameterInfo);
else
binder = new ComplexTypeModelBinder(parameterInfo);
binder = new ComplexTypeModelBinder(parameterInfo, _serializer);

AddToCache(parameterInfo, token, binder);



Loading…
取消
儲存