diff --git a/F2.Repository.Demo/LibraryDbScope.cs b/F2.Repository.Demo/LibraryDbScope.cs new file mode 100644 index 0000000..4183f1d --- /dev/null +++ b/F2.Repository.Demo/LibraryDbScope.cs @@ -0,0 +1,37 @@ +using F2.Repository.Abstracts; +using F2.Repository.Demo.Models; +using F2.Repository.Demo.Repositories; +using Microsoft.EntityFrameworkCore.ChangeTracking; + +namespace F2.Repository.Demo; + +/// +/// Repositories scope +/// +public class LibraryDbScope : DbContextScope +{ + public LibraryDbScope(LibraryContext context, BookRepository bookRepository, AuthorRepository authorRepository) : base(context) + { + BookRepository = bookRepository; + AuthorRepository = authorRepository; + + 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; } +} \ No newline at end of file diff --git a/F2.Repository.Demo/Repositories/BookRepository.cs b/F2.Repository.Demo/Repositories/BookRepository.cs index a0b2654..4cdabf1 100644 --- a/F2.Repository.Demo/Repositories/BookRepository.cs +++ b/F2.Repository.Demo/Repositories/BookRepository.cs @@ -1,5 +1,4 @@ -using AutoMapper; -using F2.Repository.Abstracts; +using F2.Repository.Abstracts; using F2.Repository.Demo.Models; namespace F2.Repository.Demo.Repositories diff --git a/F2.Repository.Demo/Repositories/LibraryDbScope.cs b/F2.Repository.Demo/Repositories/LibraryDbScope.cs deleted file mode 100644 index b2b0f05..0000000 --- a/F2.Repository.Demo/Repositories/LibraryDbScope.cs +++ /dev/null @@ -1,38 +0,0 @@ -using Microsoft.EntityFrameworkCore.ChangeTracking; -using F2.Repository.Abstracts; -using F2.Repository.Demo.Models; -using System; - -namespace F2.Repository.Demo.Repositories -{ - /// - /// Repositories scope - /// - public class LibraryDbScope : DbContextScope - { - public LibraryDbScope(LibraryContext context, BookRepository bookRepository, AuthorRepository authorRepository) : base(context) - { - BookRepository = bookRepository; - AuthorRepository = authorRepository; - - 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; } - } -} \ No newline at end of file diff --git a/F2.Repository.Demo/Startup.cs b/F2.Repository.Demo/Startup.cs index f142c5a..d8725d5 100644 --- a/F2.Repository.Demo/Startup.cs +++ b/F2.Repository.Demo/Startup.cs @@ -5,7 +5,6 @@ using Microsoft.Extensions.DependencyInjection; using F2.Repository.Demo.Mapper; using F2.Repository.Demo.Models; -using F2.Repository.Demo.Repositories; using F2.Repository.Extensions; namespace F2.Repository.Demo; diff --git a/F2.Repository.Demo/appsettings.Development.json b/F2.Repository.Demo/appsettings.Development.json index e203e94..79c9f28 100644 --- a/F2.Repository.Demo/appsettings.Development.json +++ b/F2.Repository.Demo/appsettings.Development.json @@ -5,5 +5,8 @@ "System": "Information", "Microsoft": "Information" } + }, + "ConnectionStrings": { + "Demo": "Username=Library;Password=develop;Server=localhost;Port=1440;Database=Library;" } } diff --git a/F2.Repository.Demo/appsettings.json b/F2.Repository.Demo/appsettings.json index 2ea9fd3..021a4b5 100644 --- a/F2.Repository.Demo/appsettings.json +++ b/F2.Repository.Demo/appsettings.json @@ -8,8 +8,5 @@ "Microsoft.EntityFrameworkCore": "Information" } }, - "AllowedHosts": "*", - "ConnectionStrings": { - "Demo": "Username=Library;Password=develop;Server=localhost;Port=1440;Database=Library;" - } + "AllowedHosts": "*" } diff --git a/F2.Repository.Tests/DatabaseConnectionTest.cs b/F2.Repository.Tests/DatabaseConnectionTest.cs index 0eeae78..07d4e61 100644 --- a/F2.Repository.Tests/DatabaseConnectionTest.cs +++ b/F2.Repository.Tests/DatabaseConnectionTest.cs @@ -1,7 +1,7 @@ -using Microsoft.EntityFrameworkCore; -using Microsoft.Extensions.DependencyInjection; +using F2.Repository.Demo; using F2.Repository.Demo.Models; -using F2.Repository.Demo.Repositories; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.DependencyInjection; using System; using System.Linq; using System.Threading.Tasks; @@ -21,9 +21,8 @@ public DatabaseConnectionTest(TestContainerApplicationFactory testFixure) { _testFixure = testFixure; _scope = _testFixure.Server.Services.CreateScope(); - - _libraryScope = _scope.ServiceProvider.GetRequiredService(); _context = _scope.ServiceProvider.GetRequiredService(); + _libraryScope = _scope.ServiceProvider.GetRequiredService(); } [Fact] diff --git a/F2.Repository.Tests/TestContainerApplicationFactory.cs b/F2.Repository.Tests/TestContainerApplicationFactory.cs index c78901b..f8b8f93 100644 --- a/F2.Repository.Tests/TestContainerApplicationFactory.cs +++ b/F2.Repository.Tests/TestContainerApplicationFactory.cs @@ -36,20 +36,27 @@ public TestContainerApplicationFactory() protected override void ConfigureWebHost(IWebHostBuilder builder) { - builder.ConfigureTestServices(services => - { - var descriptor = services.SingleOrDefault(d => d.ServiceType == typeof(DbContextOptions)); - services?.Remove(descriptor); - services.AddDbContext(options => + base.ConfigureWebHost(builder); + builder + .ConfigureTestServices(services => { - options.UseNpgsql(_applicationDatabase.GetConnectionString(), - npgsqlOptions => npgsqlOptions.MigrationsAssembly(typeof(LibraryContext).Assembly.FullName)) - .EnableSensitiveDataLogging(true) - .EnableDetailedErrors() - ; + var descriptor = services.SingleOrDefault(d => d.ServiceType == typeof(DbContextOptions)); + services?.Remove(descriptor); + services.AddDbContext(options => + { + // connection string from container + options.UseNpgsql(_applicationDatabase.GetConnectionString(), + npgsqlOptions => npgsqlOptions.MigrationsAssembly(typeof(LibraryContext).Assembly.FullName)) + .EnableSensitiveDataLogging(true) + .EnableDetailedErrors() + ; - }, ServiceLifetime.Scoped); - }); + }, ServiceLifetime.Scoped); + }) + .ConfigureAppConfiguration((hostingContext, config) => + { + }) + ; } Task IAsyncLifetime.InitializeAsync() diff --git a/F2.Repository.Tests/TestControllerApi.cs b/F2.Repository.Tests/TestControllerApi.cs index 888ac45..bf9b94b 100644 --- a/F2.Repository.Tests/TestControllerApi.cs +++ b/F2.Repository.Tests/TestControllerApi.cs @@ -1,6 +1,7 @@ +using F2.Repository.Demo; using F2.Repository.Demo.Models; -using F2.Repository.Demo.Repositories; using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.DependencyInjection; using System; using System.Threading.Tasks; using Xunit; @@ -9,77 +10,78 @@ namespace F2.Repository.Tests { public class TestControllerApi : IClassFixture { - private readonly TestContainerApplicationFactory _serverFixture; - private readonly LibraryDbScope _libraryScope; - - public TestControllerApi(TestContainerApplicationFactory serverFixture) - { - _serverFixture = serverFixture; - _libraryScope = _serverFixture.GetService(); - } - - [Fact] - public async Task CreateExampleDataIfNotExists() - { - var a1 = new Author { Name = "William Shakespeare" }; - var a2 = new Author { Name = "Oibi.dev" }; - - _libraryScope.AuthorRepository.Create(a1); - _libraryScope.AuthorRepository.Create(a2); - - var affectedRows = await _libraryScope.SaveChangesAsync(); - Assert.NotEqual(Guid.Empty, a1.Id); - Assert.NotEqual(default, affectedRows); - - var b1 = new Book { Title = "Hamlet", Isbn = "1234567890123" }; - var b2 = new Book { Title = "King Lear", Isbn = "0987654321045" }; - var b3 = new Book { Id = Guid.NewGuid(), Title = "Random Othello w/ no authors" }; - - _libraryScope.BookRepository.Create(b1); - _libraryScope.BookRepository.Create(b2); - _libraryScope.BookRepository.Create(b3); - - a1.Books = [b1, b2]; - a2.Books = [b2, b3]; - - affectedRows = await _libraryScope.SaveChangesAsync(); - Assert.NotEqual(default, affectedRows); - - var results = await _libraryScope.AuthorRepository - .Include(i => i.Books) - .ToListAsync(); - - Assert.NotEmpty(results); - foreach (var author in results) - { - Assert.NotEmpty(author.Name); - Assert.NotEmpty(author.Books); - } - } - - [Fact] - public async Task ToListAsync() - { - var b1 = new Book { Title = "Hamlet", Isbn = "1234567890123" }; - _libraryScope.BookRepository.Create(b1); - var affectedRows = await _libraryScope.SaveChangesAsync(); - Assert.Equal(1, affectedRows); - - var results = await _libraryScope.BookRepository.ToListAsync(); - Assert.NotNull(results); - } - - [Fact] - public async Task CanCreateAndDelete() - { - var a1 = new Author { Name = "William Shakespeare" }; - _libraryScope.AuthorRepository.Create(a1); - await _libraryScope.SaveChangesAsync(default); - - _libraryScope.AuthorRepository.RemoveRange(a1); - await _libraryScope.SaveChangesAsync(default); - - Assert.NotEqual(Guid.Empty, a1.Id); - } - } + private readonly TestContainerApplicationFactory _serverFixture; + private readonly IServiceScope _scope; + private readonly LibraryDbScope _libraryScope; + public TestControllerApi(TestContainerApplicationFactory serverFixture) + { + _serverFixture = serverFixture; + _scope = _serverFixture.Server.Services.CreateScope(); + _libraryScope = _scope.ServiceProvider.GetRequiredService(); + } + + [Fact] + public async Task CreateExampleDataIfNotExists() + { + var a1 = new Author { Name = "William Shakespeare" }; + var a2 = new Author { Name = "Oibi.dev" }; + + _libraryScope.AuthorRepository.Create(a1); + _libraryScope.AuthorRepository.Create(a2); + + var affectedRows = await _libraryScope.SaveChangesAsync(); + Assert.NotEqual(Guid.Empty, a1.Id); + Assert.NotEqual(default, affectedRows); + + var b1 = new Book { Title = "Hamlet", Isbn = "1234567890123" }; + var b2 = new Book { Title = "King Lear", Isbn = "0987654321045" }; + var b3 = new Book { Id = Guid.NewGuid(), Title = "Random Othello w/ no authors" }; + + _libraryScope.BookRepository.Create(b1); + _libraryScope.BookRepository.Create(b2); + _libraryScope.BookRepository.Create(b3); + + a1.Books = [b1, b2]; + a2.Books = [b2, b3]; + + affectedRows = await _libraryScope.SaveChangesAsync(); + Assert.NotEqual(default, affectedRows); + + var results = await _libraryScope.AuthorRepository + .Include(i => i.Books) + .ToListAsync(); + + Assert.NotEmpty(results); + foreach (var author in results) + { + Assert.NotEmpty(author.Name); + Assert.NotEmpty(author.Books); + } + } + + [Fact] + public async Task ToListAsync() + { + var b1 = new Book { Title = "Hamlet", Isbn = "1234567890123" }; + _libraryScope.BookRepository.Create(b1); + var affectedRows = await _libraryScope.SaveChangesAsync(); + Assert.Equal(1, affectedRows); + + var results = await _libraryScope.BookRepository.ToListAsync(); + Assert.NotNull(results); + } + + [Fact] + public async Task CanCreateAndDelete() + { + var a1 = new Author { Name = "William Shakespeare" }; + _libraryScope.AuthorRepository.Create(a1); + await _libraryScope.SaveChangesAsync(default); + + _libraryScope.AuthorRepository.RemoveRange(a1); + await _libraryScope.SaveChangesAsync(default); + + Assert.NotEqual(Guid.Empty, a1.Id); + } + } } \ No newline at end of file diff --git a/F2.Repository.Tests/appsettings.test.json b/F2.Repository.Tests/appsettings.test.json index 2ea9fd3..021a4b5 100644 --- a/F2.Repository.Tests/appsettings.test.json +++ b/F2.Repository.Tests/appsettings.test.json @@ -8,8 +8,5 @@ "Microsoft.EntityFrameworkCore": "Information" } }, - "AllowedHosts": "*", - "ConnectionStrings": { - "Demo": "Username=Library;Password=develop;Server=localhost;Port=1440;Database=Library;" - } + "AllowedHosts": "*" } diff --git a/F2.Repository/F2.Repository.csproj b/F2.Repository/F2.Repository.csproj index 452e4f5..bfb01ea 100644 --- a/F2.Repository/F2.Repository.csproj +++ b/F2.Repository/F2.Repository.csproj @@ -1,31 +1,37 @@  - net8.0 - enable - enable fabrizi.software + FABRIZI.SOFTWARE info@fabrizi.software Generic repository base pattern for EF Core + DatabaseScope - README.md - F2,resository - 8.0.6.1 + false + enable + true True + enable + icon.png + https://image.flaticon.com/icons/svg/2232/2232243.svg + README.md + + false + F2, repository, pattern, aspnetcore, efcore, scope + git snupkg + net8.0 + F2.Repository + 320f5e09-36cb-4c5b-8f02-72cbf1003ad1 + 8.0.11 - - True - \ - + + True + \ + - - all - runtime; build; native; contentfiles; analyzers; buildtransitive - @@ -33,28 +39,10 @@ - - True - \ - + + True + \ + - - true - info@fabrizi.software - https://github.com/TheTrigger/Oibi.Repository - git - https://github.com/TheTrigger/Oibi.Repository - repository, pattern, aspnetcore, efcore, scope - - https://image.flaticon.com/icons/svg/2232/2232243.svg - 8.0.6 - FABRIZI.SOFTWARE - false - false - 320f5e09-36cb-4c5b-8f02-72cbf1003ad1 - F2.Repository - icon.png - - diff --git a/docker-compose.yml b/docker-compose.yml index f4b9de0..c42b269 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -3,14 +3,9 @@ # debug database # launch Update-Database from vsconsole to migrate the database -version: "3.7" - services: - - - # MAIN APPLICATION DATABASE database_default: - image: postgres:14-alpine + image: postgres:16-alpine ports: - 1440:5432 environment: @@ -19,8 +14,5 @@ services: volumes: - database_application_data:/var/lib/postgresql/data - - volumes: - database_application_data: - \ No newline at end of file + database_application_data: \ No newline at end of file