Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,5 @@ $RECYCLE.BIN/
/.vs/config/applicationhost.config
/.vs/Grace/v15/sqlite3/storage.ide
/.vs/Grace/v15/sqlite3/storage.ide-journal
/.vs/Grace/v15
/.vs/Grace/DesignTimeBuild/.dtbcache
9 changes: 6 additions & 3 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
environment:
build_version: 6.3.3
Version: $(build_version)
build_version: 6.3.4
Version: $(build_version)-Nightly%APPVEYOR_BUILD_NUMBER%
COVERALLS_REPO_TOKEN:
secure: +OWHMxYHaMp6iRNNLZcMZq423PhYWxMky+B2C0p3U8v7tpdoKRMzWZKJ1LuYO60O
version: $(build_version)-{build}
Expand All @@ -24,7 +24,10 @@ after_build:
test_script:
- cmd: cd tests/Grace.Tests/
- cmd: CodeCoverageAppVeyor.cmd
- sh: dotnet test tests/Grace.Tests/Grace.Tests.csproj
artifacts:
- path: Grace*.nupkg
name: Grace
os: Visual Studio 2017
image:
- Visual Studio 2017
- Ubuntu
12 changes: 12 additions & 0 deletions src/Grace.Dynamic/Grace.Dynamic.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<Description>IL Generation library for Grace dependency injection container</Description>
<Authors>Ian Johnson</Authors>
<TargetFrameworks>netstandard1.1;net45</TargetFrameworks>
<TargetFrameworks Condition="'$(OS)' != 'Windows_NT'">netstandard1.1</TargetFrameworks>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<AssemblyName>Grace.Dynamic</AssemblyName>
<AssemblyOriginatorKeyFile>..\Grace.snk</AssemblyOriginatorKeyFile>
Expand All @@ -22,10 +23,21 @@
<DebugType>full</DebugType>
</PropertyGroup>

<PropertyGroup Condition="'$(OS)' == 'Windows_NT'">
<!-- Nuget source link -->
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<EmbedUntrackedSources>true</EmbedUntrackedSources>
<AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Grace\Grace.csproj" />
</ItemGroup>

<ItemGroup Condition="'$(OS)' == 'Windows_NT'">
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0-beta-62909-01" PrivateAssets="All" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard1.1' ">
<PackageReference Include="System.Reflection.Emit" Version="4.0.1" />
<PackageReference Include="System.Reflection.Emit.Lightweight" Version="4.0.1" />
Expand Down
14 changes: 13 additions & 1 deletion src/Grace.Factory/Grace.Factory.csproj
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Description>IL Generation library for Grace dependency injection container</Description>
<Authors>Ian Johnson</Authors>
<TargetFrameworks>netstandard1.1;net45</TargetFrameworks>
<TargetFrameworks Condition="'$(OS)' != 'Windows_NT'">netstandard1.1</TargetFrameworks>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<AssemblyName>Grace.Factory</AssemblyName>
<AssemblyOriginatorKeyFile>..\Grace.snk</AssemblyOriginatorKeyFile>
Expand All @@ -22,10 +23,21 @@
<DebugType>full</DebugType>
</PropertyGroup>

<PropertyGroup Condition="'$(OS)' == 'Windows_NT'">
<!-- Nuget source link -->
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<EmbedUntrackedSources>true</EmbedUntrackedSources>
<AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Grace\Grace.csproj" />
</ItemGroup>

<ItemGroup Condition="'$(OS)' == 'Windows_NT'">
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0-beta-62909-01" PrivateAssets="All" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard1.1' ">
<PackageReference Include="System.Reflection.Emit" Version="4.0.1" />
<PackageReference Include="System.Reflection.Emit.Lightweight" Version="4.0.1" />
Expand Down
2 changes: 1 addition & 1 deletion src/Grace/DependencyInjection/IExportRegistrationBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public interface IExportRegistrationBlock
/// <param name="apply">decorator logic</param>
/// <param name="applyAfterLifestyle"></param>
void ExportDecorator<T>(Func<T, T> apply, bool applyAfterLifestyle = true);

