From 3652ec6613e1f401077ea2e2a84ed0dd5a53fd37 Mon Sep 17 00:00:00 2001 From: Webster Date: Wed, 6 Dec 2023 23:23:36 +0000 Subject: [PATCH] #19 version bump read me changes + test.yml --- .github/workflows/test.yml | 36 +++ .../DataAccess.Repository.csproj | 2 +- .../readme.md | 2 +- README.md | 248 +++++++++--------- 4 files changed, 162 insertions(+), 126 deletions(-) create mode 100644 .github/workflows/test.yml diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..715561f --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,36 @@ +name: Branch actions + +on: push + +jobs: + + test_DataAccess: + + runs-on: ubuntu-latest + + steps: + - name: Checkout + uses: actions/checkout@v3 + with: + fetch-depth: 0 + + # Install the .NET Core workload + - name: Install .NET Core + uses: actions/setup-dotnet@v3 + with: + dotnet-version: 8.0.x + + # Set up local NuGet repo + - name: Setup GitHub NuGet + run: dotnet nuget add source --username USERNAME --password ${{ secrets.GITHUB_TOKEN }} --store-password-in-clear-text --name github "https://nuget.pkg.github.com/Ian-Webster/index.json" + + - name: Check for Changes in Repository Project + run: | + if [[ $(git diff --name-only ${{ github.event.before }} ${{ github.sha }} 'DataAccess.Repository/') ]]; then + echo "Changes detected in DataAccess.Repository project. Restoring packages, running build and testing..." + dotnet restore 'DataAccess.Repository' + dotnet build --configuration Release 'DataAccess.Repository' + dotnet test DataAccess.Repository' + else + echo "No changes in DataAccess.Repository project. Skipping build." + fi \ No newline at end of file diff --git a/DataAccess.Repository/DataAccess.Repository.csproj b/DataAccess.Repository/DataAccess.Repository.csproj index a3de38b..fc90e49 100644 --- a/DataAccess.Repository/DataAccess.Repository.csproj +++ b/DataAccess.Repository/DataAccess.Repository.csproj @@ -9,7 +9,7 @@ A simple base repository to be used in other projects requiring data access https://github.com/Ian-Webster/DataAccess https://github.com/Ian-Webster/DataAccess - 2.0.1 + 2.0.2 diff --git a/Extensions/DataAccess.Repository.HotChocolate/readme.md b/Extensions/DataAccess.Repository.HotChocolate/readme.md index 9995479..17079ff 100644 --- a/Extensions/DataAccess.Repository.HotChocolate/readme.md +++ b/Extensions/DataAccess.Repository.HotChocolate/readme.md @@ -11,7 +11,7 @@ This extension provides GraphQL query functionality via the [HotChocolate Librar 3. Add the following NuGet packages to your Repository project; 1. [HotChocolate](https://www.nuget.org/packages/HotChocolate/13.7.0?_src=template) 2. [HotChocolate.Data](https://www.nuget.org/packages/HotChocolate.Data/13.7.0?_src=template) - 3. TBC- this extension + 3. [DataAccess.Repository](https://github.com/Ian-Webster/DataAccess/pkgs/nuget/DataAccess.Repository) 4. Modify your IoC services to add and configure HotChocolate; ```csharp // set up HotChocolate diff --git a/README.md b/README.md index c254857..67d2339 100644 --- a/README.md +++ b/README.md @@ -1,124 +1,124 @@ -# DataAccess - -## Introduction - -A simple data access base repository that can be used in other projects - -## Usage - -Full example project can be found here https://github.com/Ian-Webster/sandbox/tree/main/nuget-samples/DataAccess.Sample - -### Install NuGet packages - -Install the following NuGet packges into your project -1. [DataAccess.Repository](https://github.com/Ian-Webster/DataAccess/pkgs/nuget/DataAccess.Repository) -2. [Microsoft.EntityFrameworkCore](https://www.nuget.org/packages/Microsoft.EntityFrameworkCore/7.0.0) -3. [Microsoft.EntityFrameworkCore.Relational](https://www.nuget.org/packages/Microsoft.EntityFrameworkCore.Relational/7.0.0) -4. Your choice of EF core database targeting package (for example https://www.nuget.org/packages/Microsoft.EntityFrameworkCore.SqlServer) - -### Create entites and entity type configurations - -1. Create your database entity classes, as an example a movie entity; - ```csharp - public class Movie - { - public Guid MovieId { get; set; } - public string Name { get; set; } - } - ``` -2. Create your entity type configuration classes, as an example an entity type configuration for the movie entity; - ```csharp - public class MovieEntityTypeConfiguration: IEntityTypeConfiguration - { - public void Configure(EntityTypeBuilder builder) - { - builder.HasKey(pk => pk.MovieId); - builder.ToTable(nameof(Movie), SchemaNames.Public); - } - } - ``` - -### Create a database context class - -You will need to create a database context class that inherits from [DbContext](https://learn.microsoft.com/en-us/dotnet/api/system.data.entity.dbcontext?view=entity-framework-6.2.0) - -As an example here is the database context class for the Movie database; - -```csharp -public class MovieContext: DbContext // inherit from DbContext -{ - // inject DbContextOptions and base to the base class - public MovieContext(DbContextOptions options): base(options) - { - } - - // override OnModelCreating so we can apply our entity type configurations - protected override void OnModelCreating(ModelBuilder modelBuilder) - { - base.OnModelCreating(modelBuilder); - // get the the assembly your EntityTypeConfigurations are in - // in this case they are in the same assembly as this context class - var assembly = Assembly.GetAssembly(typeof(MovieContext)); - if (assembly == null) - { - throw new Exception($"Failed to get assembly for {nameof(MovieContext)}"); - } - modelBuilder.ApplyConfigurationsFromAssembly(assembly); - } -} -``` - -### Create repository classes - -As an example the repository class for the movie entity; - -```csharp -using DataAccess.Repository; -using DataAccess.Sample.Data.DatabaseContexts; -using DataAccess.Sample.Domain.Entities; - -public class MovieRepository: IMovieRepository -{ - private readonly IRepository _movieRepository; - // inject RepositoryFactory for creating IRepository instances - public MovieRepository(RepositoryFactory repositoryFactory) - { - // create an instance of IRepository for our entity (Movie) - _movieRepository = repositoryFactory.GetRepositoryByType(); - } - - public async Task GetMovieById(Guid movieId, CancellationToken token) - { - // perform a database read for the Movie entity - return await _movieRepository.FirstOrDefault(m => m.MovieId == movieId, token); - } -} -``` - -### Configure IoC - -In your IoC bootstrapping you need to; -1. Add your database context, as an example here is the set up for the MovieContext connecting to a postgres database; - ```csharp - builder.Services.AddDbContext(options => - { - options.UseNpgsql(builder.Configuration.GetConnectionString("PostgresConnection")); - }); - ``` -2. Set up your services; - ```csharp - // add RepositoryFactory (this will be needed by your repository class) - builder.Services.AddScoped>(); - // add your repositories - builder.Services.AddScoped(); - ``` - - -## Version history - -- 2.0.0 - work in progress commit to support the DataAccess.Repository.HotChocolate package -- 1.0.4 - added usage instructions to readme file -- 1.0.3 - updated main readme file -- 1.0.2 - changes to main build action to attempt to fix NuGet publishing issue -- 1.0.1 - testing NuGet publish on merge to main post pull request -- 1.0.0 - initial test publication of the NuGet package +# DataAccess + +## Introduction + +A simple data access base repository that can be used in other projects + +## Usage + +Full example project can be found here https://github.com/Ian-Webster/sandbox/tree/main/nuget-samples/DataAccess.Sample + +### Install NuGet packages + +Install the following NuGet packges into your project +1. [DataAccess.Repository](https://github.com/Ian-Webster/DataAccess/pkgs/nuget/DataAccess.Repository) +2. [Microsoft.EntityFrameworkCore](https://www.nuget.org/packages/Microsoft.EntityFrameworkCore/7.0.0) +3. [Microsoft.EntityFrameworkCore.Relational](https://www.nuget.org/packages/Microsoft.EntityFrameworkCore.Relational/7.0.0) +4. Your choice of EF core database targeting package (for example https://www.nuget.org/packages/Microsoft.EntityFrameworkCore.SqlServer) + +### Create entites and entity type configurations + +1. Create your database entity classes, as an example a movie entity; + ```csharp + public class Movie + { + public Guid MovieId { get; set; } + public string Name { get; set; } + } + ``` +2. Create your entity type configuration classes, as an example an entity type configuration for the movie entity; + ```csharp + public class MovieEntityTypeConfiguration: IEntityTypeConfiguration + { + public void Configure(EntityTypeBuilder builder) + { + builder.HasKey(pk => pk.MovieId); + builder.ToTable(nameof(Movie), SchemaNames.Public); + } + } + ``` + +### Create a database context class + +You will need to create a database context class that inherits from [DbContext](https://learn.microsoft.com/en-us/dotnet/api/system.data.entity.dbcontext?view=entity-framework-6.2.0) + +As an example here is the database context class for the Movie database; + +```csharp +public class MovieContext: DbContext // inherit from DbContext +{ + // inject DbContextOptions and base to the base class + public MovieContext(DbContextOptions options): base(options) + { + } + + // override OnModelCreating so we can apply our entity type configurations + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + base.OnModelCreating(modelBuilder); + // get the the assembly your EntityTypeConfigurations are in + // in this case they are in the same assembly as this context class + var assembly = Assembly.GetAssembly(typeof(MovieContext)); + if (assembly == null) + { + throw new Exception($"Failed to get assembly for {nameof(MovieContext)}"); + } + modelBuilder.ApplyConfigurationsFromAssembly(assembly); + } +} +``` + +### Create repository classes + +As an example the repository class for the movie entity; + +```csharp +using DataAccess.Repository; +using DataAccess.Sample.Data.DatabaseContexts; +using DataAccess.Sample.Domain.Entities; + +public class MovieRepository: IMovieRepository +{ + private readonly IRepository _movieRepository; + // inject RepositoryFactory for creating IRepository instances + public MovieRepository(RepositoryFactory repositoryFactory) + { + // create an instance of IRepository for our entity (Movie) + _movieRepository = repositoryFactory.GetRepositoryByType(); + } + + public async Task GetMovieById(Guid movieId, CancellationToken token) + { + // perform a database read for the Movie entity + return await _movieRepository.FirstOrDefault(m => m.MovieId == movieId, token); + } +} +``` + +### Configure IoC + +In your IoC bootstrapping you need to; +1. Add your database context, as an example here is the set up for the MovieContext connecting to a postgres database; + ```csharp + builder.Services.AddDbContext(options => + { + options.UseNpgsql(builder.Configuration.GetConnectionString("PostgresConnection")); + }); + ``` +2. Set up your services; + ```csharp + // add RepositoryFactory (this will be needed by your repository class) + builder.Services.AddScoped>(); + // add your repositories + builder.Services.AddScoped(); + ``` + + +## Version history + +- 2.0.0 - work in progress commit to support the DataAccess.Repository.HotChocolate package +- 1.0.4 - added usage instructions to readme file +- 1.0.3 - updated main readme file +- 1.0.2 - changes to main build action to attempt to fix NuGet publishing issue +- 1.0.1 - testing NuGet publish on merge to main post pull request +- 1.0.0 - initial test publication of the NuGet package