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

View File

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

View File

@ -55,7 +55,7 @@ namespace Selector.Cache
await Task.WhenAll(tasks); 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) public static void AddCachingConsumerFactories(this IServiceCollection services)
{ {
services.AddTransient<IAudioFeatureInjectorFactory, AudioFeatureInjectorFactory>(); services.AddTransient<IAudioFeatureInjectorFactory, AudioFeatureInjectorFactory>();
services.AddTransient<AudioFeatureInjectorFactory>();
services.AddTransient<IPlayCounterFactory, PlayCounterCachingFactory>(); services.AddTransient<IPlayCounterFactory, PlayCounterCachingFactory>();
services.AddTransient<PlayCounterCachingFactory>();
services.AddTransient<ICacheWriterFactory, CacheWriterFactory>(); services.AddTransient<ICacheWriterFactory, CacheWriterFactory>();
services.AddTransient<CacheWriterFactory>();
services.AddTransient<IPublisherFactory, PublisherFactory>(); 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 System.Threading.Tasks;
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Authorization;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Selector.Web.Service;
using Selector.Web.Hubs; using Selector.Web.Hubs;
using Selector.Web.Extensions;
using Selector.Extensions;
using Selector.Model; using Selector.Model;
using Selector.Model.Extensions; using Selector.Model.Extensions;
using Selector.Cache; using Selector.Cache;
@ -97,14 +96,10 @@ namespace Selector.Web
if (config.RedisOptions.Enabled) if (config.RedisOptions.Enabled)
services.AddRedisServices(config.RedisOptions.ConnectionString); services.AddRedisServices(config.RedisOptions.ConnectionString);
services.AddSingleton<IRefreshTokenFactoryProvider, CachingRefreshTokenFactoryProvider>(); services.AddSpotify();
services.AddSingleton<AudioFeaturePuller>(); services.AddCachingSpotify();
services.AddSingleton<CacheHubProxy>(); services.AddCacheHubProxy();
services.AddHostedService<CacheHubProxyService>();
services.AddTransient<INowPlayingMappingFactory, NowPlayingMappingFactory>();
services.AddScoped<IUserMapping, NowPlayingUserMapping>();
} }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. // 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 System.Text;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using IF.Lastfm.Core.Api;
namespace Selector.Extensions namespace Selector.Extensions
{ {
public static class ServiceExtensions public static class ServiceExtensions
@ -10,7 +12,29 @@ namespace Selector.Extensions
public static void AddConsumerFactories(this IServiceCollection services) public static void AddConsumerFactories(this IServiceCollection services)
{ {
services.AddTransient<IAudioFeatureInjectorFactory, AudioFeatureInjectorFactory>(); services.AddTransient<IAudioFeatureInjectorFactory, AudioFeatureInjectorFactory>();
services.AddTransient<AudioFeatureInjectorFactory>();
services.AddTransient<IPlayCounterFactory, PlayCounterFactory>(); 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>();
} }
} }
} }