Skip to content

Commit

Permalink
Merge pull request #20 from Ian-Webster/19-add-extension-for-hotchoco…
Browse files Browse the repository at this point in the history
…late

19 Add extension for HotChocolate
  • Loading branch information
Ian-Webster authored Dec 5, 2023
2 parents f73f9a5 + e90a243 commit fd0a791
Show file tree
Hide file tree
Showing 26 changed files with 341 additions and 69 deletions.
6 changes: 1 addition & 5 deletions .github/workflows/branch.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
name: Branch actions

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
on: push

jobs:

Expand Down
18 changes: 9 additions & 9 deletions DataAccess.Repository.Tests/DataAccess.Repository.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

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

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="7.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="7.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.0" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.3.1" />
<PackageReference Include="NUnit.Analyzers" Version="3.5.0">
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="8.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="8.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="NUnit" Version="4.0.1" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
<PackageReference Include="NUnit.Analyzers" Version="3.10.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.2.0">
<PackageReference Include="coverlet.collector" Version="6.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
5 changes: 2 additions & 3 deletions DataAccess.Repository.Tests/Tests/Add.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,10 @@ public async Task Should_Add_Expected_Data(Book data)
var result = await repo.Add(data, Token);

// assert
Assert.IsTrue(result);
Assert.That(result, Is.True);

var addedBook = await repo.FirstOrDefault(p => p.BookId == data.BookId, Token);

Assert.IsNotNull(addedBook);
StringAssert.AreEqualIgnoringCase(JsonConvert.SerializeObject(data), JsonConvert.SerializeObject(addedBook));
Assert.That(JsonConvert.SerializeObject(data), Is.EqualTo(JsonConvert.SerializeObject(addedBook)));
}
}
4 changes: 2 additions & 2 deletions DataAccess.Repository.Tests/Tests/Exists.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public async Task Should_Return_True_When_BookExists(string bookIdString)
var result = await repo.Exists(b => b.BookId == bookId, Token);

// assert
Assert.IsTrue(result);
Assert.That(result, Is.True);
}

[TestCase("D2FD9BD6-6439-4A59-8C28-A6CBE947A55A")]
Expand All @@ -44,6 +44,6 @@ public async Task Should_Return_False_When_BookDoesNotExist(string bookIdString)
var result = await repo.Exists(b => b.BookId == bookId, Token);

// assert
Assert.IsFalse(result);
Assert.That(result, Is.False);
}
}
8 changes: 4 additions & 4 deletions DataAccess.Repository.Tests/Tests/FirstOrDefault.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public async Task Should_Return_Null_When_NoDataExists()
var result = await repo.FirstOrDefault(p => p.Name != String.Empty, Token);

// assert
Assert.IsNull(result);
Assert.That(result, Is.Null);
}

[Test]
Expand All @@ -34,7 +34,7 @@ public async Task Should_Return_Null_When_DataNotFound()
var result = await repo.FirstOrDefault(p => p.BookId == Guid.Empty, Token);

// assert
Assert.IsNull(result);
Assert.That(result, Is.Null);
}

[TestCaseSource(nameof(FirstOrDefaultBookTestCaseData))]
Expand All @@ -50,10 +50,10 @@ public async Task Should_Get_Expected_Item_When_MatchingItemFound(Guid bookId)
var result = await repo.FirstOrDefault(p => p.BookId == bookId, Token);

// assert
Assert.IsNotNull(result);
Assert.That(result, Is.Not.Null);

var expectedBook = BookTestData.GetBookData().First(b => b.BookId == bookId);

StringAssert.AreEqualIgnoringCase(JsonConvert.SerializeObject(expectedBook), JsonConvert.SerializeObject(result));
Assert.That(JsonConvert.SerializeObject(expectedBook), Is.EqualTo(JsonConvert.SerializeObject(result)));
}
}
16 changes: 9 additions & 7 deletions DataAccess.Repository.Tests/Tests/List.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ public async Task Should_Return_EmptyList_When_NoDataExists()
var result = await repo.List(p => p.Name.Contains("Book"), Token);

// assert
Assert.IsNotNull(result);
Assert.IsEmpty(result);
var enumerable = result.ToList();
Assert.That(enumerable, Is.Not.Null);
Assert.That(enumerable, Is.Empty);
}

[Test]
Expand All @@ -34,8 +35,9 @@ public async Task Should_Return_EmptyList_When_NoMatchingDataFound()
var result = await repo.List(p => p.Name.Contains("wobble"), Token);

// assert
Assert.IsNotNull(result);
Assert.IsEmpty(result);
var enumerable = result.ToList();
Assert.That(enumerable, Is.Not.Null);
Assert.That(enumerable, Is.Empty);
}

