简单方便的实现自定义服务方式

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

Easy and convenient way to implement custom services

问题

我需要实现一个Service基类,它将成为一个带有3个请求CheckPayPayStatus的MVC API控制器。每个请求和响应都有其基本属性,以及额外的参数作为通用类,以便为每个服务单独使用。

示例:

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

public class TestServiceCheckRequest : CheckRequest<TestServiceCheckRequestParams> {}

我已经想出了如下的解决方案。

/// <typeparam name="T1">检查请求参数</typeparam>
/// <typeparam name="T2">检查响应参数</typeparam>
/// <typeparam name="T3">支付请求参数</typeparam>
/// <typeparam name="T4">支付响应参数</typeparam>
/// <typeparam name="T5">支付状态请求参数</typeparam>
/// <typeparam name="T6">支付状态响应参数</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);
}

但是,具有6个通用参数的此解决方案似乎不太方便,我正在寻找另一种解决方案。是否有办法在基类中使用类似IServiceParams的接口,而不是所有通用参数,并在派生类中使用实现,或类似的方法?

英文:

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&lt;TParams&gt;
{
	public decimal Amount { get; set; }
	public string AccountId { get; set; }
	public TParams? AdditionalParams { get; set; }
}

public class TestServiceCheckRequest : CheckRequest&lt;TestServiceCheckReuestParams&gt; {}

I came up with something like this yet.

/// &lt;typeparam name=&quot;T1&quot;&gt;Check Request Params&lt;/typeparam&gt;
/// &lt;typeparam name=&quot;T2&quot;&gt;Check Response Params&lt;/typeparam&gt;
/// &lt;typeparam name=&quot;T3&quot;&gt;Pay Request Params&lt;/typeparam&gt;
/// &lt;typeparam name=&quot;T4&quot;&gt;Pay Response Params&lt;/typeparam&gt;
/// &lt;typeparam name=&quot;T5&quot;&gt;Payment Status Request Params&lt;/typeparam&gt;
/// &lt;typeparam name=&quot;T6&quot;&gt;Payment Status Response Params&lt;/typeparam&gt;
[ApiController]
[Route(&quot;[controller]/[action]&quot;)]
public abstract class Service&lt;T1, T2, T3, T4, T5, T6&gt; : ControllerBase
{
	[HttpPost]
	public abstract Task&lt;CheckResponse&lt;T1&gt;&gt; Check(CheckRequest&lt;T2&gt; request);
	[HttpPost]
	public abstract Task&lt;CheckResponse&lt;T3&gt;&gt; Pay(CheckRequest&lt;T4&gt; request);
	[HttpPost]
	public abstract Task&lt;CheckResponse&lt;T5&gt;&gt; Paystatus(CheckRequest&lt;T6&gt; 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 ?

答案1

得分: 0

为实现一个服务基类,该基类将成为一个MVC API控制器,具有3个请求Check、Pay和PayStatus,您可以使用以下代码结构。

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

public class TestServiceCheckRequest : CheckRequest<TestServiceCheckReuestParams> {}

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

您可以在基类中使用IServiceParams接口,而不是所有的泛型参数,并在派生类中使用具体的实现,如下所示:

public interface IServiceParams { }

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

public class TestServiceCheckRequest : CheckRequest<TestServiceCheckReuestParams> { }

public interface ICheckServiceParams : IServiceParams { }

public interface IPayServiceParams : IServiceParams { }

public interface IPayStatusServiceParams : IServiceParams { }

[ApiController]
[Route("[controller]/[action]")]
public abstract class Service : ControllerBase
{
    [HttpPost]
    public abstract Task<CheckResponse<T1>> Check<T1, T2>(CheckRequest<T2> request) where T1 : class where T2 : ICheckServiceParams;
    [HttpPost]
    public abstract Task<CheckResponse<T3>> Pay<T3, T4>(CheckRequest<T4> request) where T3 : class where T4 : IPayServiceParams;
    [HttpPost]
    public abstract Task<CheckResponse<T5>> Paystatus<T5, T6>(CheckRequest<T6> request) where T5 : class where T6 : IPayStatusServiceParams;
}

通过这种实现方式,您可以在基类中使用IServiceParams接口,而不是所有的泛型参数,并在派生类中使用具体的实现。

英文:

To implement a Service base class that is going to be an MVC API Controller with 3 requests Check, Pay, and PayStatus, you can use the following code structure.

public class CheckRequest&lt;TParams&gt;
{
    public decimal Amount { get; set; }
    public string AccountId { get; set; }
    public TParams? AdditionalParams { get; set; }
}

public class TestServiceCheckRequest : CheckRequest&lt;TestServiceCheckReuestParams&gt; {}

[ApiController]
[Route(&quot;[controller]/[action]&quot;)]
public abstract class Service : ControllerBase
{
    [HttpPost]
    public abstract Task&lt;CheckResponse&lt;T1&gt;&gt; Check&lt;T1, T2&gt;(CheckRequest&lt;T2&gt; request) where T1 : class where T2 : class;
    [HttpPost]
    public abstract Task&lt;CheckResponse&lt;T3&gt;&gt; Pay&lt;T3, T4&gt;(CheckRequest&lt;T4&gt; request) where T3 : class where T4 : class;
    [HttpPost]
    public abstract Task&lt;CheckResponse&lt;T5&gt;&gt; Paystatus&lt;T5, T6&gt;(CheckRequest&lt;T6&gt; request) where T5 : class where T6 : class;
}

You can use the IServiceParams interface in the base class instead of all generic arguments, and use implementations in derived classes, like this:

public interface IServiceParams { }

public class CheckRequest&lt;TParams&gt; where TParams : IServiceParams
{
    public decimal Amount { get; set; }
    public string AccountId { get; set; }
    public TParams? AdditionalParams { get; set; }
}

public class TestServiceCheckRequest : CheckRequest&lt;TestServiceCheckReuestParams&gt; { }

public interface ICheckServiceParams : IServiceParams { }

public interface IPayServiceParams : IServiceParams { }

public interface IPayStatusServiceParams : IServiceParams { }

[ApiController]
[Route(&quot;[controller]/[action]&quot;)]
public abstract class Service : ControllerBase
{
    [HttpPost]
    public abstract Task&lt;CheckResponse&lt;T1&gt;&gt; Check&lt;T1, T2&gt;(CheckRequest&lt;T2&gt; request) where T1 : class where T2 : ICheckServiceParams;
    [HttpPost]
    public abstract Task&lt;CheckResponse&lt;T3&gt;&gt; Pay&lt;T3, T4&gt;(CheckRequest&lt;T4&gt; request) where T3 : class where T4 : IPayServiceParams;
    [HttpPost]
    public abstract Task&lt;CheckResponse&lt;T5&gt;&gt; Paystatus&lt;T5, T6&gt;(CheckRequest&lt;T6&gt; request) where T5 : class where T6 : IPayStatusServiceParams;
}

With this implementation, you can use the IServiceParams interface in the base class instead of all generic arguments, and use implementations in derived classes.

答案2

得分: 0

我将坚持使用带有通用参数的解决方案,因为目前还没有更好的解决方案。

英文:

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

huangapple
  • 本文由 发表于 2023年2月27日 16:03:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/75578015.html
匿名

发表评论

匿名网友

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

确定