Init
This commit is contained in:
82
Prefab.Catalog/Module.cs
Normal file
82
Prefab.Catalog/Module.cs
Normal file
@@ -0,0 +1,82 @@
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Prefab.Catalog.Data;
|
||||
using Prefab.Catalog.Data.Services;
|
||||
using Prefab.Catalog.Domain.Services;
|
||||
using Prefab.Data.Seeder;
|
||||
using Prefab.Module;
|
||||
using Prefab.Shared;
|
||||
using Prefab.Shared.Catalog;
|
||||
using Prefab.Shared.Catalog.Categories;
|
||||
using Prefab.Shared.Catalog.Products;
|
||||
|
||||
namespace Prefab.Catalog;
|
||||
|
||||
public class Module : IModule
|
||||
{
|
||||
public WebApplicationBuilder Build(WebApplicationBuilder builder)
|
||||
{
|
||||
builder.Services.AddScoped<IUniqueChecker, UniqueChecker>();
|
||||
builder.Services.AddScoped<IPricingService, PricingService>();
|
||||
builder.Services.AddSingleton<RuleEvaluator>();
|
||||
builder.Services.AddSingleton<VariantResolver>();
|
||||
builder.Services.AddSeeder<Seeder>(RunMode.RunAlways);
|
||||
|
||||
ConfigureModuleClient(builder);
|
||||
|
||||
return builder;
|
||||
}
|
||||
|
||||
public WebApplication Configure(WebApplication app) => app;
|
||||
|
||||
private static void ConfigureModuleClient(WebApplicationBuilder builder)
|
||||
{
|
||||
builder.Services.AddOptions<ModuleClientOptions>().Configure<IConfiguration>((options, configuration) =>
|
||||
{
|
||||
configuration.GetSection("Prefab:Catalog:Client").Bind(options);
|
||||
});
|
||||
|
||||
builder.Services.AddScoped<App.Categories.CategoryClient>();
|
||||
builder.Services.AddScoped<App.Products.ProductClient>();
|
||||
|
||||
builder.Services.AddHttpClient<CategoryClientHttp>((sp, client) =>
|
||||
{
|
||||
var options = sp.GetRequiredService<IOptions<ModuleClientOptions>>().Value;
|
||||
var baseAddress = options.BaseAddress ?? new Uri("http://prefab-catalog");
|
||||
client.BaseAddress = baseAddress;
|
||||
});
|
||||
|
||||
builder.Services.AddHttpClient<ProductClientHttp>((sp, client) =>
|
||||
{
|
||||
var options = sp.GetRequiredService<IOptions<ModuleClientOptions>>().Value;
|
||||
var baseAddress = options.BaseAddress ?? new Uri("http://prefab-catalog");
|
||||
client.BaseAddress = baseAddress;
|
||||
});
|
||||
|
||||
builder.Services.AddScoped<IModuleClient>(sp =>
|
||||
{
|
||||
var options = sp.GetRequiredService<IOptions<ModuleClientOptions>>().Value;
|
||||
|
||||
if (options.Transport == ModuleClientTransport.Http)
|
||||
{
|
||||
var categoryClient = sp.GetRequiredService<CategoryClientHttp>();
|
||||
var productClient = sp.GetRequiredService<ProductClientHttp>();
|
||||
return new ModuleClientHttp(categoryClient, productClient, productClient);
|
||||
}
|
||||
|
||||
var inProcessCategory = sp.GetRequiredService<App.Categories.CategoryClient>();
|
||||
var inProcessProduct = sp.GetRequiredService<App.Products.ProductClient>();
|
||||
return new ModuleClient(inProcessCategory, inProcessProduct, inProcessProduct);
|
||||
});
|
||||
|
||||
builder.Services.AddScoped<ICategoryClient>(sp => sp.GetRequiredService<IModuleClient>().Category);
|
||||
builder.Services.AddScoped<IProductClient>(sp => sp.GetRequiredService<IModuleClient>().Product);
|
||||
builder.Services.AddScoped<IPriceQuoteClient>(sp => sp.GetRequiredService<IModuleClient>().PriceQuote);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user