Add ServiceManager support for parent manager (#16)

Prepare integration project
This commit is contained in:
2022-09-09 11:53:35 +02:00
committed by GitHub
parent a9ca960106
commit 820b1c9c2e
51 changed files with 2445 additions and 2231 deletions
+166
View File
@@ -0,0 +1,166 @@
# To learn more about .editorconfig see https://aka.ms/editorconfigdocs
###############################
# Core EditorConfig Options #
###############################
# All files
[*]
indent_style = space
# XML project files
[*.{csproj,vbproj,vcxproj,vcxproj.filters,proj,projitems,shproj}]
indent_size = 2
# XML config files
[*.{props,targets,ruleset,config,nuspec,resx,vsixmanifest,vsct}]
indent_size = 2
# Code files
[*.{cs,csx,vb,vbx}]
indent_size = 4
insert_final_newline = true
charset = utf-8-bom
###############################
# .NET Coding Conventions #
###############################
[*.{cs,vb}]
# Organize usings
dotnet_sort_system_directives_first = true
# this. preferences
dotnet_style_qualification_for_field = true:error
dotnet_style_qualification_for_property = true:error
dotnet_style_qualification_for_method = true:error
dotnet_style_qualification_for_event = true:error
# Language keywords vs BCL types preferences
dotnet_style_predefined_type_for_locals_parameters_members = true:silent
dotnet_style_predefined_type_for_member_access = true:silent
# Parentheses preferences
dotnet_style_parentheses_in_arithmetic_binary_operators = always_for_clarity:silent
dotnet_style_parentheses_in_relational_binary_operators = always_for_clarity:silent
dotnet_style_parentheses_in_other_binary_operators = always_for_clarity:silent
dotnet_style_parentheses_in_other_operators = never_if_unnecessary:silent
# Modifier preferences
dotnet_style_require_accessibility_modifiers = for_non_interface_members:silent
dotnet_style_readonly_field = true:suggestion
# Expression-level preferences
dotnet_style_object_initializer = true:suggestion
dotnet_style_collection_initializer = true:suggestion
dotnet_style_explicit_tuple_names = true:suggestion
dotnet_style_null_propagation = true:suggestion
dotnet_style_coalesce_expression = true:suggestion
dotnet_style_prefer_is_null_check_over_reference_equality_method = true:silent
dotnet_style_prefer_inferred_tuple_names = true:suggestion
dotnet_style_prefer_inferred_anonymous_type_member_names = true:suggestion
dotnet_style_prefer_auto_properties = true:silent
dotnet_style_prefer_conditional_expression_over_assignment = true:silent
dotnet_style_prefer_conditional_expression_over_return = true:silent
###############################
# Naming Conventions #
###############################
# Style Definitions
dotnet_naming_style.pascal_case_style.capitalization = pascal_case
# Use PascalCase for constant fields
dotnet_naming_rule.constant_fields_should_be_pascal_case.severity = suggestion
dotnet_naming_rule.constant_fields_should_be_pascal_case.symbols = constant_fields
dotnet_naming_rule.constant_fields_should_be_pascal_case.style = pascal_case_style
dotnet_naming_symbols.constant_fields.applicable_kinds = field
dotnet_naming_symbols.constant_fields.applicable_accessibilities = *
dotnet_naming_symbols.constant_fields.required_modifiers = const
dotnet_style_prefer_simplified_boolean_expressions = true:suggestion
dotnet_style_operator_placement_when_wrapping = beginning_of_line
tab_width = 4
end_of_line = crlf
dotnet_style_prefer_compound_assignment = true:suggestion
dotnet_style_prefer_simplified_interpolation = true:suggestion
dotnet_style_namespace_match_folder = true:suggestion
dotnet_style_allow_multiple_blank_lines_experimental = true:silent
dotnet_style_allow_statement_immediately_after_block_experimental = false:suggestion
dotnet_code_quality_unused_parameters = all:error
###############################
# C# Coding Conventions #
###############################
[*.cs]
# var preferences
csharp_style_var_for_built_in_types = true:silent
csharp_style_var_when_type_is_apparent = true:silent
csharp_style_var_elsewhere = true:silent
# Expression-bodied members
csharp_style_expression_bodied_methods = false:silent
csharp_style_expression_bodied_constructors = false:silent
csharp_style_expression_bodied_operators = false:silent
csharp_style_expression_bodied_properties = true:silent
csharp_style_expression_bodied_indexers = true:silent
csharp_style_expression_bodied_accessors = true:silent
# Pattern matching preferences
csharp_style_pattern_matching_over_is_with_cast_check = true:suggestion
csharp_style_pattern_matching_over_as_with_null_check = true:suggestion
# Null-checking preferences
csharp_style_throw_expression = true:suggestion
csharp_style_conditional_delegate_call = true:suggestion
# Modifier preferences
csharp_preferred_modifier_order = public,private,protected,internal,static,extern,new,virtual,abstract,sealed,override,readonly,unsafe,volatile,async:suggestion
# Expression-level preferences
csharp_prefer_braces = true:silent
csharp_style_deconstructed_variable_declaration = true:suggestion
csharp_prefer_simple_default_expression = true:suggestion
csharp_style_pattern_local_over_anonymous_function = true:suggestion
csharp_style_inlined_variable_declaration = true:suggestion
###############################
# C# Formatting Rules #
###############################
# New line preferences
csharp_new_line_before_open_brace = all
csharp_new_line_before_else = true
csharp_new_line_before_catch = true
csharp_new_line_before_finally = true
csharp_new_line_before_members_in_object_initializers = true
csharp_new_line_before_members_in_anonymous_types = true
csharp_new_line_between_query_expression_clauses = true
# Indentation preferences
csharp_indent_case_contents = true
csharp_indent_switch_labels = true
csharp_indent_labels = flush_left
# Space preferences
csharp_space_after_cast = false
csharp_space_after_keywords_in_control_flow_statements = true
csharp_space_between_method_call_parameter_list_parentheses = false
csharp_space_between_method_declaration_parameter_list_parentheses = false
csharp_space_between_parentheses = false
csharp_space_before_colon_in_inheritance_clause = true
csharp_space_after_colon_in_inheritance_clause = true
csharp_space_around_binary_operators = before_and_after
csharp_space_between_method_declaration_empty_parameter_list_parentheses = false
csharp_space_between_method_call_name_and_opening_parenthesis = false
csharp_space_between_method_call_empty_parameter_list_parentheses = false
# Wrapping preferences
csharp_preserve_single_line_statements = true
csharp_preserve_single_line_blocks = true
csharp_using_directive_placement = outside_namespace:error
csharp_prefer_simple_using_statement = true:error
csharp_style_namespace_declarations = file_scoped:error
csharp_style_prefer_method_group_conversion = true:silent
csharp_style_expression_bodied_lambdas = true:silent
csharp_style_expression_bodied_local_functions = false:silent
csharp_indent_braces = false
csharp_style_prefer_null_check_over_type_check = true:suggestion
csharp_style_prefer_local_over_anonymous_function = true:error
csharp_style_prefer_index_operator = true:suggestion
csharp_style_prefer_range_operator = true:suggestion
csharp_style_implicit_object_creation_when_type_is_apparent = true:suggestion
csharp_style_prefer_tuple_swap = true:suggestion
csharp_style_unused_value_assignment_preference = discard_variable:error
csharp_style_unused_value_expression_statement_preference = discard_variable:silent
csharp_prefer_static_local_function = true:suggestion
csharp_style_allow_embedded_statements_on_same_line_experimental = true:silent
csharp_style_allow_blank_lines_between_consecutive_braces_experimental = false:suggestion
csharp_style_allow_blank_line_after_colon_in_constructor_initializer_experimental = true:silent
csharp_style_prefer_parameter_null_checking = true:suggestion
csharp_style_prefer_switch_expression = true:suggestion
csharp_style_prefer_pattern_matching = true:silent
csharp_style_prefer_not_pattern = true:suggestion
csharp_style_prefer_extended_property_pattern = true:suggestion
###############################
# VB Coding Conventions #
###############################
[*.vb]
# Modifier preferences
visual_basic_preferred_modifier_order = Partial,Default,Private,Protected,Public,Friend,NotOverridable,Overridable,MustOverride,Overloads,Overrides,MustInherit,NotInheritable,Static,Shared,Shadows,ReadOnly,WriteOnly,Dim,Const,WithEvents,Widening,Narrowing,Custom,Async:suggestion
+25
View File
@@ -0,0 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<LangVersion>latest</LangVersion>
<Authors>Alexandru Macocian</Authors>
<Company />
<Product></Product>
<Description>Integration with Microsoft.DependencyInjection library.</Description>
<AssemblyVersion>1.0.0</AssemblyVersion>
<FileVersion>1.0.0</FileVersion>
<PackageProjectUrl>https://github.com/AlexMacocian/Slim</PackageProjectUrl>
<Version>1.0.0</Version>
<EnableNETAnalyzers>true</EnableNETAnalyzers>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="6.0.0" />
<PackageReference Include="Slim" Version="1.8.1" />
</ItemGroup>
</Project>
+30 -31
View File
@@ -4,42 +4,41 @@ using Slim.Tests.Models;
using System;
using System.Threading.Tasks;
namespace Slim.Tests
namespace Slim.Tests;
[TestClass]
public sealed class MemoryLeakTests
{
[TestClass]
public sealed class MemoryLeakTests
private const int ScopesCount = 10000;
[TestMethod]
public async Task CreateAndReleaseScopesDisposeManagerGCCollects()
{
private const int ScopesCount = 10000;
var di = new ServiceManager();
di.RegisterSingleton<IIndependentService, IndependentService>();
di.RegisterScoped<IDependentService, DependentService>();
var independentService = di.GetService<IIndependentService>();
var dependentService = di.GetService<IDependentService>();
[TestMethod]
public async Task CreateAndReleaseScopesDisposeManagerGCCollects()
var weakRefs = new WeakReference[ScopesCount];
for (int i = 0; i < ScopesCount; i++)
{
var di = new ServiceManager();
di.RegisterSingleton<IIndependentService, IndependentService>();
di.RegisterScoped<IDependentService, DependentService>();
var independentService = di.GetService<IIndependentService>();
var dependentService = di.GetService<IDependentService>();
var scopedDi = di.CreateScope();
weakRefs[i] = new WeakReference(scopedDi);
var scopedDependentService = scopedDi.GetService<IDependentService>();
scopedDependentService.Should().NotBeNull();
scopedDependentService.Should().NotBe(dependentService);
scopedDependentService.IndependentService.Should().NotBeNull();
scopedDependentService.IndependentService.Should().Be(independentService);
}
var weakRefs = new WeakReference[ScopesCount];
for (int i = 0; i < ScopesCount; i++)
{
var scopedDi = di.CreateScope();
weakRefs[i] = new WeakReference(scopedDi);
var scopedDependentService = scopedDi.GetService<IDependentService>();
scopedDependentService.Should().NotBeNull();
scopedDependentService.Should().NotBe(dependentService);
scopedDependentService.IndependentService.Should().NotBeNull();
scopedDependentService.IndependentService.Should().Be(independentService);
}
await Task.Delay(1500);
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
foreach(var weakRef in weakRefs)
{
weakRef.IsAlive.Should().BeFalse();
}
await Task.Delay(1500);
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
foreach (var weakRef in weakRefs)
{
weakRef.IsAlive.Should().BeFalse();
}
}
}
@@ -1,13 +1,12 @@
namespace Slim.Tests.Models
{
public sealed class DependentOnPrivateCtrService : IDependentOnPrivateCtrService
{
private readonly IPrivateConstructorService privateConstructorService;
namespace Slim.Tests.Models;
public DependentOnPrivateCtrService(
IPrivateConstructorService privateConstructorService)
{
this.privateConstructorService = privateConstructorService;
}
public sealed class DependentOnPrivateCtrService : IDependentOnPrivateCtrService
{
private readonly IPrivateConstructorService privateConstructorService;
public DependentOnPrivateCtrService(
IPrivateConstructorService privateConstructorService)
{
this.privateConstructorService = privateConstructorService;
}
}
@@ -1,19 +1,18 @@
namespace Slim.Tests.Models
{
public class DependentOnServiceManagerService : IDependentOnServiceManagerService
{
public IServiceManager ServiceManager { get; }
public IServiceProducer ServiceProducer { get; }
public IServiceProvider ServiceProvider { get; }
namespace Slim.Tests.Models;
public DependentOnServiceManagerService(
IServiceManager serviceManager,
IServiceProvider serviceProvider,
IServiceProducer serviceProducer)
{
this.ServiceManager = serviceManager;
this.ServiceProvider = serviceProvider;
this.ServiceProducer = serviceProducer;
}
public class DependentOnServiceManagerService : IDependentOnServiceManagerService
{
public IServiceManager ServiceManager { get; }
public IServiceProducer ServiceProducer { get; }
public IServiceProvider ServiceProvider { get; }
public DependentOnServiceManagerService(
IServiceManager serviceManager,
IServiceProvider serviceProvider,
IServiceProducer serviceProducer)
{
this.ServiceManager = serviceManager;
this.ServiceProvider = serviceProvider;
this.ServiceProducer = serviceProducer;
}
}
@@ -1,12 +1,11 @@
namespace Slim.Tests.Models
{
public sealed class DependentOnThrowingService : IDependentOnThrowingService
{
private readonly IThrowingService throwingService;
namespace Slim.Tests.Models;
public DependentOnThrowingService(IThrowingService throwingService)
{
this.throwingService = throwingService;
}
public sealed class DependentOnThrowingService : IDependentOnThrowingService
{
private readonly IThrowingService throwingService;
public DependentOnThrowingService(IThrowingService throwingService)
{
this.throwingService = throwingService;
}
}
+8 -9
View File
@@ -1,12 +1,11 @@
namespace Slim.Tests.Models
{
public class DependentService : IDependentService
{
public DependentService(IIndependentService independentService)
{
this.IndependentService = independentService;
}
namespace Slim.Tests.Models;
public IIndependentService IndependentService { get; }
public class DependentService : IDependentService
{
public DependentService(IIndependentService independentService)
{
this.IndependentService = independentService;
}
public IIndependentService IndependentService { get; }
}
+8 -9
View File
@@ -1,12 +1,11 @@
namespace Slim.Tests.Models
{
public sealed class DependentService2 : IDependentService2
{
public IndependentService IndependentService { get; }
namespace Slim.Tests.Models;
public DependentService2(IndependentService independentService)
{
this.IndependentService = independentService;
}
public sealed class DependentService2 : IDependentService2
{
public IndependentService IndependentService { get; }
public DependentService2(IndependentService independentService)
{
this.IndependentService = independentService;
}
}
@@ -1,12 +1,11 @@
using Slim.Attributes;
namespace Slim.Tests.Models
namespace Slim.Tests.Models;
public sealed class DoNotInjectConstructorService
{
public sealed class DoNotInjectConstructorService
[DoNotInject]
public DoNotInjectConstructorService()
{
[DoNotInject]
public DoNotInjectConstructorService()
{
}
}
}
@@ -1,6 +1,5 @@
namespace Slim.Tests.Models
namespace Slim.Tests.Models;
public interface IDependentOnPrivateCtrService
{
public interface IDependentOnPrivateCtrService
{
}
}
@@ -1,9 +1,8 @@
namespace Slim.Tests.Models
namespace Slim.Tests.Models;
public interface IDependentOnServiceManagerService
{
public interface IDependentOnServiceManagerService
{
IServiceManager ServiceManager { get; }
IServiceProducer ServiceProducer { get; }
IServiceProvider ServiceProvider { get; }
}
IServiceManager ServiceManager { get; }
IServiceProducer ServiceProducer { get; }
IServiceProvider ServiceProvider { get; }
}
@@ -1,6 +1,5 @@
namespace Slim.Tests.Models
namespace Slim.Tests.Models;
public interface IDependentOnThrowingService
{
public interface IDependentOnThrowingService
{
}
}
+4 -5
View File
@@ -1,7 +1,6 @@
namespace Slim.Tests.Models
namespace Slim.Tests.Models;
public interface IDependentService
{
public interface IDependentService
{
IIndependentService IndependentService { get; }
}
IIndependentService IndependentService { get; }
}
+4 -5
View File
@@ -1,7 +1,6 @@
namespace Slim.Tests.Models
namespace Slim.Tests.Models;
public interface IDependentService2
{
public interface IDependentService2
{
public IndependentService IndependentService { get; }
}
public IndependentService IndependentService { get; }
}
+8 -9
View File
@@ -1,12 +1,11 @@
namespace Slim.Tests.Models
{
public class IDisposableService : IIDisposableService
{
public bool DisposeCalled { get; set; }
namespace Slim.Tests.Models;
public void Dispose()
{
this.DisposeCalled = true;
}
public class IDisposableService : IIDisposableService
{
public bool DisposeCalled { get; set; }
public void Dispose()
{
this.DisposeCalled = true;
}
}
@@ -1,12 +1,11 @@
namespace Slim.Tests.Models
{
public sealed class IDisposableSingletonService : IIDisposableService
{
public bool DisposeCalled { get; set; }
namespace Slim.Tests.Models;
public void Dispose()
{
this.DisposeCalled = true;
}
public sealed class IDisposableSingletonService : IIDisposableService
{
public bool DisposeCalled { get; set; }
public void Dispose()
{
this.DisposeCalled = true;
}
}
+4 -5
View File
@@ -1,9 +1,8 @@
using System;
namespace Slim.Tests.Models
namespace Slim.Tests.Models;
public interface IIDisposableService : IDisposable
{
public interface IIDisposableService : IDisposable
{
bool DisposeCalled { get; set; }
}
bool DisposeCalled { get; set; }
}
+3 -4
View File
@@ -1,6 +1,5 @@
namespace Slim.Tests.Models
namespace Slim.Tests.Models;
public interface IIndependentService
{
public interface IIndependentService
{
}
}
+3 -4
View File
@@ -1,6 +1,5 @@
namespace Slim.Tests.Models
namespace Slim.Tests.Models;
public interface IMultiInterfaceService1
{
public interface IMultiInterfaceService1
{
}
}
+3 -4
View File
@@ -1,6 +1,5 @@
namespace Slim.Tests.Models
namespace Slim.Tests.Models;
public interface IMultiInterfaceService2
{
public interface IMultiInterfaceService2
{
}
}
@@ -1,6 +1,5 @@
namespace Slim.Tests.Models
namespace Slim.Tests.Models;
public interface IPrivateConstructorService
{
public interface IPrivateConstructorService
{
}
}
+3 -4
View File
@@ -1,6 +1,5 @@
namespace Slim.Tests.Models
namespace Slim.Tests.Models;
public interface ISharedInterface
{
public interface ISharedInterface
{
}
}
+3 -4
View File
@@ -1,6 +1,5 @@
namespace Slim.Tests.Models
namespace Slim.Tests.Models;
public interface ISharedService1 : ISharedInterface
{
public interface ISharedService1 : ISharedInterface
{
}
}
+3 -4
View File
@@ -1,6 +1,5 @@
namespace Slim.Tests.Models
namespace Slim.Tests.Models;
public interface ISharedService2 : ISharedInterface
{
public interface ISharedService2 : ISharedInterface
{
}
}
+3 -4
View File
@@ -1,6 +1,5 @@
namespace Slim.Tests.Models
namespace Slim.Tests.Models;
public interface ISharedService3 : ISharedInterface
{
public interface ISharedService3 : ISharedInterface
{
}
}
+3 -4
View File
@@ -1,6 +1,5 @@
namespace Slim.Tests.Models
namespace Slim.Tests.Models;
public interface IThrowingService
{
public interface IThrowingService
{
}
}
+3 -4
View File
@@ -1,6 +1,5 @@
namespace Slim.Tests.Models
namespace Slim.Tests.Models;
public class IndependentService : IIndependentService
{
public class IndependentService : IIndependentService
{
}
}
+16 -10
View File
@@ -1,13 +1,19 @@
namespace Slim.Tests.Models
{
public class MultiConstructorService : IIndependentService
{
public MultiConstructorService(int x, int y, object z)
{
}
namespace Slim.Tests.Models;
public MultiConstructorService()
{
}
public class MultiConstructorService : IIndependentService
{
public int X { get; }
public int Y { get; }
public object Z { get; }
public MultiConstructorService(int x, int y, object z)
{
this.X = x;
this.Y = y;
this.Z = z;
}
public MultiConstructorService()
{
}
}
+3 -4
View File
@@ -1,6 +1,5 @@
namespace Slim.Tests.Models
namespace Slim.Tests.Models;
public class MultiInterfaceService : IMultiInterfaceService1, IMultiInterfaceService2
{
public class MultiInterfaceService : IMultiInterfaceService1, IMultiInterfaceService2
{
}
}
+3 -4
View File
@@ -1,6 +1,5 @@
namespace Slim.Tests.Models
namespace Slim.Tests.Models;
public class NoInterfaceService
{
public class NoInterfaceService
{
}
}
@@ -1,9 +1,8 @@
namespace Slim.Tests.Models
namespace Slim.Tests.Models;
public sealed class PrivateConstructorService : IPrivateConstructorService
{
public sealed class PrivateConstructorService : IPrivateConstructorService
private PrivateConstructorService()
{
private PrivateConstructorService()
{
}
}
}
@@ -1,33 +1,38 @@
using Slim.Attributes;
namespace Slim.Tests.Models
namespace Slim.Tests.Models;
public sealed class ServiceWithPrefferedConstructor
{
public sealed class ServiceWithPrefferedConstructor
public bool CalledPrefferedConstructor { get; init; }
public bool CalledPrefferedConstructor0 { get; init; }
public bool CalledPrefferedConstructor1 { get; init; }
public IIndependentService IndependentService { get; }
public IDependentService DependentService { get; }
public IDependentService2 DependentService2 { get; }
public ServiceWithPrefferedConstructor()
{
public bool CalledPrefferedConstructor { get; init; }
public bool CalledPrefferedConstructor0 { get; init; }
public bool CalledPrefferedConstructor1 { get; init; }
}
public ServiceWithPrefferedConstructor()
{
}
[PreferredConstructor]
public ServiceWithPrefferedConstructor(IIndependentService independentService)
{
this.CalledPrefferedConstructor = true;
this.IndependentService = independentService;
}
[PreferredConstructor]
public ServiceWithPrefferedConstructor(IIndependentService independentService)
{
this.CalledPrefferedConstructor = true;
}
[PreferredConstructor(Priority = 0)]
public ServiceWithPrefferedConstructor(IDependentService dependentService)
{
this.CalledPrefferedConstructor0 = true;
this.DependentService = dependentService;
}
[PreferredConstructor(Priority = 0)]
public ServiceWithPrefferedConstructor(IDependentService dependentService)
{
this.CalledPrefferedConstructor0 = true;
}
[PreferredConstructor(Priority = 1)]
public ServiceWithPrefferedConstructor(IDependentService2 dependentService2)
{
this.CalledPrefferedConstructor1 = true;
}
[PreferredConstructor(Priority = 1)]
public ServiceWithPrefferedConstructor(IDependentService2 dependentService2)
{
this.CalledPrefferedConstructor1 = true;
this.DependentService2 = dependentService2;
}
}
+3 -4
View File
@@ -1,6 +1,5 @@
namespace Slim.Tests.Models
namespace Slim.Tests.Models;
public sealed class SharedService1 : ISharedService1
{
public sealed class SharedService1 : ISharedService1
{
}
}
+3 -4
View File
@@ -1,6 +1,5 @@
namespace Slim.Tests.Models
namespace Slim.Tests.Models;
public sealed class SharedService2 : ISharedService2
{
public sealed class SharedService2 : ISharedService2
{
}
}
+3 -4
View File
@@ -1,6 +1,5 @@
namespace Slim.Tests.Models
namespace Slim.Tests.Models;
public sealed class SharedService3 : ISharedService3
{
public sealed class SharedService3 : ISharedService3
{
}
}
+5 -6
View File
@@ -1,12 +1,11 @@
using System;
namespace Slim.Tests.Models
namespace Slim.Tests.Models;
public sealed class ThrowingService : IThrowingService
{
public sealed class ThrowingService : IThrowingService
public ThrowingService()
{
public ThrowingService()
{
throw new InvalidOperationException();
}
throw new InvalidOperationException();
}
}
@@ -2,24 +2,23 @@
using Slim.Tests.Models;
using System;
namespace Slim.Tests.Resolvers
namespace Slim.Tests.Resolvers;
public sealed class DependentServiceResolver : IDependencyResolver
{
public sealed class DependentServiceResolver : IDependencyResolver
public bool CanResolve(Type type)
{
public bool CanResolve(Type type)
if (type == typeof(IDependentService))
{
if (type == typeof(IDependentService))
{
return true;
}
return false;
return true;
}
public object Resolve(IServiceProvider serviceProvider, Type type)
{
var independentService = serviceProvider.GetService<IIndependentService>();
return new DependentService(independentService);
}
return false;
}
public object Resolve(IServiceProvider serviceProvider, Type type)
{
var independentService = serviceProvider.GetService<IIndependentService>();
return new DependentService(independentService);
}
}
@@ -2,26 +2,25 @@
using Slim.Tests.Models;
using System;
namespace Slim.Tests.Resolvers
namespace Slim.Tests.Resolvers;
public class IndependentServiceResolver : IDependencyResolver
{
public class IndependentServiceResolver : IDependencyResolver
public int Called { get; private set; }
public bool CanResolve(Type type)
{
public int Called { get; private set; }
public bool CanResolve(Type type)
if (type == typeof(IIndependentService))
{
if (type == typeof(IIndependentService))
{
return true;
}
return false;
return true;
}
public object Resolve(IServiceProvider serviceProvider, Type type)
{
this.Called++;
return new IndependentService();
}
return false;
}
public object Resolve(IServiceProvider serviceProvider, Type type)
{
this.Called++;
return new IndependentService();
}
}
File diff suppressed because it is too large Load Diff
+11
View File
@@ -16,6 +16,13 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Pipelines", "Pipelines", "{
.github\workflows\ci.yaml = .github\workflows\ci.yaml
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Slim.Integration", "Slim.Integration\Slim.Integration.csproj", "{13975492-5299-4589-91C1-C33E4FCEDD72}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{D20E2AFD-6BE0-4CCC-AD87-D2961CD1763B}"
ProjectSection(SolutionItems) = preProject
.editorconfig = .editorconfig
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -30,6 +37,10 @@ Global
{F03F7353-0BD5-4691-A318-BC6FC93AB00E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F03F7353-0BD5-4691-A318-BC6FC93AB00E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F03F7353-0BD5-4691-A318-BC6FC93AB00E}.Release|Any CPU.Build.0 = Release|Any CPU
{13975492-5299-4589-91C1-C33E4FCEDD72}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{13975492-5299-4589-91C1-C33E4FCEDD72}.Debug|Any CPU.Build.0 = Debug|Any CPU
{13975492-5299-4589-91C1-C33E4FCEDD72}.Release|Any CPU.ActiveCfg = Release|Any CPU
{13975492-5299-4589-91C1-C33E4FCEDD72}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
+7 -8
View File
@@ -1,12 +1,11 @@
using System;
namespace Slim.Attributes
namespace Slim.Attributes;
/// <summary>
/// Attribute used to mark constructors to not be used by the <see cref="ServiceManager"/>.
/// </summary>
[AttributeUsage(AttributeTargets.Constructor)]
public sealed class DoNotInjectAttribute : Attribute
{
/// <summary>
/// Attribute used to mark constructors to not be used by the <see cref="ServiceManager"/>.
/// </summary>
[AttributeUsage(AttributeTargets.Constructor)]
public sealed class DoNotInjectAttribute : Attribute
{
}
}
@@ -1,23 +1,22 @@
using System;
namespace Slim.Attributes
namespace Slim.Attributes;
/// <summary>
/// Attribute used to mark constructors to be used with priority by the <see cref="ServiceManager"/>.
/// </summary>
[AttributeUsage(AttributeTargets.Constructor)]
public sealed class PreferredConstructorAttribute : Attribute
{
/// <summary>
/// Attribute used to mark constructors to be used with priority by the <see cref="ServiceManager"/>.
/// Priority of the constructor. Constructors are ordered using this value when present.
/// </summary>
[AttributeUsage(AttributeTargets.Constructor)]
public sealed class PreferredConstructorAttribute : Attribute
{
/// <summary>
/// Priority of the constructor. Constructors are ordered using this value when present.
/// </summary>
public int Priority { get; set; } = int.MaxValue - 1;
public int Priority { get; set; } = int.MaxValue - 1;
/// <summary>
/// Creates a new instance of <see cref="PreferredConstructorAttribute"/>.
/// </summary>
public PreferredConstructorAttribute()
{
}
/// <summary>
/// Creates a new instance of <see cref="PreferredConstructorAttribute"/>.
/// </summary>
public PreferredConstructorAttribute()
{
}
}
+31 -32
View File
@@ -1,44 +1,43 @@
using System;
using System.Runtime.Serialization;
namespace Slim.Exceptions
namespace Slim.Exceptions;
/// <summary>
/// Exception thrown when <see cref="IServiceManager"/> fails to create a service.
/// </summary>
public class DependencyInjectionException : Exception
{
/// <summary>
/// Exception thrown when <see cref="IServiceManager"/> fails to create a service.
/// Creates a new instance of <see cref="DependencyInjectionException"/>.
/// </summary>
public class DependencyInjectionException : Exception
public DependencyInjectionException()
{
/// <summary>
/// Creates a new instance of <see cref="DependencyInjectionException"/>.
/// </summary>
public DependencyInjectionException()
{
}
}
/// <summary>
/// Creates a new instance of <see cref="DependencyInjectionException"/>.
/// </summary>
/// <param name="message">Message of the exception.</param>
public DependencyInjectionException(string message) : base(message)
{
}
/// <summary>
/// Creates a new instance of <see cref="DependencyInjectionException"/>.
/// </summary>
/// <param name="message">Message of the exception.</param>
public DependencyInjectionException(string message) : base(message)
{
}
/// <summary>
/// Creates a new instance of <see cref="DependencyInjectionException"/>.
/// </summary>
/// <param name="message">Message of the exception.</param>
/// <param name="innerException">Inner exception.</param>
public DependencyInjectionException(string message, Exception innerException) : base(message, innerException)
{
}
/// <summary>
/// Creates a new instance of <see cref="DependencyInjectionException"/>.
/// </summary>
/// <param name="message">Message of the exception.</param>
/// <param name="innerException">Inner exception.</param>
public DependencyInjectionException(string message, Exception innerException) : base(message, innerException)
{
}
/// <summary>
/// Creates a new instance of <see cref="DependencyInjectionException"/>.
/// </summary>
/// <param name="info">Serialization info.</param>
/// <param name="context">Streaming context.</param>
protected DependencyInjectionException(SerializationInfo info, StreamingContext context) : base(info, context)
{
}
/// <summary>
/// Creates a new instance of <see cref="DependencyInjectionException"/>.
/// </summary>
/// <param name="info">Serialization info.</param>
/// <param name="context">Streaming context.</param>
protected DependencyInjectionException(SerializationInfo info, StreamingContext context) : base(info, context)
{
}
}
+36 -33
View File
@@ -1,40 +1,43 @@
using System;
using System.Collections.Generic;
namespace Slim
namespace Slim;
/// <summary>
/// Interface for <see cref="ServiceManager"/>.
/// </summary>
public interface IServiceManager : IServiceProvider, IServiceProducer
{
/// <summary>
/// Interface for <see cref="ServiceManager"/>.
/// Gets the <see cref="IServiceManager"/> that created the current <see cref="IServiceManager"/>. Returns null in case there is no parent.
/// </summary>
public interface IServiceManager : IServiceProvider, IServiceProducer
{
/// <summary>
/// Allow modifications to scoped <see cref="IServiceManager"/> created from this <see cref="IServiceManager"/>.
/// </summary>
bool AllowScopedManagerModifications { get; set; }
/// <summary>
/// Returns true if <see cref="IServiceManager"/> is readonly.
/// </summary>
bool IsReadOnly { get; }
/// <summary>
/// Builds all registered singletons.
/// </summary>
void BuildSingletons();
/// <summary>
/// Marks a type of exception to be caught and handled.
/// </summary>
/// <typeparam name="T">Type of exception to catch.</typeparam>
/// <param name="handle">Handler of the exception. Handler returns true if the exception should be thrown again.</param>
/// <exception cref="ArgumentException"></exception>
/// <exception cref="ArgumentNullException"></exception>
void HandleException<T>(Func<IServiceProvider, T, bool> handle)
where T : Exception;
/// <summary>
/// Clears all registered types, singletons, factories and exception handlers.
/// </summary>
/// <remarks>
/// Calls <see cref="IDisposable.Dispose"/> on all <see cref="IDisposable"/> singletons.
/// </remarks>
void Clear();
}
IServiceManager ParentServiceManager { get; }
/// <summary>
/// Allow modifications to scoped <see cref="IServiceManager"/> created from this <see cref="IServiceManager"/>.
/// </summary>
bool AllowScopedManagerModifications { get; set; }
/// <summary>
/// Returns true if <see cref="IServiceManager"/> is readonly.
/// </summary>
bool IsReadOnly { get; }
/// <summary>
/// Builds all registered singletons.
/// </summary>
void BuildSingletons();
/// <summary>
/// Marks a type of exception to be caught and handled.
/// </summary>
/// <typeparam name="T">Type of exception to catch.</typeparam>
/// <param name="handle">Handler of the exception. Handler returns true if the exception should be thrown again.</param>
/// <exception cref="ArgumentException"></exception>
/// <exception cref="ArgumentNullException"></exception>
void HandleException<T>(Func<IServiceProvider, T, bool> handle)
where T : Exception;
/// <summary>
/// Clears all registered types, singletons, factories and exception handlers.
/// </summary>
/// <remarks>
/// Calls <see cref="IDisposable.Dispose"/> on all <see cref="IDisposable"/> singletons.
/// </remarks>
void Clear();
}
+248 -249
View File
@@ -1,256 +1,255 @@
using Slim.Resolvers;
using System;
namespace Slim
namespace Slim;
/// <summary>
/// Interface allowing to produce services for the <see cref="ServiceManager"/>.
/// </summary>
public interface IServiceProducer
{
/// <summary>
/// Interface allowing to produce services for the <see cref="ServiceManager"/>.
/// Register the current service manager as a valid dependency.
/// </summary>
public interface IServiceProducer
{
/// <summary>
/// Register the current service manager as a valid dependency.
/// </summary>
/// <remarks>Registers current service manager as <see cref="ServiceManager"/>, <see cref="IServiceManager"/>, <see cref="IServiceProducer"/>, <see cref="IServiceProvider"/>.</remarks>
void RegisterServiceManager();
/// <summary>
/// Returns true if there exists a registration for <paramref name="tInterface"/>.
/// </summary>
/// <param name="tInterface">Type of registered service.</param>
/// <returns>True if service <paramref name="tInterface"/> is registered. Otherwise returns false.</returns>
bool IsRegistered(Type tInterface);
/// <summary>
/// Returns true if there exists a registration for <typeparamref name="T"/>.
/// </summary>
/// <typeparam name="T">Type of registered service.</typeparam>
/// <returns>True if service <typeparamref name="T"/> is registered. Otherwise returns false.</returns>
bool IsRegistered<T>();
/// <summary>
/// Register a resolver that manually resolves dependencies.
/// </summary>
/// <param name="dependencyResolver">Resolver that manually creates a dependency.</param>
void RegisterResolver(IDependencyResolver dependencyResolver);
/// <summary>
/// Register a service into <see cref="ServiceManager"/> with <see cref="Lifetime.Singleton"/>.
/// Registers the service for all interfaces it implements.
/// </summary>
/// <param name="registerAllInterfaces">If true, <see cref="ServiceManager"/> will register all interfaces implemented by the provided class./></param>
/// <typeparam name="TClass">Type of implementation.</typeparam>
/// <exception cref="InvalidOperationException">Thrown when <see cref="ServiceManager"/> contains an entry for the provided interface type.</exception>
void RegisterSingleton<TClass>(bool registerAllInterfaces = false)
where TClass : class;
/// <summary>
/// Register a service into <see cref="ServiceManager"/> with <see cref="Lifetime.Singleton"/>.
/// Registers the service for all interfaces it implements.
/// </summary>
/// <typeparam name="TClass">Type of implementation.</typeparam>
/// <param name="registerAllInterfaces">If true, <see cref="ServiceManager"/> will register all interfaces implemented by the provided class./></param>
/// <param name="serviceFactory">Factory for the implementation.</param>
/// <exception cref="ArgumentNullException">Thrown when the provided serviceFactory is null.</exception>
/// <exception cref="InvalidOperationException">Thrown when <see cref="ServiceManager"/> contains an entry for the provided interface type.</exception>
void RegisterSingleton<TClass>(Func<IServiceProvider, TClass> serviceFactory, bool registerAllInterfaces = false)
where TClass : class;
/// <summary>
/// Register a service into <see cref="ServiceManager"/> with <see cref="Lifetime.Transient"/>.
/// Registers the service for all interfaces it implements.
/// </summary>
/// <param name="registerAllInterfaces">If true, <see cref="ServiceManager"/> will register all interfaces implemented by the provided class./></param>
/// <typeparam name="TClass">Type of implementation.</typeparam>
/// <exception cref="InvalidOperationException">Thrown when <see cref="ServiceManager"/> contains an entry for the provided interface type.</exception>
void RegisterTransient<TClass>(bool registerAllInterfaces = false)
where TClass : class;
/// <summary>
/// Register a service into <see cref="ServiceManager"/> with <see cref="Lifetime.Transient"/>.
/// Registers the service for all interfaces it implements.
/// </summary>
/// <param name="registerAllInterfaces">If true, <see cref="ServiceManager"/> will register all interfaces implemented by the provided class./></param>
/// <typeparam name="TClass">Type of implementation.</typeparam>
/// <param name="serviceFactory">Factory for the implementation.</param>
/// <exception cref="ArgumentNullException">Thrown when the provided serviceFactory is null.</exception>
/// <exception cref="InvalidOperationException">Thrown when <see cref="ServiceManager"/> contains an entry for the provided interface type.</exception>
void RegisterTransient<TClass>(Func<IServiceProvider, TClass> serviceFactory, bool registerAllInterfaces = false)
where TClass : class;
/// <summary>
/// Register a service into <see cref="ServiceManager"/> with <see cref="Lifetime.Scoped"/>.
/// Registers the service for all interfaces it implements.
/// </summary>
/// <param name="registerAllInterfaces">If true, <see cref="ServiceManager"/> will register all interfaces implemented by the provided class./></param>
/// <typeparam name="TClass">Type of implementation.</typeparam>
/// <exception cref="InvalidOperationException">Thrown when <see cref="ServiceManager"/> contains an entry for the provided interface type.</exception>
void RegisterScoped<TClass>(bool registerAllInterfaces = false)
where TClass : class;
/// <summary>
/// Register a service into <see cref="ServiceManager"/> with <see cref="Lifetime.Scoped"/>.
/// Registers the service for all interfaces it implements.
/// </summary>
/// <param name="registerAllInterfaces">If true, <see cref="ServiceManager"/> will register all interfaces implemented by the provided class./></param>
/// <typeparam name="TClass">Type of implementation.</typeparam>
/// <param name="serviceFactory">Factory for the implementation.</param>
/// <exception cref="ArgumentNullException">Thrown when the provided serviceFactory is null.</exception>
/// <exception cref="InvalidOperationException">Thrown when <see cref="ServiceManager"/> contains an entry for the provided interface type.</exception>
void RegisterScoped<TClass>(Func<IServiceProvider, TClass> serviceFactory, bool registerAllInterfaces = false)
where TClass : class;
/// <summary>
/// Register a service into <see cref="ServiceManager"/> with <see cref="Lifetime.Transient"/>.
/// </summary>
/// <typeparam name="TInterface">Type of interface.</typeparam>
/// <typeparam name="TClass">Type of implementation.</typeparam>
/// <exception cref="InvalidOperationException">Thrown when <see cref="ServiceManager"/> contains an entry for the provided interface type.</exception>
void RegisterTransient<TInterface, TClass>()
where TInterface : class
where TClass : TInterface;
/// <summary>
/// Register a service into <see cref="ServiceManager"/> with <see cref="Lifetime.Transient"/>.
/// </summary>
/// <typeparam name="TInterface">Type of interface.</typeparam>
/// <typeparam name="TClass">Type of implementation.</typeparam>
/// <param name="serviceFactory">Factory for the implementation.</param>
/// <exception cref="ArgumentNullException">Thrown when the provided serviceFactory is null.</exception>
/// <exception cref="InvalidOperationException">Thrown when <see cref="ServiceManager"/> contains an entry for the provided interface type.</exception>
void RegisterTransient<TInterface, TClass>(Func<IServiceProvider, TClass> serviceFactory)
where TInterface : class
where TClass : TInterface;
/// <summary>
/// Register a service into <see cref="ServiceManager"/> with <see cref="Lifetime.Singleton"/>.
/// </summary>
/// <typeparam name="TInterface">Type of interface.</typeparam>
/// <typeparam name="TClass">Type of implementation.</typeparam>
/// <exception cref="InvalidOperationException">Thrown when <see cref="ServiceManager"/> contains an entry for the provided interface type.</exception>
void RegisterSingleton<TInterface, TClass>()
where TInterface : class
where TClass : TInterface;
/// <summary>
/// Register a service into <see cref="ServiceManager"/> with <see cref="Lifetime.Singleton"/>.
/// </summary>
/// <typeparam name="TInterface">Type of interface.</typeparam>
/// <typeparam name="TClass">Type of implementation.</typeparam>
/// <param name="serviceFactory">Factory for implementation.</param>
/// <exception cref="ArgumentNullException">Thrown when the provided serviceFactory is null.</exception>
/// <exception cref="InvalidOperationException">Thrown when <see cref="ServiceManager"/> contains an entry for the provided interface type.</exception>
void RegisterSingleton<TInterface, TClass>(Func<IServiceProvider, TClass> serviceFactory)
where TInterface : class
where TClass : TInterface;
/// <summary>
/// Register a service into <see cref="ServiceManager"/> with <see cref="Lifetime.Scoped"/>.
/// </summary>
/// <typeparam name="TInterface">Type of interface.</typeparam>
/// <typeparam name="TClass">Type of implementation.</typeparam>
/// <exception cref="InvalidOperationException">Thrown when <see cref="ServiceManager"/> contains an entry for the provided interface type.</exception>
void RegisterScoped<TInterface, TClass>()
where TInterface : class
where TClass : TInterface;
/// <summary>
/// Register a service into <see cref="ServiceManager"/> with <see cref="Lifetime.Scoped"/>.
/// </summary>
/// <typeparam name="TInterface">Type of interface.</typeparam>
/// <typeparam name="TClass">Type of implementation.</typeparam>
/// <param name="serviceFactory">Factory for the implementation.</param>
/// <exception cref="ArgumentNullException">Thrown when the provided serviceFactory is null.</exception>
/// <exception cref="InvalidOperationException">Thrown when <see cref="ServiceManager"/> contains an entry for the provided interface type.</exception>
void RegisterScoped<TInterface, TClass>(Func<IServiceProvider, TClass> serviceFactory)
where TInterface : class
where TClass : TInterface;
/// <summary>
/// Register a service into <see cref="ServiceManager"/> with <see cref="Lifetime.Transient"/>.
/// </summary>
/// <param name="tInterface">Type of interface.</param>
/// <param name="tClass">Type of implementation.</param>
/// <exception cref="InvalidOperationException">Thrown when <see cref="ServiceManager"/> contains an entry for the provided interface type.</exception>
void RegisterTransient(Type tInterface, Type tClass);
/// <summary>
/// Register a service into <see cref="ServiceManager"/> with <see cref="Lifetime.Transient"/>.
/// </summary>
/// <param name="tInterface">Type of interface.</param>
/// <param name="tClass">Type of implementation.</param>
/// <param name="serviceFactory">Factory for implementation.</param>
/// <exception cref="ArgumentNullException">Thrown when the provided serviceFactory is null.</exception>
/// <exception cref="InvalidOperationException">Thrown when <see cref="ServiceManager"/> contains an entry for the provided interface type.</exception>
void RegisterTransient(Type tInterface, Type tClass, Func<IServiceProvider, object> serviceFactory);
/// <summary>
/// Register a service into <see cref="ServiceManager"/> with <see cref="Lifetime.Singleton"/>.
/// </summary>
/// <param name="tInterface">Type of interface.</param>
/// <param name="tClass">Type of implementation.</param>
/// <exception cref="InvalidOperationException">Thrown when <see cref="ServiceManager"/> contains an entry for the provided interface type.</exception>
void RegisterSingleton(Type tInterface, Type tClass);
/// <summary>
/// Register a service into <see cref="ServiceManager"/> with <see cref="Lifetime.Singleton"/>.
/// </summary>
/// <param name="tInterface">Type of interface.</param>
/// <param name="tClass">Type of implementation.</param>
/// <param name="serviceFactory">Factory for implementation.</param>
/// <exception cref="ArgumentNullException">Thrown when the provided serviceFactory is null.</exception>
/// <exception cref="InvalidOperationException">Thrown when <see cref="ServiceManager"/> contains an entry for the provided interface type.</exception>
void RegisterSingleton(Type tInterface, Type tClass, Func<IServiceProvider, object> serviceFactory);
/// <summary>
/// Register a service into <see cref="ServiceManager"/> with <see cref="Lifetime.Scoped"/>.
/// </summary>
/// <param name="tInterface">Type of interface.</param>
/// <param name="tClass">Type of implementation.</param>
/// <exception cref="InvalidOperationException">Thrown when <see cref="ServiceManager"/> contains an entry for the provided interface type.</exception>
void RegisterScoped(Type tInterface, Type tClass);
/// <summary>
/// Register a service into <see cref="ServiceManager"/> with <see cref="Lifetime.Scoped"/>.
/// </summary>
/// <param name="tInterface">Type of interface.</param>
/// <param name="tClass">Type of implementation.</param>
/// <param name="serviceFactory">Factory for implementation.</param>
/// <exception cref="ArgumentNullException">Thrown when the provided serviceFactory is null.</exception>
/// <exception cref="InvalidOperationException">Thrown when <see cref="ServiceManager"/> contains an entry for the provided interface type.</exception>
void RegisterScoped(Type tInterface, Type tClass, Func<IServiceProvider, object> serviceFactory);
/// <summary>
/// Register a service into <see cref="ServiceManager"/> with <see cref="Lifetime.Transient"/>.
/// Registers the service for all interfaces it implements.
/// </summary>
/// <param name="registerAllInterfaces">If true, <see cref="ServiceManager"/> will register all interfaces implemented by the provided class./></param>
/// <param name="tClass">Type of implementation.</param>
/// <exception cref="InvalidOperationException">Thrown when <see cref="ServiceManager"/> contains an entry for the provided interface type.</exception>
void RegisterTransient(Type tClass, bool registerAllInterfaces = false);
/// <summary>
/// Register a service into <see cref="ServiceManager"/> with <see cref="Lifetime.Transient"/>.
/// Registers the service for all interfaces it implements.
/// </summary>
/// <param name="registerAllInterfaces">If true, <see cref="ServiceManager"/> will register all interfaces implemented by the provided class./></param>
/// <param name="tClass">Type of implementation.</param>
/// <param name="serviceFactory">Factory for implementation.</param>
/// <exception cref="ArgumentNullException">Thrown when the provided serviceFactory is null.</exception>
/// <exception cref="InvalidOperationException">Thrown when <see cref="ServiceManager"/> contains an entry for the provided interface type.</exception>
void RegisterTransient(Type tClass, Func<IServiceProvider, object> serviceFactory, bool registerAllInterfaces = false);
/// <summary>
/// Register a service into <see cref="ServiceManager"/> with <see cref="Lifetime.Singleton"/>.
/// Registers the service for all interfaces it implements.
/// </summary>
/// <param name="registerAllInterfaces">If true, <see cref="ServiceManager"/> will register all interfaces implemented by the provided class./></param>
/// <param name="tClass">Type of implementation.</param>
/// <exception cref="InvalidOperationException">Thrown when <see cref="ServiceManager"/> contains an entry for the provided interface type.</exception>
void RegisterSingleton(Type tClass, bool registerAllInterfaces = false);
/// <summary>
/// Register a service into <see cref="ServiceManager"/> with <see cref="Lifetime.Singleton"/>.
/// Registers the service for all interfaces it implements.
/// </summary>
/// <param name="registerAllInterfaces">If true, <see cref="ServiceManager"/> will register all interfaces implemented by the provided class./></param>
/// <param name="tClass">Type of implementation.</param>
/// <param name="serviceFactory">Factory for implementation.</param>
/// <exception cref="ArgumentNullException">Thrown when the provided serviceFactory is null.</exception>
/// <exception cref="InvalidOperationException">Thrown when <see cref="ServiceManager"/> contains an entry for the provided interface type.</exception>
void RegisterSingleton(Type tClass, Func<IServiceProvider, object> serviceFactory, bool registerAllInterfaces = false);
/// <summary>
/// Register a service into <see cref="ServiceManager"/> with <see cref="Lifetime.Scoped"/>.
/// Registers the service for all interfaces it implements.
/// </summary>
/// <param name="registerAllInterfaces">If true, <see cref="ServiceManager"/> will register all interfaces implemented by the provided class./></param>
/// <param name="tClass">Type of implementation.</param>
/// <exception cref="InvalidOperationException">Thrown when <see cref="ServiceManager"/> contains an entry for the provided interface type.</exception>
void RegisterScoped(Type tClass, bool registerAllInterfaces = false);
/// <summary>
/// Register a service into <see cref="ServiceManager"/> with <see cref="Lifetime.Scoped"/>.
/// Registers the service for all interfaces it implements.
/// </summary>
/// <param name="registerAllInterfaces">If true, <see cref="ServiceManager"/> will register all interfaces implemented by the provided class./></param>
/// <param name="tClass">Type of implementation.</param>
/// <param name="serviceFactory">Factory for implementation.</param>
/// <exception cref="ArgumentNullException">Thrown when the provided serviceFactory is null.</exception>
/// <exception cref="InvalidOperationException">Thrown when <see cref="ServiceManager"/> contains an entry for the provided interface type.</exception>
void RegisterScoped(Type tClass, Func<IServiceProvider, object> serviceFactory, bool registerAllInterfaces = false);
}
/// <remarks>Registers current service manager as <see cref="ServiceManager"/>, <see cref="IServiceManager"/>, <see cref="IServiceProducer"/>, <see cref="IServiceProvider"/>.</remarks>
void RegisterServiceManager();
/// <summary>
/// Returns true if there exists a registration for <paramref name="tInterface"/>.
/// </summary>
/// <param name="tInterface">Type of registered service.</param>
/// <returns>True if service <paramref name="tInterface"/> is registered. Otherwise returns false.</returns>
bool IsRegistered(Type tInterface);
/// <summary>
/// Returns true if there exists a registration for <typeparamref name="T"/>.
/// </summary>
/// <typeparam name="T">Type of registered service.</typeparam>
/// <returns>True if service <typeparamref name="T"/> is registered. Otherwise returns false.</returns>
bool IsRegistered<T>();
/// <summary>
/// Register a resolver that manually resolves dependencies.
/// </summary>
/// <param name="dependencyResolver">Resolver that manually creates a dependency.</param>
void RegisterResolver(IDependencyResolver dependencyResolver);
/// <summary>
/// Register a service into <see cref="ServiceManager"/> with <see cref="Lifetime.Singleton"/>.
/// Registers the service for all interfaces it implements.
/// </summary>
/// <param name="registerAllInterfaces">If true, <see cref="ServiceManager"/> will register all interfaces implemented by the provided class./></param>
/// <typeparam name="TClass">Type of implementation.</typeparam>
/// <exception cref="InvalidOperationException">Thrown when <see cref="ServiceManager"/> contains an entry for the provided interface type.</exception>
void RegisterSingleton<TClass>(bool registerAllInterfaces = false)
where TClass : class;
/// <summary>
/// Register a service into <see cref="ServiceManager"/> with <see cref="Lifetime.Singleton"/>.
/// Registers the service for all interfaces it implements.
/// </summary>
/// <typeparam name="TClass">Type of implementation.</typeparam>
/// <param name="registerAllInterfaces">If true, <see cref="ServiceManager"/> will register all interfaces implemented by the provided class./></param>
/// <param name="serviceFactory">Factory for the implementation.</param>
/// <exception cref="ArgumentNullException">Thrown when the provided serviceFactory is null.</exception>
/// <exception cref="InvalidOperationException">Thrown when <see cref="ServiceManager"/> contains an entry for the provided interface type.</exception>
void RegisterSingleton<TClass>(Func<IServiceProvider, TClass> serviceFactory, bool registerAllInterfaces = false)
where TClass : class;
/// <summary>
/// Register a service into <see cref="ServiceManager"/> with <see cref="Lifetime.Transient"/>.
/// Registers the service for all interfaces it implements.
/// </summary>
/// <param name="registerAllInterfaces">If true, <see cref="ServiceManager"/> will register all interfaces implemented by the provided class./></param>
/// <typeparam name="TClass">Type of implementation.</typeparam>
/// <exception cref="InvalidOperationException">Thrown when <see cref="ServiceManager"/> contains an entry for the provided interface type.</exception>
void RegisterTransient<TClass>(bool registerAllInterfaces = false)
where TClass : class;
/// <summary>
/// Register a service into <see cref="ServiceManager"/> with <see cref="Lifetime.Transient"/>.
/// Registers the service for all interfaces it implements.
/// </summary>
/// <param name="registerAllInterfaces">If true, <see cref="ServiceManager"/> will register all interfaces implemented by the provided class./></param>
/// <typeparam name="TClass">Type of implementation.</typeparam>
/// <param name="serviceFactory">Factory for the implementation.</param>
/// <exception cref="ArgumentNullException">Thrown when the provided serviceFactory is null.</exception>
/// <exception cref="InvalidOperationException">Thrown when <see cref="ServiceManager"/> contains an entry for the provided interface type.</exception>
void RegisterTransient<TClass>(Func<IServiceProvider, TClass> serviceFactory, bool registerAllInterfaces = false)
where TClass : class;
/// <summary>
/// Register a service into <see cref="ServiceManager"/> with <see cref="Lifetime.Scoped"/>.
/// Registers the service for all interfaces it implements.
/// </summary>
/// <param name="registerAllInterfaces">If true, <see cref="ServiceManager"/> will register all interfaces implemented by the provided class./></param>
/// <typeparam name="TClass">Type of implementation.</typeparam>
/// <exception cref="InvalidOperationException">Thrown when <see cref="ServiceManager"/> contains an entry for the provided interface type.</exception>
void RegisterScoped<TClass>(bool registerAllInterfaces = false)
where TClass : class;
/// <summary>
/// Register a service into <see cref="ServiceManager"/> with <see cref="Lifetime.Scoped"/>.
/// Registers the service for all interfaces it implements.
/// </summary>
/// <param name="registerAllInterfaces">If true, <see cref="ServiceManager"/> will register all interfaces implemented by the provided class./></param>
/// <typeparam name="TClass">Type of implementation.</typeparam>
/// <param name="serviceFactory">Factory for the implementation.</param>
/// <exception cref="ArgumentNullException">Thrown when the provided serviceFactory is null.</exception>
/// <exception cref="InvalidOperationException">Thrown when <see cref="ServiceManager"/> contains an entry for the provided interface type.</exception>
void RegisterScoped<TClass>(Func<IServiceProvider, TClass> serviceFactory, bool registerAllInterfaces = false)
where TClass : class;
/// <summary>
/// Register a service into <see cref="ServiceManager"/> with <see cref="Lifetime.Transient"/>.
/// </summary>
/// <typeparam name="TInterface">Type of interface.</typeparam>
/// <typeparam name="TClass">Type of implementation.</typeparam>
/// <exception cref="InvalidOperationException">Thrown when <see cref="ServiceManager"/> contains an entry for the provided interface type.</exception>
void RegisterTransient<TInterface, TClass>()
where TInterface : class
where TClass : TInterface;
/// <summary>
/// Register a service into <see cref="ServiceManager"/> with <see cref="Lifetime.Transient"/>.
/// </summary>
/// <typeparam name="TInterface">Type of interface.</typeparam>
/// <typeparam name="TClass">Type of implementation.</typeparam>
/// <param name="serviceFactory">Factory for the implementation.</param>
/// <exception cref="ArgumentNullException">Thrown when the provided serviceFactory is null.</exception>
/// <exception cref="InvalidOperationException">Thrown when <see cref="ServiceManager"/> contains an entry for the provided interface type.</exception>
void RegisterTransient<TInterface, TClass>(Func<IServiceProvider, TClass> serviceFactory)
where TInterface : class
where TClass : TInterface;
/// <summary>
/// Register a service into <see cref="ServiceManager"/> with <see cref="Lifetime.Singleton"/>.
/// </summary>
/// <typeparam name="TInterface">Type of interface.</typeparam>
/// <typeparam name="TClass">Type of implementation.</typeparam>
/// <exception cref="InvalidOperationException">Thrown when <see cref="ServiceManager"/> contains an entry for the provided interface type.</exception>
void RegisterSingleton<TInterface, TClass>()
where TInterface : class
where TClass : TInterface;
/// <summary>
/// Register a service into <see cref="ServiceManager"/> with <see cref="Lifetime.Singleton"/>.
/// </summary>
/// <typeparam name="TInterface">Type of interface.</typeparam>
/// <typeparam name="TClass">Type of implementation.</typeparam>
/// <param name="serviceFactory">Factory for implementation.</param>
/// <exception cref="ArgumentNullException">Thrown when the provided serviceFactory is null.</exception>
/// <exception cref="InvalidOperationException">Thrown when <see cref="ServiceManager"/> contains an entry for the provided interface type.</exception>
void RegisterSingleton<TInterface, TClass>(Func<IServiceProvider, TClass> serviceFactory)
where TInterface : class
where TClass : TInterface;
/// <summary>
/// Register a service into <see cref="ServiceManager"/> with <see cref="Lifetime.Scoped"/>.
/// </summary>
/// <typeparam name="TInterface">Type of interface.</typeparam>
/// <typeparam name="TClass">Type of implementation.</typeparam>
/// <exception cref="InvalidOperationException">Thrown when <see cref="ServiceManager"/> contains an entry for the provided interface type.</exception>
void RegisterScoped<TInterface, TClass>()
where TInterface : class
where TClass : TInterface;
/// <summary>
/// Register a service into <see cref="ServiceManager"/> with <see cref="Lifetime.Scoped"/>.
/// </summary>
/// <typeparam name="TInterface">Type of interface.</typeparam>
/// <typeparam name="TClass">Type of implementation.</typeparam>
/// <param name="serviceFactory">Factory for the implementation.</param>
/// <exception cref="ArgumentNullException">Thrown when the provided serviceFactory is null.</exception>
/// <exception cref="InvalidOperationException">Thrown when <see cref="ServiceManager"/> contains an entry for the provided interface type.</exception>
void RegisterScoped<TInterface, TClass>(Func<IServiceProvider, TClass> serviceFactory)
where TInterface : class
where TClass : TInterface;
/// <summary>
/// Register a service into <see cref="ServiceManager"/> with <see cref="Lifetime.Transient"/>.
/// </summary>
/// <param name="tInterface">Type of interface.</param>
/// <param name="tClass">Type of implementation.</param>
/// <exception cref="InvalidOperationException">Thrown when <see cref="ServiceManager"/> contains an entry for the provided interface type.</exception>
void RegisterTransient(Type tInterface, Type tClass);
/// <summary>
/// Register a service into <see cref="ServiceManager"/> with <see cref="Lifetime.Transient"/>.
/// </summary>
/// <param name="tInterface">Type of interface.</param>
/// <param name="tClass">Type of implementation.</param>
/// <param name="serviceFactory">Factory for implementation.</param>
/// <exception cref="ArgumentNullException">Thrown when the provided serviceFactory is null.</exception>
/// <exception cref="InvalidOperationException">Thrown when <see cref="ServiceManager"/> contains an entry for the provided interface type.</exception>
void RegisterTransient(Type tInterface, Type tClass, Func<IServiceProvider, object> serviceFactory);
/// <summary>
/// Register a service into <see cref="ServiceManager"/> with <see cref="Lifetime.Singleton"/>.
/// </summary>
/// <param name="tInterface">Type of interface.</param>
/// <param name="tClass">Type of implementation.</param>
/// <exception cref="InvalidOperationException">Thrown when <see cref="ServiceManager"/> contains an entry for the provided interface type.</exception>
void RegisterSingleton(Type tInterface, Type tClass);
/// <summary>
/// Register a service into <see cref="ServiceManager"/> with <see cref="Lifetime.Singleton"/>.
/// </summary>
/// <param name="tInterface">Type of interface.</param>
/// <param name="tClass">Type of implementation.</param>
/// <param name="serviceFactory">Factory for implementation.</param>
/// <exception cref="ArgumentNullException">Thrown when the provided serviceFactory is null.</exception>
/// <exception cref="InvalidOperationException">Thrown when <see cref="ServiceManager"/> contains an entry for the provided interface type.</exception>
void RegisterSingleton(Type tInterface, Type tClass, Func<IServiceProvider, object> serviceFactory);
/// <summary>
/// Register a service into <see cref="ServiceManager"/> with <see cref="Lifetime.Scoped"/>.
/// </summary>
/// <param name="tInterface">Type of interface.</param>
/// <param name="tClass">Type of implementation.</param>
/// <exception cref="InvalidOperationException">Thrown when <see cref="ServiceManager"/> contains an entry for the provided interface type.</exception>
void RegisterScoped(Type tInterface, Type tClass);
/// <summary>
/// Register a service into <see cref="ServiceManager"/> with <see cref="Lifetime.Scoped"/>.
/// </summary>
/// <param name="tInterface">Type of interface.</param>
/// <param name="tClass">Type of implementation.</param>
/// <param name="serviceFactory">Factory for implementation.</param>
/// <exception cref="ArgumentNullException">Thrown when the provided serviceFactory is null.</exception>
/// <exception cref="InvalidOperationException">Thrown when <see cref="ServiceManager"/> contains an entry for the provided interface type.</exception>
void RegisterScoped(Type tInterface, Type tClass, Func<IServiceProvider, object> serviceFactory);
/// <summary>
/// Register a service into <see cref="ServiceManager"/> with <see cref="Lifetime.Transient"/>.
/// Registers the service for all interfaces it implements.
/// </summary>
/// <param name="registerAllInterfaces">If true, <see cref="ServiceManager"/> will register all interfaces implemented by the provided class./></param>
/// <param name="tClass">Type of implementation.</param>
/// <exception cref="InvalidOperationException">Thrown when <see cref="ServiceManager"/> contains an entry for the provided interface type.</exception>
void RegisterTransient(Type tClass, bool registerAllInterfaces = false);
/// <summary>
/// Register a service into <see cref="ServiceManager"/> with <see cref="Lifetime.Transient"/>.
/// Registers the service for all interfaces it implements.
/// </summary>
/// <param name="registerAllInterfaces">If true, <see cref="ServiceManager"/> will register all interfaces implemented by the provided class./></param>
/// <param name="tClass">Type of implementation.</param>
/// <param name="serviceFactory">Factory for implementation.</param>
/// <exception cref="ArgumentNullException">Thrown when the provided serviceFactory is null.</exception>
/// <exception cref="InvalidOperationException">Thrown when <see cref="ServiceManager"/> contains an entry for the provided interface type.</exception>
void RegisterTransient(Type tClass, Func<IServiceProvider, object> serviceFactory, bool registerAllInterfaces = false);
/// <summary>
/// Register a service into <see cref="ServiceManager"/> with <see cref="Lifetime.Singleton"/>.
/// Registers the service for all interfaces it implements.
/// </summary>
/// <param name="registerAllInterfaces">If true, <see cref="ServiceManager"/> will register all interfaces implemented by the provided class./></param>
/// <param name="tClass">Type of implementation.</param>
/// <exception cref="InvalidOperationException">Thrown when <see cref="ServiceManager"/> contains an entry for the provided interface type.</exception>
void RegisterSingleton(Type tClass, bool registerAllInterfaces = false);
/// <summary>
/// Register a service into <see cref="ServiceManager"/> with <see cref="Lifetime.Singleton"/>.
/// Registers the service for all interfaces it implements.
/// </summary>
/// <param name="registerAllInterfaces">If true, <see cref="ServiceManager"/> will register all interfaces implemented by the provided class./></param>
/// <param name="tClass">Type of implementation.</param>
/// <param name="serviceFactory">Factory for implementation.</param>
/// <exception cref="ArgumentNullException">Thrown when the provided serviceFactory is null.</exception>
/// <exception cref="InvalidOperationException">Thrown when <see cref="ServiceManager"/> contains an entry for the provided interface type.</exception>
void RegisterSingleton(Type tClass, Func<IServiceProvider, object> serviceFactory, bool registerAllInterfaces = false);
/// <summary>
/// Register a service into <see cref="ServiceManager"/> with <see cref="Lifetime.Scoped"/>.
/// Registers the service for all interfaces it implements.
/// </summary>
/// <param name="registerAllInterfaces">If true, <see cref="ServiceManager"/> will register all interfaces implemented by the provided class./></param>
/// <param name="tClass">Type of implementation.</param>
/// <exception cref="InvalidOperationException">Thrown when <see cref="ServiceManager"/> contains an entry for the provided interface type.</exception>
void RegisterScoped(Type tClass, bool registerAllInterfaces = false);
/// <summary>
/// Register a service into <see cref="ServiceManager"/> with <see cref="Lifetime.Scoped"/>.
/// Registers the service for all interfaces it implements.
/// </summary>
/// <param name="registerAllInterfaces">If true, <see cref="ServiceManager"/> will register all interfaces implemented by the provided class./></param>
/// <param name="tClass">Type of implementation.</param>
/// <param name="serviceFactory">Factory for implementation.</param>
/// <exception cref="ArgumentNullException">Thrown when the provided serviceFactory is null.</exception>
/// <exception cref="InvalidOperationException">Thrown when <see cref="ServiceManager"/> contains an entry for the provided interface type.</exception>
void RegisterScoped(Type tClass, Func<IServiceProvider, object> serviceFactory, bool registerAllInterfaces = false);
}
+26 -27
View File
@@ -2,34 +2,33 @@
using System.Collections.Generic;
using Slim.Exceptions;
namespace Slim
namespace Slim;
/// <summary>
/// Interface allowing to request services from the <see cref="ServiceManager"/>.
/// </summary>
public interface IServiceProvider : System.IServiceProvider, IDisposable
{
/// <summary>
/// Interface allowing to request services from the <see cref="ServiceManager"/>.
/// Returns all services that can be cast to provided type.
/// </summary>
public interface IServiceProvider : System.IServiceProvider, IDisposable
{
/// <summary>
/// Returns all services that can be cast to provided type.
/// </summary>
/// <typeparam name="T">Type of the returned services.</typeparam>
IEnumerable<T> GetServicesOfType<T>();
/// <summary>
/// Returns all services that can be cast to provided type.
/// </summary>
/// <param name="type">Type of the returned services.</param>
IEnumerable<object> GetServicesOfType(Type type);
/// <summary>
/// Creates a scoped <see cref="IServiceProvider"/>.
/// </summary>
/// <returns>Scoped <see cref="IServiceProvider"/>.</returns>
IServiceProvider CreateScope();
/// <summary>
/// Resolves and returns the required service.
/// </summary>
/// <typeparam name="TInterface">Type of required service.</typeparam>
/// <returns>Required service.</returns>
/// <exception cref="DependencyInjectionException">Thrown when unable to resolve required service.</exception>
TInterface GetService<TInterface>() where TInterface : class;
}
/// <typeparam name="T">Type of the returned services.</typeparam>
IEnumerable<T> GetServicesOfType<T>();
/// <summary>
/// Returns all services that can be cast to provided type.
/// </summary>
/// <param name="type">Type of the returned services.</param>
IEnumerable<object> GetServicesOfType(Type type);
/// <summary>
/// Creates a scoped <see cref="IServiceProvider"/>.
/// </summary>
/// <returns>Scoped <see cref="IServiceProvider"/>.</returns>
IServiceProvider CreateScope();
/// <summary>
/// Resolves and returns the required service.
/// </summary>
/// <typeparam name="TInterface">Type of required service.</typeparam>
/// <returns>Required service.</returns>
/// <exception cref="DependencyInjectionException">Thrown when unable to resolve required service.</exception>
TInterface GetService<TInterface>() where TInterface : class;
}
+6 -7
View File
@@ -1,9 +1,8 @@
namespace Slim
namespace Slim;
internal enum Lifetime
{
internal enum Lifetime
{
Transient,
Singleton,
Scoped
}
Transient,
Singleton,
Scoped
}
+17 -18
View File
@@ -1,24 +1,23 @@
using System;
namespace Slim.Resolvers
namespace Slim.Resolvers;
/// <summary>
/// Resolver that can be added to a <see cref="IServiceManager"/> to manually resolve a dependency.
/// </summary>
public interface IDependencyResolver
{
/// <summary>
/// Resolver that can be added to a <see cref="IServiceManager"/> to manually resolve a dependency.
/// Returns true if the <see cref="IDependencyResolver"/> can resolve the dependency of provided <see cref="Type"/>.
/// </summary>
public interface IDependencyResolver
{
/// <summary>
/// Returns true if the <see cref="IDependencyResolver"/> can resolve the dependency of provided <see cref="Type"/>.
/// </summary>
/// <param name="type"><see cref="Type"/> of dependency to be resolved.</param>
/// <returns>True if <see cref="IDependencyResolver"/> can resolve the dependency. Otherwise false.</returns>
bool CanResolve(Type type);
/// <summary>
/// Returns a resolved dependency of the provided type.
/// </summary>
/// <param name="serviceProvider">Reference to the calling <see cref="IServiceProvider"/>.</param>
/// <param name="type"><see cref="Type"/> of the dependency to be resolved.</param>
/// <returns>Resolved dependency of provided <see cref="Type"/>.</returns>
object Resolve(IServiceProvider serviceProvider, Type type);
}
/// <param name="type"><see cref="Type"/> of dependency to be resolved.</param>
/// <returns>True if <see cref="IDependencyResolver"/> can resolve the dependency. Otherwise false.</returns>
bool CanResolve(Type type);
/// <summary>
/// Returns a resolved dependency of the provided type.
/// </summary>
/// <param name="serviceProvider">Reference to the calling <see cref="IServiceProvider"/>.</param>
/// <param name="type"><see cref="Type"/> of the dependency to be resolved.</param>
/// <returns>Resolved dependency of provided <see cref="Type"/>.</returns>
object Resolve(IServiceProvider serviceProvider, Type type);
}
+840 -828
View File
File diff suppressed because it is too large Load Diff
+3 -3
View File
@@ -8,10 +8,10 @@
<Company />
<Product></Product>
<Description>Service lifetime management and dependency injection framework.</Description>
<AssemblyVersion>1.8.1</AssemblyVersion>
<FileVersion>1.8.1</FileVersion>
<AssemblyVersion>1.9</AssemblyVersion>
<FileVersion>1.9</FileVersion>
<PackageProjectUrl>https://github.com/AlexMacocian/Slim</PackageProjectUrl>
<Version>1.8.1</Version>
<Version>1.9</Version>
<EnableNETAnalyzers>true</EnableNETAnalyzers>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
+10
View File
@@ -59,6 +59,11 @@
Interface for <see cref="T:Slim.ServiceManager"/>.
</summary>
</member>
<member name="P:Slim.IServiceManager.ParentServiceManager">
<summary>
Gets the <see cref="T:Slim.IServiceManager"/> that created the current <see cref="T:Slim.IServiceManager"/>. Returns null in case there is no parent.
</summary>
</member>
<member name="P:Slim.IServiceManager.AllowScopedManagerModifications">
<summary>
Allow modifications to scoped <see cref="T:Slim.IServiceManager"/> created from this <see cref="T:Slim.IServiceManager"/>.
@@ -416,6 +421,11 @@
Returns true if <see cref="T:Slim.IServiceManager"/> is readonly.
</summary>
</member>
<member name="P:Slim.ServiceManager.ParentServiceManager">
<summary>
Gets the <see cref="T:Slim.IServiceManager"/> that created the current <see cref="T:Slim.IServiceManager"/>. Returns null in case there is no parent.
</summary>
</member>
<member name="M:Slim.ServiceManager.#ctor">
<summary>
Creates an instance of <see cref="T:Slim.IServiceManager"/>.