Skip to content

Commit

Permalink
Support for multiple hints
Browse files Browse the repository at this point in the history
  • Loading branch information
NikolayPianikov committed Jan 21, 2025
1 parent 75f362e commit 0623c7a
Show file tree
Hide file tree
Showing 41 changed files with 152 additions and 277 deletions.
7 changes: 0 additions & 7 deletions readme/BlazorServerApp.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,6 @@ internal partial class Composition: ServiceProviderFactory<Composition>
{
void Setup() => DI.Setup()
.DependsOn(Base)
// Specifies not to attempt to resolve types whose fully qualified name
// begins with Microsoft.Extensions., Microsoft.AspNetCore.
// since ServiceProvider will be used to retrieve them.
.Hint(
Hint.OnCannotResolveContractTypeNameRegularExpression,
@"^Microsoft\.(Extensions|AspNetCore)\..+$")

// View Models
.Bind().To<ClockViewModel>()
// Provides the composition root for Clock view model
Expand Down
7 changes: 0 additions & 7 deletions readme/BlazorServerAppPageTemplate.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,6 @@ internal partial class Composition: ServiceProviderFactory<Composition>
{
void Setup() => DI.Setup()
.DependsOn(Base)
// Specifies not to attempt to resolve types whose fully qualified name
// begins with Microsoft.Extensions., Microsoft.AspNetCore.
// since ServiceProvider will be used to retrieve them.
.Hint(
Hint.OnCannotResolveContractTypeNameRegularExpression,
@"^Microsoft\.(Extensions|AspNetCore)\..+$")

// View Models
.Bind().To<ClockViewModel>()
// Provides the composition root for Clock view model
Expand Down
7 changes: 0 additions & 7 deletions readme/BlazorWebAssemblyApp.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,6 @@ internal partial class Composition: ServiceProviderFactory<Composition>
{
void Setup() => DI.Setup()
.DependsOn(Base)
// Specifies not to attempt to resolve types whose fully qualified name
// begins with Microsoft.Extensions., Microsoft.AspNetCore.
// since ServiceProvider will be used to retrieve them.
.Hint(
Hint.OnCannotResolveContractTypeNameRegularExpression,
@"^Microsoft\.(Extensions|AspNetCore)\..+$")

// View Models
.Bind().As(Singleton).To<ClockViewModel>()
// Provides the composition root for Clock view model
Expand Down
7 changes: 0 additions & 7 deletions readme/BlazorWebAssemblyAppPageTemplate.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,6 @@ internal partial class Composition: ServiceProviderFactory<Composition>
{
void Setup() => DI.Setup()
.DependsOn(Base)
// Specifies not to attempt to resolve types whose fully qualified name
// begins with Microsoft.Extensions., Microsoft.AspNetCore.
// since ServiceProvider will be used to retrieve them.
.Hint(
Hint.OnCannotResolveContractTypeNameRegularExpression,
@"^Microsoft\.(Extensions|AspNetCore)\..+$")

// View Models
.Bind().As(Singleton).To<ClockViewModel>()
// Provides the composition root for Clock view model
Expand Down
7 changes: 0 additions & 7 deletions readme/GrpcService.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,6 @@ internal partial class Composition: ServiceProviderFactory<Composition>
{
void Setup() => DI.Setup()
.DependsOn(Base)
// Specifies not to attempt to resolve types whose fully qualified name
// begins with Microsoft.Extensions., Microsoft.AspNetCore.
// since ServiceProvider will be used to retrieve them.
.Hint(
Hint.OnCannotResolveContractTypeNameRegularExpression,
@"^Microsoft\.(Extensions|AspNetCore)\..+$")

// Provides the composition root for Greeter service
.Root<GreeterService>();
}
Expand Down
7 changes: 0 additions & 7 deletions readme/GrpcServicePageTemplate.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,6 @@ internal partial class Composition: ServiceProviderFactory<Composition>
{
void Setup() => DI.Setup()
.DependsOn(Base)
// Specifies not to attempt to resolve types whose fully qualified name
// begins with Microsoft.Extensions., Microsoft.AspNetCore.
// since ServiceProvider will be used to retrieve them.
.Hint(
Hint.OnCannotResolveContractTypeNameRegularExpression,
@"^Microsoft\.(Extensions|AspNetCore)\..+$")

// Provides the composition root for Greeter service
.Root<GreeterService>();
}
Expand Down
9 changes: 1 addition & 8 deletions readme/MinimalWebAPI.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,9 @@ internal partial class Composition: ServiceProviderFactory<Composition>
{
void Setup() => DI.Setup()
.DependsOn(Base)
// Specifies not to attempt to resolve types whose fully qualified name
// begins with Microsoft.Extensions., Microsoft.AspNetCore.
// since ServiceProvider will be used to retrieve them.
.Hint(
Hint.OnCannotResolveContractTypeNameRegularExpression,
@"^Microsoft\.(Extensions|AspNetCore)\..+$")

.Bind().As(Singleton).To<WeatherForecastService>()
.Root<IWeatherForecastService>()

// Application composition root
.Root<Program>("Root");
}
Expand Down
9 changes: 1 addition & 8 deletions readme/MinimalWebAPIPageTemplate.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,9 @@ internal partial class Composition: ServiceProviderFactory<Composition>
{
void Setup() => DI.Setup()
.DependsOn(Base)
// Specifies not to attempt to resolve types whose fully qualified name
// begins with Microsoft.Extensions., Microsoft.AspNetCore.
// since ServiceProvider will be used to retrieve them.
.Hint(
Hint.OnCannotResolveContractTypeNameRegularExpression,
@"^Microsoft\.(Extensions|AspNetCore)\..+$")

.Bind().As(Singleton).To<WeatherForecastService>()
.Root<IWeatherForecastService>()

// Application composition root
.Root<Program>("Root");
}
Expand Down
7 changes: 0 additions & 7 deletions readme/WebAPI.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,6 @@ internal partial class Composition: ServiceProviderFactory<Composition>
{
void Setup() => DI.Setup()
.DependsOn(Base)
// Specifies not to attempt to resolve types whose fully qualified name
// begins with Microsoft.Extensions., Microsoft.AspNetCore.
// since ServiceProvider will be used to retrieve them.
.Hint(
Hint.OnCannotResolveContractTypeNameRegularExpression,
@"^Microsoft\.(Extensions|AspNetCore)\..+$")

.Bind().As(Singleton).To<WeatherForecastService>()
// Provides the composition root for Weather Forecast controller
.Root<WeatherForecastController>();
Expand Down
7 changes: 0 additions & 7 deletions readme/WebAPIPageTemplate.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,6 @@ internal partial class Composition: ServiceProviderFactory<Composition>
{
void Setup() => DI.Setup()
.DependsOn(Base)
// Specifies not to attempt to resolve types whose fully qualified name
// begins with Microsoft.Extensions., Microsoft.AspNetCore.
// since ServiceProvider will be used to retrieve them.
.Hint(
Hint.OnCannotResolveContractTypeNameRegularExpression,
@"^Microsoft\.(Extensions|AspNetCore)\..+$")

.Bind().As(Singleton).To<WeatherForecastService>()
// Provides the composition root for Weather Forecast controller
.Root<WeatherForecastController>();
Expand Down
7 changes: 0 additions & 7 deletions readme/WebApp.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,6 @@ internal partial class Composition: ServiceProviderFactory<Composition>
{
void Setup() => DI.Setup()
.DependsOn(Base)
// Specifies not to attempt to resolve types whose fully qualified name
// begins with Microsoft.Extensions., Microsoft.AspNetCore.
// since ServiceProvider will be used to retrieve them.
.Hint(
Hint.OnCannotResolveContractTypeNameRegularExpression,
@"^Microsoft\.(Extensions|AspNetCore)\..+$")

.Bind().As(Singleton).To<WeatherForecastService>()
// Provides the composition root for Home controller
.Root<HomeController>();
Expand Down
7 changes: 0 additions & 7 deletions readme/WebAppPageTemplate.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,6 @@ internal partial class Composition: ServiceProviderFactory<Composition>
{
void Setup() => DI.Setup()
.DependsOn(Base)
// Specifies not to attempt to resolve types whose fully qualified name
// begins with Microsoft.Extensions., Microsoft.AspNetCore.
// since ServiceProvider will be used to retrieve them.
.Hint(
Hint.OnCannotResolveContractTypeNameRegularExpression,
@"^Microsoft\.(Extensions|AspNetCore)\..+$")

.Bind().As(Singleton).To<WeatherForecastService>()
// Provides the composition root for Home controller
.Root<HomeController>();
Expand Down
7 changes: 5 additions & 2 deletions readme/ondependencyinjection-wildcard-hint.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ using static Pure.DI.Hint;
// OnDependencyInjection = On
DI.Setup(nameof(Composition))
.Hint(OnDependencyInjectionContractTypeNameWildcard, "*IDependency")
.Hint(OnDependencyInjectionContractTypeNameWildcard, "*IService")
.RootArg<int>("id")
.Bind().To<Dependency>()
.Bind().To<Service>()
Expand All @@ -23,7 +24,9 @@ var log = new List<string>();
var composition = new Composition(log);
var service = composition.GetRoot(33);

log.ShouldBe(["Dependency injected"]);
log.ShouldBe([
"Dependency injected",
"Service injected"]);

interface IDependency;

Expand Down Expand Up @@ -108,7 +111,7 @@ partial class Composition
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public IService GetRoot(int id)
{
return new Service(OnDependencyInjection<IDependency>(new Dependency(id), null, Lifetime.Transient));
return OnDependencyInjection<IService>(new Service(OnDependencyInjection<IDependency>(new Dependency(id), null, Lifetime.Transient)), null, Lifetime.Transient);
}


Expand Down
8 changes: 7 additions & 1 deletion readme/onnewinstance-wildcard-hint.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ using static Pure.DI.Hint;

DI.Setup(nameof(Composition))
.Hint(OnNewInstance, "On")
.Hint(OnNewInstanceImplementationTypeNameWildcard, "*Dependency")
.Hint(OnNewInstanceImplementationTypeNameWildcard, "*Service")
.Bind().As(Lifetime.Singleton).To<Dependency>()
.Bind().To<Service>()
Expand All @@ -24,6 +25,7 @@ var service1 = composition.Root;
var service2 = composition.Root;

log.ShouldBe([
"Dependency created",
"Service created",
"Service created"]);

Expand Down Expand Up @@ -125,7 +127,11 @@ partial class Composition
{
if (_root._singletonDependency43 is null)
{
_root._singletonDependency43 = new Dependency();
Dependency _singletonDependency43Temp;
_singletonDependency43Temp = new Dependency();
OnNewInstance<Dependency>(ref _singletonDependency43Temp, null, Lifetime.Singleton);
Thread.MemoryBarrier();
_root._singletonDependency43 = _singletonDependency43Temp;
}
}
}
Expand Down
9 changes: 0 additions & 9 deletions samples/BlazorServerApp/Composition.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// ReSharper disable UnusedMember.Local

// ReSharper disable ArrangeTypeMemberModifiers

namespace BlazorServerApp;
Expand All @@ -15,14 +14,6 @@ namespace BlazorServerApp;
internal partial class Composition : ServiceProviderFactory<Composition>
{
void Setup() => DI.Setup()
.DependsOn(Base)
// Specifies not to attempt to resolve types whose fully qualified name
// begins with Microsoft.Extensions., Microsoft.AspNetCore.
// since ServiceProvider will be used to retrieve them.
.Hint(
Hint.OnCannotResolveContractTypeNameRegularExpression,
@"^Microsoft\.(Extensions|AspNetCore)\..+$")

// View Models
.Bind().To<ClockViewModel>()
// Provides the composition root for Clock view model
Expand Down
9 changes: 0 additions & 9 deletions samples/BlazorWebAssemblyApp/Composition.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// ReSharper disable UnusedMember.Local

// ReSharper disable ArrangeTypeMemberModifiers

namespace BlazorWebAssemblyApp;
Expand All @@ -14,14 +13,6 @@ namespace BlazorWebAssemblyApp;
internal partial class Composition : ServiceProviderFactory<Composition>
{
void Setup() => DI.Setup()
.DependsOn(Base)
// Specifies not to attempt to resolve types whose fully qualified name
// begins with Microsoft.Extensions., Microsoft.AspNetCore.
// since ServiceProvider will be used to retrieve them.
.Hint(
Hint.OnCannotResolveContractTypeNameRegularExpression,
@"^Microsoft\.(Extensions|AspNetCore)\..+$")

// View Models
.Bind().As(Singleton).To<ClockViewModel>()
// Provides the composition root for Clock view model
Expand Down
9 changes: 0 additions & 9 deletions samples/GrpcService/Composition.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// ReSharper disable UnusedMember.Local

// ReSharper disable ArrangeTypeMemberModifiers

namespace GrpcService;
Expand All @@ -11,14 +10,6 @@ namespace GrpcService;
internal partial class Composition : ServiceProviderFactory<Composition>
{
static void Setup() => DI.Setup()
.DependsOn(Base)
// Specifies not to attempt to resolve types whose fully qualified name
// begins with Microsoft.Extensions., Microsoft.AspNetCore.
// since ServiceProvider will be used to retrieve them.
.Hint(
Hint.OnCannotResolveContractTypeNameRegularExpression,
@"^Microsoft\.(Extensions|AspNetCore)\..+$")

// Provides the composition root for Greeter service
.Root<GreeterService>();
}
8 changes: 0 additions & 8 deletions samples/MinimalWebAPI/Composition.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// ReSharper disable UnusedMember.Local

// ReSharper disable ArrangeTypeMemberModifiers

namespace MinimalWebAPI;
Expand All @@ -12,13 +11,6 @@ namespace MinimalWebAPI;
internal partial class Composition : ServiceProviderFactory<Composition>
{
void Setup() => DI.Setup()
.DependsOn(Base)
// Specifies not to attempt to resolve types whose fully qualified name
// begins with Microsoft.Extensions., Microsoft.AspNetCore.
// since ServiceProvider will be used to retrieve them.
.Hint(
Hint.OnCannotResolveContractTypeNameRegularExpression,
@"^Microsoft\.(Extensions|AspNetCore)\..+$")
.Bind().As(Singleton).To<WeatherForecastService>()
.Root<IWeatherForecastService>()

Expand Down
6 changes: 3 additions & 3 deletions samples/ShroedingersCat/Program.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
using Pure.DI;
using static Pure.DI.Lifetime;
// ReSharper disable ArrangeTypeMemberModifiers
// ReSharper disable ArrangeTypeMemberModifiers
// ReSharper disable UnusedMember.Global
// ReSharper disable UnusedMember.Local
// ReSharper disable UnusedMemberInSuper.Global

namespace Sample;

using System.Diagnostics;
using Pure.DI;
using static Pure.DI.Lifetime;

// Let's create an abstraction

Expand Down
7 changes: 4 additions & 3 deletions samples/ShroedingersCatNativeAOT/Program.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using System.Diagnostics;
// ReSharper disable UnusedMember.Global
// ReSharper disable UnusedMemberInSuper.Global

using System.Diagnostics;
using Pure.DI;
using static Pure.DI.Lifetime;
// ReSharper disable UnusedMember.Global
// ReSharper disable UnusedMemberInSuper.Global

// Composition root
new Composition().Root.Run();
Expand Down
7 changes: 4 additions & 3 deletions samples/ShroedingersCatTopLevelStatements/Program.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using Pure.DI;
// ReSharper disable UnusedMemberInSuper.Global
// ReSharper disable UnusedMember.Global

using Pure.DI;
using System.Diagnostics;
using static Pure.DI.Lifetime;
// ReSharper disable UnusedMemberInSuper.Global
// ReSharper disable UnusedMember.Global

// Composition root
new Composition().Root.Run();
Expand Down
8 changes: 0 additions & 8 deletions samples/WebAPI/Composition.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// ReSharper disable UnusedMember.Local

// ReSharper disable ArrangeTypeMemberModifiers

namespace WebAPI;
Expand All @@ -13,13 +12,6 @@ namespace WebAPI;
internal partial class Composition : ServiceProviderFactory<Composition>
{
static void Setup() => DI.Setup()
.DependsOn(Base)
// Specifies not to attempt to resolve types whose fully qualified name
// begins with Microsoft.Extensions., Microsoft.AspNetCore.
// since ServiceProvider will be used to retrieve them.
.Hint(
Hint.OnCannotResolveContractTypeNameRegularExpression,
@"^Microsoft\.(Extensions|AspNetCore)\..+$")
.Bind().As(Singleton).To<WeatherForecastService>()
// Provides the composition root for Weather Forecast controller
.Root<WeatherForecastController>();
Expand Down
8 changes: 0 additions & 8 deletions samples/WebApp/Composition.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// ReSharper disable UnusedMember.Local

// ReSharper disable ArrangeTypeMemberModifiers

namespace WebApp;
Expand All @@ -13,13 +12,6 @@ namespace WebApp;
internal partial class Composition : ServiceProviderFactory<Composition>
{
static void Setup() => DI.Setup()
.DependsOn(Base)
// Specifies not to attempt to resolve types whose fully qualified name
// begins with Microsoft.Extensions., Microsoft.AspNetCore.
// since ServiceProvider will be used to retrieve them.
.Hint(
Hint.OnCannotResolveContractTypeNameRegularExpression,
@"^Microsoft\.(Extensions|AspNetCore)\..+$")
.Bind().As(Singleton).To<WeatherForecastService>()
// Provides the composition root for Home controller
.Root<HomeController>();
Expand Down
Loading

0 comments on commit 0623c7a

Please sign in to comment.