0

I am new into asp.net core webAPI. I have created a Controller method that taken a request object.

    [HttpPost]
    [Route("/api/DoWork")]
    [ValidateModelState]
    public virtual IActionResult DoWork(CustomRequest request)
    {
          //TODO: implement this method 
    }

Now this CustomRequest calss is generic class, which have multiple properties and depending upon the client/tenant will populate/set values for some of the properties.

Example : Suppose CustomRequest class structure is,

public partial class CustomRequest
{ 
    public string ReqId { get; set; }

    public DateTime? BusinessDate { get; set; }

    public DateTime? CurrentDate{ get; set; }
}

So Customer A can only set values (or send values in request) ReqId and BusinessDate and Customer B send values ReqId and CurrentDate. As to proceed with business operation either of the date is required.

It's also previously decided which client will send which of the values, and this number of properties is large.

So I was thinking of creating a relatively small class which will be subset of CustomRequest class and sufficient enough to process business operations. And I can think of two options to proceed with that.

  1. Write code inside my controller action to create small sub set class and then call another method that expect only the subset class
  2. Use Custom Model binders to create that small subset class and change the method signature.

My question is: What is the best way to handle this situation option 1 or 2 or any other option? Best way in terms of performance and customization option available (like in option 1, i don't think we can use ModelState)

Any help or guidance...

Posto
  • 7,362
  • 7
  • 44
  • 61
  • Have a look at this, it seems similar to what you are attempting: https://stackoverflow.com/questions/27894272/controller-action-with-derived-classes – Armand Aug 30 '19 at 08:01
  • @Armand, thanks for looking at it, I have checked the link and yes it's seems similar , and Accepted Answer is quite same to option 1 in our case. any idea how can we use model binders on this approach. – Posto Aug 30 '19 at 08:15
  • public class CustomRequest { public string ReqId { get; set; } public T respons{get;set} } public Class Name { public DateTime? BusinessDate { get; set; } public DateTime? CurrentDate{ get; set; } } Try this one – Bibin Aug 30 '19 at 08:22
  • I think this article covers it, I haven't used custom model binders before, but this should do the trick https://learn.microsoft.com/en-us/aspnet/core/mvc/advanced/custom-model-binding?view=aspnetcore-2.2 – Armand Aug 30 '19 at 08:26
  • From there you can use polymorphism to do the business logic – Armand Aug 30 '19 at 08:27
  • How did you decide the `client/tenant`? What is the limit for using the `CustomRequest` with all properties, for `valiation`, you could try [Custom attributes](https://learn.microsoft.com/en-us/aspnet/core/mvc/models/validation?view=aspnetcore-2.2#custom-attributes) – Edward Sep 02 '19 at 05:31

0 Answers0