Skip to content

Commit

Permalink
Various fixes and improvements
Browse files Browse the repository at this point in the history
* Merge Magneto.Microsoft into Magneto
* Remove default values for cancellation tokens, except at the top level (reduces language noise for consumers)
* Switch to System.Text.Json
* Update analyzers and apply corresponding fixes
* Rename Magneto class to Conductor (to avoid confusion with the namespace)
* Add IServiceCollection extension method and MagnetoBuilder fluent interface for simplifying startup
  • Loading branch information
shaynevanasperen committed Oct 22, 2020
1 parent a3a59a4 commit d2f30ba
Show file tree
Hide file tree
Showing 47 changed files with 525 additions and 347 deletions.
52 changes: 45 additions & 7 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,61 @@

<PropertyGroup>
<Authors>Shayne van Asperen</Authors>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<LangVersion>latest</LangVersion>
<DebugType>embedded</DebugType>
<Deterministic>true</Deterministic>
<ContinuousIntegrationBuild Condition="'$(APPVEYOR_BUILD_NUMBER)' != ''">true</ContinuousIntegrationBuild>
<IsTestProject>$(MSBuildProjectName.EndsWith('Tests'))</IsTestProject>
<IsSampleProject>$(MSBuildProjectName.StartsWith('Sample'))</IsSampleProject>
<IsPackable>false</IsPackable>
<IsPackable Condition="!$(IsTestProject) AND !$(IsSampleProject)">true</IsPackable>
<NotNCrunch>false</NotNCrunch>
<NotNCrunch Condition="'$(NCrunch)' == ''">true</NotNCrunch>
<GenerateDocumentationFile Condition="$(NotNCrunch)">$(IsPackable)</GenerateDocumentationFile>
<GeneratePackageOnBuild Condition="$(NotNCrunch)">$(IsPackable)</GeneratePackageOnBuild>
<SignAssembly Condition="$(GeneratePackageOnBuild)">true</SignAssembly>
<AssemblyOriginatorKeyFile>$(MSBuildThisFileDirectory)Magneto.snk</AssemblyOriginatorKeyFile>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageProjectUrl>https://github.com/shaynevanasperen/Magneto</PackageProjectUrl>
<PackageIcon>Magneto.png</PackageIcon>
<PackageIconUrl>https://raw.githubusercontent.com/shaynevanasperen/Magneto/master/Magneto.png</PackageIconUrl>
<RepositoryUrl>https://github.com/shaynevanasperen/Magneto.git</RepositoryUrl>
<RepositoryType>Git</RepositoryType>
<AssemblyOriginatorKeyFile>$(MSBuildThisFileDirectory)Magneto.snk</AssemblyOriginatorKeyFile>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<LangVersion>latest</LangVersion>
<APPVEYOR_BUILD_NUMBER Condition="'$(APPVEYOR_BUILD_NUMBER)' == ''">0</APPVEYOR_BUILD_NUMBER>
<MinVerTagPrefix>$(MSBuildProjectName).</MinVerTagPrefix>
<NoWarn Condition="$(IsTestProject)">$(NoWarn);CA1034;CA1051;CA1307;CA1822;CA2007;IDE0051</NoWarn>
</PropertyGroup>

<ItemGroup>
<PackageReference Update="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="2.9.3" PrivateAssets="All" />
<PackageReference Update="Microsoft.CodeQuality.Analyzers" Version="2.9.3" PrivateAssets="All" />
<PackageReference Update="Microsoft.NetCore.Analyzers" Version="2.9.3" PrivateAssets="All" />
<PackageReference Update="Text.Analyzers" Version="2.6.4" PrivateAssets="All" />
<PackageReference Condition="$(NotNCrunch)" Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="3.3.0" PrivateAssets="All" />
<PackageReference Condition="$(NotNCrunch)" Include="Microsoft.CodeQuality.Analyzers" Version="3.3.0" PrivateAssets="All" />
<PackageReference Condition="$(NotNCrunch)" Include="Microsoft.NetCore.Analyzers" Version="3.3.0" PrivateAssets="All" />
<PackageReference Condition="$(NotNCrunch) AND $(IsPackable)" Include="MinVer" Version="2.3.1" PrivateAssets="All" />
<PackageReference Condition="$(NotNCrunch) AND $(IsPackable)" Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
<PackageReference Condition="$(IsTestProject)" Include="FluentAssertions" Version="5.10.3" />
<PackageReference Condition="$(IsTestProject)" Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
<PackageReference Condition="$(IsTestProject)" Include="NSubstitute" Version="4.2.2" />
<PackageReference Condition="$(IsTestProject)" Include="Serilog" Version="2.10.0" />
<PackageReference Condition="$(IsTestProject)" Include="Specify" Version="2.4.0" />
<PackageReference Condition="$(IsTestProject)" Include="TestStack.BDDfy.Xunit" Version="1.2.0" />
<PackageReference Condition="$(IsTestProject)" Include="xunit" Version="2.4.1" />
<PackageReference Condition="$(IsTestProject)" Include="xunit.runner.visualstudio" Version="2.4.3" PrivateAssets="All" />
</ItemGroup>

