Skip to content
This repository was archived by the owner on Apr 17, 2025. It is now read-only.

Commit b809485

Browse files
author
Angelo Pirola
committed
Aggiunto DbContext generico e relativi metodi
1 parent 6ff6223 commit b809485

File tree

14 files changed

+245
-40
lines changed

14 files changed

+245
-40
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
namespace NET6CustomLibrary.EFCore.Core;
2+
3+
public class Command<TEntity, TKey> : ICommand<TEntity, TKey> where TEntity : class, IEntity<TKey>, new()
4+
{
5+
public DbContext DbContext { get; }
6+
7+
public Command(DbContext dbContext)
8+
{
9+
DbContext = dbContext ?? throw new ArgumentNullException(nameof(dbContext));
10+
}
11+
12+
public async Task CreateAsync(TEntity entity)
13+
{
14+
DbContext.Set<TEntity>().Add(entity);
15+
16+
await DbContext.SaveChangesAsync();
17+
18+
DbContext.Entry(entity).State = EntityState.Detached;
19+
}
20+
21+
public async Task UpdateAsync(TEntity entity)
22+
{
23+
DbContext.Set<TEntity>().Update(entity);
24+
25+
await DbContext.SaveChangesAsync();
26+
27+
DbContext.Entry(entity).State = EntityState.Detached;
28+
}
29+
30+
public async Task DeleteAsync(TEntity entity)
31+
{
32+
DbContext.Set<TEntity>().Remove(entity);
33+
34+
await DbContext.SaveChangesAsync();
35+
}
36+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
namespace NET6CustomLibrary.EFCore.Core;
2+
3+
public class Database<TEntity, TKey> : IDatabase<TEntity, TKey> where TEntity : class, IEntity<TKey>, new()
4+
{
5+
public DbContext DbContext { get; }
6+
7+
public Database(DbContext dbContext)
8+
{
9+
DbContext = dbContext ?? throw new ArgumentNullException(nameof(dbContext));
10+
}
11+
12+
public async Task<List<TEntity>> GetAllAsync()
13+
{
14+
return await DbContext.Set<TEntity>()
15+
.AsNoTracking()
16+
.ToListAsync();
17+
}
18+
19+
public async Task<TEntity> GetByIdAsync(TKey id)
20+
{
21+
var entity = await DbContext.Set<TEntity>()
22+
.FindAsync(id);
23+
24+
if (entity == null)
25+
{
26+
return null;
27+
}
28+
29+
DbContext.Entry(entity).State = EntityState.Detached;
30+
31+
return entity;
32+
}
33+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
namespace NET6CustomLibrary.EFCore.Core.Interfaces;
2+
3+
public interface ICommand<TEntity, TKey> where TEntity : class, IEntity<TKey>, new()
4+
{
5+
/// <summary>
6+
/// Create an item.
7+
/// </summary>
8+
/// <param name="entity"></param>
9+
/// <returns></returns>
10+
Task CreateAsync(TEntity entity);
11+
12+
/// <summary>
13+
/// Update an item.
14+
/// </summary>
15+
/// <param name="entity"></param>
16+
/// <returns></returns>
17+
Task UpdateAsync(TEntity entity);
18+
19+
/// <summary>
20+
/// Delete an item.
21+
/// </summary>
22+
/// <param name="entity"></param>
23+
/// <returns></returns>
24+
Task DeleteAsync(TEntity entity);
25+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
namespace NET6CustomLibrary.EFCore.Core.Interfaces;
2+
3+
public interface IDatabase<TEntity, TKey> where TEntity : class, IEntity<TKey>, new()
4+
{
5+
/// <summary>
6+
/// Get the list of items.
7+
/// </summary>
8+
/// <returns></returns>
9+
Task<List<TEntity>> GetAllAsync();
10+
11+
/// <summary>
12+
/// Gets the item with the specified identifier.
13+
/// </summary>
14+
/// <param name="id"></param>
15+
/// <returns></returns>
16+
Task<TEntity> GetByIdAsync(TKey id);
17+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace NET6CustomLibrary.EFCore.Core.Interfaces;
2+
3+
public interface IEntity<TKey>
4+
{
5+
/// <summary>
6+
/// Defines the type of the ID field
7+
/// </summary>
8+
TKey Id { get; set; }
9+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
namespace NET6CustomLibrary.EFCore.Infrastructure.Interfaces;
2+
3+
public interface ICommandRepository<TEntity, TKey> : ICommand<TEntity, TKey> where TEntity : class, IEntity<TKey>, new()
4+
{
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
namespace EFCoreGeneric.Infrastructure.Interfaces;
2+
3+
public interface IDatabaseRepository<TEntity, TKey> : IDatabase<TEntity, TKey> where TEntity : class, IEntity<TKey>, new()
4+
{
5+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace NET6CustomLibrary.EFCore.Infrastructure.Interfaces;
2+
3+
public interface IUnitOfWork<TEntity, TKey> : IDisposable where TEntity : class, IEntity<TKey>, new()
4+
{
5+
IDatabaseRepository<TEntity, TKey> ReadOnly { get; }
6+
ICommandRepository<TEntity, TKey> Command { get; }
7+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace NET6CustomLibrary.EFCore.Infrastructure.Repository;
2+
3+
public class CommandRepository<TEntity, TKey> : Command<TEntity, TKey>, ICommandRepository<TEntity, TKey> where TEntity : class, IEntity<TKey>, new()
4+
{
5+
public CommandRepository(DbContext dbContext) : base(dbContext)
6+
{
7+
}
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace NET6CustomLibrary.EFCore.Infrastructure.Repository;
2+
3+
public class DatabaseRepository<TEntity, TKey> : Database<TEntity, TKey>, IDatabaseRepository<TEntity, TKey> where TEntity : class, IEntity<TKey>, new()
4+
{
5+
public DatabaseRepository(DbContext dbContext) : base(dbContext)
6+
{
7+
}
8+
}

0 commit comments

Comments
 (0)