diff --git a/VContainer/Assets/Tests/ContainerTest.cs b/VContainer/Assets/Tests/ContainerTest.cs index 11359414..ad8247d5 100644 --- a/VContainer/Assets/Tests/ContainerTest.cs +++ b/VContainer/Assets/Tests/ContainerTest.cs @@ -442,6 +442,17 @@ public void RegisterWithParameter() var resolved = container.Resolve(); Assert.That(resolved.Service2, Is.EqualTo(paramValue)); } + + { + var builder = new ContainerBuilder(); + builder.Register(Lifetime.Scoped); + builder.Register(Lifetime.Scoped) + .WithParameter(resolver => resolver.Resolve()); + + var container = builder.Build(); + var resolved = container.Resolve(); + Assert.That(resolved.Service2, Is.Not.Null); + } } [Test] diff --git a/VContainer/Assets/VContainer/Runtime/IInjectParameter.cs b/VContainer/Assets/VContainer/Runtime/IInjectParameter.cs index 676faca2..337dd2a8 100644 --- a/VContainer/Assets/VContainer/Runtime/IInjectParameter.cs +++ b/VContainer/Assets/VContainer/Runtime/IInjectParameter.cs @@ -5,6 +5,6 @@ namespace VContainer public interface IInjectParameter { bool Match(Type parameterType, string parameterName); - object Value { get; } + object GetValue(IObjectResolver resolver); } } \ No newline at end of file diff --git a/VContainer/Assets/VContainer/Runtime/IObjectResolverExtensions.cs b/VContainer/Assets/VContainer/Runtime/IObjectResolverExtensions.cs index ed87fe97..cd3b15be 100644 --- a/VContainer/Assets/VContainer/Runtime/IObjectResolverExtensions.cs +++ b/VContainer/Assets/VContainer/Runtime/IObjectResolverExtensions.cs @@ -28,7 +28,7 @@ public static object ResolveOrParameter( var parameter = parameters[i]; if (parameter.Match(parameterType, parameterName)) { - return parameter.Value; + return parameter.GetValue(resolver); } } } diff --git a/VContainer/Assets/VContainer/Runtime/Internal/InjectParameter.cs b/VContainer/Assets/VContainer/Runtime/Internal/InjectParameter.cs index f90395e5..07a0cfcf 100644 --- a/VContainer/Assets/VContainer/Runtime/Internal/InjectParameter.cs +++ b/VContainer/Assets/VContainer/Runtime/Internal/InjectParameter.cs @@ -5,7 +5,7 @@ namespace VContainer.Internal sealed class TypedParameter : IInjectParameter { public readonly Type Type; - public object Value { get; } + public readonly object Value; public TypedParameter(Type type, object value) { @@ -14,12 +14,36 @@ public TypedParameter(Type type, object value) } public bool Match(Type parameterType, string _) => parameterType == Type; + + public object GetValue(IObjectResolver _) + { + return Value; + } + } + + sealed class FuncTypedParameter : IInjectParameter + { + public readonly Type Type; + public readonly Func Func; + + public FuncTypedParameter(Type type, Func func) + { + Type = type; + Func = func; + } + + public bool Match(Type parameterType, string _) => parameterType == Type; + + public object GetValue(IObjectResolver resolver) + { + return Func(resolver); + } } sealed class NamedParameter : IInjectParameter { public readonly string Name; - public object Value { get; } + public readonly object Value; public NamedParameter(string name, object value) { @@ -28,5 +52,29 @@ public NamedParameter(string name, object value) } public bool Match(Type _, string parameterName) => parameterName == Name; + + public object GetValue(IObjectResolver _) + { + return Value; + } + } + + sealed class FuncNamedParameter : IInjectParameter + { + public readonly string Name; + public readonly Func Func; + + public FuncNamedParameter(string name, Func func) + { + Name = name; + Func = func; + } + + public bool Match(Type _, string parameterName) => parameterName == Name; + + public object GetValue(IObjectResolver resolver) + { + return Func(resolver); + } } } diff --git a/VContainer/Assets/VContainer/Runtime/RegistrationBuilder.cs b/VContainer/Assets/VContainer/Runtime/RegistrationBuilder.cs index ace303b2..4f59a0a1 100644 --- a/VContainer/Assets/VContainer/Runtime/RegistrationBuilder.cs +++ b/VContainer/Assets/VContainer/Runtime/RegistrationBuilder.cs @@ -90,6 +90,13 @@ public RegistrationBuilder WithParameter(string name, object value) Parameters.Add(new NamedParameter(name, value)); return this; } + + public RegistrationBuilder WithParameter(string name, Func value) + { + Parameters = Parameters ?? new List(); + Parameters.Add(new FuncNamedParameter(name, value)); + return this; + } public RegistrationBuilder WithParameter(Type type, object value) { @@ -97,11 +104,28 @@ public RegistrationBuilder WithParameter(Type type, object value) Parameters.Add(new TypedParameter(type, value)); return this; } + + public RegistrationBuilder WithParameter(Type type, Func value) + { + Parameters = Parameters ?? new List(); + Parameters.Add(new FuncTypedParameter(type, value)); + return this; + } public RegistrationBuilder WithParameter(TParam value) { return WithParameter(typeof(TParam), value); } + + public RegistrationBuilder WithParameter(Func value) + { + return WithParameter(typeof(TParam), resolver => value(resolver)); + } + + public RegistrationBuilder WithParameter(Func value) + { + return WithParameter(typeof(TParam), _ => value()); + } protected virtual void AddInterfaceType(Type interfaceType) {