robotlegs-sharp / robotlegs-sharp-framework

An C# application framework. Ported from Robotlegs for ActionScript 3.
MIT License
84 stars 25 forks source link

Mapping Models and services with MultiModule architecture (Unity3d) #8

Closed Dukobpa3 closed 8 years ago

Dukobpa3 commented 8 years ago

So, subj :)

Can you explayn best practice for mapping something as "crosscontext". Modularity extension working well with commands, events and other "onetime things". But how I can share some data-objects between contexts? As I know, bindings can be found in parent context, but In much cases I can't make nested contexts. It should be different root views even in one scene. And in the real project is much more cases when crosscontext mappings needed.

prankard commented 8 years ago

Hey Alexander,

Multiple context isn't something that I personally use. To me it's a tool that is used when almost full applications are complete, and need to live together. For me the issue it solves is mapping the same interfaces to different values. But I would rather re-write the interfaces and values to be more flexible so I can use it in more than 1 scenario in 1 mapping than deal with the complexities of multiple context communication if I have to.

Once at work in Robotlegs for AS3 we almost had our first 2 context application. 1 for a preloader (for a lot of similar projects), then another context for the rest of the application. Then we would re-use the preloader context is other projects. But we realised we could make a child injector, map that to a singleton and just use that for a preloader values we needed. Then dispose of that injector when we were done. It was much simpler than dealing with 2 contexts at the time.

Sharing mapping is done by a parent injector. (or parent context, which contains a parent injector). This is handled automatically with the heirchy of context view's but can be done manually to have different gameobject heirchy. However, the child context's need to be uninitialized before being added as a child. You can see this in the example scene MultiContextDifferentRoots in my fork of the hello world repository.

So to share models across child context's. Map models as singletons to parent contexts. And then request them in commands in child context's.

I hope this helps.

Dukobpa3 commented 8 years ago

In as3 I was using multiple contexts when was working on modularity apps. E.g. some "lobby app", which loads "plugin apps".

With unity I've tried to make each scene as different context with the same logic as explained above. In the first scene I have one object marked as "DontDestroyOnLoad" and there "RootContext" with few global mappings.

For now I've remade it all as SingleContext app. But would you explain some recommendations how to combine multicontexts with single contexts? Where are differents?

Dukobpa3 commented 8 years ago

As I understand and as I was using multicontext in as3 - multicontexts is usefull just for messaging. Also with flash we always has root context. But unity have some differences in its architecture and we need some workarounds.

prankard commented 8 years ago

Just to clarify if it's not clear. I've modifed the UnitySingleContextBundle to have all views to be mapped to that context regardless of where they are in the heirchy. Meaning they don't have to be parented at all.

So with that ContextView + context attached to a GameObject. Attach a "DontDestroyOnLoad" flag (like you have done in yours). And it should all map to that one context.

Have a look at the snake-game example. Is how I would go about using the mutliple scenes with one context in Unity. In this example, the context is actuall created at boot of the app using the "RuntimeInitializeOnLoadMethod" attribute: https://github.com/robotlegs-sharp/unity-hello-game

This way, I can test each scene and be sure the context has booted. The downside to this is the root context and initializing the data has to be created in code.

For me, I don't usually use Scenes, but use Resouces.Load instead to change page/state in my small apps. So I only have 1 scene, and on boot it will change to the correct page/state in my code via commands.

The differences between the MutliContext and SingleContext bundles for Unity are as follows:

SingleContext

Multi Context

I've tested combining MultiContext together before, but not tried combing SingleContext with MultiContext together. I've only named them as such as I thought users would start with 'I want to start a multi context app' or 'I want to start a single context app' so I named them as such.

It sounds like your example is AS3 is a pretty solid example of when Multi Context can be useful. It sounds a lot like a menu for different contained games where you can load each seperate game as a new context which might use the same Interfaces mapped differently.

Although you did use the word 'plugin' in 'pluginapps' which sounds like it could be a plugin of code, in which case you should be using an extenison.

If you wanted to do something like that. I would suggest making a RootContext, maybe by installing a UnitySingleContextBundle and adding the Modularity Extension to it and parenting it's children. If that is what you'd like (and I've understood correctly) I'll have a look at tweaking a bundle to work as a 'RootContext'.

Dukobpa3 commented 8 years ago

Greate feature of scenes is their ability to destroy all content on unloading. And at this point would be useful to make each scene as different contexts which can be inited on loading and destroyed on unloading. And I don't need to manage content myself as you explained above (Resources.Load). But we always still need some things which should not be destroyed. It can be some servises, models, etc.

I will try to make it with combination SingleContexts and MultiContexts. Thanks for your explanation. I've got point about FallbackContainerConfig and ContextViewListenerConfig differences and it seems like what I need.

prankard commented 8 years ago

Thanks, good luck with your project. Since this I've started to use the multi scene aspect in other projects. It works fairly well, I might include some more scene extensions with the base Unity Framework later.

Dukobpa3 commented 8 years ago

Good News :) Also I have some improvements for this but it is not tested properly. maybe I will share it to public later.