[TestCaseSource(nameof(ListBookTestCaseData))]
Expand All @@ -51,13 +53,13 @@ public async Task Should_Return_Expected_Data_WhenMatchingDataFound(string searc
var result = await repo.List(p => p.Name.Contains(searchString), Token);

// assert
Assert.IsNotEmpty(result);
Assert.That(result, Is.Not.Empty);

var expectedBooks = BookTestData.GetBookData().Where(b => b.Name.Contains(searchString)).ToList();
Assert.AreEqual(expectedBooks.Count, result.Count());
Assert.That(expectedBooks.Count, Is.EqualTo(result.Count()));
expectedBooks.ForEach(b =>
{
Assert.IsTrue(result.Any(r => r.BookId == b.BookId));
Assert.That(result.Any(r => r.BookId == b.BookId), Is.True);
});

}
Expand Down
8 changes: 4 additions & 4 deletions DataAccess.Repository.Tests/Tests/Remove.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ public async Task Should_Remove_Expected_AttachedItem(Guid bookIdToRemove)
var result = await repo.Remove(bookToRemove, Token);

// assert
Assert.IsTrue(result);
Assert.That(result, Is.True);

var books = await dbSet.ToListAsync();

Assert.IsFalse(books.Any(b => b.BookId == bookIdToRemove));
Assert.That(books.Any(b => b.BookId == bookIdToRemove), Is.False);
Assert.That(BookTestData.GetBookData().Count -1, Is.EqualTo(books.Count));
}

Expand All @@ -46,11 +46,11 @@ public async Task Should_Return_False_When_TryingToRemove_UnattachedItem(Book bo
var result = await repo.Remove(bookToRemove, Token);

// assert
Assert.IsFalse(result);
Assert.That(result, Is.False);

var books = await dbSet.ToListAsync();

Assert.IsTrue(books.Any(b => b.BookId == bookToRemove.BookId));
Assert.That(books.Any(b => b.BookId == bookToRemove.BookId), Is.True);
Assert.That(BookTestData.GetBookData().Count, Is.EqualTo(books.Count));
}
}
12 changes: 6 additions & 6 deletions DataAccess.Repository.Tests/Tests/Update.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ public async Task Should_Update_ExpectedBook_When_BookAttached(Book bookToUpdate
var result = await repo.Update(book, Token);

// assert
Assert.IsTrue(result);
Assert.That(result, Is.True);

var books = await dbSet.ToListAsync();
Assert.That(BookTestData.GetBookData().Count, Is.EqualTo(books.Count));

var updatedBook = await dbSet.FirstOrDefaultAsync(b => b.BookId == bookToUpdate.BookId, Token);
Assert.IsNotNull(updatedBook);
StringAssert.AreEqualIgnoringCase(bookToUpdate.Name, updatedBook.Name);
Assert.IsFalse(books.Any(b => b.BookId != bookToUpdate.BookId && b.Name == bookToUpdate.Name));
Assert.That(updatedBook, Is.Not.Null);
Assert.That(bookToUpdate.Name, Is.EqualTo(updatedBook?.Name));
Assert.That(books.Any(b => b.BookId != bookToUpdate.BookId && b.Name == bookToUpdate.Name), Is.False);
}

[TestCaseSource(nameof(UnattachedUpdateBookTestCaseData))]
Expand All @@ -50,12 +50,12 @@ public async Task Should_NotUpdate_Books_When_BookNotAttached(Book bookToUpdate)
var result = await repo.Update(bookToUpdate, Token);

// assert
Assert.IsFalse(result);
Assert.That(result, Is.False);

var books = await dbSet.ToListAsync();
Assert.That(BookTestData.GetBookData().Count, Is.EqualTo(books.Count));

var updatedBook = await dbSet.FirstOrDefaultAsync(b => b.Name == bookToUpdate.Name, Token);
Assert.IsNull(updatedBook);
Assert.That(updatedBook, Is.Null);
}
}
8 changes: 4 additions & 4 deletions DataAccess.Repository/DataAccess.Repository.csproj
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<Title>DataAccess</Title>
<Description>A simple base repository to be used in other projects requiring data access</Description>
<PackageProjectUrl>https://github.com/Ian-Webster/DataAccess</PackageProjectUrl>
<RepositoryUrl>https://github.com/Ian-Webster/DataAccess</RepositoryUrl>
<VersionPrefix>1.0.4</VersionPrefix>
<VersionPrefix>2.0.0-wip</VersionPrefix>
<VersionSuffix></VersionSuffix>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="7.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="8.0.0" />
</ItemGroup>

</Project>
4 changes: 4 additions & 0 deletions DataAccess.Repository/IRepository.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
using System.Linq.Expressions;
using Microsoft.EntityFrameworkCore;

