Skip to content

Commit

Permalink
Merge pull request #640 from hadashiA/WithParameterResolver
Browse files Browse the repository at this point in the history
With parameter resolver
  • Loading branch information
hadashiA authored Mar 2, 2024
2 parents 6e160f3 + a8a6315 commit a562f6a
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 4 deletions.
11 changes: 11 additions & 0 deletions VContainer/Assets/Tests/ContainerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,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

0 comments on commit a562f6a

Please sign in to comment.