Skip to content

Commit

Permalink
NET8
Browse files Browse the repository at this point in the history
  • Loading branch information
TheTrigger committed Dec 12, 2023
1 parent 29c5864 commit 9354699
Show file tree
Hide file tree
Showing 10 changed files with 179 additions and 119 deletions.
40 changes: 20 additions & 20 deletions Oibi.Repository.Demo.Models/Oibi.Repository.Demo.Models.csproj
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
</PropertyGroup>
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.4">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="7.0.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.4">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="7.0.3" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL.Design" Version="1.1.0" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="8.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.0" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL.Design" Version="1.1.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Oibi.Repository\Oibi.Repository.csproj" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Oibi.Repository\Oibi.Repository.csproj" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion Oibi.Repository.Demo/Controllers/LibraryController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public LibraryController(AuthorRepository authorRepository, IMapper mapper)
}

[HttpGet]
public IActionResult Get()
public ActionResult<AuthorDto> Get()
{
var dto = _authorRepository.GetAuthorsAndBooks<AuthorDto>();

Expand Down
42 changes: 21 additions & 21 deletions Oibi.Repository.Demo/Oibi.Repository.Demo.csproj
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
</PropertyGroup>
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Oibi.Repository.Demo.Models\Oibi.Repository.Demo.Models.csproj" />
<ProjectReference Include="..\Oibi.Repository\Oibi.Repository.csproj" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Oibi.Repository.Demo.Models\Oibi.Repository.Demo.Models.csproj" />
<ProjectReference Include="..\Oibi.Repository\Oibi.Repository.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="AspNetCore.RouteAnalyzer" Version="0.5.3" />
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="12.0.0" />
<PackageReference Include="IDisposableAnalyzers" Version="4.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.4">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="7.0.5" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="7.0.3" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="AspNetCore.RouteAnalyzer" Version="0.5.3" />
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="12.0.1" />
<PackageReference Include="IDisposableAnalyzers" Version="4.0.7">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="8.0.0" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.0" />
</ItemGroup>
</Project>
46 changes: 32 additions & 14 deletions Oibi.Repository.Demo/Repositories/LibraryDbScope.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,38 @@
using Oibi.Repository.Abstracts;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using Oibi.Repository.Abstracts;
using Oibi.Repository.Demo.Models;
using System;

namespace Oibi.Repository.Demo.Repositories
{
/// <summary>
/// Repositories scope
/// </summary>
public class LibraryDbScope : DbContextScope<LibraryContext>
{
public LibraryDbScope(LibraryContext context, BookRepository bookRepository, AuthorRepository authorRepository) : base(context)
{
BookRepository = bookRepository;
AuthorRepository = authorRepository;
}
/// <summary>
/// Repositories scope
/// </summary>
public class LibraryDbScope : DbContextScope<LibraryContext>
{
public LibraryDbScope(LibraryContext context, BookRepository bookRepository, AuthorRepository authorRepository) : base(context)
{
BookRepository = bookRepository;
AuthorRepository = authorRepository;

public BookRepository BookRepository { get; }
public AuthorRepository AuthorRepository { get; }
}
context.ChangeTracker.Tracked += ChangeTracker_Tracked;
}

private void ChangeTracker_StateChanged(object sender, EntityStateChangedEventArgs e)
{
var student = e.Entry.Entity;
// e.Entry.State
}

private void ChangeTracker_Tracked(object sender, EntityTrackedEventArgs e)
{
// create dto: state, context, entity ?
var entity = e.Entry.Entity;
//throw new NotImplementedException();
}


public BookRepository BookRepository { get; }
public AuthorRepository AuthorRepository { get; }
}
}
3 changes: 2 additions & 1 deletion Oibi.Repository.Demo/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ public Startup(IConfiguration configuration)
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<LibraryContext>(config => config.UseNpgsql(_configuration.GetConnectionString("Demo")));
var connectionString = _configuration.GetConnectionString("Demo");
services.AddDbContext<LibraryContext>(config => config.UseNpgsql(connectionString));
services.AddDatabaseScope<LibraryDbScope>();

// https://github.com/AutoMapper/AutoMapper.Extensions.Microsoft.DependencyInjection
Expand Down
2 changes: 1 addition & 1 deletion Oibi.Repository.Demo/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
},
"AllowedHosts": "*",
"ConnectionStrings": {
"Demo": "User ID=Library;Password=develop;Server=localhost;Port=1440;Database=Library;Integrated Security=true;Pooling=true;"
"Demo": "User ID=Library;Password=develop;Server=localhost;Port=1440;Database=Library;Pooling=true;"
}
}
41 changes: 41 additions & 0 deletions Oibi.Repository.Tests/DatabaseConnectionTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using Microsoft.EntityFrameworkCore;
using Oibi.Repository.Demo;
using Oibi.Repository.Demo.Repositories;
using Oibi.TestHelper;
using System;
using System.Threading.Tasks;
using Xunit;

