Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Define the construction context #38

Merged
merged 13 commits into from
May 28, 2017
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: 1 addition & 1 deletion coverage.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
@SET test_assemblies=%~dp0test\Abioc.Tests\bin\%config%\Abioc.Tests.dll
@SET test_assemblies=%test_assemblies% %~dp0test\Abioc.Tests.Internal\bin\%config%\Abioc.Tests.Internal.dll
@SET xunit_results=%results_path%\Xunit.Tests.html
@SET coverage_filter=+[abioc*]* -[*.Tests]*
@SET coverage_filter=+[abioc*]* -[*.Tests]* -[Abioc.Tests.Internal]*
@SET coverage_results=%results_path%\Test.Coverage.xml

@IF NOT EXIST "%results_path%" MD "%results_path%"
Expand Down
14 changes: 7 additions & 7 deletions src/Abioc/Composition/Compositions/CompositionBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ public abstract class CompositionBase : IComposition
public abstract Type Type { get; }

/// <inheritdoc />
public abstract string GetInstanceExpression(GenerationContext context);
public abstract string GetInstanceExpression(IGenerationContext context);

/// <inheritdoc />
public abstract string GetComposeMethodName(GenerationContext context);
public abstract string GetComposeMethodName(IGenerationContext context);

/// <inheritdoc />
public virtual IEnumerable<string> GetMethods(GenerationContext context)
public virtual IEnumerable<string> GetMethods(IGenerationContext context)
{
if (context == null)
throw new ArgumentNullException(nameof(context));
Expand All @@ -32,7 +32,7 @@ public virtual IEnumerable<string> GetMethods(GenerationContext context)
}

/// <inheritdoc />
public virtual IEnumerable<(string snippet, object value)> GetFieldInitializations(GenerationContext context)
public virtual IEnumerable<(string snippet, object value)> GetFieldInitializations(IGenerationContext context)
{
if (context == null)
throw new ArgumentNullException(nameof(context));
Expand All @@ -41,7 +41,7 @@ public virtual IEnumerable<string> GetMethods(GenerationContext context)
}

