public object Locate(ExportActivationDelegate creationDelegate, IInjectionScope injectionScope, IInjectionContext injectionContext, IExportStrategy exportStrategy) { IDictionary<object, object> items = OperationContext.Current.Items(); object returnValue = items[uniqueRequestKey]; if (returnValue == null) { IDisposalScope disposalScope = injectionContext.DisposalScope; IInjectionScope requestingScope = injectionContext.RequestingScope; injectionContext.DisposalScope = OperationContext.Current.DisposalScope(); injectionContext.RequestingScope = exportStrategy.OwningScope; returnValue = creationDelegate(exportStrategy.OwningScope, injectionContext); injectionContext.DisposalScope = disposalScope; injectionContext.RequestingScope = requestingScope; if (returnValue != null) { items[uniqueRequestKey] = returnValue; } } return returnValue; }
public static void ExpectGetLog(this IInjectionContext mock, IGroboContainerLog result) { Expect.Once.On(mock) .Method("GetLog") .WithNoArguments() .Will(Return.Value(result)); }
/// <summary> /// Default constructor /// </summary> /// <param name="scope"></param> /// <param name="context"></param> /// <param name="activationDelegate"></param> /// <param name="scopeName"></param> public Scoped(IExportLocatorScope scope, IInjectionContext context, ActivationStrategyDelegate activationDelegate, string scopeName = null) { _scope = scope; _context = context; _activationDelegate = activationDelegate; _scopeName = scopeName; }
/// <summary> /// CSTOR /// </summary> /// <param name="injectionContext"></param> /// <param name="requestedName"></param> /// <param name="requestedType"></param> /// <param name="locateKey"></param> public ResolveUnknownExportArgs(IInjectionContext injectionContext, string requestedName, Type requestedType, object locateKey) { InjectionContext = injectionContext; RequestedName = requestedName; RequestedType = requestedType; LocateKey = locateKey; }
protected virtual object LocateFromChildContainer(IInjectionContextValueProvider valueProvider, IExportLocatorScope scope, StaticInjectionContext staticContext, Type type, object key, IInjectionContext context, object defaultValue, bool useDefault, bool isRequired) { var value = valueProvider.GetValueFromInjectionContext(scope, type, key, context, false); if (value != null) { return(value); } if (scope.TryLocate(type, out value, context, withKey: key)) { return(value); } if (useDefault) { return(defaultValue); } if (isRequired) { throw new LocateException(staticContext); } return(null); }
/// <summary> /// This method is called by the export strategy when attempting to locate an export /// </summary> /// <param name="creationDelegate"></param> /// <param name="exportStrategyScope"></param> /// <param name="injectionContext"></param> /// <param name="exportStrategy"></param> /// <returns></returns> public object Locate(ExportActivationDelegate creationDelegate, IInjectionScope exportStrategyScope, IInjectionContext injectionContext, IExportStrategy exportStrategy) { if (instance.Value != null) { return instance.Value; } IDisposalScope contextdisposalScope = injectionContext.DisposalScope; IInjectionScope requestingScope = injectionContext.RequestingScope; injectionContext.DisposalScope = exportStrategyScope; injectionContext.RequestingScope = exportStrategyScope; object instanceValue = creationDelegate(exportStrategyScope, injectionContext); injectionContext.DisposalScope = contextdisposalScope; injectionContext.RequestingScope = requestingScope; instance.Value = instanceValue; IDisposable disposable = instanceValue as IDisposable; if (disposable != null) { disposalScope.AddDisposable(disposable); } return instance.Value; }
/// <summary> /// Method that is called each time a delegate needs to be created /// </summary> /// <param name="scope"></param> /// <param name="disposalScope"></param> /// <param name="context"></param> /// <returns></returns> public TDelegate CreateDelegate(IExportLocatorScope scope, IDisposalScope disposalScope, IInjectionContext context) { var funcClass = new FuncClass(scope, disposalScope, context, _action, _injectionContextCreator, _arg1Id, _arg2Id, _arg3Id, _arg4Id, _arg5Id); return((TDelegate)((object)_funcMethodInfo.CreateDelegate(typeof(TDelegate), funcClass))); }
/// <summary> /// Internal filter method that loops through the collection of filters /// </summary> /// <param name="context">injection context</param> /// <param name="strategy">export strategy</param> /// <returns>true if the strategy matches</returns> private bool InternalExportStrategyFilter(IInjectionContext context, IExportStrategy strategy) { if (UseOr) { foreach (ExportStrategyFilter exportStrategyFilter in filters) { if (exportStrategyFilter(context, strategy)) { return true; } } return false; } foreach (ExportStrategyFilter exportStrategyFilter in filters) { if (!exportStrategyFilter(context, strategy)) { return false; } } return true; }
public void CanInjectAndExtractWithClasses() { IInjectionContext injection = new InjectionClass(); injection.State = 1; IBuildContext context = new BuildContext(); context.SetContextObject(injection); TaskClass task = new TaskClass(2); Assert.IsNull(task.InjectedObject); ContextInjector.Inject(context, task); Assert.IsNotNull(task.InjectedObject); Assert.AreEqual(1, task.InjectedObject.State); ReturnCode result = task.Run(); Assert.AreEqual(ReturnCode.Success, result); ContextInjector.Extract(context, task); IInjectionContext modifiedInjection = context.GetContextObject <IInjectionContext>(); Assert.AreEqual(task.NewState, modifiedInjection.State); }
private IContextHolder GetHolder(IInjectionContext context, int threadId) { Assert.AreSame(injectionContext, context); Assert.IsNull(holder, "duplicate call"); holder = NewMock <IContextHolder>(); return(holder); }
/// <summary> /// Condition meet /// </summary> /// <param name="scope">injection scope</param> /// <param name="injectionContext">injection context</param> /// <param name="exportStrategy">export strategy</param> /// <returns>condition</returns> public bool ConditionMeet(IInjectionScope scope, IInjectionContext injectionContext, IExportStrategy exportStrategy) { bool found = false; CurrentInjectionInfo[] injectionStack = injectionContext.GetInjectionStack(); for (int i = injectionStack.Length - 1; i >= 0 && !found; i--) { CurrentInjectionInfo injectionInfo = injectionStack[i]; foreach (Type ancestorType in ancestorTypes) { if (ancestorType.GetTypeInfo().IsInterface) { if(injectionInfo.ActivationType == ancestorType || (injectionInfo.CurrentExportStrategy != null && injectionInfo.CurrentExportStrategy.ExportTypes.Contains(ancestorType))) { found = true; break; } } else if (injectionInfo.ActivationType == ancestorType) { found = true; break; } } } return found; }
public void CanInjectAndExtractWithStructs() { IInjectionContext injection = new InjectionStruct(); injection.State = 1; IBuildContext context = new BuildContext(); context.SetContextObject(injection); TaskStruct task = new TaskStruct(2); Assert.IsNull(task.InjectedObject); // Still need to box / unbox the struct task IBuildTask boxed = task; ContextInjector.Inject(context, boxed); task = (TaskStruct)boxed; Assert.IsNotNull(task.InjectedObject); Assert.AreEqual(1, task.InjectedObject.State); ReturnCode result = task.Run(); Assert.AreEqual(ReturnCode.Success, result); ContextInjector.Extract(context, task); IInjectionContext modifiedInjection = context.GetContextObject <IInjectionContext>(); Assert.AreEqual(task.NewState, modifiedInjection.State); }
/// <summary> /// Activate value /// </summary> /// <param name="exportInjectionScope">injection scope</param> /// <param name="context">injection context</param> /// <param name="consider">consider filter</param> /// <param name="locateKey"></param> /// <returns>activated value</returns> public object Activate(IInjectionScope exportInjectionScope, IInjectionContext context, ExportStrategyFilter consider, object locateKey) { string parameterName = ParameterName; if (context.TargetInfo == null) { throw new Exception("Must be injecting into type"); } if (string.IsNullOrEmpty(parameterName)) { parameterName = context.TargetInfo.InjectionTargetName; } string propertyString = HttpContext.Current.Request.QueryString[parameterName]; if (context.TargetInfo.InjectionTargetType != typeof(string) && !string.IsNullOrEmpty(propertyString)) { Type underlyingType = Nullable.GetUnderlyingType(context.TargetInfo.InjectionTargetType); if (underlyingType != null) { return Convert.ChangeType(propertyString, underlyingType); } if (context.TargetInfo.InjectionTargetType.IsValueType) { return Convert.ChangeType(propertyString, context.TargetInfo.InjectionTargetType); } throw new ArgumentException("You can only import ValueTypes or Nullable Types"); } return propertyString; }
public object Locate(ExportActivationDelegate creationDelegate, IInjectionScope injectionScope, IInjectionContext injectionContext, IExportStrategy exportStrategy) { object returnValue = null; if (HttpContext.Current != null) { returnValue = HttpContext.Current.Items[uniqueId]; } if (returnValue == null) { IDisposalScope disposalScope = injectionContext.DisposalScope; IInjectionScope requestingScope = injectionContext.RequestingScope; injectionContext.DisposalScope = MVCDisposalScopeProvider.GetDisposalScope(); injectionContext.RequestingScope = exportStrategy.OwningScope; returnValue = creationDelegate(exportStrategy.OwningScope, injectionContext); injectionContext.DisposalScope = disposalScope; injectionContext.RequestingScope = requestingScope; if (returnValue != null && HttpContext.Current != null) { HttpContext.Current.Items[uniqueId] = returnValue; } } return returnValue; }
public T Create <T1, T2, T3, T4, T>(IInjectionContext context, T1 arg1, T2 arg2, T3 arg3, T4 arg4) { return((T)CreateImpl(typeof(T), context, new[] { typeof(T1), typeof(T2), typeof(T3), typeof(T4) }, arg1, arg2, arg3, arg4)); }
/// <summary> /// /// </summary> /// <typeparam name="T"></typeparam> /// <param name="scope"></param> /// <param name="context"></param> /// <param name="parameterName"></param> /// <returns></returns> public virtual bool DynamicCanLocate <T>(IExportLocatorScope scope, IInjectionContext context, string parameterName) { var value = context.GetExtraData(parameterName); if (value is T) { return(true); } value = context.GetValueByType(typeof(T)); if (value is T) { return(true); } var currentScope = scope; while (currentScope != null) { if (currentScope.GetExtraData(parameterName) != null || currentScope.Values.Any(o => o is T)) { return(true); } currentScope = currentScope.Parent; } return(scope.GetInjectionScope().CanLocate(typeof(T))); }
/// <summary> /// Default constructor /// </summary> /// <param name="scope"></param> /// <param name="disposalScope"></param> /// <param name="context"></param> /// <param name="action"></param> /// <param name="injectionContextCreator"></param> public FuncClass(IExportLocatorScope scope, IDisposalScope disposalScope, IInjectionContext context, ActivationStrategyDelegate action, IInjectionContextCreator injectionContextCreator) { _scope = scope; _disposalScope = disposalScope; _context = context; _action = action; _injectionContextCreator = injectionContextCreator; }
/// <summary> /// Can locate a type /// </summary> /// <param name="context"></param> /// <param name="resolveName"></param> /// <param name="resolveType"></param> /// <param name="consider"></param> /// <param name="locateKey"></param> /// <returns></returns> public bool CanLocate(IInjectionContext context, string resolveName, Type resolveType, ExportStrategyFilter consider, object locateKey) { if (resolveType != null && resolveType.IsInterface) { return true; } return false; }
public static void ExpectGetContext(this IContextHolder mock, IInternalContainer worker, IInjectionContext result) { Expect.Once.On(mock) .Method("GetContext") .With(worker) .Will(Return.Value(result)); }
/// <summary> /// If this strategy meets condition and the target target strategy meets condition /// </summary> /// <param name="injectionContext"></param> /// <returns></returns> public override bool MeetsCondition(IInjectionContext injectionContext) { if (exportCondition != null && !exportCondition.ConditionMeet(OwningScope, injectionContext, this)) { return false; } return strategy.MeetsCondition(injectionContext); }
/// <summary> /// activate the strategy /// </summary> /// <param name="exportInjectionScope">injection scope</param> /// <param name="context">injection context</param> /// <param name="consider">consider filter</param> /// <param name="locateKey"></param> /// <returns>activated object</returns> public override object Activate(IInjectionScope exportInjectionScope, IInjectionContext context, ExportStrategyFilter consider, object locateKey) { if (_lifestyle != null) { return _lifestyle.Locate(InternalActivate, exportInjectionScope, context, this); } return InternalActivate(exportInjectionScope, context); }
private static Func <Type, object> CreateFunc(IExportLocatorScope scope, IInjectionContext context) { return(type => { var clone = context.Clone(); return scope.Locate(type, clone); }); }
public virtual object PostBuildUp(object value, StaticInjectionContext staticContext, IExportLocatorScope scope, IInjectionContext injectionContext) { var timer = (Stopwatch)injectionContext.GetExtraData(staticContext.TargetInfo.UniqueId); timer.Stop(); return(value); }
public void InjectionContextActivate(IInjectionContext context) { if (context == null) { throw new ArgumentNullException("context"); } InjectionContextActivateCalled = true; }
public object Activate(IInjectionScope exportInjectionScope, IInjectionContext context, ExportStrategyFilter consider, object locateKey) { if (HttpContext.Current != null) { return HttpContext.Current.Request.Path; } return null; }
/// <summary> /// Create GraceOptional instance. /// </summary> /// <param name="scope"></param> /// <param name="disposalScope"></param> /// <param name="injectionContext"></param> /// <returns></returns> public GraceOptional <TResult> CreateOptional( IExportLocatorScope scope, IDisposalScope disposalScope, IInjectionContext injectionContext) { return(new GraceOptional <TResult>(scope, qualifier, () => { return (TResult)_delegate(scope, disposalScope, injectionContext); })); }
public static void ExpectCreate <T1, T2, T3, T>(this IInternalContainer mock, IInjectionContext context, T1 arg1, T2 arg2, T3 arg3, T result) { Expect .Once .On(mock) .Method("Create", typeof(T1), typeof(T2), typeof(T3), typeof(T)) .With(context, arg1, arg2, arg3) .Will(Return.Value(result)); }
public static void ExpectGetAndFail(this IInternalContainer mock, Type type, IInjectionContext context, Exception e) { Expect .Once .On(mock) .Method("Get") .With(type, context) .Will(Throw.Exception(e)); }
public static void ExpectCreate <T>(this IInternalContainer mock, IInjectionContext context, T result) { Expect .Once .On(mock) .Method("Create", typeof(T)) .With(context) .Will(Return.Value(result)); }
public static void ExpectBuildCreateFunc <T1, T>(this IInternalContainer mock, IInjectionContext context, Func <T1, T> result) { Expect .Once .On(mock) .Method("BuildCreateFunc", typeof(T1), typeof(T)) .With(context) .Will(Return.Value(result)); }
/// <summary> /// Activate the export /// </summary> /// <param name="exportInjectionScope"></param> /// <param name="context"></param> /// <param name="consider"></param> /// <param name="locateKey"></param> /// <returns></returns> public override object Activate(IInjectionScope exportInjectionScope, IInjectionContext context, ExportStrategyFilter consider, object locateKey) { object targetOject = strategy.Activate(exportInjectionScope, context, consider, locateKey); if (targetOject != null) { return propertyAccessor(targetOject); } return null; }
private T[] GetInstances <T>(IInjectionContext context, Type abstractionType) { var implementations = GetImplementations(abstractionType); var result = new T[implementations.Length]; for (var i = 0; i < result.Length; i++) { result[i] = (T)UnWrap(abstractionType, implementations[i].GetOrCreateInstance(context, creationContext)); } return(result); }
public virtual object PreBuildUp(StaticInjectionContext staticContext, IExportLocatorScope scope, IInjectionContext injectionContext) { var timer = new Stopwatch(); injectionContext.SetExtraData(staticContext.TargetInfo.UniqueId, timer); timer.Start(); return(null); }
/// <summary> /// Locate specific type using extra data or key /// </summary> /// <param name="type">type to locate</param> /// <param name="extraData">extra data to be used during construction</param> /// <param name="consider">filter out exports you don't want to consider</param> /// <param name="withKey">key to use for locating type</param> /// <param name="isDynamic">skip cache and look through exports</param> /// <returns>located instance</returns> // ReSharper disable once MethodOverloadWithOptionalParameter public override object Locate(Type type, object extraData = null, ActivationStrategyFilter consider = null, object withKey = null, bool isDynamic = false) { IInjectionContext context = extraData == null ? null : CreateInjectionContextFromExtraData(type, extraData); if (withKey == null && consider == null && !isDynamic) { return(DelegateCache.ExecuteActivationStrategyDelegateWithContext(type, this, false, context)); } return(InternalLocate(this, this, type, consider, withKey, context, false, isDynamic)); }
/// <summary> /// Default constructor /// </summary> /// <param name="scope"></param> /// <param name="disposalScope"></param> /// <param name="context"></param> /// <param name="action"></param> /// <param name="injectionContextCreator"></param> /// <param name="arg1Id"></param> /// <param name="arg2Id"></param> /// <param name="arg3Id"></param> /// <param name="arg4Id"></param> public FuncClass(IExportLocatorScope scope, IDisposalScope disposalScope, IInjectionContext context, ActivationStrategyDelegate action, IInjectionContextCreator injectionContextCreator, string arg1Id, string arg2Id, string arg3Id, string arg4Id) { _scope = scope; _disposalScope = disposalScope; _context = context; _action = action; _injectionContextCreator = injectionContextCreator; _arg1Id = arg1Id; _arg2Id = arg2Id; _arg3Id = arg3Id; _arg4Id = arg4Id; }
/// <summary> /// Called to determine if the export strategy meets the condition to be activated /// </summary> /// <param name="scope">injection scope that this export exists in</param> /// <param name="injectionContext">injection context for this request</param> /// <param name="exportStrategy">export strategy being tested</param> /// <returns>true if the export meets the condition</returns> public bool ConditionMeet(IInjectionScope scope, IInjectionContext injectionContext, IExportStrategy exportStrategy) { foreach (IExportCondition exportCondition in exportConditions) { if (!exportCondition.ConditionMeet(scope, injectionContext, exportStrategy)) { return false; } } return true; }
/// <summary> /// This method is called by the export strategy when attempting to locate an export /// </summary> /// <param name="creationDelegate"></param> /// <param name="injectionScope"></param> /// <param name="injectionContext"></param> /// <param name="exportStrategy"></param> /// <returns></returns> public object Locate(ExportActivationDelegate creationDelegate, IInjectionScope injectionScope, IInjectionContext injectionContext, IExportStrategy exportStrategy) { if (lifestyleContainer == null) { lifestyleContainer = LocateContainer(exportStrategy.OwningScope, injectionContext); } return lifestyleContainer.Locate(creationDelegate, injectionScope, injectionContext, exportStrategy); }
/// <summary> /// Method creates 2 arg Func /// </summary> /// <param name="scope"></param> /// <param name="disposalScope"></param> /// <param name="context"></param> /// <returns></returns> public Func <T1, T2, TResult> CreateFunc(IExportLocatorScope scope, IDisposalScope disposalScope, IInjectionContext context) { return((arg1, arg2) => { var newContext = context?.Clone() ?? _injectionContextCreator.CreateContext(null); newContext.SetExtraData(_t1Id, arg1); newContext.SetExtraData(_t2Id, arg2); return (TResult)_action(scope, disposalScope, newContext); }); }
/// <summary> /// Create lazy instance /// </summary> /// <param name="scope"></param> /// <param name="disposalScope"></param> /// <param name="injectionContext"></param> /// <returns></returns> public Lazy <TResult, IActivationStrategyMetadata> CreateLazy(IExportLocatorScope scope, IDisposalScope disposalScope, IInjectionContext injectionContext) { return(new Lazy <TResult, IActivationStrategyMetadata>(() => { if (_delegate == null) { _delegate = CompileDelegate(); } return (TResult)_delegate(scope, disposalScope, injectionContext); }, _metadata)); }
/// <summary> /// Create lazy instance /// </summary> /// <param name="scope"></param> /// <param name="disposalScope"></param> /// <param name="injectionContext"></param> /// <returns></returns> public Lazy <TResult> CreateLazy(IExportLocatorScope scope, IDisposalScope disposalScope, IInjectionContext injectionContext) { return(new Lazy <TResult>(() => { if (_delegate == null) { _delegate = CompileDelegate(); } return (TResult)_delegate(scope, disposalScope, injectionContext); })); }
/// <summary> /// Execute delegate by injecting parameters then executing /// </summary> /// <param name="scope"></param> /// <param name="context"></param> /// <param name="injectionContext"></param> /// <param name="delegate"></param> /// <returns></returns> public static object InjectAndExecuteDelegate(IExportLocatorScope scope, StaticInjectionContext context, IInjectionContext injectionContext, Delegate @delegate) { var executeFunc = _executeDelegateWithInjections.GetValueOrDefault(@delegate.GetType()); if (executeFunc == null) { executeFunc = CreateExecuteDelegate(@delegate); _executeDelegateWithInjections = _executeDelegateWithInjections.Add(@delegate.GetType(), executeFunc); } return(executeFunc(scope, context, injectionContext, @delegate)); }
public object GetOrCreateInstance(IInjectionContext context, ICreationContext creationContext) { if (instance == null) { lock (configurationLock) if (instance == null) { IClassFactory noParametersFactory = implementation.GetFactory(Type.EmptyTypes, creationContext); return(instance = noParametersFactory.Create(context, new object[0])); } } context.Reused(ObjectType); return(instance); }
/// <summary> /// Called to determine if the export strategy meets the condition to be activated /// </summary> /// <param name="scope">injection scope that this export exists in</param> /// <param name="injectionContext">injection context for this request</param> /// <param name="exportStrategy">export strategy being tested</param> /// <returns>true if the export meets the condition</returns> public bool ConditionMeet(IInjectionScope scope, IInjectionContext injectionContext, IExportStrategy exportStrategy) { if (injectionContext.TargetInfo != null) { foreach (Attribute injectionTypeAttribute in injectionContext.TargetInfo.InjectionTypeAttributes) { if (injectionTypeAttribute.GetType().GetTypeInfo().IsAssignableFrom(attributeType.GetTypeInfo())) { return true; } } } return false; }
/// <summary> /// Called to determine if the export strategy meets the condition to be activated /// </summary> /// <param name="scope">injection scope that this export exists in</param> /// <param name="injectionContext">injection context for this request</param> /// <param name="exportStrategy">export strategy being tested</param> /// <returns>true if the export meets the condition</returns> public bool ConditionMeet(IInjectionScope scope, IInjectionContext injectionContext, IExportStrategy exportStrategy) { if (answer.HasValue) { return answer.Value; } bool returnValue = ConfigurationManager.AppSettings[settingName] == null; if (cacheAnswer) { answer = returnValue; } return returnValue; }
/// <summary> /// Inject instance with dependencies /// </summary> /// <param name="scope">export locator scope</param> /// <param name="instance">instance to inject</param> /// <param name="extraData">extra data, can be null</param> public static void Inject(this IExportLocatorScope scope, object instance, object extraData = null) { if (scope == null) { throw new ArgumentNullException(nameof(scope)); } if (instance == null) { throw new ArgumentNullException(nameof(instance)); } var parentScope = scope; while (parentScope.Parent != null) { parentScope = parentScope.Parent; } var injectionScope = (IInjectionScope)parentScope; var value = parentScope.GetExtraDataOrDefaultValue <InjectionHashTreeHolder>(typeof(InjectionHashTreeHolder)) ?? (InjectionHashTreeHolder)parentScope.SetExtraData(typeof(InjectionHashTreeHolder), new InjectionHashTreeHolder(), false); var instanceType = instance.GetType(); var injectionDelegate = value.Delegates.GetValueOrDefault(instanceType); if (injectionDelegate == null) { injectionDelegate = injectionScope.StrategyCompiler.CreateInjectionDelegate(injectionScope, instanceType); injectionDelegate = ImmutableHashTree.ThreadSafeAdd(ref value.Delegates, instanceType, injectionDelegate); } var disposalScope = injectionScope.ScopeConfiguration.DisposalScopeProvider?.ProvideDisposalScope(scope) ?? scope; IInjectionContext context = null; if (extraData != null) { context = injectionScope.CreateContext(extraData); } injectionDelegate(scope, disposalScope, context, instance); }
/// <summary> /// Called to determine if the export strategy meets the condition to be activated /// </summary> /// <param name="scope">injection scope that this export exists in</param> /// <param name="injectionContext">injection context for this request</param> /// <param name="exportStrategy">export strategy being tested</param> /// <returns>true if the export meets the condition</returns> public bool ConditionMeet(IInjectionScope scope, IInjectionContext injectionContext, IExportStrategy exportStrategy) { IInjectionTargetInfo targetInfo = injectionContext.TargetInfo; if (targetInfo != null && injectedType != null) { foreach (Type type in injectedType) { if (targetInfo.InjectionType.GetTypeInfo().IsAssignableFrom(type.GetTypeInfo())) { return true; } } } return false; }
public object Create(IInjectionContext context, object[] args) { try { context.BeginConstruct(constructedType); return(creatorFunc(context.InternalContainer, context, args)); } catch (Exception) { context.Crash(); throw; } finally { context.EndConstruct(constructedType); } }
/// <summary> /// returns true when the app.config setting is equal to the provided value /// </summary> /// <param name="scope"></param> /// <param name="injectionContext"></param> /// <param name="exportStrategy"></param> /// <returns></returns> public bool ConditionMeet(IInjectionScope scope, IInjectionContext injectionContext, IExportStrategy exportStrategy) { if (answer.HasValue) { return answer.Value; } string appSetting = ConfigurationManager.AppSettings[settingName]; bool returnValue = false; if (appSetting != null) { if (settingValue == null) { return false; } if (appSetting.GetType() == settingValue.GetType()) { returnValue = Equals(settingValue, appSetting); } try { returnValue = Equals(settingValue, Convert.ChangeType(appSetting, settingValue.GetType())); } catch (Exception exp) { Logger.Error( string.Format("Exception thrown while converting {0} to {1}", appSetting, settingValue.GetType().FullName), supplemental, exp); } } else if (settingValue == null) { returnValue = true; } if (cacheAnswer) { answer = returnValue; } return returnValue; }
public object[] GetAll(Type type, IInjectionContext context) { try { context.BeginGetAll(type); return(GetInstances <object>(context, type)); } catch (Exception) { context.Crash(); throw; } finally { context.EndGetAll(type); } }
/// <summary> /// This method is called by the export strategy when attempting to locate an export /// </summary> /// <param name="creationDelegate"></param> /// <param name="exportStrategyScope"></param> /// <param name="injectionContext"></param> /// <param name="exportStrategy"></param> /// <returns></returns> public object Locate(ExportActivationDelegate creationDelegate, IInjectionScope exportStrategyScope, IInjectionContext injectionContext, IExportStrategy exportStrategy) { object returnValue = null; if (intanceRef != null) { returnValue = intanceRef.Target; } if (returnValue == null) { lock (lockObject) { if (intanceRef != null) { returnValue = intanceRef.Target; } if (returnValue == null) { IDisposalScope disposalScope = injectionContext.DisposalScope; IInjectionScope requeInjectionScope = injectionContext.RequestingScope; // null scope because weak exports can't have injectionContext.DisposalScope = null; injectionContext.RequestingScope = exportStrategyScope; returnValue = creationDelegate(exportStrategyScope, injectionContext); injectionContext.DisposalScope = disposalScope; injectionContext.RequestingScope = requeInjectionScope; if (returnValue != null) { intanceRef = new WeakReference(returnValue); } } } } return returnValue; }
/// <summary> /// This method is called by the export strategy when attempting to locate an export /// </summary> /// <param name="creationDelegate"></param> /// <param name="exportStrategyScope"></param> /// <param name="injectionContext"></param> /// <param name="exportStrategy"></param> /// <returns></returns> public object Locate(ExportActivationDelegate creationDelegate, IInjectionScope exportStrategyScope, IInjectionContext injectionContext, IExportStrategy exportStrategy) { object returnValue = injectionContext.RequestingScope.GetExtraData(uniqueId); if (returnValue == null) { IInjectionScope requestScope = injectionContext.RequestingScope; returnValue = creationDelegate(exportStrategyScope, injectionContext); requestScope.SetExtraData(uniqueId, returnValue); } return returnValue; }
/// <summary> /// Called to determine if the export strategy meets the condition to be activated /// </summary> /// <param name="scope">injection scope that this export exists in</param> /// <param name="injectionContext">injection context for this request</param> /// <param name="exportStrategy">export strategy being tested</param> /// <returns>true if the export meets the condition</returns> public bool ConditionMeet(IInjectionScope scope, IInjectionContext injectionContext, IExportStrategy exportStrategy) { if (answer.HasValue) { return answer.Value; } string appSetting = ConfigurationManager.AppSettings[settingName]; bool returnValue = string.IsNullOrEmpty(appSetting) || string.Compare("false", appSetting, StringComparison.CurrentCultureIgnoreCase) == 0; if (cacheAnswer) { answer = returnValue; } return returnValue; }
/// <summary> /// This method is called by the export strategy when attempting to locate an export /// </summary> /// <param name="creationDelegate"></param> /// <param name="injectionScope"></param> /// <param name="injectionContext"></param> /// <param name="exportStrategy"></param> /// <returns></returns> public object Locate(ExportActivationDelegate creationDelegate, IInjectionScope injectionScope, IInjectionContext injectionContext, IExportStrategy exportStrategy) { object returnValue = injectionContext.GetExtraData(key); if (returnValue == null) { returnValue = creationDelegate(injectionScope, injectionContext); if (returnValue != null) { injectionContext.SetExtraData(key, returnValue); } } return returnValue; }
/// <summary> /// Creates an instance in a singleton scope /// </summary> /// <param name="creationDelegate"></param> /// <param name="exportStrategyScope"></param> /// <param name="injectionContext"></param> /// <returns></returns> public static object CreateInSingletonScope(ExportActivationDelegate creationDelegate, IInjectionScope exportStrategyScope, IInjectionContext injectionContext) { object returnValue = null; IDisposalScope disposalScope = injectionContext.DisposalScope; IInjectionScope requestingScope = injectionContext.RequestingScope; injectionContext.DisposalScope = exportStrategyScope; injectionContext.RequestingScope = exportStrategyScope; returnValue = creationDelegate(exportStrategyScope, injectionContext); injectionContext.DisposalScope = disposalScope; injectionContext.RequestingScope = requestingScope; return returnValue; }
public object Locate(ExportActivationDelegate creationDelegate, IInjectionScope injectionScope, IInjectionContext injectionContext, IExportStrategy exportStrategy) { object returnValue = null; IInjectionScope locateScope = FindNamedScope(injectionContext.RequestingScope); if (locateScope == null) { throw new InjectionScopeCouldNotBeFoundException(_namedScope); } returnValue = locateScope.GetExtraData(_uniqueId); if (returnValue != null) { return returnValue; } lock (_lockObject) { returnValue = locateScope.GetExtraData(_uniqueId) ?? SingletonLifestyle.CreateInSingletonScope(creationDelegate, locateScope, injectionContext); if (returnValue != null) { locateScope.SetExtraData(_uniqueId, returnValue); IDisposable disposable = returnValue as IDisposable; if (disposable != null) { locateScope.AddDisposable(disposable); } } } return returnValue; }
/// <summary> /// ResolveAll will be called every time a collection is resolved /// </summary> /// <param name="owningScope"></param> /// <param name="context"></param> /// <param name="resolveName"></param> /// <param name="resolveType"></param> /// <param name="collectionEmpty"></param> /// <param name="consider"></param> /// <param name="locateKey"></param> /// <returns></returns> public IEnumerable<object> LocateAll(IInjectionScope owningScope, IInjectionContext context, string resolveName, Type resolveType, bool collectionEmpty, ExportStrategyFilter consider, object locateKey) { if (resolveType != null && resolveType.IsInterface && collectionEmpty) { Type createType = typeof(NSubstituteExportStrategy<>).MakeGenericType(resolveType); ICompiledExportStrategy newStrategy = Activator.CreateInstance(createType) as ICompiledExportStrategy; if (newStrategy != null) { newStrategy.SetLifestyleContainer(new SingletonLifestyle()); newStrategy.AddExportType(resolveType); owningScope.AddStrategy(newStrategy); yield return newStrategy.Activate(owningScope, context, null, locateKey); } } }
/// <summary> /// Activate the export /// </summary> /// <param name="exportInjectionScope"></param> /// <param name="context"></param> /// <param name="consider"></param> /// <param name="locateKey"></param> /// <returns></returns> public override object Activate(IInjectionScope exportInjectionScope, IInjectionContext context, ExportStrategyFilter consider, object locateKey) { if (Log.IsDebugEnabled) { if (Lifestyle != null) { Log.DebugFormat("Activating export type {0} with life cycle container {1} ", ActivationType.FullName, Lifestyle.GetType().FullName); } else { Log.DebugFormat("Activating export type {0} with no life cycle container ", ActivationType.FullName); } } try { if (_lifestyle != null) { return _lifestyle.Locate(activationDelegate, exportInjectionScope, context, this); } return activationDelegate(exportInjectionScope, context); } catch (LocateException locateException) { locateException.AddLocationInformationEntry(new StrategyBeingActivated(this)); throw; } catch (Exception exp) { GeneralLocateException locateException = new GeneralLocateException(null, (Type)null, context, exp); locateException.AddLocationInformationEntry(new StrategyBeingActivated(this)); throw locateException; } }
/// <summary> /// Locates an ILifestyle /// </summary> /// <param name="injectionScope"></param> /// <param name="injectionContext"></param> /// <returns></returns> private static ILifestyle LocateContainer(IInjectionScope injectionScope, IInjectionContext injectionContext) { ILifestyle returnValue = null; try { IPerRequestLifestyleProvider provider = injectionScope.Locate<IPerRequestLifestyleProvider>(injectionContext); if (provider != null) { returnValue = provider.ProvideContainer(); } } catch (Exception exp) { Logger.Error("Exception throw while trying to locate IPerRequestLifestyleProvider", "SingletonPerRequest", exp); } return returnValue ?? new SingletonPerInjectionContextLifestyle(); }
/// <summary> /// Method used to invoke an activation delegate /// </summary> /// <param name="exportStrategyScope"></param> /// <param name="activationDelegate"></param> /// <param name="injectionContext"></param> /// <returns></returns> public static object InvokeActivationDelegate(ExportActivationDelegate activationDelegate, IInjectionScope exportStrategyScope, IInjectionContext injectionContext) { return activationDelegate(exportStrategyScope, injectionContext); }
public void InjectionContextActivation(IInjectionContext context) { Assert.NotNull(context); ContextActivationCalled = true; }