Skip to content

Commit

Permalink
Switch to NET6, implicit usings and file scoped namespaces; fix CodeQL (
Browse files Browse the repository at this point in the history
  • Loading branch information
sungam3r committed Mar 14, 2022
1 parent e06a6c8 commit f506310
Show file tree
Hide file tree
Showing 49 changed files with 1,262 additions and 1,348 deletions.
2 changes: 2 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
version: 2
updates:

- package-ecosystem: "nuget"
directory: "/"
schedule:
interval: "daily"

- package-ecosystem: "github-actions"
directory: "/"
schedule:
Expand Down
29 changes: 24 additions & 5 deletions .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# https://github.com/github/codeql
# https://github.com/github/codeql-action
name: CodeQL analysis

on:
Expand All @@ -9,12 +11,29 @@ on:
jobs:
analyze:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Checkout source
uses: actions/checkout@v3

- name: Setup .NET Core SDK
uses: actions/setup-dotnet@v1
with:
fetch-depth: 2
- uses: github/codeql-action/init@v1
dotnet-version: '6.0.102'

- name: Initialize CodeQL
uses: github/codeql-action/init@v1
with:
queries: security-and-quality
languages: csharp
- uses: github/codeql-action/autobuild@v1
- uses: github/codeql-action/analyze@v1

- name: Install dependencies
working-directory: src
run: dotnet restore

- name: Build solution
working-directory: src
run: dotnet build --no-restore

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v1
4 changes: 2 additions & 2 deletions .github/workflows/publish-preview.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ jobs:
steps:
- name: Checkout source
uses: actions/checkout@v3
- name: Setup .NET Core 5.0 SDK
- name: Setup .NET Core 6.0 SDK
uses: actions/setup-dotnet@v2
with:
dotnet-version: '5.0.x'
dotnet-version: '6.0.x'
source-url: https://nuget.pkg.github.com/sungam3r/index.json
env:
NUGET_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}}
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/publish-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ jobs:
version="${github_ref:10}"
echo version=$version
echo "version=$version" >> $GITHUB_ENV
- name: Setup .NET Core 5.0 SDK
- name: Setup .NET Core 6.0 SDK
uses: actions/setup-dotnet@v2
with:
dotnet-version: '5.0.x'
dotnet-version: '6.0.x'
source-url: https://api.nuget.org/v3/index.json
env:
NUGET_AUTH_TOKEN: ${{secrets.NUGET_API_KEY}}
Expand Down
19 changes: 7 additions & 12 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,13 @@ jobs:
steps:
- name: Checkout source
uses: actions/checkout@v3
- name: Setup .NET Core 3.1 SDK
- name: Setup .NET Core SDKs
uses: actions/setup-dotnet@v2
with:
dotnet-version: '3.1.x'
source-url: https://nuget.pkg.github.com/sungam3r/index.json
env:
NUGET_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}}
- name: Setup .NET Core 5.0 SDK
uses: actions/setup-dotnet@v2
with:
dotnet-version: '5.0.x'
dotnet-version: |
3.1.x
5.0.x
6.0.102
source-url: https://nuget.pkg.github.com/sungam3r/index.json
env:
NUGET_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}}
Expand All @@ -52,8 +48,7 @@ jobs:
if: ${{ startsWith(matrix.os, 'ubuntu') }}
working-directory: src
run: |
dotnet tool install -g dotnet-format --version 6.0.243104 --add-source https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-tools/nuget/v3/index.json
dotnet format --check -v diag --fix-style warn --fix-analyzers warn || (echo "Run 'dotnet format' to fix formatting issues" && exit 1)
dotnet format --no-restore --verify-no-changes --severity warn || (echo "Run 'dotnet format' to fix issues" && exit 1)
- name: Build solution [Release]
working-directory: src
run: dotnet build --no-restore -c Release
Expand All @@ -67,7 +62,7 @@ jobs:
if: ${{ startsWith(matrix.os, 'ubuntu') }}
uses: codecov/codecov-action@v2.1.0
with:
files: .coverage/SteroidsDI.Tests/coverage.netcoreapp3.1.opencover.xml
files: .coverage/SteroidsDI.Tests/coverage.net6.opencover.xml

buildcheck:
needs:
Expand Down
73 changes: 35 additions & 38 deletions src/Example/Models/EntryPoint.cs
Original file line number Diff line number Diff line change
@@ -1,50 +1,47 @@
using System;
namespace Example;

namespace Example
public interface IEntryPoint
{
public interface IEntryPoint
int DoSomethingImportant();
}

