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

Commit

Permalink
Refactoring ServiceCollection
Browse files Browse the repository at this point in the history
* Remove use of IConfiguration from ServiceCollection
* Removing the Add* methods from IServiceCollection that don't accept an
  IServiceDescriptor

Fixes #116 #117
  • Loading branch information
pranavkm committed Oct 21, 2014
1 parent e0a956c commit cfa2cc6
Show file tree
Hide file tree
Showing 5 changed files with 239 additions and 82 deletions.
28 changes: 13 additions & 15 deletions src/Microsoft.Framework.DependencyInjection/IServiceCollection.cs
Original file line number Diff line number Diff line change
@@ -1,32 +1,30 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;

namespace Microsoft.Framework.DependencyInjection
{
/// <summary>
/// Specifies the contract for a collection of service descriptors.
/// </summary>
#if ASPNET50 || ASPNETCORE50
[Microsoft.Framework.Runtime.AssemblyNeutral]
#endif
public interface IServiceCollection : IEnumerable<IServiceDescriptor>
{
/// <summary>
/// Adds the <paramref name="descriptor"/> to this instance.
/// </summary>
/// <param name="descriptor">The <see cref="IServiceDescriptor"/> to add.</param>
/// <returns>A reference to the current instance of <see cref="IServiceCollection"/>.</returns>
IServiceCollection Add(IServiceDescriptor descriptor);

/// <summary>
/// Adds a sequence of <see cref="IServiceDescriptor"/> to this instance.
/// </summary>
/// <param name="descriptor">The <see cref="IEnumerable{T}"/> of <see cref="IServiceDescriptor"/>s to add.</param>
/// <returns>A reference to the current instance of <see cref="IServiceCollection"/>.</returns>
IServiceCollection Add(IEnumerable<IServiceDescriptor> descriptors);

IServiceCollection AddTransient(Type service, Type implementationType);

IServiceCollection AddTransient(Type service, Func<IServiceProvider, object> implementationFactory);

IServiceCollection AddScoped(Type service, Type implementationType);

IServiceCollection AddScoped(Type service, Func<IServiceProvider, object> implementationFactory);

IServiceCollection AddSingleton(Type service, Type implementationType);

IServiceCollection AddSingleton(Type service, Func<IServiceProvider, object> implementationFactory);

IServiceCollection AddInstance(Type service, object implementationInstance);
}
}
61 changes: 7 additions & 54 deletions src/Microsoft.Framework.DependencyInjection/ServiceCollection.cs
Original file line number Diff line number Diff line change
@@ -1,80 +1,33 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections;
using System.Collections.Generic;
using Microsoft.Framework.ConfigurationModel;

