public T ResolveNonSinglentonIStateObject <T>(object[] parameters = null, IIocContainerFlags flags = IIocContainerFlags.None) { var t = typeof(T); T newInstance = default(T); var stateManager = StateManager.Current; try { var isRecoveredFromStorage = (flags & IIocContainerFlags.RecoveredFromStorage) == IIocContainerFlags.RecoveredFromStorage; if (newInstance != null) { } else { newInstance = (T)ResolveUnPrepared(t, isRecoveredFromStorage); } if (!isRecoveredFromStorage) { InitializeObject(stateManager, this, (IStateObject)newInstance, parameters, t, flags); } } finally { if (newInstance is IStateObject) { stateManager.RemoveIDInResolution(((IStateObject)newInstance).UniqueID); } } return(newInstance); }
public T ResolveSinglenton <T>(object[] parameters = null, IIocContainerFlags flags = IIocContainerFlags.None) { var t = typeof(T); T newInstance = default(T); var stateManager = StateManager.Current; try { var isRecoveredFromStorage = (flags & IIocContainerFlags.RecoveredFromStorage) == IIocContainerFlags.RecoveredFromStorage; if (!isRecoveredFromStorage) { flags = flags | IIocContainerFlags.IsSinglenton; newInstance = (T)stateManager.GetObject(UniqueIDGenerator.GetSinglentonUniqueId(t)); if (newInstance != null) { return(newInstance); } if ((flags & IIocContainerFlags.SinglentonNonReturnIfExisting) == IIocContainerFlags.SinglentonNonReturnIfExisting) { return(default(T)); } //At this point the object was recovered from storage and it the RecoveredFromStorageFlags must //added to avoid unnecessary further processing of this object isRecoveredFromStorage = (newInstance != null); } if (newInstance == null) { newInstance = (T)ResolveUnPrepared(t, isRecoveredFromStorage); } if (newInstance is ILogic) { InitializeObject(stateManager, this, (ILogic)newInstance, parameters, t, isRecoveredFromStorage, flags); } else if (newInstance is IStateObject) { if (!isRecoveredFromStorage) { InitializeObject(stateManager, this, (IStateObject)newInstance, parameters, t, flags); } } } finally { if (newInstance is IStateObject) { stateManager.RemoveIDInResolution(((IStateObject)newInstance).UniqueID); } } return(newInstance); }
public object ResolveReduced(Type t, object[] parameters = null, IIocContainerFlags flags = IIocContainerFlags.None) { var typeofT = t; object newInstance = null; var isRecoveredFromStorage = (flags & IIocContainerFlags.RecoveredFromStorage) == IIocContainerFlags.RecoveredFromStorage; newInstance = newInstance ?? ResolveUnPrepared(t, isRecoveredFromStorage); if (newInstance is IViewManager) { //Nothing to do } else { if (!alreadyAssertedTypes.Contains(t)) { alreadyAssertedTypes.Add(t); Trace.TraceError("IocContainerImplWithUInity::Resolve WARNING: Type to resolved [" + t.FullName + "] is not an IStateObject, so there is no tracking or serialization available for that object"); } } return(newInstance); }
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); } } }
/// <summary> /// NOTE: this method is called *ONLY* if the object DOES not come from storage /// </summary> /// <param name="obj"></param> /// <param name="parameters"></param> /// <param name="t"></param> /// <param name="isRecovedFromStorage"></param> private static void InitializeObject(StateManager stateManager, IIocContainer container, IStateObject obj, object[] parameters, Type t, IIocContainerFlags flags) { if ((flags & IIocContainerFlags.IsSinglenton) == IIocContainerFlags.IsSinglenton) { //Unique ID obj.UniqueID = UniqueIDGenerator.GetSinglentonUniqueId(t); } else { //If it doesn't come from storage we must setup the UniqueID //that will be use later on for persistance if (!StateManager.IsRootLevelObject(obj)) { obj.UniqueID = stateManager.UniqueIDGenerator.GetUniqueIDForTemporaryObject(); //A new temparary object was created and it's in resolution. stateManager.AddIDInResolution(obj.UniqueID); } else { if (obj is IModel) { obj.UniqueID = stateManager.UniqueIDGenerator.GetUniqueIDForModel(); } else { obj.UniqueID = stateManager.UniqueIDGenerator.GetUniqueID(); } } } stateManager.AddNewObject(obj); if (obj is IDependentViewModel && (flags & IIocContainerFlags.NoBuild) != IIocContainerFlags.NoBuild) { try { ViewManager.Instance.Events.Suspend(); ((IDependentViewModel)obj).Build(container); } finally { ViewManager.Instance.Events.Resume(); } /* IDependants are now iLogic so this properties are injected in ILogic resolve code */ } }
public object Resolve(Type t, object[] parameters = null, IIocContainerFlags flags = IIocContainerFlags.None) { var typeofT = t; object newInstance = null; var stateManager = StateManager.Current; if (SurrogatesDirectory.IsSurrogateRegistered(typeofT)) { //Surrogates are created as top level objects //however reference tracking is important because surrogates with no references //are not persisted if (parameters == null || parameters.Length == 0) { //We must create a new instance, instance creation mechanism must be located from //the surrogates directory newInstance = SurrogatesDirectory.CreateInstanceFor(typeofT); } else { newInstance = parameters[0]; } var surrogate = stateManager.surrogateManager.GetSurrogateFor(newInstance, generateIfNotFound: true); return(newInstance); } try { var isRecoveredFromStorage = (flags & IIocContainerFlags.RecoveredFromStorage) == IIocContainerFlags.RecoveredFromStorage; if (!isRecoveredFromStorage) { if (t.GetCustomAttributes(typeof(Singleton), false).Length > 0) { flags = flags | IIocContainerFlags.IsSinglenton; newInstance = StateManager.Current.GetObject(UniqueIDGenerator.GetSinglentonUniqueId(t)); if ((flags & IIocContainerFlags.SinglentonNonReturnIfExisting) == IIocContainerFlags.SinglentonNonReturnIfExisting) { if (newInstance != null) { return(null); } } if ((flags & IIocContainerFlags.SinglentonReturnIfExisting) == IIocContainerFlags.SinglentonReturnIfExisting) { if (newInstance != null) { return(newInstance); } } //At this point the object was recovered from storage and it the RecoveredFromStorageFlags must //added to avoid unnecessary further processing of this object isRecoveredFromStorage = (newInstance != null); } } newInstance = newInstance ?? ResolveUnPrepared(t, isRecoveredFromStorage); if (newInstance is IViewManager) { //Nothing to do } else if (newInstance is ILogic) { InitializeObject(stateManager, this, (ILogic)newInstance, parameters, t, isRecoveredFromStorage, flags); } else if (newInstance is IStateObject) { if (!isRecoveredFromStorage) { InitializeObject(stateManager, this, (IStateObject)newInstance, parameters, t, flags); } } else { if (!alreadyAssertedTypes.Contains(t)) { alreadyAssertedTypes.Add(t); Trace.TraceError("IocContainerImplWithUInity::Resolve WARNING: Type to resolved [" + t.FullName + "] is not an IStateObject, so there is no tracking or serialization available for that object"); } } } finally { if (newInstance is IStateObject) { stateManager.RemoveIDInResolution(((IStateObject)newInstance).UniqueID); } } return(newInstance); }
public object ResolveDictionary(Type type, Func <object> lambdaCreation, object[] parameters = null, IIocContainerFlags flags = IIocContainerFlags.None) { var stateManager = StateManager.Current; IStateObject newInstance = (IStateObject)lambdaCreation(); ((IObservableDictionaryEntries)newInstance).Initialize(stateManager, stateManager.ReferencesManager, stateManager.surrogateManager); stateManager.AddNewTemporaryObject(newInstance, true); if (newInstance is IDependentViewModel && (flags & IIocContainerFlags.NoBuild) != IIocContainerFlags.NoBuild) { ((IDependentViewModel)newInstance).Build(this); /* IDependants are now iLogic so this properties are injected in ILogic resolve code */ } var isRecoveredFromStorage = (flags & IIocContainerFlags.RecoveredFromStorage) == IIocContainerFlags.RecoveredFromStorage; if (!isRecoveredFromStorage) { if ((flags & IIocContainerFlags.ExtraLean) != IIocContainerFlags.ExtraLean) { InitializationHelpers.CallInitializeCommon(newInstance, type); } //First we check to avoid unnecessary calls to GetInitMethod if (newInstance is IInitializable && !isRecoveredFromStorage && ((flags & IIocContainerFlags.Lean) != IIocContainerFlags.Lean)) { //Get Init must be done on the instance type, because logicType might refer to an interface InitializationHelpers.CallInitMethod((IInitializable)newInstance, parameters); } } return(newInstance); }