Dependency injection
This commit is contained in:
@@ -1,6 +0,0 @@
|
|||||||
namespace Musify.Application
|
|
||||||
{
|
|
||||||
public static class ApplicationDependencyInjection
|
|
||||||
{
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
|
||||||
|
namespace Musify.Application.Configuration
|
||||||
|
{
|
||||||
|
public static class ConfigurationDependencyInjection
|
||||||
|
{
|
||||||
|
public static void AddPlayListConfiguration(this IServiceCollection serviceDescriptors, IConfiguration configuration)
|
||||||
|
{
|
||||||
|
var playListConfiguration = PlayListConfiguration.Load(configuration);
|
||||||
|
|
||||||
|
serviceDescriptors.AddSingleton(playListConfiguration);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void AddStorageConfiguration(this IServiceCollection serviceDescriptors, IConfiguration configuration)
|
||||||
|
{
|
||||||
|
var storageConfiguration = StorageConfiguration.Load(configuration);
|
||||||
|
|
||||||
|
serviceDescriptors.AddSingleton(storageConfiguration);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -20,38 +20,38 @@ namespace Musify.Application.Configuration
|
|||||||
|
|
||||||
public int LargePictureHeight { get; private set; } = 512;
|
public int LargePictureHeight { get; private set; } = 512;
|
||||||
|
|
||||||
public PlayListConfiguration Load(IConfiguration configuration)
|
public static PlayListConfiguration Load(IConfiguration configuration)
|
||||||
{
|
{
|
||||||
var section = configuration.GetSection(SectionName).Get<PlayListConfiguration>()
|
var section = configuration.GetSection(SectionName).Get<PlayListConfiguration>()
|
||||||
?? throw new InvalidOperationException($"{SectionName} configuration section not found.");
|
?? throw new InvalidOperationException($"{SectionName} configuration section not found.");
|
||||||
|
|
||||||
Validate();
|
Validate(section);
|
||||||
|
|
||||||
return section;
|
return section;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Validate()
|
static void Validate(PlayListConfiguration configuration)
|
||||||
{
|
{
|
||||||
EnsureValue(Routes.ParentFolder, $"{SectionName}:BucketName");
|
EnsureValue(configuration.Routes.ParentFolder, $"{SectionName}:BucketName");
|
||||||
EnsureValue(Routes.SmallPictures, $"{SectionName}:Routes:SmallPictures");
|
EnsureValue(configuration.Routes.SmallPictures, $"{SectionName}:Routes:SmallPictures");
|
||||||
EnsureValue(Routes.MediumPictures, $"{SectionName}:Routes:MediumPictures");
|
EnsureValue(configuration.Routes.MediumPictures, $"{SectionName}:Routes:MediumPictures");
|
||||||
EnsureValue(Routes.LargePictures, $"{SectionName}:Routes:LargePictures");
|
EnsureValue(configuration.Routes.LargePictures, $"{SectionName}:Routes:LargePictures");
|
||||||
|
|
||||||
EnsurePositiveValue(SmallPictureWidth, $"{SectionName}:SmallPictureWidth");
|
EnsurePositiveValue(configuration.SmallPictureWidth, $"{SectionName}:SmallPictureWidth");
|
||||||
EnsurePositiveValue(SmallPictureHeight, $"{SectionName}:SmallPictureHeight");
|
EnsurePositiveValue(configuration.SmallPictureHeight, $"{SectionName}:SmallPictureHeight");
|
||||||
EnsurePositiveValue(MediumPictureWidth, $"{SectionName}:MediumPictureWidth");
|
EnsurePositiveValue(configuration.MediumPictureWidth, $"{SectionName}:MediumPictureWidth");
|
||||||
EnsurePositiveValue(MediumPictureHeight, $"{SectionName}:MediumPictureHeight");
|
EnsurePositiveValue(configuration.MediumPictureHeight, $"{SectionName}:MediumPictureHeight");
|
||||||
EnsurePositiveValue(LargePictureWidth, $"{SectionName}:LargePictureWidth");
|
EnsurePositiveValue(configuration.LargePictureWidth, $"{SectionName}:LargePictureWidth");
|
||||||
EnsurePositiveValue(LargePictureHeight, $"{SectionName}:LargePictureHeight");
|
EnsurePositiveValue(configuration.LargePictureHeight, $"{SectionName}:LargePictureHeight");
|
||||||
}
|
}
|
||||||
|
|
||||||
void EnsureValue(string value, string name)
|
static void EnsureValue(string value, string name)
|
||||||
{
|
{
|
||||||
if (string.IsNullOrEmpty(value))
|
if (string.IsNullOrEmpty(value))
|
||||||
throw new InvalidOperationException($"{name} configuration value not found.");
|
throw new InvalidOperationException($"{name} configuration value not found.");
|
||||||
}
|
}
|
||||||
|
|
||||||
void EnsurePositiveValue(int value, string name)
|
static void EnsurePositiveValue(int value, string name)
|
||||||
{
|
{
|
||||||
if (value < 1)
|
if (value < 1)
|
||||||
throw new InvalidOperationException($"{name} configuration value must be a positive integer.");
|
throw new InvalidOperationException($"{name} configuration value must be a positive integer.");
|
||||||
|
|||||||
@@ -10,23 +10,23 @@ namespace Musify.Application.Configuration
|
|||||||
|
|
||||||
public StorageRoutes Routes { get; set; } = new StorageRoutes();
|
public StorageRoutes Routes { get; set; } = new StorageRoutes();
|
||||||
|
|
||||||
public StorageConfiguration Load(IConfiguration configuration)
|
public static StorageConfiguration Load(IConfiguration configuration)
|
||||||
{
|
{
|
||||||
var section = configuration.GetSection(SectionName).Get<StorageConfiguration>()
|
var section = configuration.GetSection(SectionName).Get<StorageConfiguration>()
|
||||||
?? throw new InvalidOperationException($"{SectionName} configuration section not found.");
|
?? throw new InvalidOperationException($"{SectionName} configuration section not found.");
|
||||||
|
|
||||||
Validate();
|
Validate(section);
|
||||||
|
|
||||||
return section;
|
return section;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Validate()
|
static void Validate(StorageConfiguration configuration)
|
||||||
{
|
{
|
||||||
EnsureValue(BucketName, nameof(BucketName));
|
EnsureValue(configuration.BucketName, nameof(BucketName));
|
||||||
EnsureValue(Routes.Uploads, $"{SectionName}:Route:Upload");
|
EnsureValue(configuration.Routes.Uploads, $"{SectionName}:Route:Upload");
|
||||||
}
|
}
|
||||||
|
|
||||||
void EnsureValue(string value, string name)
|
static void EnsureValue(string value, string name)
|
||||||
{
|
{
|
||||||
if (string.IsNullOrEmpty(value))
|
if (string.IsNullOrEmpty(value))
|
||||||
throw new InvalidOperationException($"{name} configuration value not found.");
|
throw new InvalidOperationException($"{name} configuration value not found.");
|
||||||
|
|||||||
@@ -0,0 +1,13 @@
|
|||||||
|
using DispatchR.Extensions;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
|
||||||
|
namespace Musify.Application.DispatchR
|
||||||
|
{
|
||||||
|
public static class DispatchRDependencyInjection
|
||||||
|
{
|
||||||
|
public static void AddDispatchR(this IServiceCollection serviceDescriptors)
|
||||||
|
{
|
||||||
|
serviceDescriptors.AddDispatchR(typeof(DispatchRDependencyInjection).Assembly);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Musify.Application.Contracts.Application;
|
||||||
|
|
||||||
|
namespace Musify.Application.Pictures
|
||||||
|
{
|
||||||
|
public static class PictureDependencyInjection
|
||||||
|
{
|
||||||
|
public static void AddPictureService(this IServiceCollection serviceDescriptors)
|
||||||
|
{
|
||||||
|
serviceDescriptors.AddScoped<IPictureService, PictureService>();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -11,7 +11,9 @@
|
|||||||
<PackageReference Include="AWSSDK.S3" Version="4.0.19" />
|
<PackageReference Include="AWSSDK.S3" Version="4.0.19" />
|
||||||
<PackageReference Include="Keycloak.Net.Core" Version="1.0.38" />
|
<PackageReference Include="Keycloak.Net.Core" Version="1.0.38" />
|
||||||
<PackageReference Include="MassTransit" Version="9.0.1" />
|
<PackageReference Include="MassTransit" Version="9.0.1" />
|
||||||
|
<PackageReference Include="MassTransit.RabbitMQ" Version="9.0.1" />
|
||||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="10.0.5" />
|
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="10.0.5" />
|
||||||
|
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="10.0.1" />
|
||||||
<PackageReference Include="SixLabors.ImageSharp" Version="3.1.12" />
|
<PackageReference Include="SixLabors.ImageSharp" Version="3.1.12" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,33 @@
|
|||||||
|
using MassTransit;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Musify.Infrastructure.Messaging.Consumers;
|
||||||
|
|
||||||
|
namespace Musify.Infrastructure.Messaging
|
||||||
|
{
|
||||||
|
public static class MassTransitDependencyInjection
|
||||||
|
{
|
||||||
|
public static void AddMassTransitHandler(this IServiceCollection serviceDescriptors, IConfiguration configuration)
|
||||||
|
{
|
||||||
|
serviceDescriptors.AddMassTransit(options =>
|
||||||
|
{
|
||||||
|
options.AddConsumer<PictureResizerConsumer>();
|
||||||
|
|
||||||
|
options.UsingRabbitMq((busRegistrationContext, busFactoryConfigurator) =>
|
||||||
|
{
|
||||||
|
var section = configuration.GetSection("MassTransit");
|
||||||
|
|
||||||
|
var host = section["Host"]
|
||||||
|
?? throw new InvalidOperationException("MassTransit:Host configuration is missing.");
|
||||||
|
|
||||||
|
busFactoryConfigurator.Host(host);
|
||||||
|
|
||||||
|
busFactoryConfigurator.ReceiveEndpoint(PictureResizerConsumer.QueueName, endpointConfigurator =>
|
||||||
|
{
|
||||||
|
endpointConfigurator.ConfigureConsumer<PictureResizerConsumer>(busRegistrationContext);
|
||||||
|
} );
|
||||||
|
} );
|
||||||
|
} );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Musify.Application.Contracts.Infrastructure;
|
||||||
|
|
||||||
|
namespace Musify.Infrastructure.Persistence
|
||||||
|
{
|
||||||
|
public static class DatabaseDependencyInjection
|
||||||
|
{
|
||||||
|
public static void AddDatabase(this IServiceCollection serviceDescriptors, IConfiguration configuration)
|
||||||
|
{
|
||||||
|
var connectionString = configuration.GetConnectionString("Default")
|
||||||
|
?? throw new InvalidOperationException("Default Connection connection string not found.");
|
||||||
|
|
||||||
|
serviceDescriptors.AddDbContext<Database>(options =>
|
||||||
|
{
|
||||||
|
options.UseNpgsql(connectionString);
|
||||||
|
} );
|
||||||
|
|
||||||
|
serviceDescriptors.AddScoped<IDatabase>(serviceProvider => serviceProvider.GetRequiredService<Database>());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Musify.Application.Contracts.Infrastructure;
|
||||||
|
|
||||||
|
namespace Musify.Infrastructure.Pictures
|
||||||
|
{
|
||||||
|
public static class PictureDependencyInjection
|
||||||
|
{
|
||||||
|
public static void AddPictureHandler(this IServiceCollection serviceDescriptors)
|
||||||
|
{
|
||||||
|
serviceDescriptors.AddScoped<IPictureHandler, PictureHandler>();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Musify.Application.Contracts.Infrastructure;
|
||||||
|
|
||||||
|
namespace Musify.Infrastructure.Storage
|
||||||
|
{
|
||||||
|
public static class StorageDependencyInjection
|
||||||
|
{
|
||||||
|
public static void AddStorageHandler(this IServiceCollection serviceDescriptors)
|
||||||
|
{
|
||||||
|
serviceDescriptors.AddScoped<IStorageHandler, StorageHandler>();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user