Skip to content

Commit

Permalink
merged master
Browse files Browse the repository at this point in the history
  • Loading branch information
nscheibe committed Jan 19, 2024
2 parents eabc7e1 + 9fc7ca2 commit d64ffbc
Show file tree
Hide file tree
Showing 211 changed files with 10,596 additions and 468 deletions.
2 changes: 1 addition & 1 deletion .config/dotnet-tools.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"isRoot": true,
"tools": {
"dotnet-sonarscanner": {
"version": "4.8.0",
"version": "5.13.1",
"commands": [
"dotnet-sonarscanner"
]
Expand Down
29 changes: 29 additions & 0 deletions .github/workflows/pull-request.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Pull Request

on:
pull_request:
branches: ['master']

jobs:
tests:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: |
6
7
- name: Build, Test and Sonar
uses: swisslife-oss/actions/pull-request@main
with:
sonar_token: ${{ secrets.SONAR_TOKEN }}
sonar_project_key: 'SwissLife-OSS_Mongo-Extensions'
sonar_project_name: "mongo-extensions"
pr_number: ${{ github.event.pull_request.number }}
pr_source_branch: ${{ github.head_ref }}
pr_target_branch: ${{ github.base_ref }}
github_repository: ${{ github.repository }}
sonar_exclusions: ${{ vars.SONAR_EXCLUSIONS }}
44 changes: 44 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: Release

on:
push:
tags:
- "*"

jobs:
release:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Use .NET SDK from global.json
uses: actions/setup-dotnet@v3
- name: Build, Test and Push
uses: swisslife-oss/actions/release-packages@main
with:
tag: ${{ github.ref_name }}
nuget_api_key: ${{ secrets.NUGET_API_KEY }}
enable_push: 'yes'

sonar:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: |
6
7
- name: Restore tools
run: dotnet tool restore
- name: Build, Test and Sonar
uses: swisslife-oss/actions/release-sonar@main
with:
tag: ${{ github.ref_name }}
sonar_token: ${{ secrets.SONAR_TOKEN }}
sonar_project_key: 'SwissLife-OSS_Mongo-Extensions'
sonar_project_name: "mongo-extensions"
sonar_exclusions: ${{ vars.SONAR_EXCLUSIONS }}
61 changes: 61 additions & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<Project>

<PropertyGroup>
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
</PropertyGroup>

<ItemGroup>
<PackageVersion Include="Moq" Version="4.16.1" />
<PackageVersion Include="Snapshooter.Xunit" Version="0.13.0" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
<PackageVersion Include="Squadron.Mongo" Version="0.18.0" />
<PackageVersion Include="xunit" Version="2.4.2" />
<PackageVersion Include="Xunit.Priority" Version="1.1.6" />
<PackageVersion Include="FluentAssertions" Version="6.2.0" />
<PackageVersion Include="xunit.runner.visualstudio" Version="2.4.3" />
<PackageVersion Include="coverlet.msbuild" Version="6.0.0" />
</ItemGroup>

<ItemGroup>
<PackageVersion Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All"/>
</ItemGroup>

<ItemGroup>
<PackageVersion Include="MongoDB.Driver" Version="2.21.0" />
<PackageVersion Include="MongoDB.Driver.Core.Extensions.DiagnosticSources" Version="1.3.0" />
<PackageVersion Include="System.Text.Json" Version="7.0.0" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net7.0'">
<PackageVersion Include="Microsoft.Extensions.Configuration" Version="7.0.0" />
<PackageVersion Include="Microsoft.Extensions.Configuration.Abstractions" Version="7.0.0" />
<PackageVersion Include="Microsoft.Extensions.Configuration.Binder" Version="7.0.0" />
<PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
<PackageVersion Include="Microsoft.Extensions.Logging" Version="7.0.0" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net6.0'">
<PackageVersion Include="Microsoft.Extensions.Configuration" Version="6.0.0" />
<PackageVersion Include="Microsoft.Extensions.Configuration.Abstractions" Version="6.0.0" />
<PackageVersion Include="Microsoft.Extensions.Configuration.Binder" Version="6.0.0" />
<PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
<PackageVersion Include="Microsoft.Extensions.Logging" Version="6.0.0" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.1'">
<PackageVersion Include="Microsoft.Extensions.Configuration" Version="3.0.0" />
<PackageVersion Include="Microsoft.Extensions.Configuration.Abstractions" Version="3.0.0" />
<PackageVersion Include="Microsoft.Extensions.Configuration.Binder" Version="3.0.0" />
<PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="3.0.0" />
<PackageVersion Include="Microsoft.Extensions.Logging" Version="3.0.0" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0'">
<PackageVersion Include="Microsoft.Extensions.Configuration" Version="2.2.0" />
<PackageVersion Include="Microsoft.Extensions.Configuration.Abstractions" Version="2.2.0" />
<PackageVersion Include="Microsoft.Extensions.Configuration.Binder" Version="2.2.0" />
<PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="2.2.0" />
<PackageVersion Include="Microsoft.Extensions.Logging" Version="2.2.0" />
</ItemGroup>

</Project>
133 changes: 63 additions & 70 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## [![Nuget](https://img.shields.io/nuget/v/MongoDB.Extensions.Context.svg?style=flat)](https://www.nuget.org/packages/MongoDB.Extensions.Context) [![GitHub Release](https://img.shields.io/github/release/SwissLife-OSS/mongo-extensions.svg?style=flat)](https://github.com/SwissLife-OSS/Mongo-extensions/releases/latest) [![Build Status](https://dev.azure.com/swisslife-oss/swisslife-oss/_apis/build/status/MongoDB.Extensions.Release?branchName=master)](https://dev.azure.com/swisslife-oss/swisslife-oss/_build/latest?definitionId=11&branchName=master)
## [![Nuget](https://img.shields.io/nuget/v/MongoDB.Extensions.Context.svg?style=flat)](https://www.nuget.org/packages/MongoDB.Extensions.Context) [![GitHub Release](https://img.shields.io/github/release/SwissLife-OSS/mongo-extensions.svg?style=flat)](https://github.com/SwissLife-OSS/Mongo-extensions/releases/latest) [![Build Status](https://github.com/SwissLife-OSS/mongo-extensions/actions/workflows/release.yml/badge.svg)](https://github.com/SwissLife-OSS/mongo-extensions/actions/workflows/release.yml)

**MongoDB.Extensions provides a set of utility libraries for MongoDB.**

Expand Down Expand Up @@ -30,15 +30,14 @@ Create a new class and inherit from the MongoDbContext (abstract) class. Add the
```csharp
public class SimpleBlogDbContext : MongoDbContext
{
public SimpleBlogDbContext(MongoOptions mongoOptions) : base(mongoOptions)
public SimpleBlogDbContext(MongoOptions mongoOptions)
: base(mongoOptions)
{
}

protected override void OnConfiguring(IMongoDatabaseBuilder mongoDatabaseBuilder)
{
.
.
.
...
}
}
```
Expand All @@ -51,6 +50,7 @@ protected override void OnConfiguring(IMongoDatabaseBuilder mongoDatabaseBuilder
mongoDatabaseBuilder
.RegisterCamelCaseConventionPack()
.RegisterSerializer(new DateTimeOffsetSerializer())
.AddAllowedTypes("Namspace.Project")
.ConfigureConnection(con => con.ReadConcern = ReadConcern.Majority)
.ConfigureConnection(con => con.WriteConcern = WriteConcern.WMajority)
.ConfigureConnection(con => con.ReadPreference = ReadPreference.Primary)
Expand All @@ -67,25 +67,25 @@ public class TagCollectionConfiguration : IMongoCollectionConfiguration<Tag>
public void OnConfiguring(IMongoCollectionBuilder<Tag> mongoCollectionBuilder)
{
mongoCollectionBuilder
.AddBsonClassMap<Tag>(cm =>
{
cm.AutoMap();
cm.SetIgnoreExtraElements(true);
})
.WithCollectionSettings(setting =>
{
setting.ReadPreference = ReadPreference.Nearest;
setting.ReadConcern = ReadConcern.Available;
setting.WriteConcern = WriteConcern.Acknowledged;
})
.WithCollectionConfiguration(collection =>
{
var timestampIndex = new CreateIndexModel<Tag>(
Builders<Tag>.IndexKeys.Ascending(tag => tag.Name),
new CreateIndexOptions { Unique = true });

collection.Indexes.CreateOne(timestampIndex);
});
.AddBsonClassMap<Tag>(cm =>
{
cm.AutoMap();
cm.SetIgnoreExtraElements(true);
})
.WithCollectionSettings(setting =>
{
setting.ReadPreference = ReadPreference.Nearest;
setting.ReadConcern = ReadConcern.Available;
setting.WriteConcern = WriteConcern.Acknowledged;
})
.WithCollectionConfiguration(collection =>
{
var timestampIndex = new CreateIndexModel<Tag>(
Builders<Tag>.IndexKeys.Ascending(tag => tag.Name),
new CreateIndexOptions { Unique = true });

collection.Indexes.CreateOne(timestampIndex);
});
}
}
```
Expand All @@ -96,17 +96,17 @@ To use your MongoDB bootstrapping context, register it in your DI-Container.
Example:

```csharp
public static IServiceCollection AddDatabase(
this IServiceCollection services, IConfiguration configuration)
{
MongoOptions blogDbOptions = configuration
.GetMongoOptions("SimpleBlog:Database");
public static IServiceCollection AddDatabase(
this IServiceCollection services, IConfiguration configuration)
{
MongoOptions blogDbOptions = configuration
.GetMongoOptions("SimpleBlog:Database");

services.AddSingleton(blogDbOptions);
services.AddSingleton<SimpleBlogDbContext>();
services.AddSingleton(blogDbOptions);
services.AddSingleton<SimpleBlogDbContext>();

return services;
}
return services;
}
```

When the MongoDBContext is used the first time, then the connection, database and collections, serializers, classMaps, convention packs etc. will be initialized and configured according your configuration.
Expand All @@ -116,50 +116,43 @@ The MongoDbContext contains the configured MongoDB client, database and collecti

```csharp
public abstract class MongoDbContext : IMongoDbContext
{
.
.
.
public IMongoClient Client { get; }
public IMongoDatabase Database { get; }
public MongoOptions MongoOptions { get; }

public IMongoCollection<TDocument> CreateCollection<TDocument>() where TDocument : class;
.
.
.
}
{
...
public IMongoClient Client { get; }
public IMongoDatabase Database { get; }
public MongoOptions MongoOptions { get; }

public IMongoCollection<TDocument> CreateCollection<TDocument>() where TDocument : class;
...
}
```

In the following Repository class example, we use the MongoDbContext to get the configured MongoDB collection.

```csharp
public class TagRepository : ITagRepository
{
private IMongoCollection<Tag> _mongoCollection;

public TagRepository(ISimpleBlogDbContext simpleBlogDbContext)
{
if (simpleBlogDbContext == null)
throw new ArgumentNullException(nameof(simpleBlogDbContext));

_mongoCollection = simpleBlogDbContext.CreateCollection<Tag>();
}

public async Task<IEnumerable<Tag>> GetTagsAsync(
CancellationToken cancellationToken = default)
{
private IMongoCollection<Tag> _mongoCollection;

public TagRepository(ISimpleBlogDbContext simpleBlogDbContext)
{
if (simpleBlogDbContext == null)
throw new ArgumentNullException(nameof(simpleBlogDbContext));

_mongoCollection = simpleBlogDbContext.CreateCollection<Tag>();
}

public async Task<IEnumerable<Tag>> GetTagsAsync(
CancellationToken cancellationToken = default)
{
var findOptions = new FindOptions<Tag>();

IAsyncCursor<Tag> result = await _mongoCollection.FindAsync<Tag>(
Builders<Tag>.Filter.Empty, findOptions, cancellationToken);

return await result.ToListAsync();
}
.
.
.
.
var findOptions = new FindOptions<Tag>();

IAsyncCursor<Tag> result = await _mongoCollection.FindAsync<Tag>(
Builders<Tag>.Filter.Empty, findOptions, cancellationToken);

return await result.ToListAsync();
}
...
```

A full MongoDB bootstrapping example can be found in our [SimpleBlog](https://swisslife-oss.github.io/mongo-extensions/samples/) web-application.
Expand Down
3 changes: 2 additions & 1 deletion global.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"sdk": {
"version": "3.1.100"
"version": "7.0.100",
"rollForward": "latestFeature"
}
}
15 changes: 15 additions & 0 deletions nuget.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageRestore>
<add key="enabled" value="True" />
<add key="automatic" value="True" />
</packageRestore>
<activePackageSource>
<clear />
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
</activePackageSource>
<packageSources>
<clear />
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
</packageSources>
</configuration>
23 changes: 5 additions & 18 deletions samples/Context/DataAccess/DataAccess.csproj
Original file line number Diff line number Diff line change
@@ -1,28 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;netstandard2.1</TargetFrameworks>
<TargetFrameworks>net7.0</TargetFrameworks>
<AssemblyName>SimpleBlog.DataAccess</AssemblyName>
<RootNamespace>SimpleBlog.DataAccess</RootNamespace>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.1'">
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="3.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="3.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="3.0.0" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'netcoreapp3.1'">
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="3.1.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="3.1.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="3.1.0" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0'">
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="2.2.0" />
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" />
</ItemGroup>

<ItemGroup>
Expand Down
Loading

0 comments on commit d64ffbc

Please sign in to comment.