namespace DataAccess.Repository
{
public interface IRepository<TEntity> where TEntity: class
{

public DbSet<TEntity> DbSet { get; }

/// <summary>
/// Checks if a object matching the given predicate exists
/// </summary>
Expand Down
9 changes: 6 additions & 3 deletions DataAccess.Repository/Repository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@

namespace DataAccess.Repository;

// TODO: figure out how to extend this so the dbContext can be shared but not exposed outside of this class
public class Repository<TEntity> : IRepository<TEntity> where TEntity : class
{
private readonly DbContext _context;
private readonly DbSet<TEntity> _dbSet;
private readonly DbContext _context;

public Repository(DbContext context)
{
{
_context = context;
_dbSet = _context.Set<TEntity>();
}


public DbSet<TEntity> DbSet => _dbSet;

public async Task<bool> Exists(Expression<Func<TEntity, bool>> predicate, CancellationToken token)
{
return await _dbSet.AnyAsync(predicate, token);
Expand Down
17 changes: 6 additions & 11 deletions DataAccess.Repository/RepositoryFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,18 @@

namespace DataAccess.Repository;

public class RepositoryFactory<TContext> where TContext : DbContext
public class RepositoryFactory<TContext>(TContext context)
where TContext : DbContext
{
private readonly DbContext _context;
private readonly Dictionary<Type, object> _repositories;

public RepositoryFactory(TContext context)
{
_context = context;
_repositories = new Dictionary<Type, object>();
}
private readonly DbContext _context = context;
private readonly Dictionary<Type, object> _repositories = new();

public IRepository<T> GetRepositoryByType<T>() where T : class
{
var type = typeof(T);
if (_repositories.ContainsKey(type))
if (_repositories.TryGetValue(type, out var repo))
{
return (IRepository<T>)_repositories[type];
return (IRepository<T>)repo;
}

var repository = new Repository<T>(_context);
Expand Down
9 changes: 9 additions & 0 deletions DataAccess.sln
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
README.md = README.md
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DataAccess.Repository.HotChocolate", "Extensions\DataAccess.Repository.HotChocolate\DataAccess.Repository.HotChocolate.csproj", "{A787E878-7D30-4B5A-9C38-958F3A0AD3B7}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Extensions", "Extensions", "{19E38EB6-6908-4F0B-95E6-6C83866AC5E9}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -45,6 +49,10 @@ Global
{B0DCE86C-FCAE-423B-BA82-C6EC3D3C57A3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B0DCE86C-FCAE-423B-BA82-C6EC3D3C57A3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B0DCE86C-FCAE-423B-BA82-C6EC3D3C57A3}.Release|Any CPU.Build.0 = Release|Any CPU
{A787E878-7D30-4B5A-9C38-958F3A0AD3B7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A787E878-7D30-4B5A-9C38-958F3A0AD3B7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A787E878-7D30-4B5A-9C38-958F3A0AD3B7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A787E878-7D30-4B5A-9C38-958F3A0AD3B7}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -53,6 +61,7 @@ Global
{B8EDFA45-9B84-4B90-AA21-D8F2ED870E9B} = {DE3BBAC2-124B-403F-BB19-03A794FE57F1}
{01D1F0A2-B22B-41E6-96A1-3BCC516BBF06} = {DE3BBAC2-124B-403F-BB19-03A794FE57F1}
{B0DCE86C-FCAE-423B-BA82-C6EC3D3C57A3} = {DE3BBAC2-124B-403F-BB19-03A794FE57F1}
{A787E878-7D30-4B5A-9C38-958F3A0AD3B7} = {19E38EB6-6908-4F0B-95E6-6C83866AC5E9}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {E9414AEC-FCFA-419E-97A9-BB14AD678C48}
Expand Down
2 changes: 2 additions & 0 deletions DataAccess.sln.DotSettings
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:Boolean x:Key="/Default/CodeEditing/SuppressNullableWarningFix/Enabled/@EntryValue">False</s:Boolean></wpf:ResourceDictionary>
10 changes: 7 additions & 3 deletions Example/DataAccess.Example.Data/DataAccess.Example.Data.csproj
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<Folder Include="Entities\" />
<Folder Include="Queries\" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="7.0.0" />
<PackageReference Include="HotChocolate" Version="13.7.0" />
<PackageReference Include="HotChocolate.Data" Version="13.7.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="8.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\DataAccess.Repository\DataAccess.Repository.csproj" />
<ProjectReference Include="..\..\Extensions\DataAccess.Repository.HotChocolate\DataAccess.Repository.HotChocolate.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);

modelBuilder.ApplyConfigurationsFromAssembly(Assembly.GetAssembly(typeof(LibraryDatabaseContext)));
modelBuilder.ApplyConfigurationsFromAssembly(Assembly.GetAssembly(typeof(LibraryDatabaseContext)) ?? throw new InvalidOperationException());
}
}
Loading

0 comments on commit fd0a791

Please sign in to comment.