Skip to content

Commit

Permalink
Merge pull request #604 from hadashiA/ku/remove-register-opengeneric
Browse files Browse the repository at this point in the history
Remove RegisterOpenGeneric instead of to use Register
  • Loading branch information
hadashiA authored Jan 2, 2024
2 parents a202db4 + 6f2a623 commit 4c77e21
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 48 deletions.
16 changes: 8 additions & 8 deletions VContainer/Assets/Tests/ContainerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -255,10 +255,11 @@ public void ResolveOpenGeneric()
var builder = new ContainerBuilder();

builder.Register<I2, NoDependencyServiceA>(Lifetime.Transient).AsSelf();
builder.RegisterOpenGeneric(typeof(GenericsService<>), Lifetime.Transient)
builder.Register(typeof(GenericsService<>), Lifetime.Transient)
.AsImplementedInterfaces()
.AsSelf();
builder.RegisterOpenGeneric(typeof(IGenericService<,>), typeof(GenericsService2<,>), Lifetime.Singleton)
builder.Register(typeof(GenericsService2<,>), Lifetime.Singleton)
.As(typeof(IGenericService<,>))
.AsSelf();
builder.Register<HasGenericDependency>(Lifetime.Singleton);

Expand Down Expand Up @@ -464,16 +465,15 @@ public void RegisterInvalidOpenGeneric()
{
var builder = new ContainerBuilder();
Assert.Throws<VContainerException>(() =>
builder.RegisterOpenGeneric(typeof(IGenericService<>), typeof(GenericsService<int>), Lifetime.Transient)
builder.Register(typeof(GenericsService<int>), Lifetime.Transient)
.As(typeof(IGenericService<>))
);
Assert.Throws<VContainerException>(() =>
builder.RegisterOpenGeneric(typeof(IGenericService<int>), typeof(GenericsService<>), Lifetime.Transient)
);
Assert.Throws<VContainerException>(() =>
builder.RegisterOpenGeneric(typeof(I2), typeof(NoDependencyServiceA), Lifetime.Transient)
builder.Register(typeof(GenericsService<>), Lifetime.Transient)
.As(typeof(IGenericService<int>))
);
Assert.Throws<VContainerException>(() =>
builder.RegisterOpenGeneric(typeof(GenericsService<>), Lifetime.Transient)
builder.Register(typeof(GenericsService<>), Lifetime.Transient)
.As(typeof(IGenericService<int>))
);
}
Expand Down
22 changes: 11 additions & 11 deletions VContainer/Assets/Tests/ScopedContainerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,18 +106,18 @@ public void CreateScopeWithResolveOpenGeneric()
{
var builder = new ContainerBuilder();
builder.Register<NoDependencyServiceA>(Lifetime.Transient);
builder.RegisterOpenGeneric(typeof(GenericsService<>), Lifetime.Singleton)
builder.Register(typeof(GenericsService<>), Lifetime.Singleton)
.AsImplementedInterfaces()
.AsSelf();

builder.RegisterOpenGeneric(typeof(GenericsService2<,>), Lifetime.Singleton)
builder.Register(typeof(GenericsService2<,>), Lifetime.Singleton)
.AsImplementedInterfaces()
.AsSelf();

var container = builder.Build();
var scopedContainer = container.CreateScope(childBuilder =>
{
childBuilder.RegisterOpenGeneric(typeof(GenericsService<>), Lifetime.Singleton)
childBuilder.Register(typeof(GenericsService<>), Lifetime.Singleton)
.AsImplementedInterfaces()
.AsSelf();
});
Expand Down Expand Up @@ -227,7 +227,7 @@ public void ResolveFromParent()
//Assert.That(singletonService, Is.InstanceOf<ServiceA>());
Assert.That(scopedService, Is.InstanceOf<ServiceB>());
}

[Test]
public void ResolveCollectionFromParent()
{
Expand All @@ -240,11 +240,11 @@ public void ResolveCollectionFromParent()
{
childBuilder.Register<I1CollectionService>(Lifetime.Scoped);
});

var scopedService = childContainer.Resolve<I1CollectionService>();
Assert.That(scopedService.enumerable.Count(), Is.EqualTo(2));
}

[Test]
public void ResolveCollectionFromParentByContinuous()
{
Expand All @@ -258,30 +258,30 @@ public void ResolveCollectionFromParentByContinuous()
childBuilder.Register<I1, MultipleInterfaceServiceC>(Lifetime.Singleton);
childBuilder.Register<I1, MultipleInterfaceServiceD>(Lifetime.Singleton);
});

var scopedService = childContainer.Resolve<IReadOnlyList<I1>>();
var moreScopedService = childContainer.Resolve<IReadOnlyList<I1>>();
Assert.That(moreScopedService.Count(), Is.EqualTo(4));
}

[Test]
public void ResolveCollectionByLazyInstance()
{
var builder = new ContainerBuilder();
builder.Register<I1, MultipleInterfaceServiceA>(Lifetime.Scoped);
builder.Register<I1, MultipleInterfaceServiceB>(Lifetime.Scoped);
builder.Register<I1CollectionService>(Lifetime.Scoped);

var parentContainer = builder.Build();

var childContainer = parentContainer.CreateScope(childBuilder =>
{
});

var scopedService = childContainer.Resolve<I1CollectionService>();
Assert.That(scopedService.enumerable.Count(), Is.EqualTo(2));
}

public class I1CollectionService
{
public readonly IReadOnlyList<I1> enumerable;
Expand Down
33 changes: 10 additions & 23 deletions VContainer/Assets/VContainer/Runtime/ContainerBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,43 +11,30 @@ public static class ContainerBuilderExtensions
public static RegistrationBuilder Register(
this IContainerBuilder builder,
Type type,
Lifetime lifetime)
=> builder.Register(new RegistrationBuilder(type, lifetime));

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static RegistrationBuilder RegisterOpenGeneric(
this IContainerBuilder builder,
Type type,
Type implementationType,
Lifetime lifetime)
=> builder.Register(new OpenGenericRegistrationBuilder(type, implementationType, lifetime));

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static RegistrationBuilder RegisterOpenGeneric(
this IContainerBuilder builder,
Type type,
Lifetime lifetime)
=> builder.Register(new OpenGenericRegistrationBuilder(type, lifetime));
Lifetime lifetime) =>
builder.Register(type.IsGenericType && type.IsGenericTypeDefinition
? new OpenGenericRegistrationBuilder(type, lifetime)
: new RegistrationBuilder(type, lifetime));

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

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

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static RegistrationBuilder Register<TInterface1, TInterface2, TInterface3, TImplement>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,6 @@ namespace VContainer.Internal
{
public class OpenGenericRegistrationBuilder : RegistrationBuilder
{
public OpenGenericRegistrationBuilder(Type type, Type implementationType, Lifetime lifetime)
: this(implementationType, lifetime)
{
As(type);
}

public OpenGenericRegistrationBuilder(Type implementationType, Lifetime lifetime)
: base(implementationType, lifetime)
{
Expand Down

1 comment on commit 4c77e21

@vercel
Copy link

@vercel vercel bot commented on 4c77e21 Jan 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.