Skip to content

Commit

Permalink
minor fixes of enumerable
Browse files Browse the repository at this point in the history
  • Loading branch information
NikolayPianikov authored and NikolayPianikov committed Jun 20, 2018
1 parent 0d14b44 commit f609d85
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 15 deletions.
10 changes: 6 additions & 4 deletions IoC.Source/IoC.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1151,7 +1151,6 @@ namespace IoC
[DebuggerDisplay("Name = {" + nameof(ToString) + "()}")]
public sealed class Container: IContainer, IResourceStore, IObserver<ContainerEvent>
{
private const string RootName = "/";
private static long _containerId;

internal static readonly object[] EmptyArgs = Core.CollectionExtensions.EmptyArray<object>();
Expand Down Expand Up @@ -1217,7 +1216,7 @@ private static Container Create([NotNull] string name, [NotNull] IContainer pare

private static Container CreateRootContainer([NotNull][ItemNotNull] IEnumerable<IConfiguration> configurations)
{
var container = new Container(RootName, NullContainer.Shared, true);
var container = new Container(string.Empty, NullContainer.Shared, true);
container.ApplyConfigurations(configurations);
return container;
}
Expand Down Expand Up @@ -1252,13 +1251,14 @@ public bool TryRegister(IEnumerable<FullKey> keys, IDependency dependency, ILife
if (keys == null) throw new ArgumentNullException(nameof(keys));
if (dependency == null) throw new ArgumentNullException(nameof(dependency));
var isRegistered = true;
var registrationEntry = new RegistrationEntry(dependency, lifetime, Disposable.Create(UnregisterKeys), keys);
var registeredKeys = new List<FullKey>();
var registrationEntry = new RegistrationEntry(dependency, lifetime, Disposable.Create(UnregisterKeys), registeredKeys);

void UnregisterKeys()
{
lock (_lockObject)
{
foreach (var curKey in keys)
foreach (var curKey in registeredKeys)
{
if (curKey.Tag == AnyTag)
{
Expand Down Expand Up @@ -1307,6 +1307,8 @@ void UnregisterKeys()
{
break;
}

registeredKeys.Add(key);
}

if (isRegistered)
Expand Down
20 changes: 20 additions & 0 deletions IoC.Tests/IntegrationTests/ResolveEnumerablesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,26 @@ public void ContainerShouldResolveEnumerable()
}
}

[Fact]
public void ContainerShouldResolveGenericEnumerable()
{
// Given
using (var container = Container.Create())
{
Func<IMyService> func = Mock.Of<IMyService>;
// When
using (container.Bind<IMyService>().As(Lifetime.Singleton).To(ctx => func()))
using (container.Bind<IMyGenericService<TT>>().To(ctx => new MyGenericService<TT>(default(TT), ctx.Container.Inject<IMyService>())))
using (container.Bind<IMyGenericService<TT>>().Tag(1).To(ctx => new MyGenericService<TT>(default(TT), ctx.Container.Inject<IMyService>())))
using (container.Bind<IMyGenericService<TT>>().Tag("abc").To(ctx => new MyGenericService<TT>(default(TT), ctx.Container.Inject<IMyService>())))
{
// Then
var actualInstances = container.Resolve<IEnumerable<IMyGenericService<string>>>().ToList();
actualInstances.Count.ShouldBe(3);
}
}
}

[Fact]
public void ContainerShouldResolveEnumerableAfterContainerChanged()
{
Expand Down
10 changes: 6 additions & 4 deletions IoC/Container.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
[DebuggerDisplay("Name = {" + nameof(ToString) + "()}")]
public sealed class Container: IContainer, IResourceStore, IObserver<ContainerEvent>
{
private const string RootName = "/";
private static long _containerId;

internal static readonly object[] EmptyArgs = Core.CollectionExtensions.EmptyArray<object>();
Expand Down Expand Up @@ -90,7 +89,7 @@ private static Container Create([NotNull] string name, [NotNull] IContainer pare

private static Container CreateRootContainer([NotNull][ItemNotNull] IEnumerable<IConfiguration> configurations)
{
var container = new Container(RootName, NullContainer.Shared, true);
var container = new Container(string.Empty, NullContainer.Shared, true);
container.ApplyConfigurations(configurations);
return container;
}
Expand Down Expand Up @@ -125,13 +124,14 @@ public bool TryRegister(IEnumerable<FullKey> keys, IDependency dependency, ILife
if (keys == null) throw new ArgumentNullException(nameof(keys));
if (dependency == null) throw new ArgumentNullException(nameof(dependency));
var isRegistered = true;
var registrationEntry = new RegistrationEntry(dependency, lifetime, Disposable.Create(UnregisterKeys), keys);
var registeredKeys = new List<FullKey>();
var registrationEntry = new RegistrationEntry(dependency, lifetime, Disposable.Create(UnregisterKeys), registeredKeys);

void UnregisterKeys()
{
lock (_lockObject)
{
foreach (var curKey in keys)
foreach (var curKey in registeredKeys)
{
if (curKey.Tag == AnyTag)
{
Expand Down Expand Up @@ -180,6 +180,8 @@ void UnregisterKeys()
{
break;
}

registeredKeys.Add(key);
}

if (isRegistered)
Expand Down
2 changes: 1 addition & 1 deletion Samples/AspNetCore/WebApplication/WebApplication.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="IoC.AspNetCore" Version="1.1.0" />
<PackageReference Include="IoC.AspNetCore" Version="1.1.2" />
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.6" />
</ItemGroup>

Expand Down
2 changes: 1 addition & 1 deletion Samples/EntityFrameworkCore/EntityFrameworkCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="IoC.AspNetCore" Version="1.1.0" />
<PackageReference Include="IoC.AspNetCore" Version="1.1.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="2.1.0" />
</ItemGroup>

Expand Down
4 changes: 2 additions & 2 deletions Samples/ShroedingersCat/ShroedingersCat.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.0</TargetFramework>
<TargetFramework>netcoreapp2.1</TargetFramework>
<LangVersion>latest</LangVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="IoC.Container" Version="1.1.0" />
<PackageReference Include="IoC.Container" Version="1.1.2" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion Samples/UwpApp/UwpApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="IoC.Container">
<Version>1.1.0</Version>
<Version>1.1.2</Version>
</PackageReference>
<PackageReference Include="Microsoft.NETCore.UniversalWindowsPlatform">
<Version>6.1.5</Version>
Expand Down
2 changes: 1 addition & 1 deletion Samples/WcfServiceLibrary/WcfServiceLibrary.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="IoC.Container">
<Version>1.1.0</Version>
<Version>1.1.2</Version>
</PackageReference>
</ItemGroup>
<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion Samples/WpfApp/WpfApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="IoC.Container">
<Version>1.1.0</Version>
<Version>1.1.2</Version>
</PackageReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
Expand Down

0 comments on commit f609d85

Please sign in to comment.