From 3a0c7eb7e1312cfb979505d9f4e524c67fe8d8dd Mon Sep 17 00:00:00 2001 From: Pat Sissons Date: Tue, 2 Jun 2015 21:57:49 -0600 Subject: [PATCH 1/2] adding three generic mixins for Register, RegisterConstant, and RegisterLazySingleton --- Splat/ServiceLocation.cs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/Splat/ServiceLocation.cs b/Splat/ServiceLocation.cs index f34c49d1a..0defe7b3f 100644 --- a/Splat/ServiceLocation.cs +++ b/Splat/ServiceLocation.cs @@ -180,18 +180,33 @@ public static IDisposable WithResolver(this IDependencyResolver resolver) return new ActionDisposable(() => Locator.Current = origResolver); } + + public static void Register(this IMutableDependencyResolver This, Func factory, string contract = null) + { + This.Register(factory, typeof(T), contract); + } public static void RegisterConstant(this IMutableDependencyResolver This, object value, Type serviceType, string contract = null) { This.Register(() => value, serviceType, contract); } + public static void RegisterConstant(this IMutableDependencyResolver This, object value, string contract = null) + { + RegisterConstant(This, value, typeof(T), contract); + } + public static void RegisterLazySingleton(this IMutableDependencyResolver This, Func valueFactory, Type serviceType, string contract = null) { var val = new Lazy(valueFactory, LazyThreadSafetyMode.ExecutionAndPublication); This.Register(() => val.Value, serviceType, contract); } + public static void RegisterLazySingleton(this IMutableDependencyResolver This, Func valueFactory, string contract = null) + { + RegisterLazySingleton(This, valueFactory, typeof(T), contract); + } + public static void InitializeSplat(this IMutableDependencyResolver This) { This.Register(() => new DefaultLogManager(), typeof(ILogManager)); From 6c4e9eb70e906d70265d886781965570d7225b15 Mon Sep 17 00:00:00 2001 From: Kent Boogaart Date: Tue, 19 Jan 2016 18:17:34 +1030 Subject: [PATCH 2/2] Use generic type to restrict values. --- Splat/ServiceLocation.cs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Splat/ServiceLocation.cs b/Splat/ServiceLocation.cs index 0defe7b3f..53f23a31c 100644 --- a/Splat/ServiceLocation.cs +++ b/Splat/ServiceLocation.cs @@ -181,17 +181,17 @@ public static IDisposable WithResolver(this IDependencyResolver resolver) return new ActionDisposable(() => Locator.Current = origResolver); } - public static void Register(this IMutableDependencyResolver This, Func factory, string contract = null) + public static void Register(this IMutableDependencyResolver This, Func factory, string contract = null) { - This.Register(factory, typeof(T), contract); + This.Register(() => factory(), typeof(T), contract); } - + public static void RegisterConstant(this IMutableDependencyResolver This, object value, Type serviceType, string contract = null) { This.Register(() => value, serviceType, contract); } - public static void RegisterConstant(this IMutableDependencyResolver This, object value, string contract = null) + public static void RegisterConstant(this IMutableDependencyResolver This, T value, string contract = null) { RegisterConstant(This, value, typeof(T), contract); } @@ -202,9 +202,9 @@ public static void RegisterLazySingleton(this IMutableDependencyResolver This, F This.Register(() => val.Value, serviceType, contract); } - public static void RegisterLazySingleton(this IMutableDependencyResolver This, Func valueFactory, string contract = null) + public static void RegisterLazySingleton(this IMutableDependencyResolver This, Func valueFactory, string contract = null) { - RegisterLazySingleton(This, valueFactory, typeof(T), contract); + RegisterLazySingleton(This, () => valueFactory(), typeof(T), contract); } public static void InitializeSplat(this IMutableDependencyResolver This)