如何在MediatoR模式中传递对象数组类型?

huangapple go评论63阅读模式
英文:

How to pass array of objects type in MediatoR patterns?

问题

希望你一切都好!

我正在使用MediatoR设计模式,在其中我想将集合数组作为控制器中的对象接受。之前我是将其作为单一对象使用的,但现在我该如何允许MediatoR接受数组作为参数。

从:

public async Task<ActionResult<ServiceResponse<int>>> 
  AddField(**AddFieldCommand** objFieldCommand)

到:

public async Task<ActionResult<ServiceResponse<int>>> 
 AddField(**AddFieldCommand[]** objFieldCommand)

我尝试过这样做,但出现以下错误:

错误	CS0311	类型 'Application.Commands.Fields.AddFieldCommand[]' 无法用作泛型类型或方法 'IRequestHandler<TRequest, TResponse>' 中的类型参数 'TRequest'。不存在从 'Application.Commands.Fields.AddFieldCommand[]' 到 'MediatR.IRequest<Application.Common.ServiceResponse<int>>' 的隐式引用转换。	Application	C:\Users\Admin\Documents\Projects\Source Code\Application\Commands\Fields\AddFieldCommand.cs	28	Active

   public class AddFieldCommandHandler : IRequestHandler<AddFieldCommandAggregate, ServiceResponse<int>>
{
    private readonly IFieldsService _fieldService;

    public AddFieldCommandHandler(IFieldsService fieldService)
    {
        _fieldService = fieldService;
    }

    public async Task<ServiceResponse<int>> Handle(AddFieldCommandAggregate request, CancellationToken cancellationToken)
    {
        return await _fieldService.AddField(request: request, cancellationToken: cancellationToken);
    }
}

错误出现在标记为粗体的行,该行为 public class AddFieldCommandHandler。任何人能帮忙吗?

英文:

Hope You are doing well!

I am using MediatoR design pattern in which i want to accept array of collections as an object in my controller. Earlier i was using as a single object but now how can i allow the MediatoR to accept array as parameter.

From:

public async Task<ActionResult<ServiceResponse<int>>>
  AddField(**AddFieldCommand** objFieldCommand)

To:

public async Task<ActionResult<ServiceResponse<int>>> 
 AddField(**AddFieldCommand[]** objFieldCommand)

I tried doing this but it is giving me the below error:

Severity	Code	Description	Project	File	Line	Suppression State
Error	CS0311	The type 'Application.Commands.Fields.AddFieldCommand[]' cannot be used as type parameter 'TRequest' in the generic type or method 'IRequestHandler<TRequest, TResponse>'. There is no implicit reference conversion from 'Application.Commands.Fields.AddFieldCommand[]' to 'MediatR.IRequest<Application.Common.ServiceResponse<int>>'.	Application	C:\Users\Admin\Documents\Projects\Source Code\Application\Commands\Fields\AddFieldCommand.cs	28	Active


   public class AddFieldCommandHandler : IRequestHandler<AddFieldCommandAggregate, ServiceResponse<int>>
{
    private readonly IFieldsService _fieldService;

    public AddFieldCommandHandler(IFieldsService fieldService)
    {
        _fieldService = fieldService;
    }

    public async Task<ServiceResponse<int>> Handle(AddFieldCommandAggregate request, CancellationToken cancellationToken)
    {
        return await _fieldService.AddField(request: request, cancellationToken: cancellationToken);
    }
}

error comes in line public class AddFieldCommandHandler which is marked as bold.
Can any one please help me?

答案1

得分: 1

根据您的设置,我怀疑您有一个处理器接受AddFieldCommand并调用某个方法来处理它。现在您可能希望执行一个循环来处理所有项。

所以我怀疑您大致有类似于以下的代码:

public async Task Handle(AddFieldCommand data)
{
    // 这里包含了所有的逻辑
    PerformAction(data);
}

所以现在需要进行重构,如下所示:

public async Task Handle(AddFieldCommandAggregate multiData)
{
    foreach(var data in multiData.Data)
	{
		// 这里包含了所有的逻辑
		PerformAction(data);
	}
}

您还需要定义一个聚合类:

public class AddFieldCommandAggregate
{
    public AddFieldCommand[] Data { get; set; }
}
英文:

By your setup i suspect you have some handler that accepts AddFieldCommand and invokes some method with it. Now you want to perform most probably a loop to handle all items.

So i suspect you roughly have code similar to this:

public async Task Handle(AddFieldCommand data)
{
    // here you have all the logic
    PerformAction(data);
}

so now this would be needed to be refactored to:

public async Task Handle(AddFieldCommandAggregate multiData)
{
    foreach(var data in mulitData.Data)
	{
		// here you have all the logic
		PerformAction(data);
	}
}

and you would need to define aggregate class:

public class AddFieldCommandAggregate
{
    public AddFieldCommand[] Data { get; set; }
}

huangapple
  • 本文由 发表于 2023年4月17日 15:48:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76032800.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定