From 63452b45ac0d5d72a064baa34b32eb3683be1aaf Mon Sep 17 00:00:00 2001 From: David Vreony Date: Sun, 29 Mar 2020 12:11:58 +0100 Subject: [PATCH] #483 ninject now tryget service (#485) --- .../DependencyResolverTests.cs | 34 ++++++++++++++++--- .../NinjectDependencyResolver.cs | 4 +-- 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/src/Splat.Ninject.Tests/DependencyResolverTests.cs b/src/Splat.Ninject.Tests/DependencyResolverTests.cs index 16b515a86..27d5c55fe 100644 --- a/src/Splat.Ninject.Tests/DependencyResolverTests.cs +++ b/src/Splat.Ninject.Tests/DependencyResolverTests.cs @@ -38,6 +38,34 @@ public void NinjectDependencyResolver_Should_Resolve_Views() viewTwo.ShouldBeOfType(); } + /// + /// Should resolve views. + /// + [Fact] + public void NinjectDependencyResolver_Should_Return_Null() + { + var container = new StandardKernel(); + container.UseNinjectDependencyResolver(); + + var viewOne = Locator.Current.GetService(typeof(IViewFor)); + + viewOne.ShouldBeNull(); + } + + /// + /// Should resolve views. + /// + [Fact] + public void NinjectDependencyResolver_GetServices_Should_Return_Empty_Collection() + { + var container = new StandardKernel(); + container.UseNinjectDependencyResolver(); + + var viewOne = Locator.Current.GetServices(typeof(IViewFor)); + + viewOne.ShouldBeEmpty(); + } + /// /// Should resolve views. /// @@ -120,10 +148,8 @@ public void NinjectDependencyResolver_Should_UnregisterAll() Locator.CurrentMutable.UnregisterAll(typeof(IScreen)); - var result = Record.Exception(() => Locator.Current.GetService()); - - result.ShouldBeOfType(); - result.Message.ShouldStartWith("Error activating IScreen"); + var result = Locator.Current.GetService(); + result.ShouldBeNull(); } /// diff --git a/src/Splat.Ninject/NinjectDependencyResolver.cs b/src/Splat.Ninject/NinjectDependencyResolver.cs index 6dadaa554..928cd7592 100644 --- a/src/Splat.Ninject/NinjectDependencyResolver.cs +++ b/src/Splat.Ninject/NinjectDependencyResolver.cs @@ -29,8 +29,8 @@ public NinjectDependencyResolver(IKernel kernel) /// public virtual object GetService(Type serviceType, string contract = null) => string.IsNullOrEmpty(contract) - ? _kernel.Get(serviceType) - : _kernel.Get(serviceType, contract); + ? _kernel.TryGet(serviceType) + : _kernel.TryGet(serviceType, contract); /// public virtual IEnumerable GetServices(Type serviceType, string contract = null) =>