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+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
namespace NET6CustomLibrary.EFCore.Infrastructure.Repository;
2+
3+
public class UnitOfWork<TEntity, TKey> : IUnitOfWork<TEntity, TKey> where TEntity : class, IEntity<TKey>, new()
4+
{
5+
public DbContext DbContext { get; }
6+
public IDatabaseRepository<TEntity, TKey> ReadOnly { get; }
7+
public ICommandRepository<TEntity, TKey> Command { get; }
8+
9+
public UnitOfWork(DbContext dbContext, IDatabaseRepository<TEntity, TKey> databaseRepository, ICommandRepository<TEntity, TKey> commandRepository)
10+
{
11+
DbContext = dbContext;
12+
13+
ReadOnly = databaseRepository;
14+
Command = commandRepository;
15+
}
16+
17+
public void Dispose()
18+
{
19+
Dispose(true);
20+
GC.SuppressFinalize(this);
21+
}
22+
23+
protected virtual void Dispose(bool disposing)
24+
{
25+
if (disposing)
26+
{
27+
DbContext.Dispose();
28+
}
29+
}
30+
}

src/NET6CustomLibrary/Extensions/DependencyInjection.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
55
using Microsoft.AspNetCore.Http;
66
using Microsoft.AspNetCore.Routing;
7-
using Microsoft.EntityFrameworkCore;
87
using Microsoft.Extensions.Diagnostics.HealthChecks;
98
using Microsoft.OpenApi.Any;
109
using Microsoft.OpenApi.Models;
1110
using MySqlConnector;
1211
using NET6CustomLibrary.DateTime.Converters;
1312
using NET6CustomLibrary.DateTime.TypeConverters;
13+
using NET6CustomLibrary.EFCore.Infrastructure.Repository;
1414
using NET6CustomLibrary.Serilog.Services;
1515
using Serilog;
1616
using Swashbuckle.AspNetCore.SwaggerGen;
@@ -127,6 +127,18 @@ public static IMvcBuilder AddSimpleJsonOptions(this IMvcBuilder builder)
127127
}
128128
#endregion
129129