namespace Microsoft.Framework.DependencyInjection
{
/// <summary>
/// Default implementation of <see cref="IServiceCollection"/>.
/// </summary>
public class ServiceCollection : IServiceCollection
{
private readonly List<IServiceDescriptor> _descriptors;
private readonly ServiceDescriber _describe;

public ServiceCollection()
: this(new Configuration())
{
}

public ServiceCollection(IConfiguration configuration)
{
_descriptors = new List<IServiceDescriptor>();
_describe = new ServiceDescriber(configuration);
}
private readonly List<IServiceDescriptor> _descriptors = new List<IServiceDescriptor>();

/// <inheritdoc />
public IServiceCollection Add(IServiceDescriptor descriptor)
{
_descriptors.Add(descriptor);
return this;
}

/// <inheritdoc />
public IServiceCollection Add(IEnumerable<IServiceDescriptor> descriptors)
{
_descriptors.AddRange(descriptors);
return this;
}

public IServiceCollection AddTransient(Type service, Type implementationType)
{
Add(_describe.Transient(service, implementationType));
return this;
}

public IServiceCollection AddTransient(Type service, Func<IServiceProvider, object> implementationFactory)
{
return Add(_describe.Transient(service, implementationFactory));
}

public IServiceCollection AddScoped(Type service, Type implementationType)
{
Add(_describe.Scoped(service, implementationType));
return this;
}

public IServiceCollection AddScoped(Type service, Func<IServiceProvider, object> implementationFactory)
{
return Add(_describe.Scoped(service, implementationFactory));
}

public IServiceCollection AddSingleton(Type service, Type implementationType)
{
Add(_describe.Singleton(service, implementationType));
return this;
}

public IServiceCollection AddInstance(Type service, object implementationInstance)
{
Add(_describe.Instance(service, implementationInstance));
return this;
}

public IServiceCollection AddSingleton(Type service, Func<IServiceProvider, object> implementationFactory)
{
return Add(_describe.Singleton(service, implementationFactory));
}

/// <inheritdoc />
public IEnumerator<IServiceDescriptor> GetEnumerator()
{
return _descriptors.GetEnumerator();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,68 @@ namespace Microsoft.Framework.DependencyInjection
{
public static class ServiceCollectionExtensions
{
public static IServiceCollection AddTransient<TService, TImplementation>([NotNull]this IServiceCollection services)
public static IServiceCollection AddTransient([NotNull] this IServiceCollection collection,
[NotNull] Type service,
[NotNull] Type implementationType)
{
return Add(collection, service, implementationType, LifecycleKind.Transient);
}

public static IServiceCollection AddTransient([NotNull] this IServiceCollection collection,
[NotNull] Type service,
[NotNull] Func<IServiceProvider, object> implementationFactory)
{
return Add(collection, service, implementationFactory, LifecycleKind.Transient);
}

public static IServiceCollection AddScoped([NotNull] this IServiceCollection collection,
[NotNull] Type service,
[NotNull] Type implementationType)
{
return Add(collection, service, implementationType, LifecycleKind.Scoped);
}

public static IServiceCollection AddScoped([NotNull] this IServiceCollection collection,
[NotNull] Type service,
[NotNull] Func<IServiceProvider, object> implementationFactory)
{
return Add(collection, service, implementationFactory, LifecycleKind.Scoped);
}

public static IServiceCollection AddSingleton([NotNull] this IServiceCollection collection,
[NotNull] Type service,
[NotNull] Type implementationType)
{
return Add(collection, service, implementationType, LifecycleKind.Singleton);
}

public static IServiceCollection AddSingleton([NotNull] this IServiceCollection collection,
[NotNull] Type service,
[NotNull] Func<IServiceProvider, object> implementationFactory)
{
return Add(collection, service, implementationFactory, LifecycleKind.Singleton);
}

public static IServiceCollection AddInstance([NotNull] this IServiceCollection collection,
[NotNull] Type service,
[NotNull] object implementationInstance)
{
var serviceDescriptor = new ServiceDescriptor(service, implementationInstance);
return collection.Add(serviceDescriptor);
}

public static IServiceCollection AddTransient<TService, TImplementation>([NotNull] this IServiceCollection services)
{
return services.AddTransient(typeof(TService), typeof(TImplementation));
}

public static IServiceCollection AddTransient([NotNull]this IServiceCollection services, Type serviceType)
public static IServiceCollection AddTransient([NotNull] this IServiceCollection services,
[NotNull] Type serviceType)
{
return services.AddTransient(serviceType, serviceType);
}

public static IServiceCollection AddTransient<TService>([NotNull]this IServiceCollection services)
public static IServiceCollection AddTransient<TService>([NotNull] this IServiceCollection services)
{
return services.AddTransient(typeof(TService));
}
Expand All @@ -28,12 +79,13 @@ public static IServiceCollection AddTransient<TService>([NotNull] this IServiceC
return services.AddTransient(typeof(TService), implementationFactory);
}

public static IServiceCollection AddScoped<TService, TImplementation>([NotNull]this IServiceCollection services)
public static IServiceCollection AddScoped<TService, TImplementation>([NotNull] this IServiceCollection services)
{
return services.AddScoped(typeof(TService), typeof(TImplementation));
}

public static IServiceCollection AddScoped([NotNull]this IServiceCollection services, Type serviceType)
public static IServiceCollection AddScoped([NotNull] this IServiceCollection services,
[NotNull] Type serviceType)
{
return services.AddScoped(serviceType, serviceType);
}
Expand All @@ -44,22 +96,23 @@ public static IServiceCollection AddScoped<TService>([NotNull] this IServiceColl
return services.AddScoped(typeof(TService), implementationFactory);
}

public static IServiceCollection AddScoped<TService>([NotNull]this IServiceCollection services)
public static IServiceCollection AddScoped<TService>([NotNull] this IServiceCollection services)
{
return services.AddScoped(typeof(TService));
}

public static IServiceCollection AddSingleton<TService, TImplementation>([NotNull]this IServiceCollection services)
public static IServiceCollection AddSingleton<TService, TImplementation>([NotNull] this IServiceCollection services)
{
return services.AddSingleton(typeof(TService), typeof(TImplementation));
}

public static IServiceCollection AddSingleton([NotNull]this IServiceCollection services, Type serviceType)
public static IServiceCollection AddSingleton([NotNull] this IServiceCollection services,
[NotNull] Type serviceType)
{
return services.AddSingleton(serviceType, serviceType);
}

public static IServiceCollection AddSingleton<TService>([NotNull]this IServiceCollection services)
public static IServiceCollection AddSingleton<TService>([NotNull] this IServiceCollection services)
{
return services.AddSingleton(typeof(TService));
}
Expand All @@ -70,9 +123,29 @@ public static IServiceCollection AddSingleton<TService>([NotNull] this IServiceC
return services.AddSingleton(typeof(TService), implementationFactory);
}

public static IServiceCollection AddInstance<TService>([NotNull]this IServiceCollection services, TService implementationInstance)
public static IServiceCollection AddInstance<TService>([NotNull] this IServiceCollection services,
[NotNull] TService implementationInstance)
where TService : class
{
return services.AddInstance(typeof(TService), implementationInstance);
}

private static IServiceCollection Add(IServiceCollection collection,
Type service,
Type implementationType,
LifecycleKind lifeCycle)
{
var descriptor = new ServiceDescriptor(service, implementationType, lifeCycle);
return collection.Add(descriptor);
}

private static IServiceCollection Add(IServiceCollection collection,
Type service,
Func<IServiceProvider, object> implementationFactory,
LifecycleKind lifeCycle)
{
var descriptor = new ServiceDescriptor(service, implementationFactory, lifeCycle);
return collection.Add(descriptor);
}
}
}
11 changes: 8 additions & 3 deletions src/Microsoft.Framework.DependencyInjection/ServiceDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ public class ServiceDescriptor : IServiceDescriptor
/// <param name="serviceType">The <see cref="Type"/> of the service.</param>
/// <param name="implementationType">The <see cref="Type"/> implementing the service.</param>
/// <param name="lifecycle">The <see cref="LifecycleKind"/> of the service.</param>
public ServiceDescriptor(Type serviceType, Type implementationType, LifecycleKind lifecycle)
public ServiceDescriptor([NotNull] Type serviceType,
[NotNull] Type implementationType,
LifecycleKind lifecycle)
: this(serviceType, lifecycle)
{
ImplementationType = implementationType;
Expand All @@ -25,7 +27,8 @@ public ServiceDescriptor(Type serviceType, Type implementationType, LifecycleKin
/// </summary>
/// <param name="serviceType">The <see cref="Type"/> of the service.</param>
/// <param name="instance">The instance implementing the service.</param>
public ServiceDescriptor(Type serviceType, object instance)
public ServiceDescriptor([NotNull] Type serviceType,
[NotNull] object instance)
: this(serviceType, LifecycleKind.Singleton)
{
ImplementationInstance = instance;
Expand All @@ -37,7 +40,9 @@ public ServiceDescriptor(Type serviceType, object instance)
/// <param name="serviceType">The <see cref="Type"/> of the service.</param>
/// <param name="factory">A factory used for creating service instances.</param>
/// <param name="lifecycle">The <see cref="LifecycleKind"/> of the service.</param>
public ServiceDescriptor(Type serviceType, Func<IServiceProvider, object> factory, LifecycleKind lifecycle)
public ServiceDescriptor([NotNull] Type serviceType,
[NotNull] Func<IServiceProvider, object> factory,
LifecycleKind lifecycle)
: this(serviceType, lifecycle)
{
ImplementationFactory = factory;
Expand Down
Loading

0 comments on commit cfa2cc6

Please sign in to comment.