Skip to content

Commit

Permalink
使用接口类型作为Action入参,模型绑定器测试通过
Browse files Browse the repository at this point in the history
  • Loading branch information
nnhy committed Jun 18, 2024
1 parent 80f7011 commit 6565a67
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 43 deletions.
72 changes: 36 additions & 36 deletions NewLife.Remoting.Extensions/ModelBinders/InterfaceModelBinder.cs
Original file line number Diff line number Diff line change
@@ -1,51 +1,51 @@
using Microsoft.AspNetCore.Mvc.ModelBinding;

namespace NewLife.Remoting.Extensions.ModelBinders
namespace NewLife.Remoting.Extensions.ModelBinders;

/// <summary>接口模型绑定器</summary>
public class InterfaceModelBinder : IModelBinder
{
/// <summary>接口模型绑定器</summary>
public class InterfaceModelBinder : IModelBinder
/// <summary>对于Json请求,从body中读取参数</summary>
/// <param name="bindingContext"></param>
/// <returns></returns>
public async Task BindModelAsync(ModelBindingContext bindingContext)
{
/// <summary>对于Json请求,从body中读取参数</summary>
/// <param name="bindingContext"></param>
/// <returns></returns>
public async Task BindModelAsync(ModelBindingContext bindingContext)
var provider = bindingContext.HttpContext.RequestServices;
var modelType = bindingContext.ModelType;

// 从容器中获取接口类型对应实例
var model = provider.GetRequiredService(modelType);

try
{
var req = bindingContext.HttpContext.Request;
var provider = bindingContext.HttpContext.RequestServices;
var modelType = bindingContext.ModelType;
// 从容器中获取接口类型对应实例
var model = provider.GetService(modelType);

try
{
var entityBody = await req.ReadFromJsonAsync(model.GetType());

bindingContext.Result = ModelBindingResult.Success(entityBody);
}
catch (Exception ex)
{
bindingContext.ModelState.AddModelError(bindingContext.ModelName, ex.Message);
}
var entityBody = await req.ReadFromJsonAsync(model!.GetType());

bindingContext.Result = ModelBindingResult.Success(entityBody);
}
catch (Exception ex)
{
bindingContext.ModelState.AddModelError(bindingContext.ModelName, ex.Message);
}
}
}

/// <summary>模型绑定器提供者</summary>
public class InterfaceModelBinderProvider : IModelBinderProvider
/// <summary>模型绑定器提供者</summary>
public class InterfaceModelBinderProvider : IModelBinderProvider
{
/// <summary>获取绑定器</summary>
/// <param name="context"></param>
/// <returns></returns>
public IModelBinder? GetBinder(ModelBinderProviderContext context)
{
/// <summary>获取绑定器</summary>
/// <param name="context"></param>
/// <returns></returns>
public IModelBinder? GetBinder(ModelBinderProviderContext context)
{
var modelType = context.Metadata.ModelType;

if (modelType.IsInterface)
{
return new InterfaceModelBinder();
}
if (!context.Metadata.IsComplexType) return null;

return null;
var type = context.Metadata.ModelType;
if (type.IsInterface && context.Services?.GetService(type) != null)
{
return new InterfaceModelBinder();
}

return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@
<_WebToolingArtifacts Remove="Properties\**" />
</ItemGroup>

<ItemGroup>
<Compile Remove="Services\ServicModelMetadataProvider.cs" />
</ItemGroup>

<ItemGroup>
<Content Include="..\Doc\leaf.png" Link="leaf.png" PackagePath="\" />
</ItemGroup>
Expand Down
4 changes: 1 addition & 3 deletions Samples/IoTZero/Controllers/DeviceController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ public class DeviceController : BaseDeviceController
/// <summary>当前设备</summary>
public Device Device { get; set; }

private readonly QueueService _queue;
private readonly ThingService _thingService;
private readonly ITracer _tracer;

Expand All @@ -31,9 +30,8 @@ public class DeviceController : BaseDeviceController
/// <param name="deviceService"></param>
/// <param name="thingService"></param>
/// <param name="tracer"></param>
public DeviceController(IServiceProvider serviceProvider, QueueService queue, ThingService thingService, ITracer tracer) : base(serviceProvider)
public DeviceController(IServiceProvider serviceProvider, ThingService thingService, ITracer tracer) : base(serviceProvider)
{
_queue = queue;
_thingService = thingService;
_tracer = tracer;
}
Expand Down
3 changes: 3 additions & 0 deletions Samples/IoTZero/IoTZero.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
</PropertyGroup>

<ItemGroup>
<Compile Remove="Common\InterfaceModelBinderProvider.cs" />
<Compile Remove="Common\LoginRequestModelBinder.cs" />
<Compile Remove="Common\PingRequestModelBinder.cs" />
<Compile Remove="Models\LoginResponse.cs" />
<Compile Remove="Models\LogoutResponse.cs" />
<Compile Remove="Models\PingResponse.cs" />
Expand Down
5 changes: 1 addition & 4 deletions Samples/IoTZero/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,7 @@
// 启用接口响应压缩
services.AddResponseCompression();

services.AddControllersWithViews(options =>
{
options.ModelBinderProviders.Insert(0, new InterfaceModelBinderProvider());
});
services.AddControllersWithViews();

// 引入魔方
services.AddCube();
Expand Down

0 comments on commit 6565a67

Please sign in to comment.