Skip to content

Commit

Permalink
throwing exception for bad open/closed registration issue #216
Browse files Browse the repository at this point in the history
  • Loading branch information
ipjohnson committed Apr 23, 2019
1 parent 5218810 commit aa61d7a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,17 @@ public FluentExportStrategyConfiguration(IConfigurableActivationStrategy exportC
/// Export as a specific type
/// </summary>
/// <param name="type">type to export as</param>
/// <returns>configuraiton object</returns>
/// <returns>configuration object</returns>
public IFluentExportStrategyConfiguration As(Type type)
{
if (type == null) throw new ArgumentNullException(nameof(type));

if (type.GetTypeInfo().IsGenericTypeDefinition &&
!_exportConfiguration.ActivationType.GetTypeInfo().IsGenericTypeDefinition)
{
throw new ArgumentException("Exported type is not open generic but As type is open");
}

_exportConfiguration.AddExportAs(type);

return this;
Expand All @@ -64,6 +70,12 @@ public IFluentExportStrategyConfiguration AsKeyed(Type type, object key)
if (type == null) throw new ArgumentNullException(nameof(type));
if (key == null) throw new ArgumentNullException(nameof(key));

if (type.GetTypeInfo().IsGenericTypeDefinition &&
!_exportConfiguration.ActivationType.GetTypeInfo().IsGenericTypeDefinition)
{
throw new ArgumentException("Exported type is not open generic but As type is open");
}

_exportConfiguration.AddExportAsKeyed(type, key);

return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,22 @@ public void FluentExportStrategyConfiguration_With_Metadata()
Assert.Equal("Value", metadata.Metadata["Data"]);
}

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

Assert.Throws<ArgumentException>(() => container.Configure(c => c.Export(typeof(DependentService<IBasicService>)).As(typeof(IDependentService<>))));
}


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

Assert.Throws<ArgumentException>(() => container.Configure(c => c.Export(typeof(DependentService<IBasicService>)).AsKeyed(typeof(IDependentService<>), 1)));
}
#endregion

#region ImportAttribute tests
Expand All @@ -164,7 +180,7 @@ public void ImportAttribute_Honored_On_Constructor()
Assert.NotNull(classA);
Assert.NotNull(classA.BasicService);
Assert.Equal(10, classA.BasicService.Count);

var classB = container.Locate<ImportClassB>();

Assert.NotNull(classB);
Expand Down

0 comments on commit aa61d7a

Please sign in to comment.