Skip to content

Commit

Permalink
changing disposal and lifetime scope logic for issue #208 and #209
Browse files Browse the repository at this point in the history
  • Loading branch information
ipjohnson committed Mar 16, 2019
1 parent 0f5a734 commit 6dd492c
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,7 @@ public interface IInjectionScopeConfiguration
/// Export as type and base implementations, true by default
/// </summary>
bool ExportAsBase { get; }

/// <summary>
/// Override which disposal scope is used for tracking disposables, null by default
/// </summary>
IDisposalScopeProvider DisposalScopeProvider { get; }


/// <summary>
/// Function that filters out interface types.
/// First type arg is interface, second type arg is implementing, return true if should filter out
Expand Down
4 changes: 4 additions & 0 deletions src/Grace/DependencyInjection/ILocatorService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ namespace Grace.DependencyInjection
/// <summary>
/// Represents a class that can locate types, classes should not implement this interface directly rather IExportLocatorScope
/// </summary>
#if NETSTANDARD1_0
public interface ILocatorService
#else
public interface ILocatorService : IServiceProvider
#endif
{
/// <summary>
/// Can Locator type
Expand Down
66 changes: 30 additions & 36 deletions src/Grace/DependencyInjection/Impl/InjectionScope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,6 @@ public class InjectionScope : BaseExportLocatorScope, IInjectionScope
/// </summary>
protected InternalFieldStorageClass InternalFieldStorage = new InternalFieldStorageClass();

/// <summary>
/// Disposal scope providers, this or DisposalScope must be set
/// </summary>
protected IDisposalScopeProvider DisposalScopeProvider;

/// <summary>
/// Default disposal scope, this or DisposalScopeProdiver must be set
/// </summary>
protected IDisposalScope DisposalScope;

/// <summary>
/// string constant that is used to locate a lock for adding strategies to the container
/// Note: Do not use this unless you are working on container internals
Expand Down Expand Up @@ -103,9 +93,6 @@ public InjectionScope(IInjectionScopeConfiguration configuration, IInjectionScop
StrategyCollectionContainer.AddStrategy(new FuncTypeStrategy(this));
}

DisposalScopeProvider = configuration.DisposalScopeProvider;

DisposalScope = DisposalScopeProvider == null ? this : null;
}

#endregion
Expand Down Expand Up @@ -146,8 +133,8 @@ public object Locate(Type type)
var func = ActivationDelegates[hashCode & ArrayLengthMinusOne].GetValueOrDefault(type, hashCode);

return func != null ?
func(this, DisposalScope ?? DisposalScopeProvider.ProvideDisposalScope(this), null) :
InternalLocate(this, DisposalScope ?? DisposalScopeProvider.ProvideDisposalScope(this), type, null, null, null, false, false);
func(this, this, null) :
InternalLocate(this, this, type, null, null, null, false, false);
}

/// <summary>
Expand All @@ -163,8 +150,8 @@ public object LocateOrDefault(Type type, object defaultValue)
var func = ActivationDelegates[hashCode & ArrayLengthMinusOne].GetValueOrDefault(type, hashCode);

return func != null ?
func(this, DisposalScope ?? DisposalScopeProvider.ProvideDisposalScope(this), null) :
InternalLocate(this, DisposalScope ?? DisposalScopeProvider.ProvideDisposalScope(this), type, null, null, null, true,false) ?? defaultValue;
func(this, this, null) :
InternalLocate(this, this, type, null, null, null, true,false) ?? defaultValue;
}

/// <summary>
Expand Down Expand Up @@ -210,11 +197,11 @@ public object Locate(Type type, object extraData = null, ActivationStrategyFilte

if (func != null)
{
return func(this, DisposalScope ?? DisposalScopeProvider.ProvideDisposalScope(this), context);
return func(this, this, context);
}
}

return InternalLocate(this, DisposalScope ?? DisposalScopeProvider.ProvideDisposalScope(this), type, consider, withKey, context, false, isDynamic);
return InternalLocate(this, this, type, consider, withKey, context, false, isDynamic);
}

