From b8696c8a70edbbb39b6cc49d3728d069d05cfd31 Mon Sep 17 00:00:00 2001 From: noufionline Date: Wed, 28 Sep 2016 16:16:34 +0400 Subject: [PATCH 01/10] Create UnitOfWork --- Source/TrackableEntities.Patterns/UnitOfWork | 93 ++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 Source/TrackableEntities.Patterns/UnitOfWork diff --git a/Source/TrackableEntities.Patterns/UnitOfWork b/Source/TrackableEntities.Patterns/UnitOfWork new file mode 100644 index 00000000..8adc109c --- /dev/null +++ b/Source/TrackableEntities.Patterns/UnitOfWork @@ -0,0 +1,93 @@ +using System; +using System.Threading; +using System.Threading.Tasks; + +/// +/// General unit of work for committing changes across one or more repositories. +/// Inherit from this class to supply a specific DbContext. +/// Add a property for each respository for which unit of work must be done. +/// +public abstract class UnitOfWork : IUnitOfWork, IUnitOfWorkAsync, IDisposable +{ + private readonly IRepositoryFactory _repositoryFactory; + private bool _disposed; + + /// + /// 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, IRepositoryFactory repositoryFactory) + { + _repositoryFactory = repositoryFactory; + Context = context; + } + + /// + /// Gets the DbContext for the unit of work. + /// + protected DbContext Context { get; set; } + + /// + /// Disposes the DbContext. + /// + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + + protected T GetRepository() + { + return _repositoryFactory.GetRepository(); + } + + /// + /// Disposes the DbContext. + /// + /// True to release both managed and unmanaged resources; false to release only unmanaged resources. + protected virtual void Dispose(bool disposing) + { + if (_disposed) return; + if (disposing) Context.Dispose(); + _disposed = true; + } + + /// + /// Saves changes made to one or more repositories. + /// + /// The number of objects saved. + public virtual int SaveChanges() + { + if (_disposed) + throw new ObjectDisposedException("UnitOfWork"); + return Context.SaveChanges(); + } + + /// + /// Saves changes made to one or more repositories. + /// + /// A task that represents the asynchronous save operation. The task result contains the number of objects saved. + public virtual async Task SaveChangesAsync() + { + if (_disposed) + throw new ObjectDisposedException("UnitOfWork"); + return await SaveChangesAsync(CancellationToken.None); + } + + /// + /// Saves changes made to one or more repositories. + /// + /// A CancellationToken to observe while waiting for the task to complete. + /// A task that represents the asynchronous save operation. The task result contains the number of objects saved. + public virtual async Task SaveChangesAsync(CancellationToken cancellationToken) + { + if (_disposed) + throw new ObjectDisposedException("UnitOfWork"); + return await Context.SaveChangesAsync(cancellationToken); + } +} From 5df666139d3ca6328d3c926b4768dd92646ac2e5 Mon Sep 17 00:00:00 2001 From: noufionline Date: Wed, 28 Sep 2016 16:17:53 +0400 Subject: [PATCH 02/10] Create UnitOfWork --- .../UnitOfWork | 94 +++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 Source/TrackableEntities.Patterns.EF.6/UnitOfWork diff --git a/Source/TrackableEntities.Patterns.EF.6/UnitOfWork b/Source/TrackableEntities.Patterns.EF.6/UnitOfWork new file mode 100644 index 00000000..6cbdba04 --- /dev/null +++ b/Source/TrackableEntities.Patterns.EF.6/UnitOfWork @@ -0,0 +1,94 @@ +using System; +using System.Threading; +using System.Threading.Tasks; + +/// +/// General unit of work for committing changes across one or more repositories. +/// Inherit from this class to supply a specific DbContext. +/// Add a property for each respository for which unit of work must be done. +/// +public abstract class UnitOfWork : IUnitOfWork, IUnitOfWorkAsync, IDisposable +{ + private readonly IRepositoryFactory _repositoryFactory; + private bool _disposed; + + /// + /// 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, IRepositoryFactory repositoryFactory) + { + _repositoryFactory = repositoryFactory; + Context = context; + } + + /// + /// Gets the DbContext for the unit of work. + /// + protected DbContext Context { get; set; } + + /// + /// Disposes the DbContext. + /// + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + + protected T GetRepository() + { + return _repositoryFactory.GetRepository(); + } + + /// + /// Disposes the DbContext. + /// + /// True to release both managed and unmanaged resources; false to release only unmanaged resources. + protected virtual void Dispose(bool disposing) + { + if (_disposed) return; + if (disposing) Context.Dispose(); + _disposed = true; + } + + /// + /// Saves changes made to one or more repositories. + /// + /// The number of objects saved. + public virtual int SaveChanges() + { + if (_disposed) + throw new ObjectDisposedException("UnitOfWork"); + return Context.SaveChanges(); + } + + /// + /// Saves changes made to one or more repositories. + /// + /// A task that represents the asynchronous save operation. The task result contains the number of objects saved. + public virtual async Task SaveChangesAsync() + { + if (_disposed) + throw new ObjectDisposedException("UnitOfWork"); + return await SaveChangesAsync(CancellationToken.None); + } + + /// + /// Saves changes made to one or more repositories. + /// + /// A CancellationToken to observe while waiting for the task to complete. + /// A task that represents the asynchronous save operation. The task result contains the number of objects saved. + public virtual async Task SaveChangesAsync(CancellationToken cancellationToken) + { + if (_disposed) + throw new ObjectDisposedException("UnitOfWork"); + return await Context.SaveChangesAsync(cancellationToken); + } +} + From 3cccfd8248e8db63dd7ab239f288884988428185 Mon Sep 17 00:00:00 2001 From: noufionline Date: Wed, 28 Sep 2016 16:20:23 +0400 Subject: [PATCH 03/10] UnitOfWork with RepositoryFactory --- Source/TrackableEntities.Patterns.EF.6/UnitOfWork.cs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) 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. From 3bcfd91966b30ae67ccd55302403426058b05952 Mon Sep 17 00:00:00 2001 From: noufionline Date: Wed, 28 Sep 2016 16:23:02 +0400 Subject: [PATCH 04/10] Generic Repository Factory --- Source/TrackableEntities.Patterns/IRepositoryFactory | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 Source/TrackableEntities.Patterns/IRepositoryFactory 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(); + } From 3bccd1b342c1638e3cdf8a01ebc714aed17555da Mon Sep 17 00:00:00 2001 From: noufionline Date: Wed, 28 Sep 2016 19:28:36 +0400 Subject: [PATCH 05/10] Client Helper class to consume service This will help to reduce the boilerplate code required to consume WebApi service --- Source/TrackableEntities.Client/ServiceBase | 69 +++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 Source/TrackableEntities.Client/ServiceBase diff --git a/Source/TrackableEntities.Client/ServiceBase b/Source/TrackableEntities.Client/ServiceBase new file mode 100644 index 00000000..6dfa4b1b --- /dev/null +++ b/Source/TrackableEntities.Client/ServiceBase @@ -0,0 +1,69 @@ +using System; +using System.Collections.Generic; +using System.Net.Http; +using System.Threading.Tasks; + +namespace ConsoleApplication1 +{ + 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) + { + Request = request; + BaseAddress = baseAddress; + Client = new HttpClient() { BaseAddress = new Uri(baseAddress) }; + } + + public async Task> GetAll() + { + var response = await Client.GetAsync(Request); + + 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); + } + + } +} From 3ce0744d756272fffdf2d003a74da1f5b327a777 Mon Sep 17 00:00:00 2001 From: noufionline Date: Wed, 28 Sep 2016 19:33:15 +0400 Subject: [PATCH 06/10] Update ServiceBase --- Source/TrackableEntities.Client/ServiceBase | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/TrackableEntities.Client/ServiceBase b/Source/TrackableEntities.Client/ServiceBase index 6dfa4b1b..5556ea07 100644 --- a/Source/TrackableEntities.Client/ServiceBase +++ b/Source/TrackableEntities.Client/ServiceBase @@ -3,7 +3,7 @@ using System.Collections.Generic; using System.Net.Http; using System.Threading.Tasks; -namespace ConsoleApplication1 +namespace TrackableEntities.Client { public abstract class ServiceBase where T:class { From 0542362b412c4b6e5c702f011efcbdb0b1dfc893 Mon Sep 17 00:00:00 2001 From: noufionline Date: Wed, 28 Sep 2016 20:37:35 +0400 Subject: [PATCH 07/10] Delete UnitOfWork --- Source/TrackableEntities.Patterns/UnitOfWork | 93 -------------------- 1 file changed, 93 deletions(-) delete mode 100644 Source/TrackableEntities.Patterns/UnitOfWork diff --git a/Source/TrackableEntities.Patterns/UnitOfWork b/Source/TrackableEntities.Patterns/UnitOfWork deleted file mode 100644 index 8adc109c..00000000 --- a/Source/TrackableEntities.Patterns/UnitOfWork +++ /dev/null @@ -1,93 +0,0 @@ -using System; -using System.Threading; -using System.Threading.Tasks; - -/// -/// General unit of work for committing changes across one or more repositories. -/// Inherit from this class to supply a specific DbContext. -/// Add a property for each respository for which unit of work must be done. -/// -public abstract class UnitOfWork : IUnitOfWork, IUnitOfWorkAsync, IDisposable -{ - private readonly IRepositoryFactory _repositoryFactory; - private bool _disposed; - - /// - /// 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, IRepositoryFactory repositoryFactory) - { - _repositoryFactory = repositoryFactory; - Context = context; - } - - /// - /// Gets the DbContext for the unit of work. - /// - protected DbContext Context { get; set; } - - /// - /// Disposes the DbContext. - /// - public void Dispose() - { - Dispose(true); - GC.SuppressFinalize(this); - } - - protected T GetRepository() - { - return _repositoryFactory.GetRepository(); - } - - /// - /// Disposes the DbContext. - /// - /// True to release both managed and unmanaged resources; false to release only unmanaged resources. - protected virtual void Dispose(bool disposing) - { - if (_disposed) return; - if (disposing) Context.Dispose(); - _disposed = true; - } - - /// - /// Saves changes made to one or more repositories. - /// - /// The number of objects saved. - public virtual int SaveChanges() - { - if (_disposed) - throw new ObjectDisposedException("UnitOfWork"); - return Context.SaveChanges(); - } - - /// - /// Saves changes made to one or more repositories. - /// - /// A task that represents the asynchronous save operation. The task result contains the number of objects saved. - public virtual async Task SaveChangesAsync() - { - if (_disposed) - throw new ObjectDisposedException("UnitOfWork"); - return await SaveChangesAsync(CancellationToken.None); - } - - /// - /// Saves changes made to one or more repositories. - /// - /// A CancellationToken to observe while waiting for the task to complete. - /// A task that represents the asynchronous save operation. The task result contains the number of objects saved. - public virtual async Task SaveChangesAsync(CancellationToken cancellationToken) - { - if (_disposed) - throw new ObjectDisposedException("UnitOfWork"); - return await Context.SaveChangesAsync(cancellationToken); - } -} From ad4f08da1c9a7c873bd8518c36f64bee3c194a04 Mon Sep 17 00:00:00 2001 From: noufionline Date: Wed, 28 Sep 2016 20:49:34 +0400 Subject: [PATCH 08/10] Delete UnitOfWork --- .../UnitOfWork | 94 ------------------- 1 file changed, 94 deletions(-) delete mode 100644 Source/TrackableEntities.Patterns.EF.6/UnitOfWork diff --git a/Source/TrackableEntities.Patterns.EF.6/UnitOfWork b/Source/TrackableEntities.Patterns.EF.6/UnitOfWork deleted file mode 100644 index 6cbdba04..00000000 --- a/Source/TrackableEntities.Patterns.EF.6/UnitOfWork +++ /dev/null @@ -1,94 +0,0 @@ -using System; -using System.Threading; -using System.Threading.Tasks; - -/// -/// General unit of work for committing changes across one or more repositories. -/// Inherit from this class to supply a specific DbContext. -/// Add a property for each respository for which unit of work must be done. -/// -public abstract class UnitOfWork : IUnitOfWork, IUnitOfWorkAsync, IDisposable -{ - private readonly IRepositoryFactory _repositoryFactory; - private bool _disposed; - - /// - /// 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, IRepositoryFactory repositoryFactory) - { - _repositoryFactory = repositoryFactory; - Context = context; - } - - /// - /// Gets the DbContext for the unit of work. - /// - protected DbContext Context { get; set; } - - /// - /// Disposes the DbContext. - /// - public void Dispose() - { - Dispose(true); - GC.SuppressFinalize(this); - } - - protected T GetRepository() - { - return _repositoryFactory.GetRepository(); - } - - /// - /// Disposes the DbContext. - /// - /// True to release both managed and unmanaged resources; false to release only unmanaged resources. - protected virtual void Dispose(bool disposing) - { - if (_disposed) return; - if (disposing) Context.Dispose(); - _disposed = true; - } - - /// - /// Saves changes made to one or more repositories. - /// - /// The number of objects saved. - public virtual int SaveChanges() - { - if (_disposed) - throw new ObjectDisposedException("UnitOfWork"); - return Context.SaveChanges(); - } - - /// - /// Saves changes made to one or more repositories. - /// - /// A task that represents the asynchronous save operation. The task result contains the number of objects saved. - public virtual async Task SaveChangesAsync() - { - if (_disposed) - throw new ObjectDisposedException("UnitOfWork"); - return await SaveChangesAsync(CancellationToken.None); - } - - /// - /// Saves changes made to one or more repositories. - /// - /// A CancellationToken to observe while waiting for the task to complete. - /// A task that represents the asynchronous save operation. The task result contains the number of objects saved. - public virtual async Task SaveChangesAsync(CancellationToken cancellationToken) - { - if (_disposed) - throw new ObjectDisposedException("UnitOfWork"); - return await Context.SaveChangesAsync(cancellationToken); - } -} - From d6631c8c314c371ba19100c09272dbfee8a7f08d Mon Sep 17 00:00:00 2001 From: noufionline Date: Thu, 29 Sep 2016 11:41:16 +0400 Subject: [PATCH 09/10] Added new overload for GetAll This new overload will help to pass parameter dynamically and consume using querystring --- Source/TrackableEntities.Client/ServiceBase | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/Source/TrackableEntities.Client/ServiceBase b/Source/TrackableEntities.Client/ServiceBase index 5556ea07..4c8dc182 100644 --- a/Source/TrackableEntities.Client/ServiceBase +++ b/Source/TrackableEntities.Client/ServiceBase @@ -30,6 +30,26 @@ namespace TrackableEntities.Client 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); From a745ee4ec2dc6898f6a6dbdd1aa2e84c2ba7af04 Mon Sep 17 00:00:00 2001 From: noufionline Date: Fri, 30 Sep 2016 13:50:20 +0400 Subject: [PATCH 10/10] Added optional token parameter to constructor --- Source/TrackableEntities.Client/ServiceBase | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Source/TrackableEntities.Client/ServiceBase b/Source/TrackableEntities.Client/ServiceBase index 4c8dc182..53a61bdc 100644 --- a/Source/TrackableEntities.Client/ServiceBase +++ b/Source/TrackableEntities.Client/ServiceBase @@ -14,11 +14,15 @@ namespace TrackableEntities.Client public HttpClient Client { get; } - protected ServiceBase(string baseAddress, string request) + 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()