more service extensions

This commit is contained in:
andy 2021-11-29 21:48:00 +00:00
parent e41d525b0b
commit e9f593862e
7 changed files with 66 additions and 27 deletions

View File

@ -22,8 +22,7 @@ namespace Selector.CLI
{
private const int PollPeriod = 1000;
private readonly ILogger<LocalWatcherService> Logger;
private readonly ILoggerFactory LoggerFactory;
private readonly ILogger<DbWatcherService> Logger;
private readonly IServiceProvider ServiceProvider;
private readonly IWatcherFactory WatcherFactory;
@ -48,11 +47,10 @@ namespace Selector.CLI
IPublisherFactory publisherFactory,
ICacheWriterFactory cacheWriterFactory,
ILoggerFactory loggerFactory,
ILogger<DbWatcherService> logger,
IServiceProvider serviceProvider
) {
Logger = loggerFactory.CreateLogger<LocalWatcherService>();
LoggerFactory = loggerFactory;
Logger = logger;
ServiceProvider = serviceProvider;
WatcherFactory = watcherFactory;

View File

@ -1,17 +1,17 @@
using System;
using System.Threading.Tasks;
using System.CommandLine;
using System.CommandLine.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Configuration;
using Microsoft.EntityFrameworkCore;
using NLog.Extensions.Logging;
using Selector.Extensions;
using Selector.Model;
using Selector.Cache;
using Selector.Cache.Extensions;
using IF.Lastfm.Core.Api;
namespace Selector.CLI
@ -54,9 +54,7 @@ namespace Selector.CLI
{
Console.WriteLine("> Adding Last.fm credentials...");
var lastAuth = new LastAuth(config.LastfmClient, config.LastfmSecret);
services.AddSingleton(lastAuth);
services.AddTransient(sp => new LastfmClient(sp.GetService<LastAuth>()));
services.AddLastFm(config.LastfmClient, config.LastfmSecret);
}
else
{
@ -101,14 +99,12 @@ namespace Selector.CLI
Console.WriteLine("> Adding Services...");
// SERVICES
services.AddConsumerFactories();
services.AddCachingConsumerFactories();
services.AddWatcher();
services.AddSingleton<IWatcherFactory, WatcherFactory>();
services.AddSingleton<IWatcherCollectionFactory, WatcherCollectionFactory>();
// For generating spotify clients
//services.AddSingleton<IRefreshTokenFactoryProvider, RefreshTokenFactoryProvider>();
services.AddSingleton<IRefreshTokenFactoryProvider, CachingRefreshTokenFactoryProvider>();
services.AddSpotify();
services.AddCachingSpotify();
ConfigureLastFm(config, services);
ConfigureDb(config, services);

View File

@ -55,7 +55,7 @@ namespace Selector.Cache
await Task.WhenAll(tasks);
Logger.LogDebug($"Cached audio feature for [{track.DisplayString()}]");
Logger.LogDebug($"Cached play count for [{track.DisplayString()}]");
}
}
}

View File

@ -28,10 +28,19 @@ namespace Selector.Cache.Extensions
public static void AddCachingConsumerFactories(this IServiceCollection services)
{
services.AddTransient<IAudioFeatureInjectorFactory, AudioFeatureInjectorFactory>();
services.AddTransient<AudioFeatureInjectorFactory>();
services.AddTransient<IPlayCounterFactory, PlayCounterCachingFactory>();
services.AddTransient<PlayCounterCachingFactory>();
services.AddTransient<ICacheWriterFactory, CacheWriterFactory>();
services.AddTransient<CacheWriterFactory>();
services.AddTransient<IPublisherFactory, PublisherFactory>();
services.AddTransient<PublisherFactory>();
}
public static void AddCachingSpotify(this IServiceCollection services)
{
services.AddSingleton<AudioFeaturePuller>();
}
}
}

View File

@ -0,0 +1,17 @@
using Microsoft.Extensions.DependencyInjection;
using Selector.Web.Service;
namespace Selector.Web.Extensions
{
public static class ServiceExtensions
{
public static void AddCacheHubProxy(this IServiceCollection services)
{
services.AddSingleton<CacheHubProxy>();
services.AddHostedService<CacheHubProxyService>();
services.AddTransient<INowPlayingMappingFactory, NowPlayingMappingFactory>();
services.AddScoped<IUserMapping, NowPlayingUserMapping>();
}
}
}

View File

@ -4,17 +4,16 @@ using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Authorization;
using Microsoft.EntityFrameworkCore;
using Selector.Web.Service;
using Selector.Web.Hubs;
using Selector.Web.Extensions;
using Selector.Extensions;
using Selector.Model;
using Selector.Model.Extensions;
using Selector.Cache;
@ -97,14 +96,10 @@ namespace Selector.Web
if (config.RedisOptions.Enabled)
services.AddRedisServices(config.RedisOptions.ConnectionString);
services.AddSingleton<IRefreshTokenFactoryProvider, CachingRefreshTokenFactoryProvider>();
services.AddSingleton<AudioFeaturePuller>();
services.AddSpotify();
services.AddCachingSpotify();
services.AddSingleton<CacheHubProxy>();
services.AddHostedService<CacheHubProxyService>();
services.AddTransient<INowPlayingMappingFactory, NowPlayingMappingFactory>();
services.AddScoped<IUserMapping, NowPlayingUserMapping>();
services.AddCacheHubProxy();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.

View File

@ -3,6 +3,8 @@ using System.Collections.Generic;
using System.Text;
using Microsoft.Extensions.DependencyInjection;
using IF.Lastfm.Core.Api;
namespace Selector.Extensions
{
public static class ServiceExtensions
@ -10,7 +12,29 @@ namespace Selector.Extensions
public static void AddConsumerFactories(this IServiceCollection services)
{
services.AddTransient<IAudioFeatureInjectorFactory, AudioFeatureInjectorFactory>();
services.AddTransient<AudioFeatureInjectorFactory>();
services.AddTransient<IPlayCounterFactory, PlayCounterFactory>();
services.AddTransient<PlayCounterFactory>();
}
public static void AddSpotify(this IServiceCollection services)
{
services.AddSingleton<IRefreshTokenFactoryProvider, RefreshTokenFactoryProvider>();
services.AddSingleton<IRefreshTokenFactoryProvider, CachingRefreshTokenFactoryProvider>();
}
public static void AddLastFm(this IServiceCollection services, string client, string secret)
{
var lastAuth = new LastAuth(client, secret);
services.AddSingleton(lastAuth);
services.AddTransient(sp => new LastfmClient(sp.GetService<LastAuth>()));
}
public static void AddWatcher(this IServiceCollection services)
{
services.AddSingleton<IWatcherFactory, WatcherFactory>();
services.AddSingleton<IWatcherCollectionFactory, WatcherCollectionFactory>();
}
}
}