Skip to content

Commit e413ee3

Browse files
committed
Service extensions now return the service collection
1 parent 4b1bbb6 commit e413ee3

File tree

16 files changed

+280
-99
lines changed

16 files changed

+280
-99
lines changed

src/MyTested.AspNetCore.Mvc.Caching/ServiceCollectionCachingExtensions.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ public static class ServiceCollectionCachingExtensions
1313
/// Replaces the default <see cref="IMemoryCache"/> with a mocked implementation.
1414
/// </summary>
1515
/// <param name="serviceCollection">Instance of <see cref="IServiceCollection"/> type.</param>
16-
public static void ReplaceMemoryCache(this IServiceCollection serviceCollection)
16+
/// <returns>The same <see cref="IServiceCollection"/>.</returns>
17+
public static IServiceCollection ReplaceMemoryCache(this IServiceCollection serviceCollection)
1718
{
18-
serviceCollection.Replace<IMemoryCache, MockedMemoryCache>(ServiceLifetime.Transient);
19+
return serviceCollection.Replace<IMemoryCache, MockedMemoryCache>(ServiceLifetime.Transient);
1920
}
2021
}
2122
}

src/MyTested.AspNetCore.Mvc.Core/ServiceCollectionExtensions.cs

Lines changed: 129 additions & 75 deletions
Large diffs are not rendered by default.

src/MyTested.AspNetCore.Mvc.Core/Utilities/Extensions/EnumerableExtensions.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@ public static class EnumerableExtensions
1414
/// <typeparam name="T">Type of objects in the collection.</typeparam>
1515
/// <param name="collection">Collection to traverse.</param>
1616
/// <param name="action">Function to execute on each item in the collection.</param>
17-
public static void ForEach<T>(this IEnumerable<T> collection, Action<T> action)
17+
public static IEnumerable<T> ForEach<T>(this IEnumerable<T> collection, Action<T> action)
1818
{
1919
foreach (var item in collection)
2020
{
2121
action(item);
2222
}
23+
24+
return collection;
2325
}
2426
}
2527
}

src/MyTested.AspNetCore.Mvc.EntityFrameworkCore/ServiceCollectionEntityFrameworkCoreExtensions.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ public static class ServiceCollectionEntityFrameworkCoreExtensions
2525
/// Replaces the registered <see cref="DbContext"/> with an in memory scoped implementation.
2626
/// </summary>
2727
/// <param name="serviceCollection">Instance of <see cref="IServiceCollection"/> type.</param>
28-
public static void ReplaceDbContext(this IServiceCollection serviceCollection)
28+
/// <returns>The same <see cref="IServiceCollection"/>.</returns>
29+
public static IServiceCollection ReplaceDbContext(this IServiceCollection serviceCollection)
2930
{
3031
var existingDbContextOptionsService =
3132
serviceCollection.FirstOrDefault(s => BaseDbContextOptionsType.IsAssignableFrom(s.ServiceType));
@@ -51,6 +52,8 @@ public static void ReplaceDbContext(this IServiceCollection serviceCollection)
5152
var genericMethod = ReplaceDatabaseMethodInfo.MakeGenericMethod(existingDbContext.ImplementationType);
5253
genericMethod.Invoke(null, new object[] { serviceCollection });
5354
}
55+
56+
return serviceCollection;
5457
}
5558