/// <inheritdoc />
public IEnumerable<string> GetAdditionalInitializations(GenerationContext context)
public IEnumerable<string> GetAdditionalInitializations(IGenerationContext context)
{
if (context == null)
throw new ArgumentNullException(nameof(context));
Expand All @@ -50,7 +50,7 @@ public IEnumerable<string> GetAdditionalInitializations(GenerationContext contex
}

/// <inheritdoc />
public virtual IEnumerable<string> GetFields(GenerationContext context)
public virtual IEnumerable<string> GetFields(IGenerationContext context)
{
if (context == null)
throw new ArgumentNullException(nameof(context));
Expand All @@ -59,6 +59,6 @@ public virtual IEnumerable<string> GetFields(GenerationContext context)
}

/// <inheritdoc />
public abstract bool RequiresConstructionContext(GenerationContext context);
public abstract bool RequiresConstructionContext(IGenerationContext context);
}
}
16 changes: 9 additions & 7 deletions src/Abioc/Composition/Compositions/ConstructorComposition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,17 @@ public ConstructorComposition(
public bool IsDefault { get; }

/// <inheritdoc/>
public override string GetInstanceExpression(GenerationContext context)
public override string GetInstanceExpression(IGenerationContext context)
{
if (context == null)
throw new ArgumentNullException(nameof(context));

// Get the expressions for the all the constructor parameters.
IEnumerable<IParameterExpression> compositions = GetParameterExpressions(context);
IEnumerable<IParameterExpression> expressions = GetParameterExpressions(context);
IEnumerable<string> parameterExpressions =
compositions.Select(c => c.GetInstanceExpression(context));
from e in expressions
let ctx = context.Customize(recipientType: Type, serviceType: e.Type)
select e.GetInstanceExpression(ctx);

// Join the parameters expressions.
string parameters =
Expand All @@ -83,7 +85,7 @@ public override string GetInstanceExpression(GenerationContext context)
}

/// <inheritdoc/>
public override string GetComposeMethodName(GenerationContext context)
public override string GetComposeMethodName(IGenerationContext context)
{
if (context == null)
throw new ArgumentNullException(nameof(context));
Expand All @@ -93,7 +95,7 @@ public override string GetComposeMethodName(GenerationContext context)
}

/// <inheritdoc/>
public override IEnumerable<string> GetMethods(GenerationContext context)
public override IEnumerable<string> GetMethods(IGenerationContext context)
{
if (context == null)
throw new ArgumentNullException(nameof(context));
Expand All @@ -114,12 +116,12 @@ public override IEnumerable<string> GetMethods(GenerationContext context)
}

/// <inheritdoc/>
public override bool RequiresConstructionContext(GenerationContext context)
public override bool RequiresConstructionContext(IGenerationContext context)
{
return GetParameterExpressions(context).Any(c => c.RequiresConstructionContext(context));
}

private IEnumerable<IParameterExpression> GetParameterExpressions(GenerationContext context)
private IEnumerable<IParameterExpression> GetParameterExpressions(IGenerationContext context)
{
if (context == null)
throw new ArgumentNullException(nameof(context));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ public EnumerableParameterExpression(Type enumerableType, bool requiresConstruct
public Type EnumerableType { get; }

/// <inheritdoc />
public string GetInstanceExpression(GenerationContext context)
public Type Type => EnumerableType;

/// <inheritdoc />
public string GetInstanceExpression(IGenerationContext context)
{
if (context == null)
throw new ArgumentNullException(nameof(context));
Expand All @@ -52,7 +55,7 @@ public string GetInstanceExpression(GenerationContext context)
}

/// <inheritdoc />
public bool RequiresConstructionContext(GenerationContext context)
public bool RequiresConstructionContext(IGenerationContext context)
{
if (context == null)
throw new ArgumentNullException(nameof(context));
Expand Down
21 changes: 12 additions & 9 deletions src/Abioc/Composition/Compositions/FactoryComposition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public FactoryComposition(
public Type ConstructionContextType { get; }

/// <inheritdoc/>
public override string GetComposeMethodName(GenerationContext context)
public override string GetComposeMethodName(IGenerationContext context)
{
if (context == null)
throw new ArgumentNullException(nameof(context));
Expand All @@ -65,7 +65,7 @@ public override string GetComposeMethodName(GenerationContext context)
}

/// <inheritdoc/>
public override IEnumerable<string> GetMethods(GenerationContext context)
public override IEnumerable<string> GetMethods(IGenerationContext context)
{
if (context == null)
throw new ArgumentNullException(nameof(context));
Expand All @@ -86,8 +86,8 @@ public override IEnumerable<string> GetMethods(GenerationContext context)

string factoryCall =
requiresConstructionContext
? $"{GetFactoryFieldName()}(context)"
: $"{GetFactoryFieldName()}()";
? GetFactoryFieldName() + "(context)"
: GetFactoryFieldName() + "()";

string method = string.Format(
@"{0}
Expand Down Expand Up @@ -115,23 +115,26 @@ public override IEnumerable<string> GetMethods(GenerationContext context)
}

/// <inheritdoc />
public override string GetInstanceExpression(GenerationContext context)
public override string GetInstanceExpression(IGenerationContext context)
{
if (context == null)
throw new ArgumentNullException(nameof(context));

string factoryFieldName = GetComposeMethodName(context);

IEnumerable<string> expressions = context.GetUpdateParameterExpressions(implementationType: Type);
string updateParameters = string.Join(", ", expressions);

string instanceExpression =
RequiresConstructionContext()
? factoryFieldName + "(context)"
? factoryFieldName + $"(context.Update({updateParameters}))"
: factoryFieldName + "()";

return instanceExpression;
}

/// <inheritdoc />
public override IEnumerable<(string snippet, object value)> GetFieldInitializations(GenerationContext context)
public override IEnumerable<(string snippet, object value)> GetFieldInitializations(IGenerationContext context)
{
if (context == null)
throw new ArgumentNullException(nameof(context));
Expand All @@ -147,7 +150,7 @@ public override string GetInstanceExpression(GenerationContext context)
}

/// <inheritdoc />
public override IEnumerable<string> GetFields(GenerationContext context)
public override IEnumerable<string> GetFields(IGenerationContext context)
{
if (context == null)
throw new ArgumentNullException(nameof(context));
Expand All @@ -163,7 +166,7 @@ public override IEnumerable<string> GetFields(GenerationContext context)
}

/// <inheritdoc/>
public override bool RequiresConstructionContext(GenerationContext context)
public override bool RequiresConstructionContext(IGenerationContext context)
{
if (context == null)
throw new ArgumentNullException(nameof(context));
Expand Down
9 changes: 7 additions & 2 deletions src/Abioc/Composition/Compositions/IParameterExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ namespace Abioc.Composition.Compositions
/// </summary>
internal interface IParameterExpression
{
/// <summary>
/// Gets the type of the <see cref="IParameterExpression"/>.
/// </summary>
Type Type { get; }

/// <summary>
/// Gets the code for the expression (e.g. method call or instance field) to retrieve an instance of the
/// <see cref="Type"/>.
Expand All @@ -20,7 +25,7 @@ internal interface IParameterExpression
/// The code for the expression (e.g. method call or instance field) to retrieve an instance of the
/// <see cref="Type"/>.
/// </returns>
string GetInstanceExpression(GenerationContext context);
string GetInstanceExpression(IGenerationContext context);

/// <summary>
/// Returns the value indicating whether the <see cref="IParameterExpression"/> requires a
Expand All @@ -31,6 +36,6 @@ internal interface IParameterExpression
/// The value indicating whether the <see cref="IParameterExpression"/> requires a
/// <see cref="ConstructionContext{T}"/>.
/// </returns>
bool RequiresConstructionContext(GenerationContext context);
bool RequiresConstructionContext(IGenerationContext context);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public InjectedSingletonComposition(TImplementation value)
public TImplementation Value { get; }

/// <inheritdoc/>
public override string GetComposeMethodName(GenerationContext context)
public override string GetComposeMethodName(IGenerationContext context)
{
if (context == null)
throw new ArgumentNullException(nameof(context));
Expand All @@ -47,7 +47,7 @@ public override string GetComposeMethodName(GenerationContext context)
}

/// <inheritdoc/>
public override IEnumerable<string> GetMethods(GenerationContext context)
public override IEnumerable<string> GetMethods(IGenerationContext context)
{
if (context == null)
throw new ArgumentNullException(nameof(context));
Expand All @@ -73,7 +73,7 @@ public override IEnumerable<string> GetMethods(GenerationContext context)
}

/// <inheritdoc />
public override string GetInstanceExpression(GenerationContext context)
public override string GetInstanceExpression(IGenerationContext context)
{
if (context == null)
throw new ArgumentNullException(nameof(context));
Expand All @@ -83,7 +83,7 @@ public override string GetInstanceExpression(GenerationContext context)
}

/// <inheritdoc />
public override IEnumerable<(string snippet, object value)> GetFieldInitializations(GenerationContext context)
public override IEnumerable<(string snippet, object value)> GetFieldInitializations(IGenerationContext context)
{
if (context == null)
throw new ArgumentNullException(nameof(context));
Expand All @@ -96,7 +96,7 @@ public override string GetInstanceExpression(GenerationContext context)
}

/// <inheritdoc />
public override IEnumerable<string> GetFields(GenerationContext context)
public override IEnumerable<string> GetFields(IGenerationContext context)
{
if (context == null)
throw new ArgumentNullException(nameof(context));
Expand All @@ -109,7 +109,7 @@ public override IEnumerable<string> GetFields(GenerationContext context)
}

/// <inheritdoc/>
public override bool RequiresConstructionContext(GenerationContext context)
public override bool RequiresConstructionContext(IGenerationContext context)
{
if (context == null)
throw new ArgumentNullException(nameof(context));
Expand Down
22 changes: 11 additions & 11 deletions src/Abioc/Composition/Compositions/PropertyDependencyComposition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public PropertyDependencyComposition(
public (string property, Type type)[] PropertiesToInject { get; }

/// <inheritdoc />
public string GetInstanceExpression(GenerationContext context)
public string GetInstanceExpression(IGenerationContext context)
{
if (context == null)
throw new ArgumentNullException(nameof(context));
Expand All @@ -63,7 +63,7 @@ public string GetInstanceExpression(GenerationContext context)
}

/// <inheritdoc />
public string GetComposeMethodName(GenerationContext context)
public string GetComposeMethodName(IGenerationContext context)
{
if (context == null)
throw new ArgumentNullException(nameof(context));
Expand All @@ -73,7 +73,7 @@ public string GetComposeMethodName(GenerationContext context)
}

/// <inheritdoc />
public IEnumerable<string> GetMethods(GenerationContext context)
public IEnumerable<string> GetMethods(IGenerationContext context)
{
if (context == null)
throw new ArgumentNullException(nameof(context));
Expand All @@ -96,9 +96,9 @@ public IEnumerable<string> GetMethods(GenerationContext context)
instanceExpression = CodeGen.Indent(instanceExpression);

IEnumerable<string> propertyExpressions =
GetPropertyExpressions(context)
.Select(
pe => $"instance.{pe.property} = {pe.expression.GetInstanceExpression(context)};");
from pe in GetPropertyExpressions(context)
let ctx = context.Customize(recipientType: Type, serviceType: pe.expression.Type)
select $"instance.{pe.property} = {pe.expression.GetInstanceExpression(ctx)};";

string propertySetters = NewLine + string.Join(NewLine, propertyExpressions);
propertySetters = CodeGen.Indent(propertySetters);
Expand All @@ -113,7 +113,7 @@ public IEnumerable<string> GetMethods(GenerationContext context)
}

/// <inheritdoc />
public IEnumerable<string> GetFields(GenerationContext context)
public IEnumerable<string> GetFields(IGenerationContext context)
{
if (context == null)
throw new ArgumentNullException(nameof(context));
Expand All @@ -122,7 +122,7 @@ public IEnumerable<string> GetFields(GenerationContext context)
}

/// <inheritdoc />
public IEnumerable<(string snippet, object value)> GetFieldInitializations(GenerationContext context)
public IEnumerable<(string snippet, object value)> GetFieldInitializations(IGenerationContext context)
{
if (context == null)
throw new ArgumentNullException(nameof(context));
Expand All @@ -131,7 +131,7 @@ public IEnumerable<string> GetFields(GenerationContext context)
}

/// <inheritdoc />
public IEnumerable<string> GetAdditionalInitializations(GenerationContext context)
public IEnumerable<string> GetAdditionalInitializations(IGenerationContext context)
{
if (context == null)
throw new ArgumentNullException(nameof(context));
Expand All @@ -140,7 +140,7 @@ public IEnumerable<string> GetAdditionalInitializations(GenerationContext contex
}

/// <inheritdoc />
public bool RequiresConstructionContext(GenerationContext context)
public bool RequiresConstructionContext(IGenerationContext context)
{
if (context == null)
throw new ArgumentNullException(nameof(context));
Expand All @@ -150,7 +150,7 @@ public bool RequiresConstructionContext(GenerationContext context)
}

private IEnumerable<(string property, IParameterExpression expression)> GetPropertyExpressions(
GenerationContext context)
IGenerationContext context)
{
if (context == null)
throw new ArgumentNullException(nameof(context));
Expand Down
Loading