Skip to content
This repository has been archived by the owner on Nov 2, 2018. It is now read-only.

Commit

Permalink
Raename AddInstance to AddSingleton
Browse files Browse the repository at this point in the history
Fixes #319
  • Loading branch information
pranavkm committed Nov 12, 2015
1 parent bd7b1bd commit 5c2cbc7
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -596,10 +596,30 @@ public static IServiceCollection AddSingleton<TService, TImplementation>(
/// <param name="implementationInstance">The instance of the service.</param>
/// <returns>A reference to this instance after the operation has completed.</returns>
/// <seealso cref="ServiceLifetime.Singleton"/>
[Obsolete("This method will be removed in future releases. Use " +
nameof(ServiceCollectionExtensions.AddSingleton) + " instead.")]
public static IServiceCollection AddInstance(
this IServiceCollection services,
Type serviceType,
object implementationInstance)
{
return AddSingleton(services, serviceType, implementationInstance);
}

/// <summary>
/// Adds a singleton service of the type specified in <paramref name="serviceType"/> with an
/// instance specified in <paramref name="implementationInstance"/> to the
/// specified <see cref="IServiceCollection"/>.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/> to add the service to.</param>
/// <param name="serviceType">The type of the service to register.</param>
/// <param name="implementationInstance">The instance of the service.</param>
/// <returns>A reference to this instance after the operation has completed.</returns>
/// <seealso cref="ServiceLifetime.Singleton"/>
public static IServiceCollection AddSingleton(
this IServiceCollection services,
Type serviceType,
object implementationInstance)
{
if (services == null)
{
Expand Down Expand Up @@ -631,10 +651,30 @@ public static IServiceCollection AddInstance(
/// <param name="implementationInstance">The instance of the service.</param>
/// <returns>A reference to this instance after the operation has completed.</returns>
/// <seealso cref="ServiceLifetime.Singleton"/>
[Obsolete("This method will be removed in future releases. Use " +
nameof(ServiceCollectionExtensions.AddSingleton) + " instead.")]
public static IServiceCollection AddInstance<TService>(
this IServiceCollection services,
TService implementationInstance)
where TService : class
{
return AddSingleton(services, implementationInstance);
}

// <summary>
/// Adds a singleton service of the type specified in <typeparamref name="TService" /> with an
/// instance specified in <paramref name="implementationInstance"/> to the
/// specified <see cref="IServiceCollection"/>.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/> to add the service to.</param>
/// <param name="serviceType">The type of the service to register.</param>
/// <param name="implementationInstance">The instance of the service.</param>
/// <returns>A reference to this instance after the operation has completed.</returns>
/// <seealso cref="ServiceLifetime.Singleton"/>
public static IServiceCollection AddSingleton<TService>(
this IServiceCollection services,
TService implementationInstance)
where TService : class
{
if (services == null)
{
Expand All @@ -646,10 +686,9 @@ public static IServiceCollection AddInstance<TService>(
throw new ArgumentNullException(nameof(implementationInstance));
}

return services.AddInstance(typeof(TService), implementationInstance);
return services.AddSingleton(typeof(TService), implementationInstance);
}


private static IServiceCollection Add(
IServiceCollection collection,
Type serviceType,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public void ServiceInstanceCanBeResolved()
// Arrange
var collection = new ServiceCollection();
var instance = new FakeService();
collection.AddInstance(typeof(IFakeServiceInstance), instance);
collection.AddSingleton(typeof(IFakeServiceInstance), instance);
var provider = CreateServiceProvider(collection);

// Act
Expand Down Expand Up @@ -164,7 +164,7 @@ public void OuterServiceCanHaveOtherServicesInjected()
var collection = new ServiceCollection();
var fakeService = new FakeService();
collection.AddTransient<IFakeOuterService, FakeOuterService>();
collection.AddInstance<IFakeService>(fakeService);
collection.AddSingleton<IFakeService>(fakeService);
collection.AddTransient<IFakeMultipleService, FakeOneMultipleService>();
collection.AddTransient<IFakeMultipleService, FakeTwoMultipleService>();
var provider = CreateServiceProvider(collection);
Expand Down Expand Up @@ -501,7 +501,7 @@ public void ClosedServicesPreferredOverOpenGenericServices()
var collection = new ServiceCollection();
collection.AddTransient(typeof(IFakeOpenGenericService<string>), typeof(FakeService));
collection.AddTransient(typeof(IFakeOpenGenericService<>), typeof(FakeOpenGenericService<>));
collection.AddInstance("Hello world");
collection.AddSingleton("Hello world");
var provider = CreateServiceProvider(collection);

// Act
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public void SelfCircularDependencyGenericIndirect()
public void NoCircularDependencyGeneric()
{
var serviceProvider = new ServiceCollection()
.AddInstance(new SelfCircularDependencyGeneric<string>())
.AddSingleton(new SelfCircularDependencyGeneric<string>())
.AddTransient<SelfCircularDependencyGeneric<int>>()
.BuildServiceProvider();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,21 +107,21 @@ public void AddWithFactoryAddsServiceWithRightLifecyle(
Assert.Equal(lifeCycle, descriptor.Lifetime);
}

public static TheoryData AddInstanceData
public static TheoryData AddSingletonData
{
get
{
return new TheoryData<Action<IServiceCollection>>
{
{ collection => collection.AddInstance<IFakeService>(_instance) },
{ collection => collection.AddInstance(typeof(IFakeService), _instance) },
{ collection => collection.AddSingleton<IFakeService>(_instance) },
{ collection => collection.AddSingleton(typeof(IFakeService), _instance) },
};
}
}

[Theory]
[MemberData(nameof(AddInstanceData))]
public void AddInstance_AddsWithSingletonLifecycle(Action<IServiceCollection> addAction)
[MemberData(nameof(AddSingletonData))]
public void AddSingleton_AddsWithSingletonLifecycle(Action<IServiceCollection> addAction)
{
// Arrange
var collection = new ServiceCollection();
Expand All @@ -137,7 +137,7 @@ public void AddInstance_AddsWithSingletonLifecycle(Action<IServiceCollection> ad
}

[Theory]
[MemberData(nameof(AddInstanceData))]
[MemberData(nameof(AddSingletonData))]
public void TryAddNoOpFailsIfExists(Action<IServiceCollection> addAction)
{
// Arrange
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public void GetServices_WithBuildServiceProvider_Returns_EmptyList_WhenNoService
{
// Arrange
var serviceCollection = new ServiceCollection();
serviceCollection.AddInstance<IEnumerable<IFoo>>(new List<IFoo>());
serviceCollection.AddSingleton<IEnumerable<IFoo>>(new List<IFoo>());
var serviceProvider = serviceCollection.BuildServiceProvider();

// Act
Expand All @@ -170,7 +170,7 @@ public void NonGeneric_GetServices_WithBuildServiceProvider_Returns_EmptyList_Wh
{
// Arrange
var serviceCollection = new ServiceCollection();
serviceCollection.AddInstance<IEnumerable<IFoo>>(new List<IFoo>());
serviceCollection.AddSingleton<IEnumerable<IFoo>>(new List<IFoo>());
var serviceProvider = serviceCollection.BuildServiceProvider();

// Act
Expand Down
35 changes: 18 additions & 17 deletions test/Microsoft.Extensions.DependencyInjection.Tests/project.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
{
"version": "0.1-alpha-*",
"compilationOptions": {
"keyFile": "../../tools/Key.snk"
},
"dependencies": {
"Microsoft.AspNet.Testing": "1.0.0-*",
"Microsoft.Extensions.DependencyInjection": "1.0.0-*",
"Microsoft.Extensions.DependencyInjection.Specification.Tests": "1.0.0-*",
"xunit.runner.aspnet": "2.0.0-aspnet-*"
},
"frameworks": {
"dnx451": {},
"dnxcore50": {}
},
"commands": {
"test": "xunit.runner.aspnet"
}
"version": "0.1-alpha-*",
"compilationOptions": {
"keyFile": "../../tools/Key.snk",
"warningsAsErrors": true
},
"dependencies": {
"Microsoft.AspNet.Testing": "1.0.0-*",
"Microsoft.Extensions.DependencyInjection": "1.0.0-*",
"Microsoft.Extensions.DependencyInjection.Specification.Tests": "1.0.0-*",
"xunit.runner.aspnet": "2.0.0-aspnet-*"
},
"frameworks": {
"dnx451": { },
"dnxcore50": { }
},
"commands": {
"test": "xunit.runner.aspnet"
}
}

0 comments on commit 5c2cbc7

Please sign in to comment.