namespace Oibi.Repository.Tests;

public class DatabaseConnectionTest : IClassFixture<ServerFixture<Startup>>
{
private readonly ServerFixture<Startup> _serverFixture;
private readonly LibraryDbScope _libraryScope;

public DatabaseConnectionTest(ServerFixture<Startup> serverFixture)
{
_serverFixture = serverFixture;
_libraryScope = _serverFixture.GetService<LibraryDbScope>();
}

[Fact]
public async Task TestDatabaseConnection()
{
Exception connectionException = null;

try
{
// Tentativo di eseguire una semplice operazione di lettura dal database.
var count = await _libraryScope.BookRepository.CountAsync();
Assert.True(count >= 0, "Connessione al database riuscita e conteggio libri ottenuto.");
}
catch (Exception ex)
{
connectionException = ex;
Assert.Fail(ex.Message);
}

Assert.Null(connectionException); // Assicurati che non ci siano eccezioni, indicando problemi di connessione.
}
}
62 changes: 31 additions & 31 deletions Oibi.Repository.Tests/Oibi.Repository.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,40 +1,40 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<None Remove="appsettings.tests.json" />
</ItemGroup>
<ItemGroup>
<None Remove="appsettings.test.json" />
</ItemGroup>

<ItemGroup>
<Content Include="appsettings.tests.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<Content Include="appsettings.test.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</Content>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
<PackageReference Include="Oibi.TestHelper" Version="6.0.8" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.2.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="Oibi.TestHelper" Version="8.0.0" />
<PackageReference Include="xunit" Version="2.6.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.5">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Oibi.Repository.Demo\Oibi.Repository.Demo.csproj" />
<ProjectReference Include="..\Oibi.Repository\Oibi.Repository.csproj" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Oibi.Repository.Demo\Oibi.Repository.Demo.csproj" />
<ProjectReference Include="..\Oibi.Repository\Oibi.Repository.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
},
"AllowedHosts": "*",
"ConnectionStrings": {
"Demo": "User ID=develop;Password=develop;Server=localhost;Port=1440;Database=Library;Integrated Security=true;Pooling=true;",
"Demo": "User ID=Library;Password=develop;Server=localhost;Port=1440;Database=Library;Pooling=true;"
}
}
58 changes: 29 additions & 29 deletions Oibi.Repository/Oibi.Repository.csproj
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<Authors>[email protected]</Authors>
<Description>Generic repository base pattern for EF Core + DatabaseScope</Description>
</PropertyGroup>
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Authors>[email protected]</Authors>
<Description>Generic repository base pattern for EF Core + DatabaseScope</Description>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Meziantou.Analyzer" Version="2.0.20">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.2.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="7.0.4" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Meziantou.Analyzer" Version="2.0.120">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.2.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="8.0.0" />
</ItemGroup>

<PropertyGroup>
<IncludeOpenAPIAnalyzers>true</IncludeOpenAPIAnalyzers>
<Copyright>[email protected]</Copyright>
<PackageProjectUrl>https://github.com/TheTrigger/Oibi.Repository</PackageProjectUrl>
<RepositoryType>git</RepositoryType>
<RepositoryUrl>https://github.com/TheTrigger/Oibi.Repository</RepositoryUrl>
<PackageTags>repository, pattern, aspnetcore, efcore, scope</PackageTags>
<PackageReleaseNotes></PackageReleaseNotes>
<PackageIconUrl>https://image.flaticon.com/icons/svg/2232/2232243.svg</PackageIconUrl>
<Version>7.0.4</Version>
<Company>oibi.dev</Company>
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
<UserSecretsId>320f5e09-36cb-4c5b-8f02-72cbf1003ad1</UserSecretsId>
</PropertyGroup>
<PropertyGroup>
<IncludeOpenAPIAnalyzers>true</IncludeOpenAPIAnalyzers>
<Copyright>[email protected]</Copyright>
<PackageProjectUrl>https://github.com/TheTrigger/Oibi.Repository</PackageProjectUrl>
<RepositoryType>git</RepositoryType>
<RepositoryUrl>https://github.com/TheTrigger/Oibi.Repository</RepositoryUrl>
<PackageTags>repository, pattern, aspnetcore, efcore, scope</PackageTags>
<PackageReleaseNotes></PackageReleaseNotes>
<PackageIconUrl>https://image.flaticon.com/icons/svg/2232/2232243.svg</PackageIconUrl>
<Version>8.0.0</Version>
<Company>oibi.dev</Company>
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
<UserSecretsId>320f5e09-36cb-4c5b-8f02-72cbf1003ad1</UserSecretsId>
</PropertyGroup>

</Project>

0 comments on commit 9354699

Please sign in to comment.