Tôi cần dịch vụ dữ liệu được bản địa hóa. Tất cả các đáp ứng Dtos được bản địa hóa chia sẻ các thuộc tính giống nhau. I E. Tôi đã xác định một giao diện (ILocalizedDto
) để đánh dấu các Dtos đó. Ở bên yêu cầu, có ILocalizedRequest
cho các yêu cầu yêu cầu bản địa hóa.Cách viết plugin ServiceStack cần cả yêu cầu và phản hồi Dtos
Sử dụng IPlugin
Tôi đã quản lý để triển khai tính năng được yêu cầu. Tuy nhiên tôi khá chắc chắn rằng việc thực hiện không phải là thread an toàn và bổ sung tôi không biết nếu tôi có thể sử dụng IHttpRequest.GetHashCode() như là định danh cho một yêu cầu/chu kỳ phản ứng.
Cách chính xác để triển khai plugin ServiceStack sử dụng cả yêu cầu và phản hồi là gì? I E. có một số IHttpRequest.Context để lưu trữ dữ liệu trong hoặc là nó có thể để có được yêu cầu dto tại thời gian phản ứng?
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();
}
}
Có vẻ câu hỏi của mình IHttpRequest/Response sẽ được thực hiện cụ thể. Giả sử thực hiện là ASP.NET, thông thường các công cụ có thể được lưu trữ trong bộ sưu tập HttpContext.Items. Thật không may, IHttpContext dường như không hiển thị nó ... http://msdn.microsoft.com/en-us/library/ms689291%28v=vs.90%29.aspx –