diff --git a/src/Grace/DependencyInjection/IFluentWithCtorConfiguration.cs b/src/Grace/DependencyInjection/IFluentWithCtorConfiguration.cs index 3f8ae140..fed92db2 100644 --- a/src/Grace/DependencyInjection/IFluentWithCtorConfiguration.cs +++ b/src/Grace/DependencyInjection/IFluentWithCtorConfiguration.cs @@ -64,6 +64,13 @@ public interface IFluentWithCtorConfiguration : IFluentExportStrategy /// /// IFluentWithCtorConfiguration Named(string name); + + /// + /// Use a specific type for parameter + /// + /// + /// + IFluentWithCtorConfiguration Use(Type type); } /// @@ -129,5 +136,12 @@ public interface IFluentWithCtorConfiguration : IFluentExportStrat /// /// IFluentWithCtorConfiguration Named(string name); + + /// + /// Use specific type to satisfy parameter + /// + /// + /// + IFluentWithCtorConfiguration Use(Type type); } } diff --git a/src/Grace/DependencyInjection/Impl/ConstructorParameterInfo.cs b/src/Grace/DependencyInjection/Impl/ConstructorParameterInfo.cs index 343c9127..bf9126e5 100644 --- a/src/Grace/DependencyInjection/Impl/ConstructorParameterInfo.cs +++ b/src/Grace/DependencyInjection/Impl/ConstructorParameterInfo.cs @@ -61,5 +61,10 @@ public ConstructorParameterInfo(object exportFunc) /// Is the parameter dynamic /// public bool IsDynamic { get; set; } + + /// + /// Use specific type for parameter + /// + public Type UseType { get; set; } } } diff --git a/src/Grace/DependencyInjection/Impl/Expressions/InstantiationExpressionCreator.cs b/src/Grace/DependencyInjection/Impl/Expressions/InstantiationExpressionCreator.cs index 6a383da1..1d1fd1d5 100644 --- a/src/Grace/DependencyInjection/Impl/Expressions/InstantiationExpressionCreator.cs +++ b/src/Grace/DependencyInjection/Impl/Expressions/InstantiationExpressionCreator.cs @@ -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) { diff --git a/src/Grace/DependencyInjection/Impl/FluentWithCtorConfiguration.cs b/src/Grace/DependencyInjection/Impl/FluentWithCtorConfiguration.cs index 5a7a869e..eb6214c8 100644 --- a/src/Grace/DependencyInjection/Impl/FluentWithCtorConfiguration.cs +++ b/src/Grace/DependencyInjection/Impl/FluentWithCtorConfiguration.cs @@ -35,6 +35,18 @@ public IFluentWithCtorConfiguration Named(string name) return this; } + /// + /// Use a specific type for parameter + /// + /// + /// + public IFluentWithCtorConfiguration Use(Type type) + { + _constructorParameterInfo.UseType = type; + + return this; + } + /// /// Applies a filter to be used when resolving a parameter constructor /// It will be called each time the parameter is resolved @@ -248,5 +260,17 @@ public IFluentWithCtorConfiguration Named(string name) return this; } + + /// + /// Use specific type to satisfy parameter + /// + /// + /// + public IFluentWithCtorConfiguration Use(Type type) + { + _constructorParameterInfo.UseType = type; + + return this; + } } } diff --git a/tests/Grace.Tests/DependencyInjection/Registration/FluentWithCtorConfigurationTests.cs b/tests/Grace.Tests/DependencyInjection/Registration/FluentWithCtorConfigurationTests.cs index fbf1bece..98ae32c9 100644 --- a/tests/Grace.Tests/DependencyInjection/Registration/FluentWithCtorConfigurationTests.cs +++ b/tests/Grace.Tests/DependencyInjection/Registration/FluentWithCtorConfigurationTests.cs @@ -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)) + .WithCtorParam() + .Use(typeof(BasicService[])); + }); + + var instance = container.Locate>(); + + Assert.NotNull(instance); + Assert.Equal(1, instance.Value.Length); + Assert.IsType(instance.Value[0]); + } + + [Fact] + public void WithCtor_Generic_Use() + { + var container = new DependencyInjectionContainer(); + + container.Configure(c => + { + c.Export>() + .WithCtorParam() + .Use(typeof(BasicService[])); + }); + + var instance = container.Locate>(); + + Assert.NotNull(instance); + Assert.Equal(1, instance.Value.Length); + Assert.IsType(instance.Value[0]); + } + + #endregion + } }