mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-07-30 13:35:57 +08:00
renamed from service to repository, makes more sense i guess
This commit is contained in:
@ -0,0 +1,11 @@
|
||||
using Ocelot.Library.Infrastructure.Responses;
|
||||
|
||||
namespace Ocelot.Library.Infrastructure.Repository
|
||||
{
|
||||
public class CannotAddDataError : Error
|
||||
{
|
||||
public CannotAddDataError(string message) : base(message)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
using Ocelot.Library.Infrastructure.Responses;
|
||||
|
||||
namespace Ocelot.Library.Infrastructure.Repository
|
||||
{
|
||||
public class CannotFindDataError : Error
|
||||
{
|
||||
public CannotFindDataError(string message) : base(message)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
using Ocelot.Library.Infrastructure.Responses;
|
||||
|
||||
namespace Ocelot.Library.Infrastructure.Repository
|
||||
{
|
||||
public interface IScopedRequestDataRepository
|
||||
{
|
||||
Response Add<T>(string key, T value);
|
||||
Response<T> Get<T>(string key);
|
||||
}
|
||||
}
|
@ -0,0 +1,49 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Ocelot.Library.Infrastructure.Responses;
|
||||
|
||||
namespace Ocelot.Library.Infrastructure.Repository
|
||||
{
|
||||
public class ScopedRequestDataRepository : IScopedRequestDataRepository
|
||||
{
|
||||
private readonly IHttpContextAccessor _httpContextAccessor;
|
||||
|
||||
public ScopedRequestDataRepository(IHttpContextAccessor httpContextAccessor)
|
||||
{
|
||||
_httpContextAccessor = httpContextAccessor;
|
||||
}
|
||||
|
||||
public Response Add<T>(string key, T value)
|
||||
{
|
||||
try
|
||||
{
|
||||
_httpContextAccessor.HttpContext.Items.Add(key, value);
|
||||
return new OkResponse();
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
return new ErrorResponse(new List<Error>
|
||||
{
|
||||
new CannotAddDataError(string.Format($"Unable to add data for key: {key}, exception: {exception.Message}"))
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public Response<T> Get<T>(string key)
|
||||
{
|
||||
object obj;
|
||||
|
||||
if(_httpContextAccessor.HttpContext.Items.TryGetValue(key, out obj))
|
||||
{
|
||||
var data = (T) obj;
|
||||
return new OkResponse<T>(data);
|
||||
}
|
||||
|
||||
return new ErrorResponse<T>(new List<Error>
|
||||
{
|
||||
new CannotFindDataError($"Unable to find data for key: {key}")
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user