228 lines
6.8 KiB
C#
228 lines
6.8 KiB
C#
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using Microsoft.AspNetCore.Builder;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using xCommons.Extensions;
|
|
using xDataService.DI;
|
|
using xDataService.Models;
|
|
using xMessageService.DI;
|
|
using xServices.Configurations;
|
|
using xServices.Constants;
|
|
using xServices.Interfaces;
|
|
using xServices.Providers;
|
|
using xStorageService.DI;
|
|
|
|
namespace xServices.DI
|
|
{
|
|
public static partial class XDIHelperExtension
|
|
{
|
|
/// <summary>
|
|
/// Retrive Services Configurations ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <returns></returns>
|
|
public static XServicesConfiguration GetXServicesConfiguration(
|
|
this IConfiguration source
|
|
)
|
|
{
|
|
//
|
|
var xConfig = source
|
|
.GetSection(ConfigurationNodeNames.SERVICES_NODE);
|
|
var result = xConfig.Get<XServicesConfiguration>();
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Register Service Configuration ...
|
|
/// </summary>
|
|
/// <param name="services"></param>
|
|
/// <param name="configuration"></param>
|
|
public static void AddXServicesConfiguration(
|
|
this IServiceCollection services,
|
|
IConfiguration configuration
|
|
)
|
|
{
|
|
//
|
|
var config = configuration
|
|
.GetXServicesConfiguration();
|
|
if (config.IsNull())
|
|
{
|
|
config = new XServicesConfiguration();
|
|
}
|
|
|
|
//
|
|
services.AddSingleton(config);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Register Service Configuration ...
|
|
/// </summary>
|
|
/// <param name="services"></param>
|
|
/// <param name="configuration"></param>
|
|
public static void AddXServicesConfiguration(
|
|
this IServiceCollection services,
|
|
XServicesConfiguration configuration
|
|
)
|
|
{
|
|
//
|
|
if (configuration.IsNull())
|
|
{
|
|
configuration = new XServicesConfiguration();
|
|
}
|
|
|
|
//
|
|
services.AddSingleton(configuration);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Register Services ...
|
|
/// </summary>
|
|
/// <param name="services"></param>
|
|
/// <param name="configuration"></param>
|
|
/// <param name="databases"></param>
|
|
/// <param name="lifeTime"></param>
|
|
public static void AddXServices(
|
|
this IServiceCollection services,
|
|
IConfiguration configuration,
|
|
IEnumerable<XDatabaseDescriptor> databases = null,
|
|
ServiceLifetime lifeTime = ServiceLifetime.Scoped
|
|
)
|
|
{
|
|
//
|
|
var config = configuration.GetXServicesConfiguration();
|
|
|
|
//
|
|
AddServices(
|
|
config: config,
|
|
services: services,
|
|
lifeTime: lifeTime,
|
|
databases: databases,
|
|
configuration: configuration
|
|
);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Register Services ...
|
|
/// </summary>
|
|
/// <param name="services"></param>
|
|
/// <param name="configuration"></param>
|
|
/// <param name="config"></param>
|
|
/// <param name="databases"></param>
|
|
/// <param name="lifeTime"></param>
|
|
public static void AddXServices(
|
|
this IServiceCollection services,
|
|
IConfiguration configuration,
|
|
XServicesConfiguration config,
|
|
IEnumerable<XDatabaseDescriptor> databases = null,
|
|
ServiceLifetime lifeTime = ServiceLifetime.Scoped
|
|
)
|
|
{
|
|
//
|
|
AddServices(
|
|
config: config,
|
|
services: services,
|
|
lifeTime: lifeTime,
|
|
databases: databases,
|
|
configuration: configuration
|
|
);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Use All Required Middlewares ...
|
|
/// </summary>
|
|
/// <param name="app"></param>
|
|
/// <param name="databases"></param>
|
|
/// <param name="isDevelopmentEnvironment"></param>
|
|
public static void UseXServices(
|
|
this IApplicationBuilder app,
|
|
IEnumerable<XDatabaseDescriptor> databases = null,
|
|
bool isDevelopmentEnvironment = false
|
|
)
|
|
{
|
|
//
|
|
// Use Storage Service ...
|
|
app.UseXStorageService();
|
|
|
|
//
|
|
#region Use Data Data Service ...
|
|
if (databases.HasChild())
|
|
{
|
|
//
|
|
databases
|
|
.ToList()
|
|
.ForEach(database =>
|
|
{
|
|
//
|
|
app.UseXDatabase(
|
|
descriptor: database,
|
|
isDevelopmentEnvironment: isDevelopmentEnvironment
|
|
);
|
|
});
|
|
}
|
|
#endregion
|
|
}
|
|
|
|
//
|
|
#region Private ...
|
|
/// <summary>
|
|
/// Register Required Services ...
|
|
/// </summary>
|
|
/// <param name="services"></param>
|
|
/// <param name="config"></param>
|
|
/// <param name="configuration"></param>
|
|
/// <param name="databases"></param>
|
|
/// <param name="lifeTime"></param>
|
|
private static void AddServices(
|
|
this IServiceCollection services,
|
|
IConfiguration configuration,
|
|
XServicesConfiguration config = null,
|
|
IEnumerable<XDatabaseDescriptor> databases = null,
|
|
ServiceLifetime lifeTime = ServiceLifetime.Transient
|
|
)
|
|
{
|
|
//
|
|
// Check config ...
|
|
if (config.IsNull())
|
|
{
|
|
config = new XServicesConfiguration();
|
|
}
|
|
|
|
//
|
|
// Storage Service ...
|
|
services.AddXStorageService(configuration);
|
|
|
|
//
|
|
// Message Service ...
|
|
services.AddXMessageService(config: configuration);
|
|
|
|
//
|
|
#region Register Data Service ...
|
|
//
|
|
if (databases.HasChild())
|
|
{
|
|
//
|
|
databases
|
|
.ToList()
|
|
.ForEach(database =>
|
|
{
|
|
//
|
|
// Register DataBase ...
|
|
services.AddXDatabase(
|
|
lifetime: lifeTime,
|
|
descriptor: database,
|
|
configuration: configuration
|
|
);
|
|
});
|
|
}
|
|
|
|
//
|
|
// Register Repository Services ...
|
|
services.AddScoped<IXTestRepositoryService, XTestRepositoryService>();
|
|
#endregion
|
|
}
|
|
#endregion
|
|
}
|
|
} |