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 20, 2025
1 parent 75f362e commit 0d9689d
Show file tree
Hide file tree
Showing 30 changed files with 79 additions and 167 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
7 changes: 0 additions & 7 deletions samples/BlazorServerApp/Composition.cs
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().To<ClockViewModel>()
// Provides the composition root for Clock view model
Expand Down
7 changes: 0 additions & 7 deletions samples/BlazorWebAssemblyApp/Composition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,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 samples/GrpcService/Composition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,6 @@ 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>();
}
6 changes: 0 additions & 6 deletions samples/MinimalWebAPI/Composition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,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>()
.Root<IWeatherForecastService>()

Expand Down
6 changes: 0 additions & 6 deletions samples/WebAPI/Composition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@ 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
6 changes: 0 additions & 6 deletions samples/WebApp/Composition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@ 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
4 changes: 3 additions & 1 deletion src/Pure.DI.Core/Core/ApiInvocationProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,9 @@ MemberAccessExpressionSyntax memberAccess when memberAccess.Kind() == SyntaxKind
var hintArgs = arguments.GetArgs(invocation.ArgumentList, "hint", "value");
if (hintArgs is [{ Expression: { } hintNameExpression }, { Expression: { } hintValueExpression }])
{
metadataVisitor.VisitHint(new MdHint(semantic.GetConstantValue<Hint>(semanticModel, hintNameExpression), semantic.GetRequiredConstantValue<string>(semanticModel, hintValueExpression)));
var values = new LinkedList<string>();
values.AddLast(semantic.GetRequiredConstantValue<string>(semanticModel, hintValueExpression));
metadataVisitor.VisitHint(new MdHint(semantic.GetConstantValue<Hint>(semanticModel, hintNameExpression), values));
}

break;
Expand Down
2 changes: 1 addition & 1 deletion src/Pure.DI.Core/Core/Code/Comments.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ where match.Success
continue;
}

result[setting] = hint.Groups[2].Value;
result.GetOrAdd(setting, _ => new LinkedList<string>()).AddLast(hint.Groups[2].Value);
}

return result;
Expand Down
Loading

0 comments on commit 0d9689d

Please sign in to comment.