BindInterfacesTo and BindInterfacesAndSelfTo - mariaheine/Zenject-But-Wiki GitHub Wiki

If you end up using the ITickable, IInitializable, and IDisposable interfaces as described above, you will often end up with code like this:

Container.Bind(typeof(Foo), typeof(IInitializable), typeof(IDisposable)).To<Logger>().AsSingle();

This can be a bit verbose sometimes. Also, it is not ideal because if I later on decide that Foo doesn't need a Tick() or a Dispose() then I have to keep the installer in sync.

A better idea might be to just always use the interfaces like this:

Container.Bind(new[] { typeof(Foo) }.Concat(typeof(Foo).GetInterfaces())).To<Foo>().AsSingle();

This pattern is useful enough that Zenject includes a custom bind method for it. The above code is equivalent to:

Container.BindInterfacesAndSelfTo<Foo>().AsSingle();

Now, we can add and remove interfaces to/from Foo and the installer remains the same.

In some cases you might only want to bind the interfaces, and keep Foo hidden from other classes. In that case you would use the BindInterfacesTo method instead:

Container.BindInterfacesTo<Foo>().AsSingle()

Which, in this case, would expand to:

Container.Bind(typeof(IInitializable), typeof(IDisposable)).To<Foo>().AsSingle();
⚠️ **GitHub.com Fallback** ⚠️