From cc13f749b9ac8595aa77fb49c9dc4a9c2e89132e Mon Sep 17 00:00:00 2001 From: hadashi Date: Tue, 4 May 2021 20:40:40 +0900 Subject: [PATCH] Reduce interface members --- .../VContainer/Runtime/ContainerBuilder.cs | 11 ++---- .../Runtime/ContainerBuilderExtensions.cs | 36 ++++++------------- .../Unity/ContainerBuilderUnityExtensions.cs | 26 ++++---------- 3 files changed, 19 insertions(+), 54 deletions(-) diff --git a/VContainer/Assets/VContainer/Runtime/ContainerBuilder.cs b/VContainer/Assets/VContainer/Runtime/ContainerBuilder.cs index 3f81aa8e..59a27f46 100644 --- a/VContainer/Assets/VContainer/Runtime/ContainerBuilder.cs +++ b/VContainer/Assets/VContainer/Runtime/ContainerBuilder.cs @@ -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 registrationBuilder) where T : RegistrationBuilder; void RegisterBuildCallback(Action container); - bool Exists(Type type, bool includeInterfaceTypes = false); } @@ -49,10 +45,7 @@ public class ContainerBuilder : IContainerBuilder readonly IList registrationBuilders = new List(); List> buildCallbacks; - public RegistrationBuilder Register(Type type, Lifetime lifetime) - => Register(new RegistrationBuilder(type, lifetime)); - - public RegistrationBuilder Register(RegistrationBuilder registrationBuilder) + public T Register(T registrationBuilder) where T : RegistrationBuilder { registrationBuilders.Add(registrationBuilder); return registrationBuilder; diff --git a/VContainer/Assets/VContainer/Runtime/ContainerBuilderExtensions.cs b/VContainer/Assets/VContainer/Runtime/ContainerBuilderExtensions.cs index 574c9985..c4c37263 100644 --- a/VContainer/Assets/VContainer/Runtime/ContainerBuilderExtensions.cs +++ b/VContainer/Assets/VContainer/Runtime/ContainerBuilderExtensions.cs @@ -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( this IContainerBuilder builder, Lifetime lifetime) @@ -79,51 +85,31 @@ public static RegistrationBuilder RegisterFactory( this IContainerBuilder builder, Func> factoryFactory, Lifetime lifetime) - { - var registrationBuilder = new FactoryRegistration(factoryFactory, lifetime); - builder.Register(registrationBuilder); - return registrationBuilder; - } + => builder.Register(new FactoryRegistration(factoryFactory, lifetime)); public static RegistrationBuilder RegisterFactory( this IContainerBuilder builder, Func> factoryFactory, Lifetime lifetime) - { - var registrationBuilder = new FactoryRegistration(factoryFactory, lifetime); - builder.Register(registrationBuilder); - return registrationBuilder; - } + => builder.Register(new FactoryRegistration(factoryFactory, lifetime)); public static RegistrationBuilder RegisterFactory( this IContainerBuilder builder, Func> factoryFactory, Lifetime lifetime) - { - var registrationBuilder = new FactoryRegistration(factoryFactory, lifetime); - builder.Register(registrationBuilder); - return registrationBuilder; - } + => builder.Register(new FactoryRegistration(factoryFactory, lifetime)); public static RegistrationBuilder RegisterFactory( this IContainerBuilder builder, Func> factoryFactory, Lifetime lifetime) - { - var registrationBuilder = new FactoryRegistration(factoryFactory, lifetime); - builder.Register(registrationBuilder); - return registrationBuilder; - } + => builder.Register(new FactoryRegistration(factoryFactory, lifetime)); public static RegistrationBuilder RegisterFactory( this IContainerBuilder builder, Func> factoryFactory, Lifetime lifetime) - { - var registrationBuilder = new FactoryRegistration(factoryFactory, lifetime); - builder.Register(registrationBuilder); - return registrationBuilder; - } + => builder.Register(new FactoryRegistration(factoryFactory, lifetime)); [Obsolete("IObjectResolver is registered by default. This method does nothing.")] public static void RegisterContainer(this IContainerBuilder builder) diff --git a/VContainer/Assets/VContainer/Runtime/Unity/ContainerBuilderUnityExtensions.cs b/VContainer/Assets/VContainer/Runtime/Unity/ContainerBuilderUnityExtensions.cs index 9024d2cf..113891c5 100644 --- a/VContainer/Assets/VContainer/Runtime/Unity/ContainerBuilderUnityExtensions.cs +++ b/VContainer/Assets/VContainer/Runtime/Unity/ContainerBuilderUnityExtensions.cs @@ -111,8 +111,7 @@ public static RegistrationBuilder RegisterComponent(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(this IContainerBuilder builder) @@ -123,8 +122,7 @@ public static ComponentRegistrationBuilder RegisterComponentInHierarchy(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( @@ -133,12 +131,7 @@ public static ComponentRegistrationBuilder RegisterComponentOnNewGameObject( 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( @@ -147,12 +140,7 @@ public static ComponentRegistrationBuilder RegisterComponentInNewPrefab( 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 @@ -243,8 +231,7 @@ public static SystemRegistrationBuilder RegisterSystemIntoWorld( var registrationBuilder = new SystemRegistrationBuilder(typeof(T), worldName) .IntoGroup(); - builder.Register(registrationBuilder); - return registrationBuilder; + return builder.Register(registrationBuilder); } public static SystemRegistrationBuilder RegisterSystemIntoDefaultWorld(this IContainerBuilder builder) @@ -253,8 +240,7 @@ public static SystemRegistrationBuilder RegisterSystemIntoDefaultWorld(this I var registrationBuilder = new SystemRegistrationBuilder(typeof(T), null) .IntoGroup(); - builder.Register(registrationBuilder); - return registrationBuilder; + return builder.Register(registrationBuilder); } #endif }