<ItemGroup>
<None Condition="$(IsPackable)" Include="$(MSBuildThisFileDirectory)Magneto.png" Pack="true" PackagePath="\" />
</ItemGroup>

<ItemGroup>
<SourceRoot Include="$(MSBuildThisFileDirectory)/" />
</ItemGroup>

<Target Name="FileVersion" AfterTargets="MinVer">
<PropertyGroup>
<BUILD_NUMBER Condition="'$(BUILD_NUMBER)' == ''">0</BUILD_NUMBER>
<FileVersion>$(MinVerMajor).$(MinVerMinor).$(MinVerPatch).$(BUILD_NUMBER)</FileVersion>
</PropertyGroup>
</Target>

</Project>
22 changes: 5 additions & 17 deletions Magneto.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26430.15
# Visual Studio Version 16
VisualStudioVersion = 16.0.30611.23
MinimumVisualStudioVersion = 15.0.26124.0
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{C529A908-6353-4998-AA30-4DB69B717BCD}"
EndProject
Expand All @@ -15,8 +15,6 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "samples", "samples", "{8ACE
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Samples", "samples\Samples\Samples.csproj", "{8570D686-AD15-4B2A-9EF5-E825A43662AB}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Magneto.Microsoft", "src\Magneto.Microsoft\Magneto.Microsoft.csproj", "{EF67ACA9-144B-4E8E-AB97-8D958A6BB480}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Samples.Tests", "samples\Samples.Tests\Samples.Tests.csproj", "{ED0DE9A0-5C24-4C7E-A6E0-81A26FF024EC}"
EndProject
Global
Expand Down Expand Up @@ -65,18 +63,6 @@ Global
{8570D686-AD15-4B2A-9EF5-E825A43662AB}.Release|x64.Build.0 = Release|Any CPU
{8570D686-AD15-4B2A-9EF5-E825A43662AB}.Release|x86.ActiveCfg = Release|Any CPU
{8570D686-AD15-4B2A-9EF5-E825A43662AB}.Release|x86.Build.0 = Release|Any CPU
{EF67ACA9-144B-4E8E-AB97-8D958A6BB480}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EF67ACA9-144B-4E8E-AB97-8D958A6BB480}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EF67ACA9-144B-4E8E-AB97-8D958A6BB480}.Debug|x64.ActiveCfg = Debug|Any CPU
{EF67ACA9-144B-4E8E-AB97-8D958A6BB480}.Debug|x64.Build.0 = Debug|Any CPU
{EF67ACA9-144B-4E8E-AB97-8D958A6BB480}.Debug|x86.ActiveCfg = Debug|Any CPU
{EF67ACA9-144B-4E8E-AB97-8D958A6BB480}.Debug|x86.Build.0 = Debug|Any CPU
{EF67ACA9-144B-4E8E-AB97-8D958A6BB480}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EF67ACA9-144B-4E8E-AB97-8D958A6BB480}.Release|Any CPU.Build.0 = Release|Any CPU
{EF67ACA9-144B-4E8E-AB97-8D958A6BB480}.Release|x64.ActiveCfg = Release|Any CPU
{EF67ACA9-144B-4E8E-AB97-8D958A6BB480}.Release|x64.Build.0 = Release|Any CPU
{EF67ACA9-144B-4E8E-AB97-8D958A6BB480}.Release|x86.ActiveCfg = Release|Any CPU
{EF67ACA9-144B-4E8E-AB97-8D958A6BB480}.Release|x86.Build.0 = Release|Any CPU
{ED0DE9A0-5C24-4C7E-A6E0-81A26FF024EC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{ED0DE9A0-5C24-4C7E-A6E0-81A26FF024EC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{ED0DE9A0-5C24-4C7E-A6E0-81A26FF024EC}.Debug|x64.ActiveCfg = Debug|Any CPU
Expand All @@ -97,7 +83,9 @@ Global
{BD3F8F6D-A51C-4249-ADD7-6C099C8B9E43} = {C529A908-6353-4998-AA30-4DB69B717BCD}
{58423EFB-4531-4989-9336-1FA9F4C95D7F} = {3F1747A6-02DB-43F9-987E-18C26A3B8D46}
{8570D686-AD15-4B2A-9EF5-E825A43662AB} = {8ACE88B4-2C33-4DFB-8364-64DAEE706077}
{EF67ACA9-144B-4E8E-AB97-8D958A6BB480} = {C529A908-6353-4998-AA30-4DB69B717BCD}
{ED0DE9A0-5C24-4C7E-A6E0-81A26FF024EC} = {8ACE88B4-2C33-4DFB-8364-64DAEE706077}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {FAEBD1F1-D24F-4137-8F90-F30E337457CC}
EndGlobalSection
EndGlobal
9 changes: 6 additions & 3 deletions ScenarioFor.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Specify;
using System;
using Specify;
using Specify.Stories;
using TestStack.BDDfy.Configuration;
using TestStack.BDDfy.Xunit;
Expand Down Expand Up @@ -34,9 +35,11 @@ public static class ScenarioExtensions
{
public static string GetTitle<TSut, TStory>(this Specify.ScenarioFor<TSut, TStory> scenario) where TSut : class where TStory : Story, new()
{
var type = scenario.GetType();
if (scenario == null) throw new ArgumentNullException(nameof(scenario));

var type = scenario.GetType();
var title = Configurator.Scanners
.Humanize(type.FullName.Replace(type.Namespace + ".", string.Empty))
.Humanize(type.FullName?.Replace(type.Namespace + ".", string.Empty))
.ToTitleCase();
if (scenario.Number != 0)
{
Expand Down
27 changes: 0 additions & 27 deletions samples/Samples.Tests/Properties/launchSettings.json

This file was deleted.

14 changes: 1 addition & 13 deletions samples/Samples.Tests/Samples.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,25 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<NoWarn>$(NoWarn);CA2007;CA1307</NoWarn>
</PropertyGroup>

<ItemGroup>
<Compile Include="..\..\ScenarioFor.cs" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="5.10.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
<PackageReference Include="NSubstitute" Version="4.2.1" />
<PackageReference Include="Serilog" Version="2.9.0" />
<PackageReference Include="Specify" Version="2.4.0" />
<PackageReference Include="TestStack.BDDfy.Xunit" Version="1.0.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1" PrivateAssets="All" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Samples\Samples.csproj" />
</ItemGroup>
Expand Down
8 changes: 4 additions & 4 deletions samples/Samples/Controllers/PostsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public async Task<IActionResult> Post(int id, string body)

public class AllPosts : AsyncQuery<JsonPlaceHolderHttpClient, Post[]>
{
protected override Task<Post[]> Query(JsonPlaceHolderHttpClient context, CancellationToken cancellationToken = default) =>
protected override Task<Post[]> Query(JsonPlaceHolderHttpClient context, CancellationToken cancellationToken) =>
context.GetAsync<Post[]>("/posts", cancellationToken);
}

Expand All @@ -61,7 +61,7 @@ public class PostById : AsyncCachedQuery<JsonPlaceHolderHttpClient, DistributedC
protected override DistributedCacheEntryOptions CacheEntryOptions(JsonPlaceHolderHttpClient context) =>
new DistributedCacheEntryOptions().SetAbsoluteExpiration(TimeSpan.FromSeconds(30));

protected override Task<Post> Query(JsonPlaceHolderHttpClient context, CancellationToken cancellationToken = default) =>
protected override Task<Post> Query(JsonPlaceHolderHttpClient context, CancellationToken cancellationToken) =>
context.GetAsync<Post>($"/posts/{Id}", cancellationToken);

public int Id { get; set; }
Expand All @@ -74,15 +74,15 @@ public class CommentsByPostId : AsyncCachedQuery<JsonPlaceHolderHttpClient, Memo
protected override MemoryCacheEntryOptions CacheEntryOptions(JsonPlaceHolderHttpClient context) =>
new MemoryCacheEntryOptions().SetAbsoluteExpiration(TimeSpan.FromSeconds(30));

protected override Task<Comment[]> Query(JsonPlaceHolderHttpClient context, CancellationToken cancellationToken = default) =>
protected override Task<Comment[]> Query(JsonPlaceHolderHttpClient context, CancellationToken cancellationToken) =>
context.GetAsync<Comment[]>($"/posts/{PostId}/comments", cancellationToken);

public int PostId { get; set; }
}

public class SavePost : AsyncCommand<JsonPlaceHolderHttpClient>
{
public override Task Execute(JsonPlaceHolderHttpClient context, CancellationToken cancellationToken = default) =>
public override Task Execute(JsonPlaceHolderHttpClient context, CancellationToken cancellationToken) =>
context.PostAsync($"/posts/{Post.Id}", Post, cancellationToken);

public Post Post { get; set; }
Expand Down
26 changes: 12 additions & 14 deletions samples/Samples/Controllers/UsersController.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.IO;
using System.Linq;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Magneto;
Expand All @@ -8,7 +9,6 @@
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Samples.Domain;
using Samples.Models;

Expand Down Expand Up @@ -51,7 +51,7 @@ public class AllUsers : AsyncCachedQuery<JsonPlaceHolderHttpClient, DistributedC
protected override DistributedCacheEntryOptions CacheEntryOptions(JsonPlaceHolderHttpClient context) =>
User.AllUsersCacheEntryOptions();

protected override Task<User[]> Query(JsonPlaceHolderHttpClient context, CancellationToken cancellationToken = default) =>
protected override Task<User[]> Query(JsonPlaceHolderHttpClient context, CancellationToken cancellationToken) =>
User.AllUsersAsync(context, cancellationToken);
}

Expand All @@ -62,10 +62,10 @@ public class UserById : AsyncTransformedCachedQuery<JsonPlaceHolderHttpClient, D
protected override DistributedCacheEntryOptions CacheEntryOptions(JsonPlaceHolderHttpClient context) =>
User.AllUsersCacheEntryOptions();

protected override Task<User[]> Query(JsonPlaceHolderHttpClient context, CancellationToken cancellationToken = default) =>
protected override Task<User[]> Query(JsonPlaceHolderHttpClient context, CancellationToken cancellationToken) =>
User.AllUsersAsync(context, cancellationToken);

protected override Task<User> TransformCachedResult(User[] cachedResult, CancellationToken cancellationToken = default) =>
protected override Task<User> TransformCachedResult(User[] cachedResult, CancellationToken cancellationToken) =>
Task.FromResult(cachedResult.SingleOrDefault(x => x.Id == Id));

public int Id { get; set; }
Expand All @@ -85,23 +85,21 @@ protected override MemoryCacheEntryOptions CacheEntryOptions((IFileProvider, ILo
protected override Album[] Query((IFileProvider, ILogger<AlbumsByUserId>) context)
{
var (fileProvider, _) = context;
using (var streamReader = new StreamReader(fileProvider.GetFileInfo(Album.AllAlbumsFilename).CreateReadStream()))
{
var json = streamReader.ReadToEnd();
return JsonConvert.DeserializeObject<Album[]>(json);
}
using var streamReader = new StreamReader(fileProvider.GetFileInfo(Album.AllAlbumsFilename).CreateReadStream());
var json = streamReader.ReadToEnd();
return JsonSerializer.Deserialize<Album[]>(json);
}

protected override Album[] TransformCachedResult(Album[] cachedResult) => cachedResult.Where(x => x.UserId == UserId).ToArray();

public int UserId { get; set; }
}

public class SaveAlbum : SyncCommand<(IFileProvider, JsonSerializerSettings)>
public class SaveAlbum : SyncCommand<(IFileProvider, JsonSerializerOptions)>
{
public override void Execute((IFileProvider, JsonSerializerSettings) context)
public override void Execute((IFileProvider, JsonSerializerOptions) context)
{
var (fileProvider, jsonSerializerSettings) = context;
var (fileProvider, jsonSerializerOptions) = context;
lock (fileProvider)
{
var fileInfo = fileProvider.GetFileInfo(Album.AllAlbumsFilename);
Expand All @@ -110,9 +108,9 @@ public override void Execute((IFileProvider, JsonSerializerSettings) context)
using (var streamReader = new StreamReader(fileInfo.CreateReadStream()))
json = streamReader.ReadToEnd();

var existingAlbums = JsonConvert.DeserializeObject<Album[]>(json);
var existingAlbums = JsonSerializer.Deserialize<Album[]>(json);
Album.Id = existingAlbums.Max(x => x.Id) + 1;
json = JsonConvert.SerializeObject(existingAlbums.Concat(new[] { Album }), jsonSerializerSettings);
json = JsonSerializer.Serialize(existingAlbums.Concat(new[] { Album }), jsonSerializerOptions);

File.WriteAllText(fileInfo.PhysicalPath, json);
}
Expand Down
4 changes: 2 additions & 2 deletions samples/Samples/Domain/Entities.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Caching.Distributed;
Expand Down Expand Up @@ -69,7 +69,7 @@ public class User
public static DistributedCacheEntryOptions AllUsersCacheEntryOptions() =>
new DistributedCacheEntryOptions().SetAbsoluteExpiration(TimeSpan.FromSeconds(30));

public static Task<User[]> AllUsersAsync(JsonPlaceHolderHttpClient context, CancellationToken cancellationToken = default) =>
public static Task<User[]> AllUsersAsync(JsonPlaceHolderHttpClient context, CancellationToken cancellationToken) =>
context.GetAsync<User[]>("/users", cancellationToken);
}
}
6 changes: 6 additions & 0 deletions samples/Samples/Domain/JsonPlaceHolderHttpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,13 @@ public async Task<T> GetAsync<T>(string requestUri, CancellationToken cancellati
return JsonConvert.DeserializeObject<T>(content);
}

#pragma warning disable CA1822 // Mark members as static
#pragma warning disable CA1801 // Review unused parameters
#pragma warning disable IDE0060 // Remove unused parameter
public Task<HttpResponseMessage> PostAsync<T>(string requestUri, T data, CancellationToken cancellationToken = default) =>
#pragma warning restore IDE0060 // Remove unused parameter
#pragma warning restore CA1801 // Review unused parameters
#pragma warning restore CA1822 // Mark members as static
Task.FromResult(new HttpResponseMessage(HttpStatusCode.NoContent));
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
using System;
using System;
using System.Diagnostics;
using System.Threading.Tasks;
using Magneto.Configuration;
using Microsoft.ApplicationInsights;

namespace Samples.Infrastructure
{
public class ApplicationInsightsDecorator : IDecorator
#pragma warning disable CA1812 // Avoid uninstantiated internal classes
internal class ApplicationInsightsDecorator : IDecorator
#pragma warning restore CA1812 // Avoid uninstantiated internal classes
{
readonly TelemetryClient _telemetryClient;

Expand Down
9 changes: 4 additions & 5 deletions samples/Samples/Samples.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,20 @@

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<NoWarn>$(NoWarn);CA2007;CA1819;CA1054;CA2234</NoWarn>
<NoWarn>$(NoWarn);CA1054;CA1062;CA1819;CA2007;CA2234</NoWarn>
</PropertyGroup>

<ItemGroup>
<Content Remove="wwwroot/albums.json" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="BuildBundlerMinifier" Version="3.2.435" />
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.13.1" />
<PackageReference Include="Microsoft.Extensions.Http.Polly" Version="3.1.2" />
<PackageReference Include="BuildBundlerMinifier" Version="3.2.449" />
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.15.0" />
<PackageReference Include="Microsoft.Extensions.Http.Polly" Version="3.1.9" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Magneto.Microsoft\Magneto.Microsoft.csproj" />
<ProjectReference Include="..\..\src\Magneto\Magneto.csproj" />
</ItemGroup>

Expand Down
Loading

0 comments on commit d2f30ba

Please sign in to comment.