Skip to content

Commit

Permalink
Ensure Magneto throws meaningful exception when context is not regist…
Browse files Browse the repository at this point in the history
…ered
  • Loading branch information
shaynevanasperen committed Sep 22, 2019
1 parent 8b5a391 commit aa3482b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
13 changes: 12 additions & 1 deletion src/Magneto/Core/ServiceProviderExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
using System;
using System;

namespace Magneto.Core
{
static class ServiceProviderExtensions
{
internal static T GetService<T>(this IServiceProvider serviceProvider) => (T)serviceProvider.GetService(typeof(T));

internal static T GetRequiredService<T>(this IServiceProvider serviceProvider)
{
var serviceType = typeof(T);
var service = serviceProvider.GetService(serviceType);

if (service == null)
throw new InvalidOperationException($"No service for type '{serviceType.FullName}' has been registered.");

return (T)service;
}
}
}
5 changes: 3 additions & 2 deletions src/Magneto/Magneto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace Magneto
/// and commands to express the required context as being a composite of several items. For example:
/// </para>
/// <example>
/// <c>class MyQuery : ISyncQuery&lt;(DbContext, IFileProvider), string&gt; { ... }</c>
/// class MyQuery : ISyncQuery&lt;(DbContext, IFileProvider), string&gt; { ... }
/// </example>
/// </summary>
public class Magneto : IMagneto
Expand Down Expand Up @@ -56,7 +56,8 @@ public Magneto(IServiceProvider serviceProvider)
/// </summary>
/// <typeparam name="TContext">The type of context object required.</typeparam>
/// <returns>The result from calling <see cref="IServiceProvider.GetService"/>.</returns>
protected virtual TContext GetContext<TContext>() => ServiceProvider.GetService<TContext>();
/// <exception cref="InvalidOperationException">Thrown if the <see cref="ServiceProvider"/> couldn't provide it.</exception>
protected virtual TContext GetContext<TContext>() => ServiceProvider.GetRequiredService<TContext>();

/// <inheritdoc cref="ISyncQueryMagneto.Query{TContext,TResult}"/>
public virtual TResult Query<TContext, TResult>(ISyncQuery<TContext, TResult> query) =>
Expand Down
10 changes: 10 additions & 0 deletions test/Magneto.Tests/MagnetoTests/MagnetoTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ public override void Setup()
SUT = new MagnetoTest(_serviceProvider);
}

public class ForUnavailableType : GettingContext
{
Func<Attribute> _invocation;

void WhenContextIsResolved() => _invocation = SUT.Invoking(x => x.ResolveContext<Attribute>());

void ThenItThrowsAnExceptionStatingThatTheServiceIsNotRegistered() => _invocation.Should().Throw<InvalidOperationException>()
.Which.Message.Should().Be("No service for type 'System.Attribute' has been registered.");
}

public class ForNormalType : GettingContext
{
void WhenContextIsResolved()
Expand Down

0 comments on commit aa3482b

Please sign in to comment.