在我的应用程序中,我有三层:* 数据(实体和数据访问对象)* 模型(管理器)* 演示(视图)
我决定这SubContainers
是我的选择。
在根目录GameInstaller
中,我创建每个容器并使用 LayerInstallers 手动安装它们:
public class GameInstaller : MonoInstaller
{
public GameDataLayerInstaller DataLayerInstaller;
public GameModelLayerInstaller ModelLayerInstaller;
public GamePresentationLayerInstaller PresentationLayerInstaller;
public override void InstallBindings()
{
var dataContainer = Container.CreateSubContainer();
dataContainer.Inject(DataLayerInstaller);
DataLayerInstaller.InstallBindings();
var modelContainer = dataContainer.CreateSubContainer();
modelContainer.Inject(ModelLayerInstaller);
ModelLayerInstaller.InstallBindings();
var presentationContainer = modelContainer.CreateSubContainer();
presentationContainer.Inject(PresentationLayerInstaller);
PresentationLayerInstaller.InstallBindings();
}
}
在 Model-Installer 内部,我添加GameAppManager
到图表中。
public class GameModelLayerInstaller : MonoInstaller
{
public GameAppManager GameAppManager;
public override void InstallBindings()
{
Container.BindInstance(GameAppManager);
Container.QueueForInject(GameAppManager);
}
}
在 Presentation-Installer 中,我将 GameApp 添加到图表中。
public class GamePresentationLayerInstaller : MonoInstaller
{
public GameApp GameApp;
public override void InstallBindings()
{
Container.BindInstance(GameApp);
Container.QueueForInject(GameApp);
}
}
然后我试图从GameApp.InjectDependencies(...)
方法中解决它:
public class GameApp : MonoBehaviour
{
private GameAppManager _appManager;
[Inject]
public void InjectDependencies(GameAppManager appManager)
{
_appManager = appManager;
}
}
但是 Zenject 抛出了这个异常:
ZenjectException: Unable to resolve type 'RsQuest.Game.Model.App.GameAppManager' while building object with type 'RsQuest.Game.Presentation.App.GameApp'.
Object graph:
GameApp
我该如何处理这种情况?GameObjectContext 有没有更好的方法?