diff --git a/Source/TrackableEntities.Client/ServiceBase b/Source/TrackableEntities.Client/ServiceBase new file mode 100644 index 00000000..53a61bdc --- /dev/null +++ b/Source/TrackableEntities.Client/ServiceBase @@ -0,0 +1,93 @@ +using System; +using System.Collections.Generic; +using System.Net.Http; +using System.Threading.Tasks; + +namespace TrackableEntities.Client +{ + public abstract class ServiceBase where T:class + { + public string Request { get; set; } + public string BaseAddress { get; } + + + public HttpClient Client { get; } + + + protected ServiceBase(string baseAddress, string request,string token=null) + { + Request = request; + BaseAddress = baseAddress; + Client = new HttpClient() { BaseAddress = new Uri(baseAddress) }; + if (token != null) + { + Client.SetBearerToken(token); + } + } + + public async Task> GetAll() + { + var response = await Client.GetAsync(Request); + + response.EnsureSuccessStatusCode(); + + return await response.Content.ReadAsAsync>(); + } + + public async Task> GetAll(string route,ExpandoObject parameters) + { + StringBuilder uri =new StringBuilder(); + uri.Append($"{Request}").Append("/").Append(route); + + if (parameters != null) + { + uri.Append("?"); + foreach (KeyValuePair parameter in parameters) + { + uri.Append($"{parameter.Key}={parameter.Value}").Append("&"); + } + } + var response = await Client.GetAsync(uri.ToString().Substring(0,uri.ToString().Length-1)); + + response.EnsureSuccessStatusCode(); + + return await response.Content.ReadAsAsync>(); + } + + public async Task Update(T entity) + { + var response = await Client.PutAsJsonAsync(Request, entity); + + response.EnsureSuccessStatusCode(); + + return await response.Content.ReadAsAsync(); + } + + public async Task Create(T entity) + { + var response = await Client.PostAsJsonAsync(Request, entity); + + response.EnsureSuccessStatusCode(); + + return await response.Content.ReadAsAsync(); + } + + public async Task Find(TKey id) + { + var response = await Client.GetAsync($"{Request}/{id}"); + + response.EnsureSuccessStatusCode(); + + return await response.Content.ReadAsAsync(); + } + + public async Task VerifyDeleted(TKey id) + { + string request = Request + id; + var response = await Client.GetAsync(request); + if (response.IsSuccessStatusCode) return await Task.FromResult(false); + return await Task.FromResult(true); + } + + } +} diff --git a/Source/TrackableEntities.Patterns.EF.6/UnitOfWork.cs b/Source/TrackableEntities.Patterns.EF.6/UnitOfWork.cs index 2bcbab32..9ee2cd22 100644 --- a/Source/TrackableEntities.Patterns.EF.6/UnitOfWork.cs +++ b/Source/TrackableEntities.Patterns.EF.6/UnitOfWork.cs @@ -19,20 +19,23 @@ public abstract class UnitOfWork : IUnitOfWork, IUnitOfWorkAsync, IDisposable /// Constructs a new general unit of work. /// protected UnitOfWork() { } - + /// /// Constructs a new general unit of work. /// /// Entity Framework DbContext-derived class. - protected UnitOfWork(DbContext context) + protected UnitOfWork(DbContext context,IRepositoryFactory repositoryFactory) { Context = context; + RepositoryFactory=repositoryFactory; } /// /// Gets the DbContext for the unit of work. /// protected DbContext Context { get; set; } + + protected IRepositoryFactory RepositoryFactory {get;set;} /// /// Disposes the DbContext. diff --git a/Source/TrackableEntities.Patterns/IRepositoryFactory b/Source/TrackableEntities.Patterns/IRepositoryFactory new file mode 100644 index 00000000..d21627e2 --- /dev/null +++ b/Source/TrackableEntities.Patterns/IRepositoryFactory @@ -0,0 +1,4 @@ + public interface IRepositoryFactory + { + T GetDataRepository(); + }