From bda98b96433d7a9952524fab4ec65f96998b55de Mon Sep 17 00:00:00 2001 From: transtrike Date: Thu, 21 Jan 2021 22:08:50 +0200 Subject: Generic base repo class refactored; All repos inherit generic base repo --- src/DevHive.Data/Repositories/BaseRepository.cs | 54 +++++++++++++++++++++- .../Repositories/LanguageRepository.cs | 43 +---------------- src/DevHive.Data/Repositories/PostRepository.cs | 34 +------------- src/DevHive.Data/Repositories/RoleRepository.cs | 43 +---------------- .../Repositories/TechnologyRepository.cs | 39 +--------------- src/DevHive.Data/Repositories/UserRepository.cs | 41 ++-------------- 6 files changed, 63 insertions(+), 191 deletions(-) (limited to 'src/DevHive.Data/Repositories') diff --git a/src/DevHive.Data/Repositories/BaseRepository.cs b/src/DevHive.Data/Repositories/BaseRepository.cs index b0f0f3e..dabb35b 100644 --- a/src/DevHive.Data/Repositories/BaseRepository.cs +++ b/src/DevHive.Data/Repositories/BaseRepository.cs @@ -1,11 +1,61 @@ +using System; using System.Threading.Tasks; +using DevHive.Data.Repositories.Interfaces; using Microsoft.EntityFrameworkCore; namespace DevHive.Data.Repositories { - public class BaseRepository + public class BaseRepository : IRepository + where TEntity : class { - public async Task SaveChangesAsync(DbContext context) + private readonly DbContext _context; + + public BaseRepository(DbContext context) + { + this._context = context; + this._context.ChangeTracker.AutoDetectChangesEnabled = false; + } + + public virtual async Task AddAsync(TEntity entity) + { + await this._context + .Set() + .AddAsync(entity); + + return await this.SaveChangesAsync(_context); + } + + public virtual async Task GetByIdAsync(Guid id) + { + return await this._context + .Set() + .FindAsync(id); + } + + public virtual async Task EditAsync(TEntity newEntity) + { + // Old way(backup) + // User user = await this._context.Users + // .FirstOrDefaultAsync(x => x.Id == entity.Id); + + // this._context.Update(user); + // this._context.Entry(entity).CurrentValues.SetValues(entity); + + this._context + .Set() + .Update(newEntity); + + return await this.SaveChangesAsync(_context); + } + + public virtual async Task DeleteAsync(TEntity entity) + { + this._context.Remove(entity); + + return await this.SaveChangesAsync(_context); + } + + public virtual async Task SaveChangesAsync(DbContext context) { int result = await context.SaveChangesAsync(); diff --git a/src/DevHive.Data/Repositories/LanguageRepository.cs b/src/DevHive.Data/Repositories/LanguageRepository.cs index 59c88a6..d7ee609 100644 --- a/src/DevHive.Data/Repositories/LanguageRepository.cs +++ b/src/DevHive.Data/Repositories/LanguageRepository.cs @@ -1,38 +1,22 @@ using System; using System.Threading.Tasks; -using DevHive.Common.Models.Misc; using DevHive.Data.Interfaces.Repositories; using DevHive.Data.Models; using Microsoft.EntityFrameworkCore; namespace DevHive.Data.Repositories { - public class LanguageRepository : BaseRepository, ILanguageRepository + public class LanguageRepository : BaseRepository, ILanguageRepository { private readonly DevHiveContext _context; public LanguageRepository(DevHiveContext context) + :base(context) { this._context = context; } - #region Create - public async Task AddAsync(Language entity) - { - await this._context.Languages - .AddAsync(entity); - - return await this.SaveChangesAsync(this._context); - } - #endregion - #region Read - public async Task GetByIdAsync(Guid id) - { - return await this._context.Languages - .FindAsync(id); - } - public async Task GetByNameAsync(string languageName) { return await this._context.Languages @@ -41,29 +25,6 @@ namespace DevHive.Data.Repositories } #endregion - #region Update - - public async Task EditAsync(Language entity) - { - Language language = await this._context.Languages - .FirstOrDefaultAsync(x => x.Id == entity.Id); - - this._context.Update(language); - this._context.Entry(entity).CurrentValues.SetValues(entity); - - return await this.SaveChangesAsync(this._context); - } - #endregion - - #region Delete - public async Task DeleteAsync(Language entity) - { - this._context.Languages.Remove(entity); - - return await this.SaveChangesAsync(this._context); - } - #endregion - #region Validations public async Task DoesLanguageNameExistAsync(string languageName) { diff --git a/src/DevHive.Data/Repositories/PostRepository.cs b/src/DevHive.Data/Repositories/PostRepository.cs index 71602e7..9230a2e 100644 --- a/src/DevHive.Data/Repositories/PostRepository.cs +++ b/src/DevHive.Data/Repositories/PostRepository.cs @@ -1,30 +1,22 @@ using System; using System.Threading.Tasks; -using DevHive.Common.Models.Misc; using DevHive.Data.Interfaces.Repositories; using DevHive.Data.Models; using Microsoft.EntityFrameworkCore; namespace DevHive.Data.Repositories { - public class PostRepository : BaseRepository, IPostRepository + public class PostRepository : BaseRepository, IPostRepository { private readonly DevHiveContext _context; public PostRepository(DevHiveContext context) + : base(context) { this._context = context; } #region Create - public async Task AddAsync(Post post) - { - await this._context.Posts - .AddAsync(post); - - return await this.SaveChangesAsync(this._context); - } - public async Task AddCommentAsync(Comment entity) { await this._context.Comments @@ -35,12 +27,6 @@ namespace DevHive.Data.Repositories #endregion #region Read - public async Task GetByIdAsync(Guid id) - { - return await this._context.Posts - .FindAsync(id); - } - public async Task GetPostByIssuerAndTimeCreatedAsync(Guid issuerId, DateTime timeCreated) { return await this._context.Posts @@ -63,14 +49,6 @@ namespace DevHive.Data.Repositories #endregion #region Update - public async Task EditAsync(Post newPost) - { - this._context.Posts - .Update(newPost); - - return await this.SaveChangesAsync(this._context); - } - public async Task EditCommentAsync(Comment newEntity) { this._context.Comments @@ -81,14 +59,6 @@ namespace DevHive.Data.Repositories #endregion #region Delete - public async Task DeleteAsync(Post post) - { - this._context.Posts - .Remove(post); - - return await this.SaveChangesAsync(this._context); - } - public async Task DeleteCommentAsync(Comment entity) { this._context.Comments diff --git a/src/DevHive.Data/Repositories/RoleRepository.cs b/src/DevHive.Data/Repositories/RoleRepository.cs index 25549bf..156792d 100644 --- a/src/DevHive.Data/Repositories/RoleRepository.cs +++ b/src/DevHive.Data/Repositories/RoleRepository.cs @@ -6,32 +6,17 @@ using Microsoft.EntityFrameworkCore; namespace DevHive.Data.Repositories { - public class RoleRepository : BaseRepository, IRoleRepository + public class RoleRepository : BaseRepository, IRoleRepository { private readonly DevHiveContext _context; public RoleRepository(DevHiveContext context) + :base(context) { this._context = context; } - #region Create - public async Task AddAsync(Role entity) - { - await this._context.Roles - .AddAsync(entity); - - return await this.SaveChangesAsync(this._context); - } - #endregion - #region Read - public async Task GetByIdAsync(Guid id) - { - return await this._context.Roles - .FindAsync(id); - } - public async Task GetByNameAsync(string name) { return await this._context.Roles @@ -39,30 +24,6 @@ namespace DevHive.Data.Repositories } #endregion - #region Update - public async Task EditAsync(Role newEntity) - { - Role role = await this.GetByIdAsync(newEntity.Id); - - this._context - .Entry(role) - .CurrentValues - .SetValues(newEntity); - - return await this.SaveChangesAsync(this._context); - } - #endregion - - #region Delete - public async Task DeleteAsync(Role entity) - { - this._context.Roles - .Remove(entity); - - return await this.SaveChangesAsync(this._context); - } - #endregion - #region Validations public async Task DoesNameExist(string name) { diff --git a/src/DevHive.Data/Repositories/TechnologyRepository.cs b/src/DevHive.Data/Repositories/TechnologyRepository.cs index 35ee567..83cc7aa 100644 --- a/src/DevHive.Data/Repositories/TechnologyRepository.cs +++ b/src/DevHive.Data/Repositories/TechnologyRepository.cs @@ -1,38 +1,22 @@ using System; using System.Threading.Tasks; -using DevHive.Common.Models.Misc; using DevHive.Data.Interfaces.Repositories; using DevHive.Data.Models; using Microsoft.EntityFrameworkCore; - namespace DevHive.Data.Repositories { - public class TechnologyRepository : BaseRepository, ITechnologyRepository + public class TechnologyRepository : BaseRepository, ITechnologyRepository { private readonly DevHiveContext _context; public TechnologyRepository(DevHiveContext context) + :base(context) { this._context = context; } - #region Create - public async Task AddAsync(Technology entity) - { - await this._context.Technologies - .AddAsync(entity); - - return await this.SaveChangesAsync(this._context); - } - #endregion - #region Read - public async Task GetByIdAsync(Guid id) - { - return await this._context.Technologies - .FindAsync(id); - } public async Task GetByNameAsync(string technologyName) { return await this._context.Technologies @@ -41,25 +25,6 @@ namespace DevHive.Data.Repositories } #endregion - #region Edit - public async Task EditAsync(Technology newEntity) - { - this._context.Technologies.Update(newEntity); - - return await this.SaveChangesAsync(this._context); - } - #endregion - - #region Delete - public async Task DeleteAsync(Technology entity) - { - this._context.Technologies - .Remove(entity); - - return await this.SaveChangesAsync(this._context); - } - #endregion - #region Validations public async Task DoesTechnologyNameExistAsync(string technologyName) { diff --git a/src/DevHive.Data/Repositories/UserRepository.cs b/src/DevHive.Data/Repositories/UserRepository.cs index f0c28f1..1511c63 100644 --- a/src/DevHive.Data/Repositories/UserRepository.cs +++ b/src/DevHive.Data/Repositories/UserRepository.cs @@ -8,26 +8,16 @@ using Microsoft.EntityFrameworkCore; namespace DevHive.Data.Repositories { - public class UserRepository : BaseRepository, IUserRepository + public class UserRepository : BaseRepository, IUserRepository { private readonly DevHiveContext _context; public UserRepository(DevHiveContext context) + :base(context) { this._context = context; } - #region Create - - public async Task AddAsync(User entity) - { - await this._context.Users - .AddAsync(entity); - - return await this.SaveChangesAsync(this._context); - } - #endregion - #region Read public IEnumerable QueryAll() @@ -38,7 +28,7 @@ namespace DevHive.Data.Repositories .AsEnumerable(); } - public async Task GetByIdAsync(Guid id) + public override async Task GetByIdAsync(Guid id) { return await this._context.Users .Include(x => x.Friends) @@ -80,31 +70,6 @@ namespace DevHive.Data.Repositories } #endregion - #region Update - - public async Task EditAsync(User entity) - { - User user = await this._context.Users - .FirstOrDefaultAsync(x => x.Id == entity.Id); - - this._context.Update(user); - this._context.Entry(entity).CurrentValues.SetValues(entity); - - return await this.SaveChangesAsync(this._context); - } - #endregion - - #region Delete - - public async Task DeleteAsync(User entity) - { - this._context.Users - .Remove(entity); - - return await this.SaveChangesAsync(this._context); - } - #endregion - #region Validations public async Task DoesUserExistAsync(Guid id) -- cgit v1.2.3