-
Notifications
You must be signed in to change notification settings - Fork 9
Description
Hi!
I recently discovered UniState and I wanted to give it a shot but it does not have an integration that works out of the box with Reflex DI.
Since you are already supporting VContainer and Zenject, I made my own Reflex integration and I thought it might align with the project needs. It would be nice if we can discuss about this.
In a TL:DR; way, what I've done it's to take VContainer's integration as a guide and port it to work with Reflex code since they both use ContainerBuilder patterns.
You can look at my integration code here: https://gist.github.com/CristianRos/3fbbbb7e87aec00bdf686620ec022084
There are some decisions in there that might not be obvious if you are not already familiar with Reflex like:
- Reflex implies the Lifetime in the builder function
builder.AddTransient()
andbuilder.AddSingleton()
so I decided to split them into RegisterStateMachine and RegisterStateMachineAsSingleton. Same with RegisterState implementations. - Reflex doesn't have an
.AsSelf().AsImplementedInterfaces();
implementation so I made a helper functionGetContractsAndSelf();
. If I understood right what that does it's to take all the state associated interfaces and bind them to the actual state and self.
I actually tried to test it with the Dice game tutorial you posted in the Readme.
This is what shows up in the Reflex debugger. That's all the bindings to each state. I'm not sure that's desired, but hey it works!
- Reflex doesn't have a RegisterEntryPoint either so I ended up creating a bootstrapper and injecting the EntryPoint:
DiceInstaller
using Reflex.Core;
using UniState;
using UnityEngine;
public class DiceInstaller : MonoBehaviour, IInstaller
{
public void InstallBindings(ContainerBuilder builder)
{
builder.RegisterStateMachine<IStateMachine, StateMachine>();
builder.AddSingleton(container =>
{
IStateMachine stateMachine = container.Resolve<IStateMachine>();
return new DiceEntryPoint(stateMachine);
}, typeof(DiceEntryPoint));
builder.RegisterState<StartGameState>();
builder.RegisterState<RollDiceState>();
builder.RegisterState<LostState>();
builder.RegisterState<WinState>();
}
}
DiceGameBootstrapper
using Reflex.Attributes;
using UnityEngine;
public class DiceGameBootstrapper : MonoBehaviour
{
[Inject] readonly DiceEntryPoint _diceEntryPoint;
void Awake()
{
_diceEntryPoint.Start();
}
}
Everything else it's literally a copy pasted version of your tutorial to test everything works alright and I believe it does, at least I do get the Dice game example working.
I also omitted the preprocessor directive #if UNISTATE_REFLEX_SUPPORT
as it isn't convenient for my use-case right now and I'm also not familiarized with how to set it up.
Let me know what do you think, I'll be glad to contribute and help!