130+
#region "EFCORE DBContext Generics"
131+
public static IServiceCollection AddDbContextGenericsMethods<TDbContext>(this IServiceCollection services) where TDbContext : DbContext
132+
{
133+
services.AddScoped<DbContext, TDbContext>();
134+
services.AddScoped(typeof(IUnitOfWork<,>), typeof(UnitOfWork<,>));
135+
services.AddScoped(typeof(IDatabaseRepository<,>), typeof(DatabaseRepository<,>));
136+
services.AddScoped(typeof(ICommandRepository<,>), typeof(CommandRepository<,>));
137+
138+
return services;
139+
}
140+
#endregion
141+
130142
#region "EFCORE DBContext"
131143
public static IServiceCollection AddDbContextUseMySql<TDbContext>(this IServiceCollection services, string connectionString, int retryOnFailure) where TDbContext : DbContext
132144
{

src/NET6CustomLibrary/GlobalUsings.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,14 @@
66
global using System.Text.Json.Serialization;
77
global using System.Threading;
88
global using System.Threading.Tasks;
9+
global using EFCoreGeneric.Infrastructure.Interfaces;
10+
global using Microsoft.EntityFrameworkCore;
911
global using Microsoft.Extensions.DependencyInjection;
1012
global using Microsoft.Extensions.Hosting;
1113
global using Microsoft.Extensions.Logging;
14+
global using NET6CustomLibrary.EFCore.Core;
15+
global using NET6CustomLibrary.EFCore.Core.Interfaces;
16+
global using NET6CustomLibrary.EFCore.Infrastructure.Interfaces;
1217
global using NET6CustomLibrary.RabbitMQ.Abstractions;
1318
global using RabbitMQ.Client;
14-
global using RabbitMQ.Client.Events;
19+
global using RabbitMQ.Client.Events;
Lines changed: 43 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,49 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

3-
<PropertyGroup>
4-
<TargetFrameworks>net6.0</TargetFrameworks>
5-
<Authors>Angelo Pirola</Authors>
6-
<Product>.NET6 library custom extension</Product>
7-
<Description>open source custom dotnet extension library</Description>
8-
<PackageLicenseExpression>MIT</PackageLicenseExpression>
9-
<PackageProjectUrl>https://github.com/AngeloDotNet/NET6CustomLibrary</PackageProjectUrl>
10-
<PackageIcon>LibraryTools.png</PackageIcon>
11-
<PackageTags>csharp net6 open source custom dotnet extension library</PackageTags>
12-
<RepositoryType>git</RepositoryType>
13-
<RepositoryUrl>https://github.com/AngeloDotNet/NET6CustomLibrary.git</RepositoryUrl>
14-
<RepositoryBranch>main</RepositoryBranch>
15-
<PackageReadmeFile>README.md</PackageReadmeFile>
16-
</PropertyGroup>
3+
<PropertyGroup>
4+
<TargetFrameworks>net6.0</TargetFrameworks>
5+
<Authors>Angelo Pirola</Authors>
6+
<Product>.NET6 library custom extension</Product>
7+
<Description>open source custom dotnet extension library</Description>
8+
<PackageLicenseExpression>MIT</PackageLicenseExpression>
9+
<PackageProjectUrl>https://github.com/AngeloDotNet/NET6CustomLibrary</PackageProjectUrl>
10+
<PackageIcon>LibraryTools.png</PackageIcon>
11+
<PackageTags>csharp net6 open source custom dotnet extension library</PackageTags>
12+
<RepositoryType>git</RepositoryType>
13+
<RepositoryUrl>https://github.com/AngeloDotNet/NET6CustomLibrary.git</RepositoryUrl>
14+
<RepositoryBranch>main</RepositoryBranch>
15+
<PackageReadmeFile>README.md</PackageReadmeFile>
16+
</PropertyGroup>
1717

18-
<ItemGroup>
19-
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="7.0.0" />
20-
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="7.0.0" />
21-
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.0" />
22-
<PackageReference Include="RabbitMQ.Client" Version="6.4.0" />
23-
<PackageReference Include="Serilog.AspNetCore" Version="6.1.0" />
24-
<PackageReference Include="Serilog.Settings.Configuration" Version="3.4.0" />
25-
<PackageReference Include="Serilog.Sinks.Console" Version="4.1.0" />
26-
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
27-
<PackageReference Include="Microsoft.AspNetCore.Localization" Version="2.2.0" />
28-
<PackageReference Include="Nerdbank.GitVersioning" Version="3.5.119">
29-
<PrivateAssets>all</PrivateAssets>
30-
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
31-
</PackageReference>
32-
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="7.0.0" />
33-
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="6.5.0" />
34-
</ItemGroup>
18+
<ItemGroup>
19+
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.4" />
20+
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.4">
21+
<PrivateAssets>all</PrivateAssets>
22+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
23+
</PackageReference>
24+
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="7.0.0" />
25+
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="7.0.0" />
26+
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.0" />
27+
<PackageReference Include="RabbitMQ.Client" Version="6.4.0" />
28+
<PackageReference Include="Serilog.AspNetCore" Version="6.1.0" />
29+
<PackageReference Include="Serilog.Settings.Configuration" Version="3.4.0" />
30+
<PackageReference Include="Serilog.Sinks.Console" Version="4.1.0" />
31+
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
32+
<PackageReference Include="Microsoft.AspNetCore.Localization" Version="2.2.0" />
33+
<PackageReference Include="Nerdbank.GitVersioning" Version="3.5.119">
34+
<PrivateAssets>all</PrivateAssets>
35+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
36+
</PackageReference>
37+
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="7.0.0" />
38+
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="6.5.0" />
39+
</ItemGroup>
3540

36-
<ItemGroup>
37-
<None Include="..\..\LibraryTools.png">
38-
<Pack>True</Pack>
39-
<PackagePath></PackagePath>
40-
</None>
41-
<None Include="..\..\README.md" Pack="true" PackagePath="\" />
42-
</ItemGroup>
41+
<ItemGroup>
42+
<None Include="..\..\LibraryTools.png">
43+
<Pack>True</Pack>
44+
<PackagePath></PackagePath>
45+
</None>
46+
<None Include="..\..\README.md" Pack="true" PackagePath="\" />
47+
</ItemGroup>
4348

4449
</Project>

0 commit comments

Comments
 (0)