jasontaylordev / NorthwindTraders

Northwind Traders is a sample application built using ASP.NET Core and Entity Framework Core.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Suggestion: Request with record type

acesteb opened this issue · comments

Suggestion for Request record type. What do you guys think?

  1. Creating the request using record type
    public record Ping(int Id): IRequest<string>;
public record Ping(int Id) : IRequest<string> 
{
  public class Handler : IRequestHandler<Ping, string>
  {
      public Task<string> Handle(Ping request, CancellationToken cancellationToken)
      {
          Console.WriteLine(request.Id);
          return Task.FromResult("Pong");
      }
  }
}

vs

public record Ping : IRequest<string>
{
   public Ping(int Id) 
   {
        Id = id;
   }
   public int Id {get;}
 
    public class Handler : IRequestHandler<Ping, string>
    {
        public Task<string> Handle(Ping request, CancellationToken cancellationToken)
        {
            Console.WriteLine(request.Id);
            return Task.FromResult("Pong");
        }
    }
}

Record types provide you with immutable objects. In terms of DDD, records types saves you some typing regarding internal setters and a constructor.