5659
private static void ReplaceDatabase<TDbContext>(IServiceCollection serviceCollection)
Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,25 @@
11
namespace MyTested.AspNetCore.Mvc.Options
22
{
33
using System;
4-
using System.Linq;
54
using Microsoft.Extensions.DependencyInjection;
6-
using Utilities.Extensions;
5+
using Microsoft.Extensions.Options;
76

87
public class OptionsTestPlugin : IServiceRegistrationPlugin
98
{
10-
private const string OptionsPrefix = "IOptions<";
11-
9+
private readonly Type defaultOptionsServiceType = typeof(IOptions<>);
10+
private readonly Type defaultOptionsImplementationType = typeof(OptionsManager<>);
11+
1212
public Func<ServiceDescriptor, bool> ServiceSelectorPredicate
1313
{
1414
get
1515
{
1616
return
1717
serviceDescriptor =>
18-
serviceDescriptor.ServiceType.Name.StartsWith(OptionsPrefix);
18+
serviceDescriptor.ServiceType == defaultOptionsServiceType &&
19+
serviceDescriptor.ImplementationType == defaultOptionsImplementationType;
1920
}
2021
}
2122

22-
public Action<IServiceCollection> ServiceRegistrationDelegate
23-
=> serviceCollection => serviceCollection
24-
.ToArray()
25-
.ForEach(s => serviceCollection.ReplaceLifetime(s.ServiceType, ServiceLifetime.Scoped));
23+
public Action<IServiceCollection> ServiceRegistrationDelegate => serviceCollection => serviceCollection.ReplaceOptions();
2624
}
2725
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
namespace MyTested.AspNetCore.Mvc
2+
{
3+
using Microsoft.Extensions.DependencyInjection;
4+
using Microsoft.Extensions.Options;
5+
6+
/// <summary>
7+
/// Contains options extension methods for <see cref="IServiceCollection"/>.
8+
/// </summary>
9+
public static class ServiceCollectionOptionsExtensions
10+
{
11+
/// <summary>
12+
/// Replaces the default <see cref="IOptions{TOptions}"/> with a scoped implementation.
13+
/// </summary>
14+
/// <param name="serviceCollection">Instance of <see cref="IServiceCollection"/> type.</param>
15+
/// <returns>The same <see cref="IServiceCollection"/>.</returns>
16+
public static IServiceCollection ReplaceOptions(this IServiceCollection serviceCollection)
17+
{
18+
return serviceCollection.Replace(typeof(IOptions<>), typeof(OptionsManager<>), ServiceLifetime.Scoped);
19+
}
20+
}
21+
}

src/MyTested.AspNetCore.Mvc.Session/ServiceCollectionSessionExtensions.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ public static class ServiceCollectionSessionExtensions
1313
/// Replaces the default <see cref="ISessionStore"/> with a mocked implementation.
1414
/// </summary>
1515
/// <param name="serviceCollection">Instance of <see cref="IServiceCollection"/> type.</param>
16-
public static void ReplaceSession(this IServiceCollection serviceCollection)
16+
/// <returns>The same <see cref="IServiceCollection"/>.</returns>
17+
public static IServiceCollection ReplaceSession(this IServiceCollection serviceCollection)
1718
{
18-
serviceCollection.ReplaceTransient<ISessionStore, MockedSessionStore>();
19+
return serviceCollection.ReplaceTransient<ISessionStore, MockedSessionStore>();
1920
}
2021
}
2122
}

src/MyTested.AspNetCore.Mvc.ViewFeatures/ServiceCollectionViewFeaturesExtensions.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ public static class ServiceCollectionViewFeaturesExtensions
1313
/// Replaces the default <see cref="ITempDataProvider"/> with a mocked implementation.
1414
/// </summary>
1515
/// <param name="serviceCollection">Instance of <see cref="IServiceCollection"/> type.</param>
16-
public static void ReplaceTempDataProvider(this IServiceCollection serviceCollection)
16+
/// <returns>The same <see cref="IServiceCollection"/>.</returns>
17+
public static IServiceCollection ReplaceTempDataProvider(this IServiceCollection serviceCollection)
1718
{
18-
serviceCollection.ReplaceSingleton<ITempDataProvider, MockedTempDataProvider>();
19+
return serviceCollection.ReplaceSingleton<ITempDataProvider, MockedTempDataProvider>();
1920
}
2021
}
2122
}

test/MyTested.AspNetCore.Mvc.EntityFrameworkCore.Test/project.json

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
{
22
"buildOptions": {
33
"warningsAsErrors": true,
4-
"keyFile": "../../tools/Key.snk",
5-
"copyToOutput": {
6-
"include": [
7-
"testconfig.json"
8-
]
9-
}
4+
"keyFile": "../../tools/Key.snk"
105
},
116

127
"testRunner": "xunit",

test/MyTested.AspNetCore.Mvc.Options.Test/MyTested.AspNetCore.Mvc.Options.Test.xproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,8 @@
1515
<PropertyGroup>
1616
<SchemaVersion>2.0</SchemaVersion>
1717
</PropertyGroup>
18+
<ItemGroup>
19+
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
20+
</ItemGroup>
1821
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.targets" Condition="'$(VSToolsPath)' != ''" />
1922
</Project>

0 commit comments

Comments
 (0)