I'm working on a Unity3D project using Zenject for dependency injection. In my project, i have a UpgradeCardButton component which is injected with a ButtonUpgradeCommand object.
ButtonUpgradeCommand is constructed with an object of type UpgradeCardVisual and an enum Enums.UpgradeType.
UpgradeCardVisual object resides in the same transform with the UpgradeCardButton object in the hieararchy, and the specific Enums.UpgradeType to be injected into the ButtonUpgradeCommand can be read from the UpgradeCardButton object via an GetUpgradeType() method.
My current implementation currently uses a FromMethod() which looks at the ParentContext and casts that object into a UpgradeCardButton and then performs generic operations to access required dependencies.
My question is, my current implementation does not really make sense as resolving these dependencies involve casting objects and using base MonoBehaviour methods, is there a more concise way to get the references to the objects which are required when working with nested dependencies?
Here are the source codes for the files involved:
UpgradesInstaller: This Zenject installer class is responsible for setting up dependency bindings.
public class UpgradesInstaller : MonoInstaller
{
public override void InstallBindings()
{
Container.Bind<IUpgradeCommand>().To<ButtonUpgradeCommand>().AsTransient();
Container.Bind<UpgradeCardVisual>().FromMethod(GetVisualFromButtonUpgrade).AsTransient();
Container.Bind<Enums.UpgradeType>().FromMethod(GetUpgradeTypeFromButtonUpgrade).AsTransient();
}
private UpgradeCardVisual GetVisualFromButtonUpgrade(InjectContext context)
{
// Get the UpgradeCardButton component from the context
UpgradeCardButton upgradeButton = context.ParentContext.ObjectInstance as UpgradeCardButton;
// Return the UpgradeCardVisual component obtained from the UpgradeCardButton
return upgradeButton.transform.GetComponent<UpgradeCardVisual>();
}
private Enums.UpgradeType GetUpgradeTypeFromButtonUpgrade(InjectContext context)
{
// Get the UpgradeCardButton component from the context
UpgradeCardButton upgradeButton = context.ParentContext.ObjectInstance as UpgradeCardButton;
// Return the upgradeType obtained from the UpgradeCardButton
return upgradeButton.GetUpgradeType();
}
}
ButtonUpgradeCommand: This class represents a command that performs upgrades and requires dependencies injected from UpgradeCardButton.
public class ButtonUpgradeCommand : IUpgradeCommand
{
// Injected dependencies
private UpgradeCardVisual _upgradeVisual;
private Enums.UpgradeType _upgradeType;
// Constructor injection
public ButtonUpgradeCommand(UpgradeCardVisual upgradeVisual, Enums.UpgradeType upgradeType)
{
_upgradeVisual = upgradeVisual;
_upgradeType = upgradeType;
}
// Methods and logic for upgrades...
}
UpgradeCardButton: This MonoBehaviour represents a button that triggers an upgrade action. It also contains dependencies to be injected into ButtonUpgradeCommand.
public class UpgradeCardButton : MonoBehaviour
{
[SerializeField] Enums.UpgradeType _upgradeType;
private IUpgradeCommand _upgradeCommand;
private IUpgradeCommand _checkCanBuyCommand;
// Constructor injection
[Inject]
public void Construct(IUpgradeCommand upgradeCommand)
{
_upgradeCommand = upgradeCommand;
}
public Enums.UpgradeType GetUpgradeType() => _upgradeType;
// Methods and logic for upgrade button behavior...
}
I am wondering if there is a more concise and efficient way to bind dependencies via using Zenject.