/// <summary>
Expand Down Expand Up @@ -242,7 +229,7 @@ public T Locate<T>(object extraData = null, ActivationStrategyFilter consider =
/// <returns>list of all type</returns>
public List<object> LocateAll(Type type, object extraData = null, ActivationStrategyFilter consider = null, IComparer<object> comparer = null)
{
return ((IInjectionScope)this).InternalLocateAll(this, DisposalScope ?? DisposalScopeProvider.ProvideDisposalScope(this), type, extraData, consider, comparer);
return ((IInjectionScope)this).InternalLocateAll(this, this, type, extraData, consider, comparer);
}

/// <summary>
Expand All @@ -256,7 +243,7 @@ public List<object> LocateAll(Type type, object extraData = null, ActivationStra
/// <returns>list of all located</returns>
public List<T> LocateAll<T>(Type type = null, object extraData = null, ActivationStrategyFilter consider = null, IComparer<T> comparer = null)
{
return ((IInjectionScope)this).InternalLocateAll(this, DisposalScope ?? DisposalScopeProvider.ProvideDisposalScope(this), type ?? typeof(T), extraData, consider, comparer);
return ((IInjectionScope)this).InternalLocateAll(this, this, type ?? typeof(T), extraData, consider, comparer);
}

/// <summary>
Expand Down Expand Up @@ -319,7 +306,7 @@ public bool TryLocate(Type type, out object value, object extraData = null, Acti
public object LocateByName(string name, object extraData = null, ActivationStrategyFilter consider = null)
{
return ((IInjectionScope)this).LocateByNameFromChildScope(this,
DisposalScope ?? DisposalScopeProvider.ProvideDisposalScope(this), name, extraData, consider, false);
this, name, extraData, consider, false);
}

/// <summary>
Expand All @@ -331,8 +318,8 @@ public object LocateByName(string name, object extraData = null, ActivationStrat
/// <returns></returns>
public List<object> LocateAllByName(string name, object extraData = null, ActivationStrategyFilter consider = null)
{
return ((IInjectionScope)this).InternalLocateAllByName(this,
DisposalScope ?? DisposalScopeProvider.ProvideDisposalScope(this),
return ((IInjectionScope)this).InternalLocateAllByName(this,
this,
name,
extraData,
consider);
Expand All @@ -349,7 +336,7 @@ public List<object> LocateAllByName(string name, object extraData = null, Activa
public bool TryLocateByName(string name, out object value, object extraData = null, ActivationStrategyFilter consider = null)
{
value = ((IInjectionScope)this).LocateByNameFromChildScope(this,
DisposalScope ?? DisposalScopeProvider.ProvideDisposalScope(this), name, extraData, consider, true);
this, name, extraData, consider, true);

return value != null;
}
Expand All @@ -359,11 +346,9 @@ public bool TryLocateByName(string name, out object value, object extraData = nu
/// </summary>
/// <param name="scopeName">scope name</param>
/// <returns>new scope</returns>
public IExportLocatorScope BeginLifetimeScope(string scopeName = "")
public virtual IExportLocatorScope BeginLifetimeScope(string scopeName = "")
{
return InternalFieldStorage.LifetimeScopeProvider == null
? new LifetimeScope(this, this, scopeName, ActivationDelegates)
: InternalFieldStorage.LifetimeScopeProvider.CreateScope(this, scopeName, ActivationDelegates);
return new LifetimeScope(this, this, scopeName, ActivationDelegates);
}

/// <summary>
Expand Down Expand Up @@ -679,6 +664,19 @@ public IInjectionScope CreateChildScope(Action<IExportRegistrationBlock> configu
return newScope;
}

#if !NETSTANDARD1_0
object IServiceProvider.GetService(Type type)
{
var hashCode = type.GetHashCode();

var func = ActivationDelegates[hashCode & ArrayLengthMinusOne].GetValueOrDefault(type, hashCode);

return func != null ?
func(this, this, null) :
InternalLocate(this, this, type, null, null, null, true, false);
}
#endif

#endregion

#region Non public members
Expand Down Expand Up @@ -737,7 +735,7 @@ private object InternalLocate(IExportLocatorScope scope, IDisposalScope disposal
compiledDelegate = AddObjectFactory(type, compiledDelegate);
}

return compiledDelegate(scope, disposalScope ?? (DisposalScope ?? DisposalScopeProvider.ProvideDisposalScope(scope)), injectionContext);
return compiledDelegate(scope, disposalScope ?? this, injectionContext);
}

if (Parent != null)
Expand Down Expand Up @@ -855,6 +853,7 @@ private void LocateEnumerablesFromStrategyCollection<TStrategy, TValue>(IActivat
}
}


private string DebugDisplayString => "Exports: " + StrategyCollectionContainer.GetAllStrategies().Count();

#endregion
Expand Down Expand Up @@ -922,12 +921,7 @@ protected class InternalFieldStorageClass
/// Scope configuration
/// </summary>
public IInjectionScopeConfiguration ScopeConfiguration;

/// <summary>
/// Provides IExportLocatorScope when requested
/// </summary>
public ILifetimeScopeProvider LifetimeScopeProvider;


/// <summary>
/// Creates injection context when needed
/// </summary>
Expand Down
13 changes: 13 additions & 0 deletions src/Grace/DependencyInjection/Impl/LifetimeScope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -290,5 +290,18 @@ protected virtual object LocateFromParent(Type type, object extraData, Activatio
{
return _injectionScope.LocateFromChildScope(this, this, type, extraData, consider, key, allowNull, isDynamic);
}

#if !NETSTANDARD1_0
object IServiceProvider.GetService(Type type)
{
var hashCode = type.GetHashCode();

var func = ActivationDelegates[hashCode & ArrayLengthMinusOne].GetValueOrDefault(type, hashCode);

return func != null
? func(this, this, null)
: LocateFromParent(type, null, null, null, allowNull: true, isDynamic: false);
}
#endif
}
}
4 changes: 1 addition & 3 deletions src/Grace/DependencyInjection/InjectionExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,14 @@ public static void Inject(this IExportLocatorScope scope, object instance, objec
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);
injectionDelegate(scope, scope, context, instance);
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/Grace/Grace.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<Description>Grace is a feature rich Dependency Injection Container</Description>
<Authors>Ian Johnson</Authors>
<TargetFrameworks>netstandard1.0;net45</TargetFrameworks>
<TargetFrameworks>netstandard1.0;netstandard2.0;net45</TargetFrameworks>
<TargetFrameworks Condition="'$(OS)' != 'Windows_NT'">netstandard1.0</TargetFrameworks>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<AssemblyName>Grace</AssemblyName>
Expand Down

0 comments on commit 6dd492c

Please sign in to comment.