public override void BuildUp(IObjectBuildContext ctx) { if (ctx.SkipCreationPolicy) { _log.Verbose("Factory policy is skipped."); return; } IReflector r = Reflector.Bind(_factoryType, ReflectorPolicy.TypeAll); MethodBase[] methods = FilterByHintsAndSort(r.GetMethods(_methodName)); if (methods.Length == 0) ExceptionHelper.ThrowNoValidMethod(_factoryType, _methodName); foreach (MethodBase method in methods) { if (IsReady(method, ctx)) { object[] args = PrepareArgs(ctx, method); object instance = r.Invoke(_methodName, args); ctx.Instance = instance; _log.Verbose("Factory policy create an instance of type [{0}].", instance.GetType()); return; } } _log.Debug("Factory policy create instance fail."); ExceptionHelper.ThrowMethodNotReady(_factoryType, _methodName); }
public override void BuildUp(IObjectBuildContext ctx) { if (ctx.SkipCreationPolicy) { _log.Verbose("Activator policy is skipped."); return; } ConstructorInfo[] ctors = ctx.ComponentType.GetConstructors(); MethodBase[] methods = FilterByHintsAndSort(ctors); if (methods.Length == 0) ExceptionHelper.ThrowNoValidConstructor(ctx.ComponentType); foreach (MethodBase method in methods) { if (IsReady(method, ctx)) { object[] args = PrepareArgs(ctx, method); object instance = Activator.CreateInstance(ctx.ComponentType, args); ctx.Instance = instance; _log.Verbose("Activator policy create an instance of type [{0}].", instance.GetType()); return; } } _log.Debug("Activator policy create instance fail."); ExceptionHelper.ThrowConstructorNotReady(ctx.ComponentType); }
private object Lookup(IComponentInfo info) { _log.Info("Start build object [{0}].", info.Id); try { IObjectBuildContext ctx = _b.Build(info); if (ctx.LifeCycleManagement) { _lifecycleContainer[ctx.Instance] = ctx; } _log.Info("Build object [{0}] success.", info.Id); return(ctx.Instance); } catch (ObjectBuilderException) { _log.Warning("Build object [{0}] fail.", info.Id); throw; } catch (ManifestException) { _log.Warning("Build object [{0}] fail.", info.Id); throw; } catch (Exception ex) { _log.Warning("Build object [{0}] fail.", info.Id); ex.CreateWrapException <ObjectBuilderException>(); return(null); } }
public override void BuildUp(IObjectBuildContext ctx) { if (ctx.SkipCreationPolicy) { _log.Verbose("Factory policy is skipped."); return; } IReflector r = Reflector.Bind(_factoryType, ReflectorPolicy.TypeAll); MethodBase[] methods = FilterByHintsAndSort(r.GetMethods(_methodName)); if (methods.Length == 0) { ExceptionHelper.ThrowNoValidMethod(_factoryType, _methodName); } foreach (MethodBase method in methods) { if (IsReady(method, ctx)) { object[] args = PrepareArgs(ctx, method); object instance = r.Invoke(_methodName, args); ctx.Instance = instance; _log.Verbose("Factory policy create an instance of type [{0}].", instance.GetType()); return; } } _log.Debug("Factory policy create instance fail."); ExceptionHelper.ThrowMethodNotReady(_factoryType, _methodName); }
public static object Build(string val, Type targetType, IObjectBuildContext ctx) { //Note:If it is injection, save it in to ctx.InjectedParameters if (string.IsNullOrEmpty(val)) { object result = ctx.Kernal.Lookup(targetType); ctx.InjectedParameters.Add(result); return(result); } if (val.BracketedBy(StringPair.Template_Bracket)) { string id = val.UnBracketing(StringPair.Template_Bracket); if (id == NULL) { return(null); } object result = ctx.Kernal.Lookup(id); ctx.InjectedParameters.Add(result); return(result); } else { return(TypeCast.ChangeToTypeOrNullableType(val, targetType)); } }
public bool ReadyToBuild(IObjectBuildContext ctx) { Type targetType = (ctx.ComponentType != null) ? ctx.ComponentType : ctx.ServiceType; IReflector r = Reflector.Bind(targetType, ReflectorPolicy.TypePublic); return(PolicyUtility.IsReady(_propertyValue, r.GetPropertyType(_propertyName), ctx)); }
public override void BuildUp(IObjectBuildContext ctx) { if (ctx.SkipCreationPolicy) { _log.Verbose("Activator policy is skipped."); return; } ConstructorInfo[] ctors = ctx.ComponentType.GetConstructors(); MethodBase[] methods = FilterByHintsAndSort(ctors); if (methods.Length == 0) { ExceptionHelper.ThrowNoValidConstructor(ctx.ComponentType); } foreach (MethodBase method in methods) { if (IsReady(method, ctx)) { object[] args = PrepareArgs(ctx, method); object instance = Activator.CreateInstance(ctx.ComponentType, args); ctx.Instance = instance; _log.Verbose("Activator policy create an instance of type [{0}].", instance.GetType()); return; } } _log.Debug("Activator policy create instance fail."); ExceptionHelper.ThrowConstructorNotReady(ctx.ComponentType); }
public void TearDown(object instance, IObjectBuildContext ctx) { IDisposable d = instance as IDisposable; if (d != null) { d.Dispose(); } }
public void BuildUp(IObjectBuildContext ctx) { if (ctx.SkipCreationPolicy) { _log.Verbose("Instance policy is skipped."); return; } _log.Verbose("Instance policy return instance of type [{0}].", _instance.GetType()); ctx.Instance = _instance; }
public void BuildUp(IObjectBuildContext ctx) { if (ctx.SkipCreationPolicy) { _log.Verbose("Instance policy is skipped."); return; } _log.Verbose("Instance policy return instance of type [{0}].", _instance.GetType()); ctx.Instance = _instance; }
public void BuildUp(IObjectBuildContext ctx) { if (ctx.SkipInitializationPolicy) { return; } IReflector r = Reflector.Bind(ctx.Instance); object val = PolicyUtility.Build(_propertyValue, r.GetPropertyType(_propertyName), ctx); r.SetPropertyValue(_propertyName, val); }
public void TearDown(object instance, IObjectBuildContext ctx) { //Note:Reverse order to teardown _log.Debug("Teardown [PostInitialization] policies."); TearDownSteps(instance, ctx, Stages.PostInitialization); _log.Debug("Teardown [Initialization] policies."); TearDownSteps(instance, ctx, Stages.Initialization); _log.Debug("Teardown [Creation] policies."); TearDownSteps(instance, ctx, Stages.Creation); _log.Debug("Teardown [PreCreation] policies."); TearDownSteps(instance, ctx, Stages.PreCreation); _log.Debug("Teardown complete."); }
public virtual void TearDown(object instance, IObjectBuildContext ctx) { if (!ctx.LifeCycleManagement || !_teardown) return; if (ctx.SkipCreationPolicy) return; foreach (object injection in ctx.InjectedParameters) { ctx.Kernal.TearDown(injection); } }
public void TearDown(object instance, IObjectBuildContext ctx) { //Note:Reverse order to teardown _log.Debug("Teardown [PostInitialization] policies."); TearDownSteps(instance, ctx, Stages.PostInitialization); _log.Debug("Teardown [Initialization] policies."); TearDownSteps(instance, ctx, Stages.Initialization); _log.Debug("Teardown [Creation] policies."); TearDownSteps(instance, ctx, Stages.Creation); _log.Debug("Teardown [PreCreation] policies."); TearDownSteps(instance, ctx, Stages.PreCreation); _log.Debug("Teardown complete."); }
private static void OnObjectBuilt(IObjectBuildContext ctx) { var instance = ctx.ObjectInstance; if (instance == null) { return; } SetObjectSetting(ctx.ServiceLocator, ctx.ObjectSetting, instance); if (!RemotingServices.IsTransparentProxy(instance) && instance is IInitializable) { ((IInitializable)instance).Init(); } }
public override bool ReadyToBuild(IObjectBuildContext ctx) { ConstructorInfo[] ctors = ctx.ComponentType.GetConstructors(); MethodBase[] methods = FilterByHintsAndSort(ctors); if (methods.Length == 0) return false; foreach (MethodBase method in methods) { if (IsReady(method, ctx)) return true; } return false; }
public override bool ReadyToBuild(IObjectBuildContext ctx) { IReflector r = Reflector.Bind(_factoryType, ReflectorPolicy.TypePublic); MethodBase[] methods = FilterByHintsAndSort(r.GetMethods(this._methodName)); if (methods.Length == 0) return false; foreach (MethodBase method in methods) { if (IsReady(method, ctx)) return true; } return false; }
private static void BuildUpSteps(IObjectBuildContext ctx, Stages stage) { try { for (int idx = 0; idx < ctx.Policies.Count; idx++) { if (ctx.Policies[idx].Stage == stage) ctx.Policies[idx].BuildUp(ctx); } } catch (Exception ex) { ExceptionHelper.ThrowBuildUpError(ex, stage); } }
public void BuildUp(IObjectBuildContext ctx) { if (_instance != null) { _log.Verbose("Singleton policy already hold an instance."); ctx.Instance = _instance; ctx.SkipCreationPolicy = true; ctx.SkipInitializationPolicy = true; ctx.SkipPostInitializationPolicy = true; } else { _log.Verbose("Singleton policy hold nothing, pass to next policy."); } }
public static bool IsReady(string val, Type targetType, IObjectBuildContext ctx) { if (string.IsNullOrEmpty(val)) return ctx.Kernal.ReadyToBuild(targetType); if (val.BracketedBy(StringPair.Template_Bracket)) { string id = val.UnBracketing(StringPair.Template_Bracket); if (id == NULL) return true; else return ctx.Kernal.ReadyToBuild(id); } else return true; }
public void BuildUp(IObjectBuildContext ctx) { if (_instance != null) { _log.Verbose("Singleton policy already hold an instance."); ctx.Instance = _instance; ctx.SkipCreationPolicy = true; ctx.SkipInitializationPolicy = true; ctx.SkipPostInitializationPolicy = true; } else { _log.Verbose("Singleton policy hold nothing, pass to next policy."); } }
private static void TearDownSteps(object instance, IObjectBuildContext ctx, Stages stage) { try { for (int idx = ctx.Policies.Count - 1; idx >= 0; idx--) { if (ctx.Policies[idx].Stage == stage) { ctx.Policies[idx].TearDown(instance, ctx); } } } catch (Exception ex) { ExceptionHelper.ThrowTearDownError(ex, stage); } }
private static void BuildUpSteps(IObjectBuildContext ctx, Stages stage) { try { for (int idx = 0; idx < ctx.Policies.Count; idx++) { if (ctx.Policies[idx].Stage == stage) { ctx.Policies[idx].BuildUp(ctx); } } } catch (Exception ex) { ExceptionHelper.ThrowBuildUpError(ex, stage); } }
public virtual void TearDown(object instance, IObjectBuildContext ctx) { if (!ctx.LifeCycleManagement || !_teardown) { return; } if (ctx.SkipCreationPolicy) { return; } foreach (object injection in ctx.InjectedParameters) { ctx.Kernal.TearDown(injection); } }
public override bool ReadyToBuild(IObjectBuildContext ctx) { ConstructorInfo[] ctors = ctx.ComponentType.GetConstructors(); MethodBase[] methods = FilterByHintsAndSort(ctors); if (methods.Length == 0) { return(false); } foreach (MethodBase method in methods) { if (IsReady(method, ctx)) { return(true); } } return(false); }
public void OnBuildComplete(IObjectBuildContext ctx) { lock (_lock) { if (_instance == null) _instance = ctx.Instance; else { if (!Object.ReferenceEquals(_instance, ctx.Instance)) { //Note:Another thread create an instance already, release instance build by this thread ctx.Kernal.TearDown(ctx.Instance); //Note:And return the instance created by other thread. ctx.Instance = _instance; } } } }
protected bool IsReady(MethodBase method, IObjectBuildContext ctx) { foreach (ParameterInfo pInfo in method.GetParameters()) { string key = pInfo.Name; if ((!_injectionHints.ContainsKey(key)) && pInfo.IsOptional) continue; else { string val = null; _injectionHints.TryGetValue(key, out val); if (!PolicyUtility.IsReady(val, pInfo.ParameterType, ctx)) return false; } } return true; }
public override bool ReadyToBuild(IObjectBuildContext ctx) { IReflector r = Reflector.Bind(_factoryType, ReflectorPolicy.TypePublic); MethodBase[] methods = FilterByHintsAndSort(r.GetMethods(this._methodName)); if (methods.Length == 0) { return(false); } foreach (MethodBase method in methods) { if (IsReady(method, ctx)) { return(true); } } return(false); }
public void OnBuildComplete(IObjectBuildContext ctx) { lock (_lock) { if (_instance == null) { _instance = ctx.Instance; } else { if (!Object.ReferenceEquals(_instance, ctx.Instance)) { //Note:Another thread create an instance already, release instance build by this thread ctx.Kernal.TearDown(ctx.Instance); //Note:And return the instance created by other thread. ctx.Instance = _instance; } } } }
protected object[] PrepareArgs(IObjectBuildContext ctx, MethodBase method) { ParameterInfo[] pInfos = method.GetParameters(); object[] args = new object[pInfos.Length]; string val; for (int idx = 0; idx < args.Length; idx++) { ParameterInfo pInfo = pInfos[idx]; bool hint = _injectionHints.TryGetValue(pInfo.Name, out val); if (!hint && pInfo.IsOptional) { args[idx] = pInfo.DefaultValue; } else { args[idx] = PolicyUtility.Build(val, pInfo.ParameterType, ctx); } } return(args); }
public void TearDown(object instance, IObjectBuildContext ctx) { if (!ctx.LifeCycleManagement || !_teardown) { return; } if (ctx.SkipInitializationPolicy) { return; } //Note:Not care about "direct casted value", it's not coming from container. if (_propertyValue == null || (_propertyValue.StartsWith("#{") && _propertyValue.EndsWith("}"))) { IReflector r = Reflector.Bind(ctx.Instance); object val = r.GetPropertyValue(_propertyName); if (!val.GetType().IsValueType) { r.SetPropertyValue(_propertyName, null); } ctx.Kernal.TearDown(val); } }
protected bool IsReady(MethodBase method, IObjectBuildContext ctx) { foreach (ParameterInfo pInfo in method.GetParameters()) { string key = pInfo.Name; if ((!_injectionHints.ContainsKey(key)) && pInfo.IsOptional) { continue; } else { string val = null; _injectionHints.TryGetValue(key, out val); if (!PolicyUtility.IsReady(val, pInfo.ParameterType, ctx)) { return(false); } } } return(true); }
public static object Build(string val, Type targetType, IObjectBuildContext ctx) { //Note:If it is injection, save it in to ctx.InjectedParameters if (string.IsNullOrEmpty(val)) { object result = ctx.Kernal.Lookup(targetType); ctx.InjectedParameters.Add(result); return result; } if (val.BracketedBy(StringPair.Template_Bracket)) { string id = val.UnBracketing(StringPair.Template_Bracket); if (id == NULL) return null; object result = ctx.Kernal.Lookup(id); ctx.InjectedParameters.Add(result); return result; } else { return TypeCast.ChangeToTypeOrNullableType(val, targetType); } }
public static bool IsReady(string val, Type targetType, IObjectBuildContext ctx) { if (string.IsNullOrEmpty(val)) { return(ctx.Kernal.ReadyToBuild(targetType)); } if (val.BracketedBy(StringPair.Template_Bracket)) { string id = val.UnBracketing(StringPair.Template_Bracket); if (id == NULL) { return(true); } else { return(ctx.Kernal.ReadyToBuild(id)); } } else { return(true); } }
public override void OnBuildComplete(IObjectBuildContext ctx) { //Note:Nothing to do. }
public abstract bool ReadyToBuild(IObjectBuildContext ctx);
protected object[] PrepareArgs(IObjectBuildContext ctx, MethodBase method) { ParameterInfo[] pInfos = method.GetParameters(); object[] args = new object[pInfos.Length]; string val; for (int idx = 0; idx < args.Length; idx++) { ParameterInfo pInfo = pInfos[idx]; bool hint = _injectionHints.TryGetValue(pInfo.Name, out val); if (!hint && pInfo.IsOptional) args[idx] = pInfo.DefaultValue; else args[idx] = PolicyUtility.Build(val, pInfo.ParameterType, ctx); } return args; }
public abstract bool ReadyToBuild(IObjectBuildContext ctx);
public void BuildUp(IObjectBuildContext ctx) { //Note:Nothing to do. }
public void TearDown(object instance, IObjectBuildContext ctx) { }
public abstract void OnBuildComplete(IObjectBuildContext ctx);
public abstract void BuildUp(IObjectBuildContext ctx);
private static void TearDownSteps(object instance, IObjectBuildContext ctx, Stages stage) { try { for (int idx = ctx.Policies.Count - 1; idx >= 0; idx--) { if (ctx.Policies[idx].Stage == stage) ctx.Policies[idx].TearDown(instance, ctx); } } catch (Exception ex) { ExceptionHelper.ThrowTearDownError(ex, stage); } }
public void TearDown(object instance, IObjectBuildContext ctx) { //Note:Nothing to do. }
public void TearDown(object instance, IObjectBuildContext ctx) { IDisposable d = instance as IDisposable; if (d != null) d.Dispose(); }
public bool ReadyToBuild(IObjectBuildContext ctx) { return true; }
public void BuildUp(IObjectBuildContext ctx) { }
public void OnBuildComplete(IObjectBuildContext ctx) { }
public abstract void OnBuildComplete(IObjectBuildContext ctx);
public abstract void BuildUp(IObjectBuildContext ctx);
public bool ReadyToBuild(IObjectBuildContext ctx) { return(true); }
public void BuildUp(IObjectBuildContext ctx) { }
public void OnBuildComplete(IObjectBuildContext ctx) { }
public void TearDown(object instance, IObjectBuildContext ctx) { //Note:Nothing to do. }
public void TearDown(object instance, IObjectBuildContext ctx) { }
public override void OnBuildComplete(IObjectBuildContext ctx) { //Note:Nothing to do. }