public class EntryPoint : IEntryPoint
{
//private IRepository _repository; - this is an example of what you wanted to do but got the error below
private readonly Defer<IRepository> _repository1;
private readonly Func<IRepository> _repository2;
private readonly IRepositoryFactory _repoFactory;

public EntryPoint(
// Error while validating the service descriptor 'ServiceType: Example.IEntryPoint Lifetime: Singleton ImplementationType: Example.EntryPoint':
// Cannot consume scoped service 'Example.IRepository' from singleton 'Example.IEntryPoint'.
//IRepository repository
Defer<IRepository> repository1,
Func<IRepository> repository2,
IRepositoryFactory repoFactory)
{
int DoSomethingImportant();
_repository1 = repository1;
_repository2 = repository2;
_repoFactory = repoFactory;
}

public class EntryPoint : IEntryPoint
public int DoSomethingImportant()
{
//private IRepository _repository; - this is an example of what you wanted to do but got the error below
private readonly Defer<IRepository> _repository1;
private readonly Func<IRepository> _repository2;
private readonly IRepositoryFactory _repoFactory;

public EntryPoint(
// Error while validating the service descriptor 'ServiceType: Example.IEntryPoint Lifetime: Singleton ImplementationType: Example.EntryPoint':
// Cannot consume scoped service 'Example.IRepository' from singleton 'Example.IEntryPoint'.
//IRepository repository
Defer<IRepository> repository1,
Func<IRepository> repository2,
IRepositoryFactory repoFactory)
{
_repository1 = repository1;
_repository2 = repository2;
_repoFactory = repoFactory;
}
// All 3 APIs return the same instance
var repo1 = _repository1.Value;
var repo2 = _repository2();
var repo3 = _repoFactory.GetPersonsRepo();

public int DoSomethingImportant()
{
// All 3 APIs return the same instance
var repo1 = _repository1.Value;
var repo2 = _repository2();
var repo3 = _repoFactory.GetPersonsRepo();

if (!ReferenceEquals(repo1, repo2) || !ReferenceEquals(repo1, repo3))
throw new InvalidOperationException();

var persons = repo1.GetPersons();
if (!ReferenceEquals(repo1, repo2) || !ReferenceEquals(repo1, repo3))
throw new InvalidOperationException();

foreach (var person in persons)
{
Console.WriteLine($"{person.Name} ({person.Age})");
}
var persons = repo1.GetPersons();

return persons.Count;
foreach (var person in persons)
{
Console.WriteLine($"{person.Name} ({person.Age})");
}

return persons.Count;
}
}
11 changes: 5 additions & 6 deletions src/Example/Models/IRepositoryFactory.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
namespace Example
namespace Example;

public interface IRepositoryFactory
{
public interface IRepositoryFactory
{
// method name does not matter
IRepository GetPersonsRepo();
}
// method name does not matter
IRepository GetPersonsRepo();
}
11 changes: 5 additions & 6 deletions src/Example/Models/Person.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
namespace Example
namespace Example;

public class Person
{
public class Person
{
public string? Name { get; set; }
public string? Name { get; set; }

public int Age { get; set; }
}
public int Age { get; set; }
}
51 changes: 23 additions & 28 deletions src/Example/Models/Repository.cs
Original file line number Diff line number Diff line change
@@ -1,38 +1,33 @@
using System;
using System.Collections.Generic;
using System.Threading;
namespace Example;

namespace Example
public interface IRepository
{
public interface IRepository
List<Person> GetPersons();
}

public class Repository : IRepository
{
public Repository()
{
List<Person> GetPersons();
Console.WriteLine("Initializing repository");
Thread.Sleep(3000);
Console.WriteLine("Repository initialized");
}

public class Repository : IRepository
public List<Person> GetPersons()
{
public Repository()
return new List<Person>
{
Console.WriteLine("Initializing repository");
Thread.Sleep(3000);
Console.WriteLine("Repository initialized");
}

public List<Person> GetPersons()
{
return new List<Person>
new Person
{
Name = "Chip",
Age = 31
},
new Person
{
new Person
{
Name = "Chip",
Age = 31
},
new Person
{
Name = "Dale",
Age = 32
}
};
}
Name = "Dale",
Age = 32
}
};
}
}
23 changes: 11 additions & 12 deletions src/Example/PersonsController.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
using Microsoft.AspNetCore.Mvc;

namespace Example
{
[Route("api/[controller]")]
public class PersonsController : ControllerBase
{
private readonly IEntryPoint _entry;
namespace Example;

public PersonsController(IEntryPoint entry)
{
_entry = entry;
}
[Route("api/[controller]")]
public class PersonsController : ControllerBase
{
private readonly IEntryPoint _entry;

[HttpGet]
public object Get() => new { Count = _entry.DoSomethingImportant() };
public PersonsController(IEntryPoint entry)
{
_entry = entry;
}

[HttpGet]
public object Get() => new { Count = _entry.DoSomethingImportant() };
}
16 changes: 6 additions & 10 deletions src/Example/Program.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
namespace Example;

namespace Example
public class Program
{
public class Program
{
public static void Main(string[] args) => CreateHostBuilder(args).Build().Run();
public static void Main(string[] args) => CreateHostBuilder(args).Build().Run();

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder => webBuilder.UseStartup<Startup>());
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder => webBuilder.UseStartup<Startup>());
}
Loading

0 comments on commit f506310

Please sign in to comment.