Skip to content

Commit

Permalink
#19 version bump read me changes + test.yml
Browse files Browse the repository at this point in the history
  • Loading branch information
Ian-Webster committed Dec 6, 2023
1 parent 226f60f commit 3652ec6
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 126 deletions.
36 changes: 36 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion DataAccess.Repository/DataAccess.Repository.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<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>2.0.1</VersionPrefix>
<VersionPrefix>2.0.2</VersionPrefix>
<VersionSuffix></VersionSuffix>
</PropertyGroup>

Expand Down
2 changes: 1 addition & 1 deletion Extensions/DataAccess.Repository.HotChocolate/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
248 changes: 124 additions & 124 deletions README.md
Original file line number Diff line number Diff line change
@@ -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<Movie>
{
public void Configure(EntityTypeBuilder<Movie> 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<Movie> _movieRepository;
// inject RepositoryFactory for creating IRepository instances
public MovieRepository(RepositoryFactory<MovieContext> repositoryFactory)
{
// create an instance of IRepository for our entity (Movie)
_movieRepository = repositoryFactory.GetRepositoryByType<Movie>();
}

public async Task<Movie?> 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<MovieContext>(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<RepositoryFactory<MovieContext>>();
// add your repositories
builder.Services.AddScoped<IMovieRepository, MovieRepository>();
```


## 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<Movie>
{
public void Configure(EntityTypeBuilder<Movie> 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<Movie> _movieRepository;
// inject RepositoryFactory for creating IRepository instances
public MovieRepository(RepositoryFactory<MovieContext> repositoryFactory)
{
// create an instance of IRepository for our entity (Movie)
_movieRepository = repositoryFactory.GetRepositoryByType<Movie>();
}

public async Task<Movie?> 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<MovieContext>(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<RepositoryFactory<MovieContext>>();
// add your repositories
builder.Services.AddScoped<IMovieRepository, MovieRepository>();
```


## 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

0 comments on commit 3652ec6

Please sign in to comment.