140 lines
4.9 KiB
C#
140 lines
4.9 KiB
C#
using System;
|
|
using Microsoft.AspNetCore.Builder;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using xCommons.Extensions;
|
|
using xEventService.Configuration;
|
|
using xEventService.Constants;
|
|
using xEventService.Interfaces;
|
|
using xEventService.Models;
|
|
using xEventService.Providers;
|
|
|
|
namespace xEventService.DI {
|
|
public static partial class XDIHelperExtension {
|
|
/// <summary>
|
|
/// Extract EventService Configurations ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <returns></returns>
|
|
public static XEventServiceConfiguration GetXEventServiceConfigurations (this IConfiguration source) {
|
|
//
|
|
var configSection = source
|
|
.GetSection (ConfigurationNodeNames.EVENT_SERVICE_NODE_NAME);
|
|
var result = configSection.Get<XEventServiceConfiguration> ();
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Register Event Service Configurations ...
|
|
/// </summary>
|
|
/// <param name="services"></param>
|
|
/// <param name="configuration"></param>
|
|
public static void AddXEventServiceConfigurations (
|
|
this IServiceCollection services,
|
|
IConfiguration configuration
|
|
) {
|
|
//
|
|
var config = configuration.GetXEventServiceConfigurations ();
|
|
services.AddXEventServiceConfigurations (config);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Register Event Service Configurations ...
|
|
/// </summary>
|
|
/// <param name="services"></param>
|
|
/// <param name="configuration"></param>
|
|
public static void AddXEventServiceConfigurations (
|
|
this IServiceCollection services,
|
|
XEventServiceConfiguration configuration
|
|
) {
|
|
//
|
|
services.AddSingleton<XEventServiceConfiguration> (configuration);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Register Event Service ...
|
|
/// </summary>
|
|
/// <param name="services"></param>
|
|
/// <param name="configuration"></param>
|
|
public static void AddXEventService (
|
|
this IServiceCollection services,
|
|
IConfiguration configuration
|
|
) {
|
|
//
|
|
// Check Configuration Registered ...
|
|
var config = services.GetRegisteredService<XEventServiceConfiguration> ();
|
|
if (config.IsNull ()) {
|
|
//
|
|
Console.WriteLine ($"XEventService: there is no provided XEventServiceConfiguration, using default ...");
|
|
|
|
//
|
|
// Register Default Configuration ...
|
|
services.AddXEventServiceConfigurations (configuration);
|
|
}
|
|
|
|
//
|
|
// Register IXEventBus ...
|
|
var serviceBas = services.GetRegisteredService<IXEventBus> ();
|
|
if (serviceBas.IsNull ()) {
|
|
services.AddSingleton<IXEventBus, XEventBus> ();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Register an Event Handler ...
|
|
/// </summary>
|
|
/// <param name="services"></param>
|
|
/// <param name="lifetime"></param>
|
|
/// <typeparam name="TEvent"></typeparam>
|
|
/// <typeparam name="THandler"></typeparam>
|
|
/// <returns></returns>
|
|
public static void AddXEventHandler<TEvent, THandler> (
|
|
this IServiceCollection services,
|
|
ServiceLifetime lifetime = ServiceLifetime.Transient
|
|
)
|
|
where TEvent : XEvent
|
|
where THandler : IXEventHandler<TEvent> {
|
|
//
|
|
services.Add (new ServiceDescriptor (
|
|
serviceType: typeof (THandler),
|
|
implementationType: typeof (THandler),
|
|
lifetime: lifetime
|
|
));
|
|
|
|
//
|
|
services.Add (new ServiceDescriptor (
|
|
serviceType: typeof (IXEventHandler<TEvent>),
|
|
implementationType: typeof (THandler),
|
|
lifetime: lifetime
|
|
));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Subscribe to specific Event ...
|
|
/// </summary>
|
|
/// <param name="app"></param>
|
|
/// <typeparam name="TEvent"></typeparam>
|
|
/// <typeparam name="THandler"></typeparam>
|
|
/// <returns></returns>
|
|
public static void XEventSubscribe<TEvent, THandler> (
|
|
this IApplicationBuilder app,
|
|
bool toExchange = true
|
|
)
|
|
where TEvent : XEvent
|
|
where THandler : IXEventHandler<TEvent> {
|
|
//
|
|
var eventName = typeof (TEvent).Name;
|
|
|
|
//
|
|
// Retrieve EventBus from IOC ...
|
|
var eventBus = app.ApplicationServices.GetRequiredService<IXEventBus> ();
|
|
|
|
//
|
|
// Subscribe to Event Handler ...
|
|
var isSubscribed = eventBus.Subscribe<TEvent, THandler> ();
|
|
Console.WriteLine ($"XEventService: Event Handler subscription result: {eventName} => {isSubscribed} ...");
|
|
}
|
|
}
|
|
} |