/// <summary>
/// Export an expression tree
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Linq.Expressions;
using System.Reflection;
using Grace.DependencyInjection.Impl;
using Grace.DependencyInjection.Impl.CompiledStrategies;
using Grace.DependencyInjection.Impl.Expressions;

namespace Grace.DependencyInjection
Expand Down Expand Up @@ -117,6 +118,12 @@ public static IFluentExportInstanceConfiguration<T> ExportNamedValue<T>(
throw new Exception("This method can only be used on members (i.e. ExportNamedValue(() => SomeProperty))");
}

public static void ExportDecoratorFactory<T, TResult>(this IExportRegistrationBlock registrationBlock,
Func<T, TResult> factory)
{
registrationBlock.AddActivationStrategy(new CompiledFactoryDecoratorStrategy<TResult>(factory, registrationBlock.OwningScope));
}

/// <summary>
/// Import all members of a specific type and can be filtered
/// </summary>
Expand Down Expand Up @@ -280,11 +287,11 @@ public static IExportRegistrationBlock ExcludeTypeFromAutoRegistration(this IExp
public static IExportRegistrationBlock ExportInitialize<T>(this IExportRegistrationBlock block,
Action<T> initializeAction)
{
var func = new Func<object,object>(instance =>
{
initializeAction((T) instance);
return instance;
});
var func = new Func<object, object>(instance =>
{
initializeAction((T)instance);
return instance;
});

block.AddInspector(new ExportInitializeInspector(func, typeof(T)));

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using System;
using System.Collections.Generic;
using System.Text;
using Grace.DependencyInjection.Impl.Expressions;
using Grace.DependencyInjection.Lifestyle;

namespace Grace.DependencyInjection.Impl.CompiledStrategies
{
public class CompiledFactoryDecoratorStrategy<T> : ConfigurableActivationStrategy, ICompiledDecoratorStrategy
{
private Delegate _delegate;

/// <summary>
/// Default constructor
/// </summary>
/// <param name="delegate"></param>
/// <param name="injectionScope">owning injection scope</param>
public CompiledFactoryDecoratorStrategy(Delegate @delegate, IInjectionScope injectionScope) : base(typeof(T), injectionScope)
{
_delegate = @delegate;
}

/// <summary>
/// Type of activation strategy
/// </summary>
public override ActivationStrategyType StrategyType { get; } = ActivationStrategyType.DecoratorStrategy;

/// <summary>
/// Get an activation expression for this strategy
/// </summary>
/// <param name="scope"></param>
/// <param name="request"></param>
/// <param name="lifestyle"></param>
/// <returns></returns>
public IActivationExpressionResult GetDecoratorActivationExpression(IInjectionScope scope,
IActivationExpressionRequest request, ICompiledLifestyle lifestyle)
{
if (lifestyle == null)
{
return InternalGetDecoratorActivationExpression(scope, request);
}

if (ApplyAfterLifestyle)
{
return lifestyle.ProvideLifestyleExpression(
scope, request, lifestyleRequest => InternalGetDecoratorActivationExpression(scope, lifestyleRequest));
}

return lifestyle.ProvideLifestyleExpression(
scope, request, lifestyleRequest => InternalGetDecoratorActivationExpression(scope, request));
}

/// <summary>
/// Apply the decorator after a lifestyle has been used
/// </summary>
public bool ApplyAfterLifestyle { get; set; }

/// <summary>
/// Get decorator expression
/// </summary>
/// <param name="scope"></param>
/// <param name="request"></param>
/// <returns></returns>
protected virtual IActivationExpressionResult InternalGetDecoratorActivationExpression(IInjectionScope scope, IActivationExpressionRequest request)
{
return ExpressionUtilities.CreateExpressionForDelegate(_delegate, false, InjectionScope, request, this);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,8 @@ protected virtual IActivationExpressionResult GetExpressionFromStrategyCollectio
{
var strategy = request.Filter == null ? collection.GetPrimary() : null;

if (strategy != null)
if (strategy != null &&
strategy != request.RequestingStrategy)
{
var result = ActivationExpressionForStrategy(scope, request, strategy);

Expand Down Expand Up @@ -622,6 +623,11 @@ protected virtual IActivationExpressionResult SelectStrategyFromCollection(IActi
}
}

if (request.RequestingStrategy == strategy)
{
continue;
}

result = strategy.GetActivationExpression(scope, request);

if (result != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ protected IActivationExpressionResult CallExportFunc(IActivationStrategy strateg
var newRequest = request.NewRequest(parameter.ParameterType, strategy, strategy.ActivationType,
RequestType.ConstructorParameter, parameter, false, true);

return ExpressionUtilities.CreateExpressionForDelegate(exportDelegate, ShouldTrackDisposable(configurationExternallyOwned, injectionScope, strategy), injectionScope, newRequest);
return ExpressionUtilities.CreateExpressionForDelegate(exportDelegate, ShouldTrackDisposable(configurationExternallyOwned, injectionScope, strategy), injectionScope, newRequest, strategy);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,25 +74,16 @@ public IActivationExpressionResult CreateExpression(IInjectionScope scope, IActi
/// <summary>
/// Method info for add method on IDisposalScope
/// </summary>
protected MethodInfo AddMethod
{
get
{
return _addMethod ??
(_addMethod = typeof(IDisposalScope).GetTypeInfo().DeclaredMethods.First(m => m.Name == "AddDisposable" && m.GetParameters().Length == 1));
}
}
protected MethodInfo AddMethod=> _addMethod ??
(_addMethod = typeof(IDisposalScope).GetTypeInfo().DeclaredMethods.First(m => m.Name == "AddDisposable" &&
m.GetParameters().Length == 1));


/// <summary>
/// Method info for add method on IDisposalScope with cleanup delegate
/// </summary>
protected MethodInfo AddMethodWithCleanup
{
get
{
return _addMethod ??
(_addMethod = typeof(IDisposalScope).GetTypeInfo().DeclaredMethods.First(m => m.Name == "AddDisposable" && m.GetParameters().Length == 2));
}
}
protected MethodInfo AddMethodWithCleanup => _addMethod ??
(_addMethod = typeof(IDisposalScope).GetTypeInfo().DeclaredMethods.First(m => m.Name == "AddDisposable" &&
m.GetParameters().Length == 2));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public override IActivationExpressionResult CreateExpression(IInjectionScope sco
protected virtual IActivationExpressionResult CreateCallExpression(IInjectionScope scope, IActivationExpressionRequest request, TypeActivationConfiguration activationConfiguration, ActivationStrategyDelegate activationDelegate)
{
return ExpressionUtilities.CreateExpressionForDelegate(activationDelegate, activationConfiguration.ExternallyOwned,
scope, request);
scope, request, activationConfiguration.ActivationStrategy);
}

/// <summary>
Expand Down Expand Up @@ -220,7 +220,7 @@ private Expression CreateLocateExpression(ParameterInfo parameter, IInjectionSco
var delegateValue = (Delegate)parameterInfo.ExportFunc;

var expressionCall = ExpressionUtilities.CreateExpressionForDelegate(delegateValue,
activationConfiguration.ExternallyOwned, scope, newRequest);
activationConfiguration.ExternallyOwned, scope, newRequest, activationConfiguration.ActivationStrategy);

var standardParameters = delegateValue.GetMethodInfo()
.GetParameters()
Expand Down Expand Up @@ -255,7 +255,7 @@ private Expression CreateLocateExpression(ParameterInfo parameter, IInjectionSco
var compiledDelegate = request.Services.Compiler.CompileDelegate(scope, expressionCall);

expressionCall =
ExpressionUtilities.CreateExpressionForDelegate(compiledDelegate, false, scope, newRequest);
ExpressionUtilities.CreateExpressionForDelegate(compiledDelegate, false, scope, newRequest, activationConfiguration.ActivationStrategy);

return expressionCall.Expression;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,10 @@ public static IActivationExpressionResult[] CreateExpressionsForTypes(IActivatio
/// <param name="allowDisposableTracking"></param>
/// <param name="scope"></param>
/// <param name="request"></param>
/// <param name="requestingStrategy"></param>
/// <returns></returns>
public static IActivationExpressionResult CreateExpressionForDelegate(Delegate delegateInstance, bool allowDisposableTracking, IInjectionScope scope,
IActivationExpressionRequest request)
IActivationExpressionRequest request, IActivationStrategy requestingStrategy)
{
var methodInfo = delegateInstance.GetMethodInfo();

Expand All @@ -61,7 +62,7 @@ public static IActivationExpressionResult CreateExpressionForDelegate(Delegate d
// Handle closure based delegates differently
if (delegateInstance.Target != null && delegateInstance.Target.GetType().FullName == _closureName)
{
resultsExpressions = CreateExpressionsForTypes(request.RequestingStrategy, scope, request, methodInfo.ReturnType,
resultsExpressions = CreateExpressionsForTypes(requestingStrategy, scope, request, methodInfo.ReturnType,
methodInfo.GetParameters().
Where(p => !(p.Position == 0 && p.ParameterType.FullName == "System.Runtime.CompilerServices.Closure")).
Select(p => p.ParameterType).ToArray());
Expand All @@ -72,7 +73,7 @@ public static IActivationExpressionResult CreateExpressionForDelegate(Delegate d
}
else
{
resultsExpressions = CreateExpressionsForTypes(request.RequestingStrategy, scope, request, methodInfo.ReturnType,
resultsExpressions = CreateExpressionsForTypes(requestingStrategy, scope, request, methodInfo.ReturnType,
methodInfo.GetParameters().Select(p => p.ParameterType).ToArray());

expression = methodInfo.IsStatic
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ public bool SetupWrappersForRequest(IInjectionScope scope, IActivationExpression
{
var primary = request.Filter == null ? collection.GetPrimary() : null;

if (primary != null)
if (primary != null && primary != request.RequestingStrategy)
{
wrappers = ImmutableLinkedList<IActivationPathNode>.Empty
.Add(new WrapperActivationPathNode(primary, wrappedType, null))
Expand All @@ -207,6 +207,7 @@ public bool SetupWrappersForRequest(IInjectionScope scope, IActivationExpression
}

if (pass &&
request.RequestingStrategy != strategy &&
(request.Filter == null || request.Filter(strategy)))
{
wrappers = ImmutableLinkedList<IActivationPathNode>.Empty
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ public FluentDecoratorStrategyConfiguration(ICompiledDecoratorStrategy strategy)
{
_strategy = strategy;
}



/// <summary>
/// Apply decorator after lifestyle, by default it's before
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public class InjectionContextValueProvider : IInjectionContextValueProvider
/// <summary>
/// Get data from injection context
/// </summary>
/// <param name="scope"></param>
/// <param name="locator"></param>
/// <param name="type"></param>
/// <param name="key"></param>
/// <param name="context"></param>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ protected override IActivationExpressionResult CreateExpression(IInjectionScope
protected virtual IActivationExpressionResult CreateExpression(IInjectionScope scope,
IActivationExpressionRequest request)
{
return ExpressionUtilities.CreateExpressionForDelegate(DelegateInstance, ShouldTrackDisposable(scope), scope, request);
return ExpressionUtilities.CreateExpressionForDelegate(DelegateInstance, ShouldTrackDisposable(scope), scope, request, this);
}

private bool ShouldTrackDisposable(IInjectionScope scope)
Expand Down
Loading