我需要为本地化数据提供服务。本地化的所有响应Dtos共享相同的属性。即我定义了一个接口(ILocalizedDto
)来标记那些Dtos。在请求方面,请求本地化的请求有ILocalizedRequest
。
使用IPlugin
我已设法实现所需的功能。但是我很确定实现不是线程安全的,另外我不知道我是否可以使用IHttpRequest.GetHashCode()作为一个请求/响应周期的标识符。
实现一个使用请求和响应Dto的ServiceStack插件的正确方法是什么?即是否有一些IHttpRequest.Context用于存储数据或是否可以在响应时获取请求dto?
internal class LocalizationFeature : IPlugin
{
public static bool Enabled { private set; get; }
/// <summary>
/// Activate the localization mechanism, so every response Dto which is a <see cref="ILocalizedDto" />
/// will be translated.
/// </summary>
/// <param name="appHost">The app host</param>
public void Register(IAppHost appHost)
{
if (Enabled)
{
return;
}
Enabled = true;
var filter = new LocalizationFilter();
appHost.RequestFilters.Add(filter.RequestFilter);
appHost.ResponseFilters.Add(filter.ResponseFilter);
}
}
// My request/response filter
public class LocalizationFilter
{
private readonly Dictionary<int,ILocalizedRequest> localizedRequests = new Dictionary<int, ILocalizedRequest>();
public ILocalizer Localizer { get; set; }
public void RequestFilter(IHttpRequest req, IHttpResponse res, object requestDto)
{
var localizedRequest = requestDto as ILocalizedRequest;
if (localizedRequest != null)
{
localizedRequests.Add(GetRequestId(req), localizedRequest);
}
}
public void ResponseFilter(IHttpRequest req, IHttpResponse res, object response)
{
var requestId = GetRequestId(req);
if (!(response is ILocalizedDto) || !localizedRequests.ContainsKey(requestId))
{
return;
}
var localizedDto = response as ILocalizedDto;
var localizedRequest = localizedRequests[requestId];
localizedRequests.Remove(requestId);
Localizer.Translate(localizedDto, localizedRequest.Language);
}
private static int GetRequestId(IHttpRequest req)
{
return req.GetHashCode();
}
}
答案 0 :(得分:2)
首先:当您需要的只是Language
属性的值时,为什么需要本身的请求?
以下代码是您的方案的合法解决方案:
public class LocalizationFeature : IPlugin
{
public const string LanguageKey = "X-Language";
public void Register(IAppHost appHost)
{
this.GlobalRequestFilters.Add(this.InterceptRequest);
this.GlobalResponseFilters.Add(this.InterceptResponse);
}
private void InterceptRequest(IRequest request,
IResponse response,
object dto)
{
var localizedRequest = dto as ILocalizedRequest;
if (localizedRequest != null)
{
request.SetItem(LanguageKey,
localizedRequest.Language);
}
}
private void InterceptResponse(IRequest request,
IResponse response,
object dto)
{
var localizedDto = dto as ILocalizedDto;
if (localizedDto != null)
{
var language = request.GetParam(LanguageKey) ?? request.GetItem(LanguageKey);
if (!string.IsNullOrEmpty(language))
{
Localizer.Translate(localizedDto,
language);
}
}
}
}
这里有趣的是var language = request.GetParam(LanguageKey) ?? request.GetItem(LanguageKey);
,这使您有机会使用密钥&#34; X-Language&#为HTTP语句,cookie或表单数据(如果适用)注入语言34 ;. (您还可以执行另一个?? DefaultLanguage
注入默认语言进行翻译...)
如果不以这种方式提供,请求的Language
集将从请求的Items
中读取并使用。
编辑:
此外,作为pointed out by @mythz,您还可以使用request.Dto
方法中的InterceptResponse
访问请求DTO:
private void InterceptResponse(IRequest request,
IResponse response,
object dto)
{
var localizedRequest = request.Dto as ILocalizedRequest;
var localizedDto = dto as ILocalizedDto;
if (localizedRequest != null
&& localizedDto != null)
{
Localizer.Translate(localizedDto,
localizedRequest.Language);
}
}