我有一个基本的抽象类,它接受来自抽象类的类型,我正在试图找出如何正确实现它的方法。我正在试图弄清楚如何正确实现它。
基类:
public abstract class ApiService<TRequest, TResponse>
where TRequest : ApiRequest
where TResponse : ApiResponse
{
public abstract TResponse Execute(TRequest Request);
}
ApiRequest类:
public abstract class ApiRequest
{
}
ApiResponse课程:
public abstract class ApiResponse
{
public bool Succeeded { get; set; }
}
我已经创建了一个TestService类来尝试对其进行排序,但这些概念并不适合我:
public class TestService : ApiService<ApiRequest, ApiResponse>
{
public override ApiResponse Execute(ApiRequest Request)
{
ApiResponse response;
return (response);
}
非常感谢您提供的任何帮助,并帮助我进一步理解抽象类!谢谢!
所以我的问题是:我不知道如何在Execute方法中实现ApiResponse,因为你无法实例化一个抽象类。
答案 0 :(得分:4)
Generics和Polymorphizem很好,但它必须在某些时候停止。在您的情况下,您有一个良好的API界面,很明显您传递了TRequest
并获得了TResponse
。
您应该添加的是如何处理特定案例。添加一层IRequestHander<TRequest,TResult>
,知道如何从特定Result
创建特定的Request
。
然后使用Factory
设计模式,您的API函数将调用工厂以获取适合其获得的请求的特定处理程序。它将执行RequestHander
并返回从中获取的Response
。
public class SpecificRequestA : ApiRequest {}
public class SpecificResponseA : ApiResponse{}
public interface IRequestHander<TRequest,TResponse>
where TRequest : ApiRequest
where TResponse : ApiResponse
{
TResponse Exeute(TRequest request);
}
public class SpecificRequestHandlerA : IRequestHander<SpecificRequestA,SpecificResponseA>
{
SpecificResponseA Execute(SpecificRequestA request)
{
//create the new response out of the given request. Here you know exactly on what you are working :)
}
}
然后添加工厂。
也可以考虑将请求实现为Request<TResponse>
- 看看它是否更适合您的情况
我建议查看依赖注入和DI容器(如Castle,Ninject,Unity,Simple Injector) 负责初始化。
答案 1 :(得分:0)
我在c#MVC UI Layer中使用以下ApiClient类:
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using System.Web;
using System.Web.Helpers;
using System.Web.Mvc;
namespace DocumentManager.UI
{
public class ApiClient<T>
{
public ApiClientErrorTypes Error{get;private set;}
private string baseUri = @"http://localhost/DocumentManager.WebAPI/";
public T ApiGet(string requestUrl)
{
using (var client = new HttpClient())
{
var requestUri = new Uri(baseUri + requestUrl);
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
if (ApiAuthToken.token != null)
{
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", ApiAuthToken.token.ToString());
}
var response = client.GetAsync(requestUri).Result;
string contentString = response.Content.ReadAsStringAsync().Result;
if (response.IsSuccessStatusCode)
{
T result = JsonConvert.DeserializeObject<T>(contentString);
return result;
}
if (int.Parse(response.StatusCode.ToString()) > 400 && int.Parse(response.StatusCode.ToString()) <= 499)
Error = ApiClientErrorTypes.UnAuthorised;
else
Error = ApiClientErrorTypes.Technical;
return default(T);
}
}
public T ApiPost(string requestUrl, HttpContent encodedContent)
{
using(var client = new HttpClient())
{
var requestUri = new Uri(baseUri + requestUrl);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
if (ApiAuthToken.token != null)
{
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", ApiAuthToken.token.ToString());
}
var response = client.PostAsync(requestUri, encodedContent).Result;
string contentString = response.Content.ReadAsStringAsync().Result;
if (response.IsSuccessStatusCode)
{
T result = JsonConvert.DeserializeObject<T>(contentString);
return result;
}
if (int.Parse(response.StatusCode.ToString()) > 400 && int.Parse(response.StatusCode.ToString()) <= 499)
Error = ApiClientErrorTypes.UnAuthorised;
else
Error = ApiClientErrorTypes.Technical;
return default(T);
}
}
public bool ApiPostBool(string requestUrl, HttpContent encodedContent)
{
using (var client = new HttpClient())
{
var requestUri = new Uri(baseUri + requestUrl);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
if (ApiAuthToken.token != null)
{
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", ApiAuthToken.token.ToString());
}
var response = client.PostAsync(requestUri, encodedContent).Result;
string contentString = response.Content.ReadAsStringAsync().Result;
if (response.IsSuccessStatusCode)
{
return true;
}
return false;
}
}
}
}
我从MVC控制器以下列方式调用它:
var apiClient = new ApiClient<Document>();
var doc = apiClient.ApiGet("api/document/get/" + id);
if (doc != null)
{
//do stuff here
}
我使用以下Web API层方法返回此项
namespace DocumentManager.WebApi.Controllers
{
[RoutePrefix("api/document")]
public class DocumentController : BaseController
{
[Route("get/{id}")]
public IHttpActionResult Get(int id)
{
return Ok(DACDocument.Read(new DataContext(),id));
}
}
}
此后面有一个实体框架数据层(DAC ...)
我使用这种架构结构的原因是我希望多个MVC UI前端应用程序与API后端绑定。
解决方案中的项目是 数据(类库) API(Web API) UI(Web MVC)
如果这有帮助,请标记为答案!