From bad676922e18c7186c1a4375afd8534bfa8f91c9 Mon Sep 17 00:00:00 2001 From: Wise Duho Date: Wed, 28 Apr 2021 13:38:27 +0000 Subject: [PATCH] updated generic repositories to factor in non integer id's added iauditable entity as base for all auditable entities. Iauditable entiity can then be used in the saved changes method to get all entries --- .../Interfaces/IGenericRepositoryAsync.cs | 9 +++++++-- Domain/Common/AuditableBaseEntity.cs | 14 ++++++++++---- Domain/Common/BaseEntity.cs | 10 ++++++++-- Domain/Common/IAuditableEntity.cs | 17 +++++++++++++++++ Domain/Common/IEntity.cs | 7 +++++++ Domain/Domain.csproj | 4 ++++ .../Contexts/ApplicationDbContext.cs | 2 +- .../Repositories/GenericRepositoryAsync.cs | 13 +++++++++++-- .../ServiceRegistration.cs | 1 + 9 files changed, 66 insertions(+), 11 deletions(-) create mode 100644 Domain/Common/IAuditableEntity.cs create mode 100644 Domain/Common/IEntity.cs diff --git a/Application/Interfaces/IGenericRepositoryAsync.cs b/Application/Interfaces/IGenericRepositoryAsync.cs index e5147ec..fb23a92 100644 --- a/Application/Interfaces/IGenericRepositoryAsync.cs +++ b/Application/Interfaces/IGenericRepositoryAsync.cs @@ -5,13 +5,18 @@ namespace Application.Interfaces { - public interface IGenericRepositoryAsync where T : class + public interface IGenericRepositoryAsync where T : class { - Task GetByIdAsync(int id); + Task GetByIdAsync(TId id); Task> GetAllAsync(); Task> GetPagedReponseAsync(int pageNumber, int pageSize); Task AddAsync(T entity); Task UpdateAsync(T entity); Task DeleteAsync(T entity); } + + public interface IGenericRepositoryAsync : IGenericRepositoryAsync where T : class + { + + } } diff --git a/Domain/Common/AuditableBaseEntity.cs b/Domain/Common/AuditableBaseEntity.cs index 3baa8af..4d455fd 100644 --- a/Domain/Common/AuditableBaseEntity.cs +++ b/Domain/Common/AuditableBaseEntity.cs @@ -1,15 +1,21 @@ using System; using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; using System.Text; namespace Domain.Common { - public abstract class AuditableBaseEntity + public abstract class AuditableBaseEntity : BaseEntity, IAuditableEntity { - public virtual int Id { get; set; } - public string CreatedBy { get; set; } + [StringLength(40)] public string CreatedBy { get; set; } public DateTime Created { get; set; } - public string LastModifiedBy { get; set; } + [StringLength(40)] public string LastModifiedBy { get; set; } public DateTime? LastModified { get; set; } + } + + + public abstract class AuditableBaseEntity:AuditableBaseEntity + { + } } diff --git a/Domain/Common/BaseEntity.cs b/Domain/Common/BaseEntity.cs index a01b261..c26ff54 100644 --- a/Domain/Common/BaseEntity.cs +++ b/Domain/Common/BaseEntity.cs @@ -1,11 +1,17 @@ using System; using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; using System.Text; namespace Domain.Common { - public abstract class BaseEntity + public abstract class BaseEntity:IEntity + { + [Key] public virtual T Id { get; set; } + } + + public abstract class BaseEntity:IEntity { public virtual int Id { get; set; } } -} +} \ No newline at end of file diff --git a/Domain/Common/IAuditableEntity.cs b/Domain/Common/IAuditableEntity.cs new file mode 100644 index 0000000..e004126 --- /dev/null +++ b/Domain/Common/IAuditableEntity.cs @@ -0,0 +1,17 @@ +using System; + +namespace Domain.Common +{ + public interface IAuditableEntity : IAuditableEntity + { + public T Id { get; set; } + } + + public interface IAuditableEntity + { + public string CreatedBy { get; set; } + public DateTime Created { get; set; } + public string LastModifiedBy { get; set; } + public DateTime? LastModified { get; set; } + } +} \ No newline at end of file diff --git a/Domain/Common/IEntity.cs b/Domain/Common/IEntity.cs new file mode 100644 index 0000000..92086c8 --- /dev/null +++ b/Domain/Common/IEntity.cs @@ -0,0 +1,7 @@ +namespace Domain.Common +{ + public interface IEntity + { + public T Id { get; set; } + } +} \ No newline at end of file diff --git a/Domain/Domain.csproj b/Domain/Domain.csproj index 766038a..bd79ce5 100644 --- a/Domain/Domain.csproj +++ b/Domain/Domain.csproj @@ -5,4 +5,8 @@ 1.1.0 + + + + diff --git a/Infrastructure.Persistence/Contexts/ApplicationDbContext.cs b/Infrastructure.Persistence/Contexts/ApplicationDbContext.cs index 4128893..b522381 100644 --- a/Infrastructure.Persistence/Contexts/ApplicationDbContext.cs +++ b/Infrastructure.Persistence/Contexts/ApplicationDbContext.cs @@ -27,7 +27,7 @@ public ApplicationDbContext(DbContextOptions options, IDat public override Task SaveChangesAsync(CancellationToken cancellationToken = new CancellationToken()) { - foreach (var entry in ChangeTracker.Entries()) + foreach (var entry in ChangeTracker.Entries()) { switch (entry.State) { diff --git a/Infrastructure.Persistence/Repositories/GenericRepositoryAsync.cs b/Infrastructure.Persistence/Repositories/GenericRepositoryAsync.cs index 91a459e..8193d65 100644 --- a/Infrastructure.Persistence/Repositories/GenericRepositoryAsync.cs +++ b/Infrastructure.Persistence/Repositories/GenericRepositoryAsync.cs @@ -9,7 +9,7 @@ namespace Infrastructure.Persistence.Repository { - public class GenericRepositoryAsync : IGenericRepositoryAsync where T : class + public class GenericRepositoryAsync : IGenericRepositoryAsync where T : class { private readonly ApplicationDbContext _dbContext; @@ -18,7 +18,7 @@ public GenericRepositoryAsync(ApplicationDbContext dbContext) _dbContext = dbContext; } - public virtual async Task GetByIdAsync(int id) + public virtual async Task GetByIdAsync(TId id) { return await _dbContext.Set().FindAsync(id); } @@ -58,5 +58,14 @@ public async Task> GetAllAsync() .Set() .ToListAsync(); } + + + } + + public class GenericRepositoryAsync : GenericRepositoryAsync, IGenericRepositoryAsync where T : class + { + public GenericRepositoryAsync(ApplicationDbContext dbContext) : base(dbContext) + { + } } } diff --git a/Infrastructure.Persistence/ServiceRegistration.cs b/Infrastructure.Persistence/ServiceRegistration.cs index c3419da..c008d03 100644 --- a/Infrastructure.Persistence/ServiceRegistration.cs +++ b/Infrastructure.Persistence/ServiceRegistration.cs @@ -30,6 +30,7 @@ public static void AddPersistenceInfrastructure(this IServiceCollection services } #region Repositories services.AddTransient(typeof(IGenericRepositoryAsync<>), typeof(GenericRepositoryAsync<>)); + services.AddTransient(typeof(IGenericRepositoryAsync<,>), typeof(GenericRepositoryAsync<,>)); services.AddTransient(); #endregion }