Search code examples
c#asp.net-apicontroller

Easy and convenient way to implement custom services


I need to implement Service base class that is going to be an MVC API Controller with 3 requests Check, Pay, PayStatus. Every request and response has its base properties, and additional parameters as generic class to use individually for each service.

Example:

public class CheckRequest<TParams>
{
    public decimal Amount { get; set; }
    public string AccountId { get; set; }
    public TParams? AdditionalParams { get; set; }
}

public class TestServiceCheckRequest : CheckRequest<TestServiceCheckReuestParams> {}

I came up with something like this yet.

/// <typeparam name="T1">Check Request Params</typeparam>
/// <typeparam name="T2">Check Response Params</typeparam>
/// <typeparam name="T3">Pay Request Params</typeparam>
/// <typeparam name="T4">Pay Response Params</typeparam>
/// <typeparam name="T5">Payment Status Request Params</typeparam>
/// <typeparam name="T6">Payment Status Response Params</typeparam>
[ApiController]
[Route("[controller]/[action]")]
public abstract class Service<T1, T2, T3, T4, T5, T6> : ControllerBase
{
    [HttpPost]
    public abstract Task<CheckResponse<T1>> Check(CheckRequest<T2> request);
    [HttpPost]
    public abstract Task<CheckResponse<T3>> Pay(CheckRequest<T4> request);
    [HttpPost]
    public abstract Task<CheckResponse<T5>> Paystatus(CheckRequest<T6> request);
}

But this solution with 6 generic arguments seems not so convenient and I am looking for another solution. Is there any way to use interface like IServiceParams in base class instead of all generic arguments, and use implementations in derived classes, or something like that ?


Solution

  • Well, I will stick with my solution with generic arguments yet, as there are no better solution yet.