Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
**/.classpath
**/.dockerignore
**/.env
**/.git
**/.gitignore
**/.project
**/.settings
**/.toolstarget
**/.vs
**/.vscode
**/*.*proj.user
**/*.dbmdl
**/*.jfm
**/azds.yaml
**/bin
**/charts
**/docker-compose*
**/Dockerfile*
**/node_modules
**/npm-debug.log
**/obj
**/secrets.dev.yaml
**/values.dev.yaml
LICENSE
README.md
!**/.gitignore
!.git/HEAD
!.git/config
!.git/packed-refs
!.git/refs/heads/**
20 changes: 20 additions & 0 deletions src/ApiTest/ApiTest.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<UserSecretsId>9a4c2a0c-d9b0-47d9-8bf6-156509435f06</UserSecretsId>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.21.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Samhammer.DependencyInjection\Samhammer.DependencyInjection.csproj" />
</ItemGroup>

</Project>
6 changes: 6 additions & 0 deletions src/ApiTest/ApiTest.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@ApiTest_HostAddress = http://localhost:5025

GET {{ApiTest_HostAddress}}/weatherforecast/
Accept: application/json

###
37 changes: 37 additions & 0 deletions src/ApiTest/Controllers/WeatherForecastController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using ApiTest.Services;
using Microsoft.AspNetCore.Mvc;

namespace ApiTest.Controllers
{
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
private static readonly string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};

private readonly ILogger<WeatherForecastController> _logger;
private readonly IParentClassTest _parentTest;

public WeatherForecastController(ILogger<WeatherForecastController> logger, IParentClassTest parentClassTest)
{
_logger = logger;
_parentTest = parentClassTest;
}

[HttpGet(Name = "GetWeatherForecast")]
public IEnumerable<WeatherForecast> Get()
{
_parentTest.PrintSomething();
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
})
.ToArray();
}
}
}
30 changes: 30 additions & 0 deletions src/ApiTest/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# See https://aka.ms/customizecontainer to learn how to customize your debug container and how Visual Studio uses this Dockerfile to build your images for faster debugging.

# This stage is used when running from VS in fast mode (Default for Debug configuration)
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
USER app
WORKDIR /app
EXPOSE 8080
EXPOSE 8081


# This stage is used to build the service project
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
ARG BUILD_CONFIGURATION=Release
WORKDIR /src
COPY ["ApiTest/ApiTest.csproj", "ApiTest/"]
RUN dotnet restore "./ApiTest/ApiTest.csproj"
COPY . .
WORKDIR "/src/ApiTest"
RUN dotnet build "./ApiTest.csproj" -c $BUILD_CONFIGURATION -o /app/build

# This stage is used to publish the service project to be copied to the final stage
FROM build AS publish
ARG BUILD_CONFIGURATION=Release
RUN dotnet publish "./ApiTest.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false

# This stage is used in production or when running from VS in regular mode (Default when not using the Debug configuration)
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "ApiTest.dll"]
33 changes: 33 additions & 0 deletions src/ApiTest/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using ApiTest.Services;
using Samhammer.DependencyInjection;

var builder = WebApplication.CreateBuilder(args);
// Add services to the container.

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

builder.Host.ConfigureServices(services => services.ResolveDependencies());

Check failure on line 12 in src/ApiTest/Program.cs

View workflow job for this annotation

GitHub Actions / dotnet

[dotnet] src/ApiTest/Program.cs#L12

ASP0012: Suggest using builder.Services instead of ConfigureServices (https://aka.ms/aspnet/analyzers) [/home/runner/work/Samhammer.DependencyInjection/Samhammer.DependencyInjection/src/ApiTest/ApiTest.csproj]
Raw output
/home/runner/work/Samhammer.DependencyInjection/Samhammer.DependencyInjection/src/ApiTest/Program.cs(12,14): warning ASP0012: Suggest using builder.Services instead of ConfigureServices (https://aka.ms/aspnet/analyzers) [/home/runner/work/Samhammer.DependencyInjection/Samhammer.DependencyInjection/src/ApiTest/ApiTest.csproj]
builder.Host.UseDefaultServiceProvider(options =>
{
options.ValidateOnBuild = true;
});

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}

app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();

app.Run();
10 changes: 10 additions & 0 deletions src/ApiTest/Services/ChildClassTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Samhammer.DependencyInjection.Attributes;

namespace ApiTest.Services
{
public class ChildClassTest : IChildClassTest
{
}

public interface IChildClassTest { }
}
23 changes: 23 additions & 0 deletions src/ApiTest/Services/ParentClassTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Samhammer.DependencyInjection.Attributes;

namespace ApiTest.Services
{
[InjectAb]
public class ParentClassTest : IParentClassTest
{
public ParentClassTest(IChildClassTest childClassTest)
{
Console.WriteLine("Creating test class");
}

public void PrintSomething()
{
Console.WriteLine("Printing in test class");
}
}

public interface IParentClassTest
{
public void PrintSomething();
}
}
13 changes: 13 additions & 0 deletions src/ApiTest/WeatherForecast.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace ApiTest
{
public class WeatherForecast
{
public DateOnly Date { get; set; }

public int TemperatureC { get; set; }

public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);

public string? Summary { get; set; }
}
}
8 changes: 8 additions & 0 deletions src/ApiTest/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
10 changes: 10 additions & 0 deletions src/ApiTest/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"EnabledTest": true
}
18 changes: 18 additions & 0 deletions src/Samhammer.DependencyInjection.Test/DependencyResolverTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,24 @@ public DependencyResolverTest()
serviceCollection.AddLogging(builder => builder.ClearProviders());
}

[Fact]
private void GetClassContains_WithDefaultLifetime_Scoped()
{
// act
serviceCollection.ResolveDependencies();
serviceProvider = serviceCollection.BuildServiceProvider();

IClassContainsDefaultLifetime service;

using (var scope = serviceProvider.CreateScope())
{
service = scope.ServiceProvider.GetService<IClassContainsDefaultLifetime>();
}

// assert
service.Should().NotBeNull().And.BeOfType<ClassContainsDefaultLifetime>();
}

[Fact]
private void GetClass_WithDefaultLifetime_Scoped()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Samhammer.DependencyInjection.Attributes;

namespace Samhammer.DependencyInjection.Test.TestData.InjectedClass
{
[Inject]
public class ClassContainsDefaultLifetime : IClassContainsDefaultLifetime
{
public ClassContainsDefaultLifetime(IClassDefaultLifetime classDefault)
{
Console.WriteLine($"Creating service {classDefault.ToString()}");
}
}

public interface IClassContainsDefaultLifetime
{
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
using Samhammer.DependencyInjection.Attributes;
using System;
using Samhammer.DependencyInjection.Attributes;

namespace Samhammer.DependencyInjection.Test.TestData.InjectedClass
{
[Inject]
public class ClassDefaultLifetime : IClassDefaultLifetime
{
public ClassDefaultLifetime()
{
Console.WriteLine("Creating service");
}
}

public interface IClassDefaultLifetime
Expand Down
10 changes: 8 additions & 2 deletions src/Samhammer.DependencyInjection.sln
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
..\README.md = ..\README.md
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Samhammer.DependencyInjection.Override", "Samhammer.DependencyInjection.Override\Samhammer.DependencyInjection.Override.csproj", "{3229D5A2-838F-433A-BC7D-32D76A08CCD2}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Samhammer.DependencyInjection.Override", "Samhammer.DependencyInjection.Override\Samhammer.DependencyInjection.Override.csproj", "{3229D5A2-838F-433A-BC7D-32D76A08CCD2}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Samhammer.DependencyInjection.Override.Test", "Samhammer.DependencyInjection.Override.Test\Samhammer.DependencyInjection.Override.Test.csproj", "{E5197D26-077D-46AD-92BE-32C3DB192C44}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Samhammer.DependencyInjection.Override.Test", "Samhammer.DependencyInjection.Override.Test\Samhammer.DependencyInjection.Override.Test.csproj", "{E5197D26-077D-46AD-92BE-32C3DB192C44}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ApiTest", "ApiTest\ApiTest.csproj", "{6A652F59-57F5-4CA3-B552-4A57EC9A42F9}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -38,6 +40,10 @@ Global
{E5197D26-077D-46AD-92BE-32C3DB192C44}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E5197D26-077D-46AD-92BE-32C3DB192C44}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E5197D26-077D-46AD-92BE-32C3DB192C44}.Release|Any CPU.Build.0 = Release|Any CPU
{6A652F59-57F5-4CA3-B552-4A57EC9A42F9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6A652F59-57F5-4CA3-B552-4A57EC9A42F9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6A652F59-57F5-4CA3-B552-4A57EC9A42F9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6A652F59-57F5-4CA3-B552-4A57EC9A42F9}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
22 changes: 22 additions & 0 deletions src/Samhammer.DependencyInjection/Attributes/InjectAbAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Samhammer.DependencyInjection.Attributes;

namespace Samhammer.DependencyInjection.Attributes
{
[AttributeUsage(AttributeTargets.Class)]
public class InjectAbAttribute : DependencyInjectionAttribute
{
public Target Target { get; }

public InjectAbAttribute(Target target = Target.Matching, ServiceLifetime lifetime = ServiceLifetime.Scoped)
: base(lifetime)
{
Target = target;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Samhammer.DependencyInjection.Attributes;

namespace Samhammer.DependencyInjection.Attributes
{
[AttributeUsage(AttributeTargets.Class)]
public class InjectAbWithConfigsAttribute : DependencyInjectionAttribute

Check failure on line 12 in src/Samhammer.DependencyInjection/Attributes/InjectAbWithFlagAttribute.cs

View workflow job for this annotation

GitHub Actions / dotnet

[dotnet] src/Samhammer.DependencyInjection/Attributes/InjectAbWithFlagAttribute.cs#L12

SA1649: File name should match first type name (https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1649.md) [/home/runner/work/Samhammer.DependencyInjection/Samhammer.DependencyInjection/src/Samhammer.DependencyInjection/Samhammer.DependencyInjection.csproj]
Raw output
/home/runner/work/Samhammer.DependencyInjection/Samhammer.DependencyInjection/src/Samhammer.DependencyInjection/Attributes/InjectAbWithFlagAttribute.cs(12,18): warning SA1649: File name should match first type name (https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1649.md) [/home/runner/work/Samhammer.DependencyInjection/Samhammer.DependencyInjection/src/Samhammer.DependencyInjection/Samhammer.DependencyInjection.csproj]
{
public Target Target { get; }

public string ConfigSections { get; set; }

public InjectAbWithConfigsAttribute(string configSections, Target target = Target.Matching, ServiceLifetime lifetime = ServiceLifetime.Scoped)
: base(lifetime)
{
Target = target;
ConfigSections = configSections;
}
}
}
Loading