Skip to content

Commit

Permalink
Merge pull request #216 from hadashiA/ku/refactor-register
Browse files Browse the repository at this point in the history
Reduce interface members
  • Loading branch information
hadashiA authored May 4, 2021
2 parents ac26b0f + cc13f74 commit 4e5344d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 54 deletions.
11 changes: 2 additions & 9 deletions VContainer/Assets/VContainer/Runtime/ContainerBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,8 @@ namespace VContainer
public interface IContainerBuilder
{
object ApplicationOrigin { get; set; }

RegistrationBuilder Register(Type type, Lifetime lifetime);
RegistrationBuilder Register(RegistrationBuilder registrationBuilder);

T Register<T>(T registrationBuilder) where T : RegistrationBuilder;
void RegisterBuildCallback(Action<IObjectResolver> container);

bool Exists(Type type, bool includeInterfaceTypes = false);
}

Expand Down Expand Up @@ -49,10 +45,7 @@ public class ContainerBuilder : IContainerBuilder
readonly IList<RegistrationBuilder> registrationBuilders = new List<RegistrationBuilder>();
List<Action<IObjectResolver>> buildCallbacks;

public RegistrationBuilder Register(Type type, Lifetime lifetime)
=> Register(new RegistrationBuilder(type, lifetime));

public RegistrationBuilder Register(RegistrationBuilder registrationBuilder)
public T Register<T>(T registrationBuilder) where T : RegistrationBuilder
{
registrationBuilders.Add(registrationBuilder);
return registrationBuilder;
Expand Down
36 changes: 11 additions & 25 deletions VContainer/Assets/VContainer/Runtime/ContainerBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ namespace VContainer
{
public static class ContainerBuilderExtensions
{
public static RegistrationBuilder Register(
this IContainerBuilder builder,
Type type,
Lifetime lifetime)
=> builder.Register(new RegistrationBuilder(type, lifetime));

public static RegistrationBuilder Register<T>(
this IContainerBuilder builder,
Lifetime lifetime)
Expand Down Expand Up @@ -79,51 +85,31 @@ public static RegistrationBuilder RegisterFactory<T>(
this IContainerBuilder builder,
Func<IObjectResolver, Func<T>> factoryFactory,
Lifetime lifetime)
{
var registrationBuilder = new FactoryRegistration<T>(factoryFactory, lifetime);
builder.Register(registrationBuilder);
return registrationBuilder;
}
=> builder.Register(new FactoryRegistration<T>(factoryFactory, lifetime));

public static RegistrationBuilder RegisterFactory<TParam1, T>(
this IContainerBuilder builder,
Func<IObjectResolver, Func<TParam1, T>> factoryFactory,
Lifetime lifetime)
{
var registrationBuilder = new FactoryRegistration<TParam1, T>(factoryFactory, lifetime);
builder.Register(registrationBuilder);
return registrationBuilder;
}
=> builder.Register(new FactoryRegistration<TParam1, T>(factoryFactory, lifetime));

public static RegistrationBuilder RegisterFactory<TParam1, TParam2, T>(
this IContainerBuilder builder,
Func<IObjectResolver, Func<TParam1, TParam2, T>> factoryFactory,
Lifetime lifetime)
{
var registrationBuilder = new FactoryRegistration<TParam1, TParam2, T>(factoryFactory, lifetime);
builder.Register(registrationBuilder);
return registrationBuilder;
}
=> builder.Register(new FactoryRegistration<TParam1, TParam2, T>(factoryFactory, lifetime));

public static RegistrationBuilder RegisterFactory<TParam1, TParam2, TParam3, T>(
this IContainerBuilder builder,
Func<IObjectResolver, Func<TParam1, TParam2, TParam3, T>> factoryFactory,
Lifetime lifetime)
{
var registrationBuilder = new FactoryRegistration<TParam1, TParam2, TParam3, T>(factoryFactory, lifetime);
builder.Register(registrationBuilder);
return registrationBuilder;
}
=> builder.Register(new FactoryRegistration<TParam1, TParam2, TParam3, T>(factoryFactory, lifetime));

public static RegistrationBuilder RegisterFactory<TParam1, TParam2, TParam3, TParam4, T>(
this IContainerBuilder builder,
Func<IObjectResolver, Func<TParam1, TParam2, TParam3, TParam4, T>> factoryFactory,
Lifetime lifetime)
{
var registrationBuilder = new FactoryRegistration<TParam1, TParam2, TParam3, TParam4, T>(factoryFactory, lifetime);
builder.Register(registrationBuilder);
return registrationBuilder;
}
=> builder.Register(new FactoryRegistration<TParam1, TParam2, TParam3, TParam4, T>(factoryFactory, lifetime));

[Obsolete("IObjectResolver is registered by default. This method does nothing.")]
public static void RegisterContainer(this IContainerBuilder builder)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,7 @@ public static RegistrationBuilder RegisterComponent<TInterface>(this IContainerB
var registrationBuilder = new ComponentRegistrationBuilder(component).As(typeof(TInterface));
// Force inject execution
registrationBuilder.OnAfterBuild((registration, container) => registration.SpawnInstance(container));
builder.Register(registrationBuilder);
return registrationBuilder;
return builder.Register(registrationBuilder);
}

public static ComponentRegistrationBuilder RegisterComponentInHierarchy<T>(this IContainerBuilder builder)
Expand All @@ -123,8 +122,7 @@ public static ComponentRegistrationBuilder RegisterComponentInHierarchy<T>(this
var registrationBuilder = new ComponentRegistrationBuilder(scene, typeof(T));
// Force inject execution
registrationBuilder.OnAfterBuild((registration, container) => registration.SpawnInstance(container));
builder.Register(registrationBuilder);
return registrationBuilder;
return builder.Register(registrationBuilder);
}

public static ComponentRegistrationBuilder RegisterComponentOnNewGameObject<T>(
Expand All @@ -133,12 +131,7 @@ public static ComponentRegistrationBuilder RegisterComponentOnNewGameObject<T>(
string newGameObjectName = null)
where T : Component
{
var registrationBuilder = new ComponentRegistrationBuilder(
newGameObjectName,
typeof(T),
lifetime);
builder.Register(registrationBuilder);
return registrationBuilder;
return builder.Register(new ComponentRegistrationBuilder(newGameObjectName, typeof(T), lifetime));
}

public static ComponentRegistrationBuilder RegisterComponentInNewPrefab<T>(
Expand All @@ -147,12 +140,7 @@ public static ComponentRegistrationBuilder RegisterComponentInNewPrefab<T>(
Lifetime lifetime)
where T : Component
{
var registrationBuilder = new ComponentRegistrationBuilder(
prefab,
typeof(T),
lifetime);
builder.Register(registrationBuilder);
return registrationBuilder;
return builder.Register(new ComponentRegistrationBuilder(prefab, typeof(T), lifetime));
}

#if VCONTAINER_ECS_INTEGRATION
Expand Down Expand Up @@ -243,8 +231,7 @@ public static SystemRegistrationBuilder RegisterSystemIntoWorld<T>(
var registrationBuilder = new SystemRegistrationBuilder(typeof(T), worldName)
.IntoGroup<SimulationSystemGroup>();

builder.Register(registrationBuilder);
return registrationBuilder;
return builder.Register(registrationBuilder);
}

public static SystemRegistrationBuilder RegisterSystemIntoDefaultWorld<T>(this IContainerBuilder builder)
Expand All @@ -253,8 +240,7 @@ public static SystemRegistrationBuilder RegisterSystemIntoDefaultWorld<T>(this I
var registrationBuilder = new SystemRegistrationBuilder(typeof(T), null)
.IntoGroup<SimulationSystemGroup>();

builder.Register(registrationBuilder);
return registrationBuilder;
return builder.Register(registrationBuilder);
}
#endif
}
Expand Down

1 comment on commit 4e5344d

@vercel
Copy link

@vercel vercel bot commented on 4e5344d May 4, 2021

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.