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); }
internal void RegisterType(Type typeToRegister) { #if DETAILED_DEBUG Trace.TraceInformation("Processing type " + typeToRegister.FullName); #endif if (typeToRegister != null && typeof(IStateObject).IsAssignableFrom(typeToRegister) && //This is to make eliminating false cases faster (typeof(IDependentModel).IsAssignableFrom(typeToRegister) || typeof(IDependentViewModel).IsAssignableFrom(typeToRegister) || typeof(IViewModel).IsAssignableFrom(typeToRegister) || (typeof(IModel).IsAssignableFrom(typeToRegister)))) { try { if (!typeToRegister.IsAbstract && !typeToRegister.IsInterface) { if (typeof(NoInterception).IsAssignableFrom(typeToRegister)) { TraceUtil.TraceInformation("Excluding type from interception " + typeToRegister.FullName); non_interceptedTypes.Add(typeToRegister); } else { interceptedTypes.Add(typeToRegister); if (!typeToRegister.IsGenericType) { InitializationHelpers.LoadTypeHierarchy(typeToRegister); } } TypeCacheUtils.AddClientTypeRegistration(typeToRegister); } } catch (Exception registerTypeException) { Trace.TraceError("Bootstrapper::RegisterAllTypes problem while registering type " + typeToRegister.FullName + " Message " + registerTypeException.Message); } } }
/// <summary> /// Performs several initialization tasks, related to the IoCContainer setup /// All types are registered here for interception. /// </summary> /// <returns></returns> public static void Initialize(Assembly mainAssembly = null) { //System.Web.HttpContext.Current.ApplicationInstance.BeginRequest -= ApplicationInstance_BeginRequest; //System.Web.HttpContext.Current.ApplicationInstance.BeginRequest += ApplicationInstance_BeginRequest; if (initialized) { return; } Debug.WriteLine("WebMap Application not initialized. Starting initialization"); TypePropertiesCache.SetupInterceptionDelegates( new InterceptionDelegates() { isASupportedValueTypeForIListDelegate = TypeCacheUtils.IsSupportedGenericTypeForList, ProcessGetterNoAction = LazyBehaviours.ProcessGetterNoAction, ProcessGetterNonTopLevelIStateObject = LazyBehaviours.ProcessGetterNonTopLevelIStateObject, ProcessGetterStrongReference = LazyBehaviours.ProcessGetterStrongReference, ProcessGetterSurrogate = LazyBehaviours.ProcessGetterSurrogate, ProcessGetterTopLevelIStateObject = LazyBehaviours.ProcessGetterTopLevelIStateObject, ProcessGetterWeakReference = LazyBehaviours.ProcessGetterWeakReference, ProcessSetterMostCases = LazyBehaviours.ProcessSetterMostCases, ProcessSetterObjectReference = LazyBehaviours.ProcessSetterObjectReference, ProcessSetterSimpleTypes = LazyBehaviours.ProcessSetterSimpleTypes, ProcessSetterStrongReference = LazyBehaviours.ProcessSetterStrongReference, ProcessSetterSurrogate = LazyBehaviours.ProcessSetterSurrogate, ProcessSetterWeakReference = LazyBehaviours.ProcessSetterWeakReference, ProcessSetterVisibleState = LazyBehaviours.ProcessSetterVisibleState }); SurrogatesDirectory.TypeToContractedString = TypeCacheUtils.AssemblyQualifiedNameCache; SurrogatesDirectory.ContractedStringToType = TypeCacheUtils.GetType; var aliasConfig = System.Configuration.ConfigurationManager.AppSettings["UniqueIDAliasEnabled"]; var aliasEnabled = false; if (aliasConfig != null) { aliasEnabled = Convert.ToBoolean(aliasConfig); } TypePropertiesCache.ALIAS_ENABLED = aliasEnabled; //Connect delegates that are required by Dictionary implementations DictionaryUtils.Current = StateManager.GetCurrent; DictionaryUtils.CreatePromise = EventPromiseInfo.CreateEventInstancePromise; DictionaryUtils.RetrieveDelegateFromPromise = PromiseUtils.RetrieveDelegateFromPromise; DictionaryUtils.GetObjectContainingMethod = EventPromiseInfo.GetObjectContainingMethod; MEFManager.PlatformInitializer().Initialize(); BaseClassFindInit._findMethod = (_baseType, parameters, types) => { Type[] paramsTypes = new Type[] { }; if (types != null) { paramsTypes = types; } var initCandidates = new List <MethodInfo>(); var methods = _baseType.GetMethods(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly); foreach (var methodInfo in methods) { if (InitializationHelpers.IsMethodValidCandidate(methodInfo, paramsTypes)) //explicit interface implementation { initCandidates.Add(methodInfo); } } var selectedInit = InitializationHelpers.GetSelectedInit(initCandidates, paramsTypes); return(selectedInit); }; IocContainerImplWithUnity.InitializeResolver(); var container = IocContainerImplWithUnity.Current; RegisterAllTypes(container, mainAssembly); TypeCacheUtils.LoadClientTypeMetadataTable(); initialized = true; Debug.WriteLine("WebMap Runtime Initialization DONE!"); }
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> /// Processes all given types to register metadata and interception information needed by the /// WebMap Helpers infraestructure /// </summary> /// <param name="typesToRegister"></param> internal void RegisterTypes(IEnumerable <Type> typesToRegister) { foreach (var typeToRegister in typesToRegister) { if (!typeToRegister.IsAbstract || typeToRegister.IsInterface) { RegisterType(typeToRegister); } } var copyOfinterceptedTypes = interceptedTypes.ToArray(); #if !BACKGROUNDTASK System.Threading.Tasks.Task.Run(() => { backgroundPauser.Wait(); Trace.WriteLine("Starting background generation of proxy types"); List <Type> interceptedTypesEx = new List <Type>(); int counter = 0; foreach (var typeToRegister in copyOfinterceptedTypes) { backgroundPauser.Wait(); Type interceptedType = null; try { Trace.WriteLine("Building proxy for " + (counter++) + " of " + copyOfinterceptedTypes.Length + " " + typeToRegister.FullName); interceptedType = Intercept.PreLoadProxyType(typeToRegister); interceptedTypesEx.Add(interceptedType); } catch { //Trace.WriteLine("Error Building proxy for " + typeToRegister.FullName); } //ThreadPool.QueueUserWorkItem((a) => { // backgroundPauser.Wait(); // try // { // foreach(var prop in TypePropertiesCache.GetArrayPropertiesOrderedByIndex(interceptedType)) // { // var temp = prop.JsonProperty; // } // } // catch // { // } // Trace.WriteLine("Done processing jsonproperties for " + typeToRegister.FullName); //}); try { //Trace.WriteLine("Registering TypeHierarchy for " + typeToRegister.FullName); if (!typeToRegister.IsGenericType) { InitializationHelpers.LoadTypeHierarchy(typeToRegister); } } catch { //Trace.WriteLine("Error TypeHierarchy for " + typeToRegister.FullName); } } }); #endif // Trace.WriteLine("End background generation of proxy types"); // /* System.Threading.Tasks.Parallel.ForEach(interceptedTypes,(interceptedType) => // { // try // { // PropertySkipperContractResolver.CommonInstanceClient.ResolveContract(interceptedType); // } // catch // { // Trace.WriteLine("Error with property skipper preload"); // } // });*/ //}); }