英文:
Easy and convenient way to implement custom services
问题
我需要实现一个Service
基类,它将成为一个带有3个请求Check
,Pay
,PayStatus
的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<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 ?
答案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<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;
}
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<TParams> where TParams : IServiceParams
{
public decimal Amount { get; set; }
public string AccountId { get; 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;
}
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论