在我的应用程序中,我有三个层:*数据(实体和数据访问对象)*模型(管理人员)*表示(视图)

我决定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内部,我将GameAppManager添加到图中。
public class GameModelLayerInstaller : MonoInstaller
{
public GameAppManager GameAppManager;
public override void InstallBindings()
{
Container.BindInstance(GameAppManager);
Container.QueueForInject(GameAppManager);
}
}在演示文稿安装程序中,我将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有什么更好的方法吗?
发布于 2017-10-26 12:28:44
我对正在发生的事情最好的猜测是GameApp被注射了两次。一次通过表示容器,一次通过SceneContext容器。默认情况下,场景中的任何MonoBehaviours都被假定为属于SceneContext,除非它们在GameObjectContext中。所以你是对的,我认为你想要做的最好的方法就是使用GameObjectContext。通常你不需要自己打电话给DiContainer.CreateSubContainer。
如果您已经在启动时获得了希望在子容器中的MonoBehaviours,那么您可以直接为这些对象添加一个GameObjectContext父转换。或者,如果它们是动态创建的,那么您可以将其中一个FromSubContainerResolve.ByNewPrefabX方法用作工厂或直接在场景容器上使用。
https://stackoverflow.com/questions/46932959
复制相似问题