/// <summary> /// Gets all instances of the given <typeparamref name="T"/>. Must return an empty /// collection if the service is not available (must not return <c>null</c> or throw). /// </summary> /// <typeparam name="T">The type for the object we want to retrieve.</typeparam> /// <param name="resolver">The resolver we are getting the service from.</param> /// <param name="contract">A optional value which will retrieve only a object registered with the same contract.</param> /// <returns>A sequence of instances of the requested <typeparamref name="T"/>. The sequence /// should be empty (not <c>null</c>) if no objects of the given type are available.</returns> public static IEnumerable <T> GetServices <T>(this IReadonlyDependencyResolver resolver, string contract = null) { if (resolver is null) { throw new ArgumentNullException(nameof(resolver)); } return(resolver.GetServices(typeof(T), contract).Cast <T>()); }
/// <summary> /// Logs command invocation including thrown exceptions /// </summary> /// <remarks><paramref name="logger"/> shouldn't be able to outlive the command!<para/> /// <paramref name="logger"/> subscribes to ThrownExceptions, so you shouldn't use <see cref="LogInvocation{TInput,TOutput}"/> outside of its view model /// </remarks> public static ReactiveCommand <TInput, TOutput> LogInvocation <TInput, TOutput>(this ReactiveCommand <TInput, TOutput> command, string name, Lazy <ILogger> logger, IReadonlyDependencyResolver dr, Func <TInput, string> format = null) { var wrapperCommand = ReactiveCommand.CreateFromTask <TInput, TOutput>(async input => { var logValue = format != null ? $"{name}: {format(input)}" : name; logger.Value.UserAction(logValue); var analyticServers = dr.GetServices <IAnalyticsServer>(); foreach (var analyticServer in analyticServers) { analyticServer.LogEvent(name); } return(await command.Execute(input)); }, command.CanExecute); wrapperCommand.ThrownExceptions.Subscribe(ex => { logger.Value.Error($"{name} exception: {ex}"); }); return(wrapperCommand); }
/// <summary> /// Gets all instances of the given <typeparamref name="T"/>. Must return an empty /// collection if the service is not available (must not return <c>null</c> or throw). /// </summary> /// <typeparam name="T">The type for the object we want to retrieve.</typeparam> /// <param name="resolver">The resolver we are getting the service from.</param> /// <param name="contract">A optional value which will retrieve only a object registered with the same contract.</param> /// <returns>A sequence of instances of the requested <typeparamref name="T"/>. The sequence /// should be empty (not <c>null</c>) if no objects of the given type are available.</returns> public static IEnumerable <T> GetServices <T>(this IReadonlyDependencyResolver resolver, string contract = null) { return(resolver.GetServices(typeof(T), contract).Cast <T>()); }