renamed from service to repository, makes more sense i guess

This commit is contained in:
TomPallister
2016-10-05 20:51:02 +01:00
parent f505fd6e90
commit d86a52c1b8
9 changed files with 10 additions and 10 deletions

View File

@ -0,0 +1,11 @@
using Ocelot.Library.Infrastructure.Responses;
namespace Ocelot.Library.Infrastructure.Repository
{
public class CannotAddDataError : Error
{
public CannotAddDataError(string message) : base(message)
{
}
}
}

View File

@ -0,0 +1,11 @@
using Ocelot.Library.Infrastructure.Responses;
namespace Ocelot.Library.Infrastructure.Repository
{
public class CannotFindDataError : Error
{
public CannotFindDataError(string message) : base(message)
{
}
}
}

View File

@ -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);
}
}

View File

@ -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}")
});
}
}
}