/// <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))); }
private object InternalLocate(IExportLocatorScope scope, IDisposalScope disposalScope, Type type, ActivationStrategyFilter consider, object key, IInjectionContext injectionContext, bool allowNull, bool isDynamic) { if (type == typeof(ILocatorService) || type == typeof(IExportLocatorScope)) { return(scope); } if (isDynamic) { if (type.IsArray) { return(DynamicArray(scope, disposalScope, type, consider, injectionContext)); } if (type.IsConstructedGenericType && type.GetGenericTypeDefinition() == typeof(IEnumerable <>)) { return(DynamicIEnumerable(scope, disposalScope, type, consider, injectionContext)); } } var compiledDelegate = InternalFieldStorage.ActivationStrategyCompiler.FindDelegate(this, type, consider, key, injectionContext, InternalFieldStorage.MissingExportStrategyProviders != ImmutableLinkedList <IMissingExportStrategyProvider> .Empty); if (compiledDelegate != null) { if (key == null && consider == null) { compiledDelegate = AddObjectFactory(type, compiledDelegate); } return(compiledDelegate(scope, disposalScope ?? (DisposalScope ?? DisposalScopeProvider.ProvideDisposalScope(scope)), injectionContext)); } if (Parent != null) { var injectionScopeParent = (IInjectionScope)Parent; return(injectionScopeParent.LocateFromChildScope(scope, disposalScope, type, injectionContext, consider, key, allowNull, isDynamic)); } if (type == typeof(IInjectionScope) && ScopeConfiguration.Behaviors.AllowInjectionScopeLocation) { return(scope); } var value = ScopeConfiguration.Implementation.Locate <IInjectionContextValueProvider>() .GetValueFromInjectionContext(scope, type, null, injectionContext, !allowNull); if (value != null) { return(value); } if (!allowNull) { throw new LocateException(new StaticInjectionContext(type)); } return(null); }
/// <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; }
private void AddToScope(IDisposalScope scope, ManualResetEvent syncEvent) { syncEvent.WaitOne(); for (int i = 0; i < 10000; i++) { scope.AddDisposable(new DisposableItem()); } }
private void AddToScopeWithCleanup(IDisposalScope scope, ManualResetEvent syncEvent, Action <DisposableItem> action) { syncEvent.WaitOne(); for (int i = 0; i < 10000; i++) { scope.AddDisposable(new DisposableItem(), action); } }
/// <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); })); }
private object DynamicArray(IExportLocatorScope scope, IDisposalScope disposalScope, Type type, ActivationStrategyFilter consider, IInjectionContext injectionContext) { if (InternalFieldStorage.DynamicArrayLocator == null) { Interlocked.CompareExchange(ref InternalFieldStorage.DynamicArrayLocator, ScopeConfiguration.Implementation.Locate <IDynamicArrayLocator>(), null); } return(InternalFieldStorage.DynamicArrayLocator.Locate(this, scope, disposalScope, type, consider, injectionContext)); }
/// <summary> /// Dispose the container /// </summary> public void Dispose() { if (disposalScope != null) { instance.Dispose(); disposalScope.Dispose(); disposalScope = null; } }
/// <summary> /// Add instance to disposal scope and return it /// </summary> /// <typeparam name="T"></typeparam> /// <param name="disposalScope"></param> /// <param name="value"></param> /// <returns></returns> public static T AddToDisposalScope <T>(IDisposalScope disposalScope, T value) { var disposable = value as IDisposable; if (disposable != null) { disposalScope.AddDisposable(disposable); } return(value); }
/// <summary> /// Method that creates one arg Func /// </summary> /// <param name="scope"></param> /// <param name="disposalScope"></param> /// <param name="context"></param> /// <returns></returns> public Func <TArg1, TResult> CreateFunc(IExportLocatorScope scope, IDisposalScope disposalScope, IInjectionContext context) { return(arg1 => { var newContext = context?.Clone() ?? _injectionContextCreator.CreateContext(null); newContext.SetExtraData(_arg1Id, arg1); return (TResult)_action(scope, disposalScope, newContext); }); }
/// <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> /// static method to create enumerable dynamic /// </summary> /// <typeparam name="T"></typeparam> /// <param name="injectionScope"></param> /// <param name="scope"></param> /// <param name="disposalScope"></param> /// <param name="consider"></param> /// <param name="injectionContext"></param> /// <returns></returns> public static object EnumerableCreateMethod <T>(IInjectionScope injectionScope, IExportLocatorScope scope, IDisposalScope disposalScope, ActivationStrategyFilter consider, IInjectionContext injectionContext) { var all = injectionScope.InternalLocateAll <T>(scope, disposalScope, typeof(T), injectionContext, consider, null); if (injectionScope.ScopeConfiguration.Behaviors.CustomEnumerableCreator != null) { return(injectionScope.ScopeConfiguration.Behaviors.CustomEnumerableCreator.CreateEnumerable(scope, all.ToArray())); } return(all); }
/// <summary> /// Create lazy instance /// </summary> /// <param name="scope"></param> /// <param name="disposalScope"></param> /// <param name="injectionContext"></param> /// <returns></returns> public Lazy <TResult, TMetadata> CreateLazy(IExportLocatorScope scope, IDisposalScope disposalScope, IInjectionContext injectionContext) { return(new Lazy <TResult, TMetadata>(() => { if (_delegate == null) { _delegate = CompileDelegate(); } return (TResult)_delegate(scope, disposalScope, injectionContext); }, _metadata)); }
/// <summary> /// Get value for object graph /// </summary> /// <typeparam name="T"></typeparam> /// <param name="scope"></param> /// <param name="disposalScope"></param> /// <param name="context"></param> /// <param name="activationDelegate"></param> /// <param name="uniqueId"></param> /// <returns></returns> public static T GetValue <T>(IExportLocatorScope scope, IDisposalScope disposalScope, IInjectionContext context, ActivationStrategyDelegate activationDelegate, string uniqueId) { var value = context.SharedData.GetExtraData(uniqueId); if (value != null) { return((T)value); } value = activationDelegate(scope, disposalScope, context); context.SharedData.SetExtraData(uniqueId, value); return((T)value); }
/// <summary> /// Add to disposal scope or use default /// </summary> /// <typeparam name="T"></typeparam> /// <param name="disposalScope"></param> /// <param name="tValue"></param> /// <param name="defaultValue"></param> /// <returns></returns> public static T AddToDisposableScopeOrDefault <T>(IDisposalScope disposalScope, T tValue, T defaultValue) { if (tValue != null) { var disposable = tValue as IDisposable; if (disposable != null) { disposalScope.AddDisposable(disposable); } return(tValue); } return(defaultValue); }
/// <summary> /// Check for null and then add to disposal scope /// </summary> /// <typeparam name="T"></typeparam> /// <param name="disposalScope"></param> /// <param name="context"></param> /// <param name="value"></param> /// <returns></returns> public static T CheckForNullAndAddToDisposalScope <T>(IDisposalScope disposalScope, StaticInjectionContext context, T value) { if (value == null) { throw new NullValueProvidedException(context); } var disposable = value as IDisposable; if (disposable != null) { disposalScope.AddDisposable(disposable); } return(value); }
private object GetInstance(IExportLocatorScope scope, IDisposalScope disposalScope, IInjectionContext context, ActivationStrategyDelegate activationDelegate) { var key = _keyFunc(scope, context); var value = _singletons.GetValueOrDefault(key); if (value != null) { return(value); } value = activationDelegate(scope, disposalScope, context); value = ImmutableHashTree.ThreadSafeAdd(ref _singletons, key, value); return(value); }
private object SingletonActivation(IExportLocatorScope scope, IDisposalScope disposalScope, IInjectionContext context) { if (_singleton != null) { return(_singleton); } lock (_lockObject) { if (_singleton == null) { _singleton = _activationDelegate(scope, disposalScope, context); } } return(_singleton); }
/// <summary> /// Get disposal scope from http context /// </summary> /// <param name="locatorScope"></param> /// <returns></returns> public static IDisposalScope GetDisposalScopeFromHttpContext(IDisposalScope locatorScope) { if (HttpContext.Current == null || !(locatorScope is DependencyInjectionContainer)) { return(locatorScope); } var disposalScope = HttpContext.Current.Items[_disposalScopeKey] as IDisposalScope; if (disposalScope != null) { return(disposalScope); } disposalScope = new DisposalScope(); HttpContext.Current.Items[_disposalScopeKey] = disposalScope; return(disposalScope); }
/// <summary> /// Get a value using lock to guarantee only one is created /// </summary> /// <typeparam name="T"></typeparam> /// <param name="scope"></param> /// <param name="disposalScope"></param> /// <param name="context"></param> /// <param name="activationDelegate"></param> /// <param name="uniqueId"></param> /// <returns></returns> public static T GetValueGuaranteeOnce <T>(IExportLocatorScope scope, IDisposalScope disposalScope, IInjectionContext context, ActivationStrategyDelegate activationDelegate, string uniqueId) { var value = context.SharedData.GetExtraData(uniqueId); if (value == null) { lock (context.SharedData.GetLockObject("SingletonPerAncestor|" + uniqueId)) { value = context.SharedData.GetExtraData(uniqueId); if (value == null) { value = activationDelegate(scope, disposalScope, context); context.SharedData.SetExtraData(uniqueId, value); } } } return((T)value); }
/// <summary> /// Configuration object constructor /// </summary> /// <param name="configuration"></param> /// <param name="parent"></param> /// <param name="name"></param> public InjectionScope(IInjectionScopeConfiguration configuration, IInjectionScope parent, string name) : base(parent, name, new ImmutableHashTree <Type, ActivationStrategyDelegate> [configuration.CacheArraySize]) { configuration.SetInjectionScope(this); InternalFieldStorage.ScopeConfiguration = configuration; InternalFieldStorage.InjectionContextCreator = configuration.Implementation.Locate <IInjectionContextCreator>(); InternalFieldStorage.CanLocateTypeService = configuration.Implementation.Locate <ICanLocateTypeService>(); InternalFieldStorage.ActivationStrategyCompiler = configuration.Implementation.Locate <IActivationStrategyCompiler>(); InternalFieldStorage.StrategyCollectionContainer = AddDisposable(configuration.Implementation.Locate <IActivationStrategyCollectionContainer <ICompiledExportStrategy> >()); InternalFieldStorage.DecoratorCollectionContainer = AddDisposable(configuration.Implementation.Locate <IActivationStrategyCollectionContainer <ICompiledDecoratorStrategy> >()); for (var i = 0; i <= ArrayLengthMinusOne; i++) { ActivationDelegates[i] = ImmutableHashTree <Type, ActivationStrategyDelegate> .Empty; } if (configuration.AutoRegisterUnknown && Parent == null) { InternalFieldStorage.MissingExportStrategyProviders = InternalFieldStorage.MissingExportStrategyProviders.Add( configuration.Implementation.Locate <IMissingExportStrategyProvider>()); } if (configuration.SupportFuncType) { StrategyCollectionContainer.AddStrategy(new FuncTypeStrategy(this)); } DisposalScopeProvider = configuration.DisposalScopeProvider; DisposalScope = DisposalScopeProvider == null ? this : null; }
/// <summary> /// Internal locate all method /// </summary> /// <typeparam name="T"></typeparam> /// <param name="scope"></param> /// <param name="disposalScope"></param> /// <param name="type"></param> /// <param name="extraData"></param> /// <param name="consider"></param> /// <param name="comparer"></param> /// <returns></returns> List <T> IInjectionScope.InternalLocateAll <T>(IExportLocatorScope scope, IDisposalScope disposalScope, Type type, object extraData, ActivationStrategyFilter consider, IComparer <T> comparer) { var returnList = new List <T>(); var context = CreateInjectionContextFromExtraData(typeof(T), extraData); var collection = StrategyCollectionContainer.GetActivationStrategyCollection(type); if (collection != null) { LocateEnumerablesFromStrategyCollection(collection, scope, disposalScope, type, context, consider, returnList); } if (type.IsConstructedGenericType) { var genericType = type.GetGenericTypeDefinition(); collection = StrategyCollectionContainer.GetActivationStrategyCollection(genericType); if (collection != null) { LocateEnumerablesFromStrategyCollection(collection, scope, disposalScope, type, context, consider, returnList); } } var injectionParent = Parent as IInjectionScope; if (injectionParent != null) { returnList.AddRange(injectionParent.InternalLocateAll <T>(scope, disposalScope, type, context, consider, null)); } if (comparer != null) { returnList.Sort(comparer); } return(returnList); }
/// <summary> /// Get a value dynamically /// </summary> /// <typeparam name="T">value to get</typeparam> /// <param name="scope">scope</param> /// <param name="disposalScope">disposal scope to use</param> /// <param name="staticInjectionContext">static injection context </param> /// <param name="context">context for call</param> /// <param name="key"></param> /// <param name="isRequired"></param> /// <param name="hasDefault"></param> /// <param name="defaultValue"></param> /// <returns></returns> public static T GetDynamicValue <T>(IExportLocatorScope scope, IDisposalScope disposalScope, StaticInjectionContext staticInjectionContext, IInjectionContext context, object key, bool isRequired, bool hasDefault, object defaultValue) { var injectionScope = scope.GetInjectionScope(); var value = injectionScope.LocateFromChildScope(scope, disposalScope, typeof(T), context, null, key, true, true); if (value != null) { return((T)value); } if (hasDefault) { return((T)defaultValue); } if (isRequired) { throw new LocateException(staticInjectionContext, $"Could not locate dynamic value for type {typeof(T).FullName}"); } return(default(T)); }
public DisposalScopeInjectionService(IDisposalScope disposalScope) { DisposalScope = disposalScope; }
/// <summary> /// Method that creates Func /// </summary> /// <param name="scope"></param> /// <param name="disposalScope"></param> /// <param name="context"></param> /// <returns></returns> public Func <T1, T2, T3, T4, T5, TResult> CreateFunc(IExportLocatorScope scope, IDisposalScope disposalScope, IInjectionContext context) { return((arg1, arg2, arg3, arg4, arg5) => { var newContext = context?.Clone() ?? _injectionContextCreator.CreateContext(null); newContext.SetExtraData(_t1Id, arg1); newContext.SetExtraData(_t2Id, arg2); newContext.SetExtraData(_t3Id, arg3); newContext.SetExtraData(_t4Id, arg4); newContext.SetExtraData(_t5Id, arg5); return (TResult)_action(scope, disposalScope, newContext); }); }
private void LocateEnumerablesFromStrategyCollection <TStrategy, TValue>(IActivationStrategyCollection <TStrategy> collection, IExportLocatorScope scope, IDisposalScope disposalScope, Type type, IInjectionContext context, ActivationStrategyFilter filter, List <TValue> returnList) where TStrategy : IWrapperOrExportActivationStrategy { foreach (var strategy in collection.GetStrategies()) { if (strategy.HasConditions) { var pass = true; foreach (var condition in strategy.Conditions) { if (!condition.MeetsCondition(strategy, new StaticInjectionContext(type))) { pass = false; break; } } if (!pass) { continue; } } if (filter != null && !filter(strategy)) { continue; } var activationDelegate = strategy.GetActivationStrategyDelegate(this, InternalFieldStorage.ActivationStrategyCompiler, type); if (activationDelegate != null) { returnList.Add( (TValue)activationDelegate(scope, disposalScope, context?.Clone())); } } }
private static T AddDisposableWithCleanup <T>(IDisposalScope scope, T value, Action <T> cleanupAction) where T : IDisposalScope { return(GetDisposalScopeFromHttpContext(scope).AddDisposable(value, cleanupAction)); }
/// <summary> /// /// </summary> /// <param name="scope"></param> /// <param name="disposalScope"></param> /// <param name="exportName"></param> /// <param name="extraData"></param> /// <param name="consider"></param> /// <returns></returns> List <object> IInjectionScope.InternalLocateAllByName(IExportLocatorScope scope, IDisposalScope disposalScope, string exportName, object extraData, ActivationStrategyFilter consider) { var context = CreateContext(extraData); var returnList = new List <object>(); var collection = StrategyCollectionContainer.GetActivationStrategyCollectionByName(exportName); foreach (var strategy in collection.GetStrategies()) { if (consider == null || consider(strategy)) { var activation = strategy.GetActivationStrategyDelegate(this, InternalFieldStorage.ActivationStrategyCompiler, typeof(object)); returnList.Add(activation(scope, disposalScope, context.Clone())); } } var injectionScopeParent = Parent as IInjectionScope; if (injectionScopeParent != null) { returnList.AddRange(injectionScopeParent.InternalLocateAllByName(scope, disposalScope, exportName, context, consider)); } return(returnList); }
/// <summary> /// Create func /// </summary> /// <param name="scope"></param> /// <param name="disposalScope"></param> /// <param name="context"></param> /// <returns></returns> public Func <TResult> CreateFunc(IExportLocatorScope scope, IDisposalScope disposalScope, IInjectionContext context) { return(() => (TResult)_action(scope, disposalScope, context)); }
/// <summary> /// /// </summary> /// <param name="childScope"></param> /// <param name="disposalScope"></param> /// <param name="name"></param> /// <param name="extraData"></param> /// <param name="consider"></param> /// <param name="allowNull"></param> /// <returns></returns> object IInjectionScope.LocateByNameFromChildScope(IExportLocatorScope childScope, IDisposalScope disposalScope, string name, object extraData, ActivationStrategyFilter consider, bool allowNull) { var collection = StrategyCollectionContainer.GetActivationStrategyCollectionByName(name); ICompiledExportStrategy strategy = null; if (collection != null) { if (consider != null) { var context = new StaticInjectionContext(typeof(object)); strategy = collection.GetStrategies() .FirstOrDefault( s => (!s.HasConditions || s.Conditions.All(con => con.MeetsCondition(s, context))) && consider(s)); } else { strategy = collection.GetPrimary(); if (strategy == null) { var context = new StaticInjectionContext(typeof(object)); strategy = collection.GetStrategies() .FirstOrDefault( s => !s.HasConditions || s.Conditions.All(con => con.MeetsCondition(s, context))); } } } if (strategy != null) { var strategyDelegate = strategy.GetActivationStrategyDelegate(this, InternalFieldStorage.ActivationStrategyCompiler, typeof(object)); return(strategyDelegate(childScope, disposalScope, CreateContext(extraData))); } if (Parent != null) { return(((IInjectionScope)Parent).LocateByNameFromChildScope(childScope, disposalScope, name, extraData, consider, allowNull)); } if (!allowNull) { throw new LocateException(new StaticInjectionContext(typeof(object))); } return(null); }
/// <summary> /// Locate an export from a child scope /// </summary> /// <param name="childScope">scope where the locate originated</param> /// <param name="disposalScope"></param> /// <param name="type">type to locate</param> /// <param name="extraData"></param> /// <param name="consider"></param> /// <param name="key"></param> /// <param name="allowNull"></param> /// <param name="isDynamic"></param> /// <returns>configuration object</returns> object IInjectionScope.LocateFromChildScope(IExportLocatorScope childScope, IDisposalScope disposalScope, Type type, object extraData, ActivationStrategyFilter consider, object key, bool allowNull, bool isDynamic) { return(InternalLocate(childScope, disposalScope, type, consider, key, CreateInjectionContextFromExtraData(type, extraData), allowNull, isDynamic)); }
public DisposalScopeImportService(IDisposalScope scope) { Scope = scope; }
private static T AddDisposable <T>(IDisposalScope scope, T value) where T : IDisposalScope { return(GetDisposalScopeFromHttpContext(scope).AddDisposable(value)); }