private void AssignGlobalControllers(ScriptEngine engine)
 {
     foreach (KeyValuePair <String, GlobalModuleController> pair in globalControllers)
     {
         engine.AddVariable(pair.Key, pair.Value);
     }
 }
        public T CreateController <T>(String moduleName, bool isGlobal = false, bool assignGlobal = false) where T : Controller, new()
        {
            TimeStamp.Start("CreateController");

            Controller controller = null;

            if (!controllers.TryGetValue(moduleName, out controller))
            {
                Stream scriptStream = null;
                ApplicationContext.Context.DAL.TryGetScriptByName(moduleName, out scriptStream);
                if (scriptStream != null && !isGlobal)
                {
                    scriptStream = ApplyMixins(moduleName, scriptStream);
                }

                //scriptStream CAN be null !!!
                ScriptEngine scriptEngine = ScriptEngine.LoadScript(scriptStream, moduleName, Debugger);

                //init script variables ant types here..
                scriptEngine.AddVariable("String", typeof(String));
                scriptEngine.AddVariable("DateTime", typeof(DateTime));
                scriptEngine.AddVariable("Workflow", new Workflow());
                scriptEngine.AddVariable("DB", new DB(scriptEngine, ApplicationContext.Context));
                scriptEngine.AddVariable("GPS", new GPS(ApplicationContext.Context.LocationProvider));
                scriptEngine.AddVariable("GPSTracking", new GPSTracking(ApplicationContext.Context.LocationTracker));
                scriptEngine.AddVariable("Gallery", new Gallery(ApplicationContext.Context, scriptEngine));

                Variables v = new Variables(ApplicationContext.Context);
                scriptEngine.AddVariable("Variables", v);
                scriptEngine.AddVariable("$", v);

                scriptEngine.AddVariable("Dialog", new Dialog(scriptEngine, ApplicationContext.Context));
                scriptEngine.AddVariable("Translate", new Translate(ApplicationContext.Context));
                scriptEngine.AddVariable("Converter", new Converter());
                scriptEngine.AddVariable("Phone", new Phone(ApplicationContext.Context));
                scriptEngine.AddVariable("FileSystem", new FileSystem(scriptEngine, ApplicationContext.Context));
                scriptEngine.AddVariable("Camera", new Camera(scriptEngine, ApplicationContext.Context));
                scriptEngine.AddVariable("Console", new BitMobile.ClientModel.Console(scriptEngine, ApplicationContext.Context));
                scriptEngine.AddVariable("BarcodeScanner", new BarcodeScanner(scriptEngine, ApplicationContext.Context));
                scriptEngine.AddVariable("Application", new ClientModel.Application(ApplicationContext.Context));
                scriptEngine.AddVariable("Clipboard", new Clipboard(ApplicationContext.Context));

                if (!isGlobal || assignGlobal)
                {
                    AssignGlobalControllers(scriptEngine);
                }

                controller = new T();
                controller.Init(scriptEngine);//, isGlobal ? null : GlobalEventsController());
                controllers.Add(moduleName, controller);
            }

            AssignBreakPoints(controller, true);

            TimeStamp.Log("CreateController", moduleName);

            return((T)controller);
        }