gustavopsantos / Reflex

Minimal dependency injection framework for Unity
MIT License
730 stars 51 forks source link

Need HasBinding<T>for Container #31

Closed phields closed 1 year ago

phields commented 1 year ago

Is your feature request related to a problem? Please describe. I'm often unsure if a certain Type has already been bound to a ContainerDescriptor in GitHub, which leads to confusion and potential errors when working with the code.

Describe the solution you'd like I would like the author to add a new interface to the ContainerDescriptor type called HasBinding, which would allow users to query whether a certain Type T has already been bound to the ContainerDescriptor.

Describe alternatives you've considered I have considered manually checking whether a Type has been bound, but this is time-consuming and error-prone.

Additional context N/A

gustavopsantos commented 1 year ago

@phields In which cases do you need that kind of API? For me its strange to have to check if something was bound or not, as it should be deterministic. Or you you have some kind of feature flags, and you want to check if some feature were enabled or not?

phields commented 1 year ago

I'm making bindings of Reflex for MessagePipe, and there are builder.HasBinding and builder.Exists functions in the VContainer and Zenject instances to check if the types have been bind. https://github.com/Cysharp/MessagePipe/blob/master/src/MessagePipe.Unity/Assets/Plugins/MessagePipe.Zenject/Runtime/DiContainerExtensions.cs https://github.com/Cysharp/MessagePipe/blob/master/src/MessagePipe.Unity/Assets/Plugins/MessagePipe.VContainer/Runtime/ContainerBuilderExtensions.cs

gustavopsantos commented 1 year ago

Hmmm, I see the usage right now, i'll be adding and releasing a new version with them later today ;)

gustavopsantos commented 1 year ago

@phields Added in latest release version 4.2.0 https://github.com/gustavopsantos/Reflex/releases/tag/4.2.0

phields commented 1 year ago

@gustavopsantos Thanks, I saw your update. I have one more question, I noticed that ProjectContainer and SceneContainer are Build() automatically, I was wondering if there is a way to get to their build object Container? like this:

        public void InstallBindings(ContainerDescriptor descriptor)
        {
                descriptor.BindMessagePipe(pipeOptions =>
            {
                pipeOptions.InstanceLifetime = InstanceLifetime.Singleton;
                pipeOptions.RequestHandlerLifetime = InstanceLifetime.Singleton;

            } );
            descriptor.onContainerCreated += container => GlobalMessagePipe.SetProvider(container.AsServiceProvider());
        }
       public Container Build()
        {
            Build(out var disposables, out var resolversByContract);
            var container = new Container(_name, resolversByContract, disposables);
            container.SetParent(_parent);

            // Clear references
            _name = null;
            _parent = null;
            _descriptors = null;

            // Call initializers
            foreach (var startable in container.All<IStartable>())
            {
                startable.Start();
            }
            onContainerCreated?.Invoke(container);
            return container;
        }
gustavopsantos commented 1 year ago

@phields Ping me in discord, there we can discuss about it with more ease. Gustavo Santos#9632