Skip to content

Commit

Permalink
Optional string parameter resolving when AnyConcreteTypeNotAlreadyReg…
Browse files Browse the repository at this point in the history
…isteredSource added
  • Loading branch information
NickRusinov committed Aug 17, 2016
1 parent 897425d commit 31439a1
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public IEnumerable<IComponentRegistration> RegistrationsFor(

var ts = service as TypedService;
if (ts == null ||
ts.ServiceType == typeof(string) ||
!ts.ServiceType.GetTypeInfo().IsClass ||
ts.ServiceType.GetTypeInfo().IsSubclassOf(typeof(Delegate)) ||
ts.ServiceType.GetTypeInfo().IsAbstract ||
Expand Down
37 changes: 37 additions & 0 deletions test/Autofac.Test/Features/ResolveAnything/ResolveAnythingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,43 @@ public void LifetimeCanBeSingleInstance()
}
}

public class RegiserTypeWithCtorParam
{
public RegiserTypeWithCtorParam(string stringParam = "MyString")
{
StringParam = stringParam;
}

public string StringParam { get; }
}

[Fact]
public void AllConcreteTypesSourceResolvesOptionalParams()
{
var cb = new ContainerBuilder();
cb.RegisterSource(new AnyConcreteTypeNotAlreadyRegisteredSource());
var container = cb.Build();

var resolved = container.Resolve<RegiserTypeWithCtorParam>();

Assert.Equal("MyString", resolved.StringParam);
}

[Fact]
public void AllConcreteTypesSourceAlreadyRegisteredResolvesOptionalParams()
{
var cb = new ContainerBuilder();

// Concrete type is already registered, but still errors
cb.RegisterType<RegiserTypeWithCtorParam>();
cb.RegisterSource(new AnyConcreteTypeNotAlreadyRegisteredSource());
var container = cb.Build();

var resolved = container.Resolve<RegiserTypeWithCtorParam>();

Assert.Equal("MyString", resolved.StringParam);
}

private static IContainer CreateResolveAnythingContainer()
{
var cb = new ContainerBuilder();
Expand Down

0 comments on commit 31439a1

Please sign in to comment.