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

Added RegistrationBuilder variations with callback support #569

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
48 changes: 48 additions & 0 deletions VContainer/Assets/VContainer/Runtime/ContainerBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,33 +12,72 @@ public static RegistrationBuilder Register(
Type type,
Lifetime lifetime)
=> builder.Register(new RegistrationBuilder(type, lifetime));

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static RegistrationBuilder Register(
this IContainerBuilder builder,
Type type,
Lifetime lifetime,
Action<object, IObjectResolver> callback)
=> builder.Register(new RegistrationBuilderWithCallback(type, lifetime, callback));

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static RegistrationBuilder Register<T>(
this IContainerBuilder builder,
Lifetime lifetime)
=> builder.Register(typeof(T), lifetime);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static RegistrationBuilder Register<T>(
this IContainerBuilder builder,
Lifetime lifetime,
Action<T, IObjectResolver> callback)
=> builder.Register(typeof(T), lifetime, (instance, resolver) => callback((T)instance, resolver));

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static RegistrationBuilder Register<TInterface, TImplement>(
this IContainerBuilder builder,
Lifetime lifetime)
where TImplement : TInterface
=> builder.Register<TImplement>(lifetime).As<TInterface>();

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static RegistrationBuilder Register<TInterface, TImplement>(
this IContainerBuilder builder,
Lifetime lifetime,
Action<TImplement, IObjectResolver> callback)
where TImplement : TInterface
=> builder.Register(lifetime, callback).As<TInterface>();

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static RegistrationBuilder Register<TInterface1, TInterface2, TImplement>(
this IContainerBuilder builder,
Lifetime lifetime)
where TImplement : TInterface1, TInterface2
=> builder.Register<TImplement>(lifetime).As(typeof(TInterface1), typeof(TInterface2));

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static RegistrationBuilder Register<TInterface1, TInterface2, TImplement>(
this IContainerBuilder builder,
Lifetime lifetime,
Action<TImplement, IObjectResolver> callback)
where TImplement : TInterface1, TInterface2
=> builder.Register(lifetime, callback).As(typeof(TInterface1), typeof(TInterface2));

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static RegistrationBuilder Register<TInterface1, TInterface2, TInterface3, TImplement>(
this IContainerBuilder builder,
Lifetime lifetime)
where TImplement : TInterface1, TInterface2, TInterface3
=> builder.Register<TImplement>(lifetime).As(typeof(TInterface1), typeof(TInterface2), typeof(TInterface3));

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static RegistrationBuilder Register<TInterface1, TInterface2, TInterface3, TImplement>(
this IContainerBuilder builder,
Lifetime lifetime,
Action<TImplement, IObjectResolver> callback)
where TImplement : TInterface1, TInterface2, TInterface3
=> builder.Register(lifetime, callback).As(typeof(TInterface1), typeof(TInterface2), typeof(TInterface3));

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static RegistrationBuilder Register<TInterface>(
Expand All @@ -47,6 +86,15 @@ public static RegistrationBuilder Register<TInterface>(
Lifetime lifetime)
where TInterface : class
=> builder.Register(new FuncRegistrationBuilder(implementationConfiguration, typeof(TInterface), lifetime));

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static RegistrationBuilder Register<TInterface>(
this IContainerBuilder builder,
Func<IObjectResolver, TInterface> implementationConfiguration,
Lifetime lifetime,
Action<TInterface, IObjectResolver> callback)
where TInterface : class
=> builder.Register(new FuncRegistrationBuilderWithCallback(implementationConfiguration, typeof(TInterface), lifetime, (instance, resolver) => callback((TInterface)instance, resolver)));

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static RegistrationBuilder RegisterInstance<TInterface>(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;

namespace VContainer.Internal
{
sealed class FuncRegistrationBuilderWithCallback : RegistrationBuilder
{
readonly Func<IObjectResolver, object> implementationProvider;
readonly Action<object, IObjectResolver> callback;

public FuncRegistrationBuilderWithCallback(
Func<IObjectResolver, object> implementationProvider,
Type implementationType,
Lifetime lifetime,
Action<object, IObjectResolver> callback) : base(implementationType, lifetime)
{
this.implementationProvider = implementationProvider;
this.callback = callback;
}

public override Registration Build()
{
var spawner = new InstanceProviderCallbackDecorator(new FuncInstanceProvider(implementationProvider), callback);
return new Registration(ImplementationType, Lifetime, InterfaceTypes, spawner);
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;

namespace VContainer.Internal
{
sealed class InstanceProviderCallbackDecorator : IInstanceProvider
{
readonly Action<object, IObjectResolver> callback;
readonly IInstanceProvider instanceProvider;

public InstanceProviderCallbackDecorator(IInstanceProvider instanceProvider, Action<object, IObjectResolver> callback)
{
this.instanceProvider = instanceProvider;
this.callback = callback;
}

public object SpawnInstance(IObjectResolver resolver)
{
var instance = instanceProvider.SpawnInstance(resolver);
callback(instance, resolver);
return instance;
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;

namespace VContainer.Internal
{
sealed class RegistrationBuilderWithCallback : RegistrationBuilder
{
readonly Action<object, IObjectResolver> callback;

public RegistrationBuilderWithCallback(Type implementationType, Lifetime lifetime, Action<object, IObjectResolver> callback)
: base(implementationType, lifetime)
{
this.callback = callback;
}

public override Registration Build()
{
var injector = InjectorCache.GetOrBuild(ImplementationType);
var spawner = new InstanceProviderCallbackDecorator(new InstanceProvider(injector, Parameters), callback);
return new Registration(
ImplementationType,
Lifetime,
InterfaceTypes,
spawner);
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions VContainer/Assets/VContainer/Tests/ContainerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -518,5 +518,35 @@ public void Inject()
var ctorInjectable = new ServiceA(new NoDependencyServiceA());
Assert.DoesNotThrow(() => container.Inject(ctorInjectable));
}

[Test]
public void RegisterWithCallback()
{
{
NoDependencyServiceA fromCallback = null;

var builder = new ContainerBuilder();
builder.Register<NoDependencyServiceA>(Lifetime.Scoped, (service, _) => fromCallback = service);

var container = builder.Build();

var fromResolve = container.Resolve<NoDependencyServiceA>();

Assert.That(fromResolve, Is.Not.Null.And.EqualTo(fromCallback));
}

{
NoDependencyServiceA fromCallback = null;

var builder = new ContainerBuilder();
builder.Register(_ => new NoDependencyServiceA(), Lifetime.Scoped, (service, _) => fromCallback = service);

var container = builder.Build();

var fromResolve = container.Resolve<NoDependencyServiceA>();

Assert.That(fromResolve, Is.Not.Null.And.EqualTo(fromCallback));
}
}
}
}