我需要为本地化数据提供服务。所有本地化的响应 Dto 共享相同的属性。即我定义了一个接口 ( ILocalizedDto
) 来标记那些 Dto。在请求端,有一个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();
}
}