From bce183be4388fb2ea20d851e6a66183c5e517df2 Mon Sep 17 00:00:00 2001 From: hendryten Date: Wed, 25 Dec 2013 17:11:28 +0800 Subject: [PATCH] Add RegisterTo method Short hand for: Register(factory => factory.GetInstance()) --- LightInject.SampleLibrary/Foo.cs | 12 ++++- LightInject.Tests/ServiceContainerTests.cs | 17 +++++++ LightInject/LightInject.cs | 59 +++++++++++++++++++++- 3 files changed, 86 insertions(+), 2 deletions(-) diff --git a/LightInject.SampleLibrary/Foo.cs b/LightInject.SampleLibrary/Foo.cs index dc751a3f..e980fb2e 100644 --- a/LightInject.SampleLibrary/Foo.cs +++ b/LightInject.SampleLibrary/Foo.cs @@ -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; } diff --git a/LightInject.Tests/ServiceContainerTests.cs b/LightInject.Tests/ServiceContainerTests.cs index 34d9f93d..f296c9d2 100644 --- a/LightInject.Tests/ServiceContainerTests.cs +++ b/LightInject.Tests/ServiceContainerTests.cs @@ -424,6 +424,23 @@ public void GetInstance_DisposablePerRequestServiceOutsideScope_ThrowsException( } + [TestMethod] + public void GetInstance_RegisteredTo_PerScopeService_ReturnsSingleInstance() + { + var container = CreateContainer(); + container.Register(new PerScopeLifetime()); + container.RegisterTo(); + container.RegisterTo(); + using (container.BeginScope()) + { + var instanceFooBar = container.GetInstance(); + var instance1 = container.GetInstance(); + var instance2 = container.GetInstance(); + + Assert.AreSame(instanceFooBar, instance2); + Assert.AreSame(instance1, instance2); + } + } #region Func Services diff --git a/LightInject/LightInject.cs b/LightInject/LightInject.cs index f2565baa..55fd8082 100644 --- a/LightInject/LightInject.cs +++ b/LightInject/LightInject.cs @@ -120,6 +120,30 @@ internal interface IServiceRegistry /// The instance that controls the lifetime of the registered service. void Register(string serviceName, ILifetime lifetime) where TImplementation : TService; + /// + /// Registers the to use the factory to get instance of . + /// + /// The service type to register. + /// The target type. + void RegisterTo() where TTarget : TService; + + /// + /// Registers the to use the factory to get instance of . + /// + /// The service type to register. + /// The target type. + /// The name of the service to be used for resolving. + void RegisterTo(string targetServiceName) where TTarget : TService; + + /// + /// Registers the to use the factory to get instance of . + /// + /// The service type to register. + /// The target type. + /// The name of the service to be used for resolving. + /// The name of the service. + void RegisterTo(string targetServiceName, string serviceName) where TTarget : TService; + /// /// Registers the with the given . /// @@ -1431,7 +1455,7 @@ public void Register(ServiceRegistration serviceRegistration) s => AddServiceRegistration(sr), (k, existing) => UpdateServiceRegistration(existing, sr)); } - + /// /// Registers services from the given . /// @@ -1859,6 +1883,39 @@ public void Register(Type serviceType, Type implementingType) RegisterService(serviceType, implementingType, null, string.Empty); } + /// + /// Registers the to use the factory to get instance of . + /// + /// The service type to register. + /// The target type. + public void RegisterTo() where TTarget : TService + { + Register(factory => factory.GetInstance()); + } + + /// + /// Registers the to use the factory to get instance of . + /// + /// The service type to register. + /// The target type. + /// The name of the service to be used for resolving. + public void RegisterTo(string targetServiceName) where TTarget : TService + { + Register(factory => factory.GetInstance(targetServiceName)); + } + + /// + /// Registers the to use the factory to get instance of . + /// + /// The service type to register. + /// The target type. + /// The name of the service to be used for resolving. + /// The name of the service. + public void RegisterTo(string targetServiceName, string serviceName) where TTarget : TService + { + Register(factory => factory.GetInstance(targetServiceName), serviceName); + } + /// /// Gets an instance of the given . ///