Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

With parameter resolver #640

Merged
merged 2 commits into from
Mar 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions VContainer/Assets/Tests/ContainerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,17 @@ public void RegisterWithParameter()
var resolved = container.Resolve<HasMethodInjection>();
Assert.That(resolved.Service2, Is.EqualTo(paramValue));
}

{
var builder = new ContainerBuilder();
builder.Register<I2, NoDependencyServiceA>(Lifetime.Scoped);
builder.Register<HasMethodInjection>(Lifetime.Scoped)
.WithParameter(resolver => resolver.Resolve<I2>());

var container = builder.Build();
var resolved = container.Resolve<HasMethodInjection>();
Assert.That(resolved.Service2, Is.Not.Null);
}
}

[Test]
Expand Down
2 changes: 1 addition & 1 deletion VContainer/Assets/VContainer/Runtime/IInjectParameter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ namespace VContainer
public interface IInjectParameter
{
bool Match(Type parameterType, string parameterName);
object Value { get; }
object GetValue(IObjectResolver resolver);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static object ResolveOrParameter(
var parameter = parameters[i];
if (parameter.Match(parameterType, parameterName))
{
return parameter.Value;
return parameter.GetValue(resolver);
}
}
}
Expand Down
52 changes: 50 additions & 2 deletions VContainer/Assets/VContainer/Runtime/Internal/InjectParameter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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<IObjectResolver, object> Func;

public FuncTypedParameter(Type type, Func<IObjectResolver, object> 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)
{
Expand All @@ -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<IObjectResolver, object> Func;

public FuncNamedParameter(string name, Func<IObjectResolver, object> func)
{
Name = name;
Func = func;
}

public bool Match(Type _, string parameterName) => parameterName == Name;

public object GetValue(IObjectResolver resolver)
{
return Func(resolver);
}
}
}
24 changes: 24 additions & 0 deletions VContainer/Assets/VContainer/Runtime/RegistrationBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,18 +90,42 @@ public RegistrationBuilder WithParameter(string name, object value)
Parameters.Add(new NamedParameter(name, value));
return this;
}

public RegistrationBuilder WithParameter(string name, Func<IObjectResolver, object> value)
{
Parameters = Parameters ?? new List<IInjectParameter>();
Parameters.Add(new FuncNamedParameter(name, value));
return this;
}

public RegistrationBuilder WithParameter(Type type, object value)
{
Parameters = Parameters ?? new List<IInjectParameter>();
Parameters.Add(new TypedParameter(type, value));
return this;
}

public RegistrationBuilder WithParameter(Type type, Func<IObjectResolver, object> value)
{
Parameters = Parameters ?? new List<IInjectParameter>();
Parameters.Add(new FuncTypedParameter(type, value));
return this;
}

public RegistrationBuilder WithParameter<TParam>(TParam value)
{
return WithParameter(typeof(TParam), value);
}

public RegistrationBuilder WithParameter<TParam>(Func<IObjectResolver, TParam> value)
{
return WithParameter(typeof(TParam), resolver => value(resolver));
}

public RegistrationBuilder WithParameter<TParam>(Func<TParam> value)
{
return WithParameter(typeof(TParam), _ => value());
}

protected virtual void AddInterfaceType(Type interfaceType)
{
Expand Down
Loading