Skip to content

Commit

Permalink
#483 ninject now tryget service (#485)
Browse files Browse the repository at this point in the history
  • Loading branch information
dpvreony authored Mar 29, 2020
1 parent 232b0c4 commit 63452b4
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
34 changes: 30 additions & 4 deletions src/Splat.Ninject.Tests/DependencyResolverTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,34 @@ public void NinjectDependencyResolver_Should_Resolve_Views()
viewTwo.ShouldBeOfType<ViewTwo>();
}

/// <summary>
/// Should resolve views.
/// </summary>
[Fact]
public void NinjectDependencyResolver_Should_Return_Null()
{
var container = new StandardKernel();
container.UseNinjectDependencyResolver();

var viewOne = Locator.Current.GetService(typeof(IViewFor<ViewModelOne>));

viewOne.ShouldBeNull();
}

/// <summary>
/// Should resolve views.
/// </summary>
[Fact]
public void NinjectDependencyResolver_GetServices_Should_Return_Empty_Collection()
{
var container = new StandardKernel();
container.UseNinjectDependencyResolver();

var viewOne = Locator.Current.GetServices(typeof(IViewFor<ViewModelOne>));

viewOne.ShouldBeEmpty();
}

/// <summary>
/// Should resolve views.
/// </summary>
Expand Down Expand Up @@ -120,10 +148,8 @@ public void NinjectDependencyResolver_Should_UnregisterAll()

Locator.CurrentMutable.UnregisterAll(typeof(IScreen));

var result = Record.Exception(() => Locator.Current.GetService<IScreen>());

result.ShouldBeOfType<ActivationException>();
result.Message.ShouldStartWith("Error activating IScreen");
var result = Locator.Current.GetService<IScreen>();
result.ShouldBeNull();
}

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions src/Splat.Ninject/NinjectDependencyResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ public NinjectDependencyResolver(IKernel kernel)
/// <inheritdoc />
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);

/// <inheritdoc />
public virtual IEnumerable<object> GetServices(Type serviceType, string contract = null) =>
Expand Down

1 comment on commit 63452b4

@Micha-kun
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! PD: Less code to maintain XD

Please sign in to comment.