Skip to content

Commit

Permalink
task: improve constructor resolution in aspnet core dependency injection
Browse files Browse the repository at this point in the history
  • Loading branch information
alastairtree committed Dec 28, 2019
1 parent 061ee04 commit 74615ce
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 8 deletions.
4 changes: 3 additions & 1 deletion LazyCache.AspNetCore/LazyCacheServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ public static IServiceCollection AddLazyCache(this IServiceCollection services)
services.TryAdd(ServiceDescriptor.Singleton<IMemoryCache, MemoryCache>());
services.TryAdd(ServiceDescriptor.Singleton<ICacheProvider, MemoryCacheProvider>());

services.TryAdd(ServiceDescriptor.Singleton<IAppCache, CachingService>());
services.TryAdd(ServiceDescriptor.Singleton<IAppCache, CachingService>(serviceProvider =>
new CachingService(
new Lazy<ICacheProvider>(serviceProvider.GetRequiredService<ICacheProvider>))));

return services;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
<TargetFramework>netcoreapp3.1</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>
Expand All @@ -16,10 +16,4 @@
<ProjectReference Include="..\LazyCache.Ninject\LazyCache.Ninject.csproj" />
</ItemGroup>

<ItemGroup>
<Reference Include="Ninject">
<HintPath>C:\Users\Alastair\.nuget\packages\ninject\3.3.4\lib\netstandard2.0\Ninject.dll</HintPath>
</Reference>
</ItemGroup>

</Project>
42 changes: 42 additions & 0 deletions LazyCache.UnitTests/AspNetCoreTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FluentAssertions;
using Microsoft.Extensions.DependencyInjection;
using NUnit.Framework;

namespace LazyCache.UnitTests
{
public class AspNetCoreTests
{
[Test]
public void CanResolveCacheFromServiceCollectionAsRequiredService()
{
var container = new ServiceCollection();
container.AddLazyCache();
var provider = container.BuildServiceProvider();

var cache = provider.GetRequiredService<IAppCache>();
var result = cache?.GetOrAdd("key", () => new object());

cache.Should().NotBeNull();
result.Should().NotBeNull();
}

[Test]
public void CanResolveCacheFromServiceCollectionAsService()
{
var container = new ServiceCollection();
container.AddLazyCache();
var provider = container.BuildServiceProvider();

var cache = provider.GetService<IAppCache>();
var result = cache?.GetOrAdd("key", () => new object());

cache.Should().NotBeNull();
result.Should().NotBeNull();
}
}
}
2 changes: 2 additions & 0 deletions LazyCache.UnitTests/LazyCache.UnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@

<ItemGroup>
<PackageReference Include="fluentassertions" Version="5.9.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.1.1" />
<PackageReference Include="nunit" Version="3.12.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.15.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.4.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\LazyCache.AspNetCore\LazyCache.AspNetCore.csproj" />
<ProjectReference Include="..\LazyCache\LazyCache.csproj" />
</ItemGroup>

Expand Down

0 comments on commit 74615ce

Please sign in to comment.