Esempio n. 1
0
        private static void InitializeObject(StateManager stateManager, IIocContainer container, ILogic logic, object[] parameters, Type logicType, bool isRecoveredFromStorage, IIocContainerFlags flags)
        {
            var  isNewLogic = true;
            bool autoWired  = false;
            bool isNoView   = false;

            //Was this an ILogicView<T>
            if (logic is ILogicView <IViewModel> )
            {
                IViewModel viewModel = null;
                isNoView = (flags & IIocContainerFlags.NoView) == IIocContainerFlags.NoView;
                //Do we need to create a view for this logic object???
                if (isNoView)
                {
                    isNewLogic = false;
                }
                else
                {
                    PropertyInfo viewModelProp = logicType.Property("ViewModel");                     //This type is fix but arbitrary
                    //Is there a view parameter? it should be the first one. If not we will consider that is a constructor parameter
                    if (parameters != null && parameters.Length > 0 && parameters[0] is IViewModel)
                    {
                        viewModel = (IViewModel)parameters[0];
                        //In case we are providing aditional parameter it determines if the logic instance is new
                        //this case can't be determine within the this method so it needs to be passed as an aditional value in the parameters
                        isNewLogic = parameters.Length > 1 ? (bool)parameters[1] : false;
                    }
                    else
                    {                       //Ok. We still dont have a view so we need to create one. We require the property to get the actual PropertyType of the logic
                        viewModel = (IViewModel)container.Resolve(viewModelProp.PropertyType);
                        try
                        {
                            ViewManager.Instance.Events.Suspend();
                            viewModel.Build(container);
                        }
                        finally
                        {
                            ViewManager.Instance.Events.Resume();
                        }
                    }
                    viewModelProp.SetValue(logic, viewModel, null);
                    if (!isRecoveredFromStorage)
                    {
                        autoWired = true;
                        ViewManager.Instance.Events.AutoWireEvents(logic);
                    }
                }
            }
            // lets inject the container
            if (logic is ICreatesObjects)
            {
                ((ICreatesObjects)logic).Container = container;
            }
            if (logic is IInteractsWithView)
            {
                ((IInteractsWithView)logic).ViewManager = ViewManager.Instance;
            }

            if (!isRecoveredFromStorage)
            {
                // lets inject all IViewModel properties
                if (logic is IStateObject)
                {
                    InitializeObject(stateManager, container, (IStateObject)logic, parameters, logicType, flags);
                    //Only Visual objects need autowire.
                    if (!autoWired && logic is IDependentViewModel)
                    {
                        ViewManager.Instance.Events.AutoWireEvents(logic);
                    }
                }
                if (isNewLogic)
                {
                    if ((flags & IIocContainerFlags.ExtraLean) != IIocContainerFlags.ExtraLean)
                    {
                        InitializationHelpers.CallInitializeCommon(logic, logicType);
                    }
                    //First we check to avoid unnecessary calls to GetInitMethod
                    if (logic is IInitializable && !isRecoveredFromStorage && !isNoView && ((flags & IIocContainerFlags.Lean) != IIocContainerFlags.Lean))
                    {
                        //Get Init must be done on the instance type, because logicType might refer to an interface
                        InitializationHelpers.CallInitMethod((IInitializable)logic, parameters);
                    }
                }
            }
            else
            {
                var dict = logic as IObservableDictionaryEntries;
                if (dict != null)
                {
                    dict.Initialize(stateManager, stateManager.ReferencesManager, stateManager.surrogateManager);
                }
            }
        }