Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion LightInject.SampleLibrary/Foo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,23 @@ public interface IFoo { }
public class Foo : IFoo
{
public static int Instances { get; set; }

public Foo()
{
Instances++;
}
}

public class FooBar : IFoo, IBar
{
public static int Instances { get; set; }

public FooBar()
{
Instances++;
}
}

public class FooWithStaticDependency : IFoo
{
public static IBar Bar { get; set; }
Expand Down
17 changes: 17 additions & 0 deletions LightInject.Tests/ServiceContainerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,23 @@ public void GetInstance_DisposablePerRequestServiceOutsideScope_ThrowsException(

}

[TestMethod]
public void GetInstance_RegisteredTo_PerScopeService_ReturnsSingleInstance()
{
var container = CreateContainer();
container.Register<FooBar>(new PerScopeLifetime());
container.RegisterTo<IFoo, FooBar>();
container.RegisterTo<IBar, FooBar>();
using (container.BeginScope())
{
var instanceFooBar = container.GetInstance<FooBar>();
var instance1 = container.GetInstance<IFoo>();
var instance2 = container.GetInstance<IBar>();

Assert.AreSame(instanceFooBar, instance2);
Assert.AreSame(instance1, instance2);
}
}

#region Func Services

Expand Down
59 changes: 58 additions & 1 deletion LightInject/LightInject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,30 @@ internal interface IServiceRegistry
/// <param name="lifetime">The <see cref="ILifetime"/> instance that controls the lifetime of the registered service.</param>
void Register<TService, TImplementation>(string serviceName, ILifetime lifetime) where TImplementation : TService;

/// <summary>
/// Registers the <typeparamref name="TService"/> to use the factory to get instance of <typeparamref name="TTarget"/>.
/// </summary>
/// <typeparam name="TService">The service type to register.</typeparam>
/// <typeparam name="TTarget">The target type.</typeparam>
void RegisterTo<TService, TTarget>() where TTarget : TService;

/// <summary>
/// Registers the <typeparamref name="TService"/> to use the factory to get instance of <typeparamref name="TTarget"/>.
/// </summary>
/// <typeparam name="TService">The service type to register.</typeparam>
/// <typeparam name="TTarget">The target type.</typeparam>
/// <param name="targetServiceName">The name of the service to be used for resolving.</param>
void RegisterTo<TService, TTarget>(string targetServiceName) where TTarget : TService;

/// <summary>
/// Registers the <typeparamref name="TService"/> to use the factory to get instance of <typeparamref name="TTarget"/>.
/// </summary>
/// <typeparam name="TService">The service type to register.</typeparam>
/// <typeparam name="TTarget">The target type.</typeparam>
/// <param name="targetServiceName">The name of the service to be used for resolving.</param>
/// <param name="serviceName">The name of the service.</param>
void RegisterTo<TService, TTarget>(string targetServiceName, string serviceName) where TTarget : TService;

/// <summary>
/// Registers the <typeparamref name="TService"/> with the given <paramref name="instance"/>.
/// </summary>
Expand Down Expand Up @@ -1431,7 +1455,7 @@ public void Register(ServiceRegistration serviceRegistration)
s => AddServiceRegistration(sr),
(k, existing) => UpdateServiceRegistration(existing, sr));
}

/// <summary>
/// Registers services from the given <paramref name="assembly"/>.
/// </summary>
Expand Down Expand Up @@ -1859,6 +1883,39 @@ public void Register(Type serviceType, Type implementingType)
RegisterService(serviceType, implementingType, null, string.Empty);
}

/// <summary>
/// Registers the <typeparamref name="TService"/> to use the factory to get instance of <typeparamref name="TTarget"/>.
/// </summary>
/// <typeparam name="TService">The service type to register.</typeparam>
/// <typeparam name="TTarget">The target type.</typeparam>
public void RegisterTo<TService, TTarget>() where TTarget : TService
{
Register<TService>(factory => factory.GetInstance<TTarget>());
}

/// <summary>
/// Registers the <typeparamref name="TService"/> to use the factory to get instance of <typeparamref name="TTarget"/>.
/// </summary>
/// <typeparam name="TService">The service type to register.</typeparam>
/// <typeparam name="TTarget">The target type.</typeparam>
/// <param name="targetServiceName">The name of the service to be used for resolving.</param>
public void RegisterTo<TService, TTarget>(string targetServiceName) where TTarget : TService
{
Register<TService>(factory => factory.GetInstance<TTarget>(targetServiceName));
}

/// <summary>
/// Registers the <typeparamref name="TService"/> to use the factory to get instance of <typeparamref name="TTarget"/>.
/// </summary>
/// <typeparam name="TService">The service type to register.</typeparam>
/// <typeparam name="TTarget">The target type.</typeparam>
/// <param name="targetServiceName">The name of the service to be used for resolving.</param>
/// <param name="serviceName">The name of the service.</param>
public void RegisterTo<TService, TTarget>(string targetServiceName, string serviceName) where TTarget : TService
{
Register<TService>(factory => factory.GetInstance<TTarget>(targetServiceName), serviceName);
}

/// <summary>
/// Gets an instance of the given <paramref name="serviceType"/>.
/// </summary>
Expand Down