Skip to content

Commit

Permalink
fixes #46 adds Use(Type) method for WithCtorParam
Browse files Browse the repository at this point in the history
  • Loading branch information
ipjohnson committed Feb 16, 2017
1 parent ab96126 commit 7df3a8f
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 1 deletion.
14 changes: 14 additions & 0 deletions src/Grace/DependencyInjection/IFluentWithCtorConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ public interface IFluentWithCtorConfiguration<in TParam> : IFluentExportStrategy
/// <param name="name"></param>
/// <returns></returns>
IFluentWithCtorConfiguration<TParam> Named(string name);

/// <summary>
/// Use a specific type for parameter
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
IFluentWithCtorConfiguration<TParam> Use(Type type);
}

/// <summary>
Expand Down Expand Up @@ -129,5 +136,12 @@ public interface IFluentWithCtorConfiguration<T, in TParam> : IFluentExportStrat
/// <param name="name"></param>
/// <returns></returns>
IFluentWithCtorConfiguration<T, TParam> Named(string name);

/// <summary>
/// Use specific type to satisfy parameter
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
IFluentWithCtorConfiguration<T, TParam> Use(Type type);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,10 @@ public ConstructorParameterInfo(object exportFunc)
/// Is the parameter dynamic
/// </summary>
public bool IsDynamic { get; set; }

/// <summary>
/// Use specific type for parameter
/// </summary>
public Type UseType { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,12 @@ protected virtual IActivationExpressionResult GetParameterExpression(ParameterIn
return CallExportFunc(configuration.ActivationStrategy, parameter, parameterInfo, injectionScope, request, configuration.ExternallyOwned);
}

var newRequest = request.NewRequest(parameter.ParameterType, configuration.ActivationStrategy, configuration.ActivationType, RequestType.ConstructorParameter, parameter, true);
var newRequest = request.NewRequest(parameterInfo?.UseType ?? parameter.ParameterType,
configuration.ActivationStrategy,
configuration.ActivationType,
RequestType.ConstructorParameter,
parameter,
true);

if (parameterInfo?.LocateWithKey != null)
{
Expand Down
24 changes: 24 additions & 0 deletions src/Grace/DependencyInjection/Impl/FluentWithCtorConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@ public IFluentWithCtorConfiguration<TParam> Named(string name)
return this;
}

/// <summary>
/// Use a specific type for parameter
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
public IFluentWithCtorConfiguration<TParam> Use(Type type)
{
_constructorParameterInfo.UseType = type;

return this;
}

/// <summary>
/// Applies a filter to be used when resolving a parameter constructor
/// It will be called each time the parameter is resolved
Expand Down Expand Up @@ -248,5 +260,17 @@ public IFluentWithCtorConfiguration<T, TParam> Named(string name)

return this;
}

/// <summary>
/// Use specific type to satisfy parameter
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
public IFluentWithCtorConfiguration<T, TParam> Use(Type type)
{
_constructorParameterInfo.UseType = type;

return this;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -266,5 +266,47 @@ public void ExportNamedValue()
}
#endregion

#region Use Tests

[Fact]
public void WithCtor_Use()
{
var container = new DependencyInjectionContainer();

container.Configure(c =>
{
c.Export(typeof(DependentService<IBasicService[]>))
.WithCtorParam<IBasicService[]>()
.Use(typeof(BasicService[]));
});

var instance = container.Locate<DependentService<IBasicService[]>>();

Assert.NotNull(instance);
Assert.Equal(1, instance.Value.Length);
Assert.IsType<BasicService>(instance.Value[0]);
}

[Fact]
public void WithCtor_Generic_Use()
{
var container = new DependencyInjectionContainer();

container.Configure(c =>
{
c.Export<DependentService<IBasicService[]>>()
.WithCtorParam<IBasicService[]>()
.Use(typeof(BasicService[]));
});

var instance = container.Locate<DependentService<IBasicService[]>>();

Assert.NotNull(instance);
Assert.Equal(1, instance.Value.Length);
Assert.IsType<BasicService>(instance.Value[0]);
}

#endregion

}
}

1 comment on commit 7df3a8f

@ipjohnson
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixes #64

Please sign in to comment.