Skip to content

Commit

Permalink
Merge pull request #526 from SettingDust/master
Browse files Browse the repository at this point in the history
feat: add `Type` arg builder to `ContainerBuilderUnityExtensions`
  • Loading branch information
hadashiA authored Jun 18, 2023
2 parents af7bd4e + df87457 commit e9ab2e8
Showing 1 changed file with 51 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,12 @@ public readonly struct EntryPointsBuilder
{
public static void EnsureDispatcherRegistered(IContainerBuilder containerBuilder)
{
if (!containerBuilder.Exists(typeof(EntryPointDispatcher), false))
if (containerBuilder.Exists(typeof(EntryPointDispatcher), false)) return;
containerBuilder.Register<EntryPointDispatcher>(Lifetime.Scoped);
containerBuilder.RegisterBuildCallback(container =>
{
containerBuilder.Register<EntryPointDispatcher>(Lifetime.Scoped);
containerBuilder.RegisterBuildCallback(container =>
{
container.Resolve<EntryPointDispatcher>().Dispatch();
});
}
container.Resolve<EntryPointDispatcher>().Dispatch();
});
}

readonly IContainerBuilder containerBuilder;
Expand Down Expand Up @@ -100,7 +98,9 @@ public static void UseComponents(
configuration(new ComponentsBuilder(builder, root));
}

public static RegistrationBuilder RegisterEntryPoint<T>(this IContainerBuilder builder, Lifetime lifetime = Lifetime.Singleton)
public static RegistrationBuilder RegisterEntryPoint<T>(
this IContainerBuilder builder,
Lifetime lifetime = Lifetime.Singleton)
{
EntryPointsBuilder.EnsureDispatcherRegistered(builder);
return builder.Register<T>(lifetime).AsImplementedInterfaces();
Expand All @@ -113,51 +113,81 @@ public static void RegisterEntryPointExceptionHandler(
builder.RegisterInstance(new EntryPointExceptionHandler(exceptionHandler));
}

public static RegistrationBuilder RegisterComponent<TInterface>(this IContainerBuilder builder, TInterface component)
public static RegistrationBuilder RegisterComponent<TInterface>(
this IContainerBuilder builder,
TInterface component)
{
var registrationBuilder = new ComponentRegistrationBuilder(component).As(typeof(TInterface));
// Force inject execution
builder.RegisterBuildCallback(container => container.Resolve<TInterface>());
return builder.Register(registrationBuilder);
}

public static ComponentRegistrationBuilder RegisterComponentInHierarchy<T>(this IContainerBuilder builder)
public static ComponentRegistrationBuilder RegisterComponentInHierarchy(
this IContainerBuilder builder,
Type type)
{
var lifetimeScope = (LifetimeScope)builder.ApplicationOrigin;
var scene = lifetimeScope.gameObject.scene;

var registrationBuilder = new ComponentRegistrationBuilder(scene, typeof(T));
var registrationBuilder = new ComponentRegistrationBuilder(scene, type);
// Force inject execution
builder.RegisterBuildCallback(container =>
{
var type = registrationBuilder.InterfaceTypes != null
? registrationBuilder.InterfaceTypes[0]
: registrationBuilder.ImplementationType;
container.Resolve(type);
});
builder.RegisterBuildCallback(
container =>
{
container.Resolve(
registrationBuilder.InterfaceTypes != null
? registrationBuilder.InterfaceTypes[0]
: registrationBuilder.ImplementationType
);
}
);
return builder.Register(registrationBuilder);
}

public static ComponentRegistrationBuilder RegisterComponentInHierarchy<T>(this IContainerBuilder builder)
{
return builder.RegisterComponentInHierarchy(typeof(T));
}

public static ComponentRegistrationBuilder RegisterComponentOnNewGameObject(
this IContainerBuilder builder,
Type type,
Lifetime lifetime,
string newGameObjectName = null)
{
return builder.Register(new ComponentRegistrationBuilder(newGameObjectName, type, lifetime));
}

public static ComponentRegistrationBuilder RegisterComponentOnNewGameObject<T>(
this IContainerBuilder builder,
Lifetime lifetime,
string newGameObjectName = null)
where T : Component
{
return builder.Register(new ComponentRegistrationBuilder(newGameObjectName, typeof(T), lifetime));
return builder.RegisterComponentOnNewGameObject(typeof(T), lifetime, newGameObjectName);
}

public static ComponentRegistrationBuilder RegisterComponentInNewPrefab<T>(
this IContainerBuilder builder,
Type type,
T prefab,
Lifetime lifetime)
where T : Component
{
return builder.Register(new ComponentRegistrationBuilder(prefab, typeof(T), lifetime));
return builder.Register(new ComponentRegistrationBuilder(prefab, type, lifetime));
}

#if VCONTAINER_ECS_INTEGRATION
public static ComponentRegistrationBuilder RegisterComponentInNewPrefab<T>(
this IContainerBuilder builder,
T prefab,
Lifetime lifetime)
where T : Component
{
return builder.RegisterComponentInNewPrefab(typeof(T), prefab, lifetime);
}

#if VCONTAINER_ECS_INTEGRATION
public readonly struct NewWorldBuilder
{
readonly IContainerBuilder containerBuilder;
Expand Down

1 comment on commit e9ab2e8

@vercel
Copy link

@vercel vercel bot commented on e9ab2e8 Jun 18, 2023

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.