Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updated generic repositories to factor in non integer id's #34

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions Application/Interfaces/IGenericRepositoryAsync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,18 @@

namespace Application.Interfaces
{
public interface IGenericRepositoryAsync<T> where T : class
public interface IGenericRepositoryAsync<T,in TId> where T : class
{
Task<T> GetByIdAsync(int id);
Task<T> GetByIdAsync(TId id);
Task<IReadOnlyList<T>> GetAllAsync();
Task<IReadOnlyList<T>> GetPagedReponseAsync(int pageNumber, int pageSize);
Task<T> AddAsync(T entity);
Task UpdateAsync(T entity);
Task DeleteAsync(T entity);
}

public interface IGenericRepositoryAsync<T> : IGenericRepositoryAsync<T, int> where T : class
{

}
}
14 changes: 10 additions & 4 deletions Domain/Common/AuditableBaseEntity.cs
Original file line number Diff line number Diff line change
@@ -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<T> : BaseEntity<T>, 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<int>
{

}
}
10 changes: 8 additions & 2 deletions Domain/Common/BaseEntity.cs
Original file line number Diff line number Diff line change
@@ -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<T>:IEntity<T>
{
[Key] public virtual T Id { get; set; }
}

public abstract class BaseEntity:IEntity<int>
{
public virtual int Id { get; set; }
}
}
}
17 changes: 17 additions & 0 deletions Domain/Common/IAuditableEntity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;

namespace Domain.Common
{
public interface IAuditableEntity<T> : 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; }
}
}
7 changes: 7 additions & 0 deletions Domain/Common/IEntity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Domain.Common
{
public interface IEntity<T>
{
public T Id { get; set; }
}
}
4 changes: 4 additions & 0 deletions Domain/Domain.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,8 @@
<Version>1.1.0</Version>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="System.ComponentModel.Annotations" Version="5.0.0" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options, IDat

public override Task<int> SaveChangesAsync(CancellationToken cancellationToken = new CancellationToken())
{
foreach (var entry in ChangeTracker.Entries<AuditableBaseEntity>())
foreach (var entry in ChangeTracker.Entries<IAuditableEntity>())
{
switch (entry.State)
{
Expand Down
13 changes: 11 additions & 2 deletions Infrastructure.Persistence/Repositories/GenericRepositoryAsync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace Infrastructure.Persistence.Repository
{
public class GenericRepositoryAsync<T> : IGenericRepositoryAsync<T> where T : class
public class GenericRepositoryAsync<T, TId> : IGenericRepositoryAsync<T,TId> where T : class
{
private readonly ApplicationDbContext _dbContext;

Expand All @@ -18,7 +18,7 @@ public GenericRepositoryAsync(ApplicationDbContext dbContext)
_dbContext = dbContext;
}

public virtual async Task<T> GetByIdAsync(int id)
public virtual async Task<T> GetByIdAsync(TId id)
{
return await _dbContext.Set<T>().FindAsync(id);
}
Expand Down Expand Up @@ -58,5 +58,14 @@ public async Task<IReadOnlyList<T>> GetAllAsync()
.Set<T>()
.ToListAsync();
}


}

public class GenericRepositoryAsync<T> : GenericRepositoryAsync<T, int>, IGenericRepositoryAsync<T> where T : class
{
public GenericRepositoryAsync(ApplicationDbContext dbContext) : base(dbContext)
{
}
}
}
1 change: 1 addition & 0 deletions Infrastructure.Persistence/ServiceRegistration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<IProductRepositoryAsync, ProductRepositoryAsync>();
#endregion
}
Expand Down