Remove Old Stuffs for Refactor xEvent Service for Kafka and RabbitMQ ...

This commit is contained in:
2026-08-18 00:00:27 +03:30
parent a94013b591
commit 699fc721f5
18 changed files with 296 additions and 989 deletions
-288
View File
@@ -1,288 +0,0 @@
using System;
using RabbitMQ.Client;
using xCommons.Extensions;
using xEventService.Configuration;
using xEventService.Models;
using xExceptions.Constants;
namespace xEventService.Extensions {
public static class ConfigurationExtensions {
/// <summary>
/// Create Connection Factory Based on
/// provided Configuration ...
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
public static ConnectionFactory GetConnectionFactory (this XEventServiceConfiguration source) {
//
// Validate Args ...
if (source.IsNull ()) {
XException.InvalidConfiguration.Throw ();
}
//
// Create Connection Factory ...
var result = new ConnectionFactory {
Port = source.Port,
HostName = source.Server,
UserName = source.Username,
Password = source.Password
};
//
// Return Result ...
return result;
}
/// <summary>
/// Create a Connection to Server ...
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
public static IConnection Connect (this XEventServiceConfiguration source) {
//
// Open Connection ...
IConnection result = null;
try {
//
// Retrieve Connection Factory ...
var connectionFactory = source.GetConnectionFactory ();
//
// Create Connection ...
result = connectionFactory.CreateConnection ();
} catch (Exception ex) {
//
Console.WriteLine ($"XEventService Exception: Connection Failed, {ex.Message} ...");
//
XException.ActionFailed.Throw ();
}
//
return result;
}
/// <summary>
/// Validate a Queue Name based on Configurations ...
/// </summary>
/// <param name="source"></param>
/// <param name="queueName"></param>
public static void ValidateQueue (
this XEventServiceConfiguration source,
string queueName
) {
//
// Validate Args ...
if (source.IsNull ()) {
XException.InvalidArgs.Throw ();
}
//
// Validate Queues ...
if ((source.Queues.IsNull () &&
!source.AllowDynamicQueues) ||
(!source.Queues.IsNull () &&
!source.Queues.Keys.HasChild () &&
!source.AllowDynamicQueues)) {
//
Console.WriteLine ($"XEventService Exception: there isn't any configured Queue ...");
//
XException.InvalidArgs.Throw ();
}
//
// Check queueName not empty ...
if (queueName.IsNullOrEmpty ()) {
//
Console.WriteLine ($"XEventService Exception: Invalid QueueName ...");
//
XException.InvalidArgs.Throw ();
}
//
// Check queueName Exists ...
if (!source.Queues.IsNull () &&
!source.Queues.ContainsKey (queueName) &&
!source.AllowDynamicQueues) {
//
Console.WriteLine ($"XEventService Exception: QueueName {queueName} not found ...");
//
XException.InvalidArgs.Throw ();
}
}
/// <summary>
/// Validate a Exchange Name based on Configurations ...
/// </summary>
/// <param name="source"></param>
/// <param name="exchangeName"></param>
public static void ValidateExchange (
this XEventServiceConfiguration source,
string exchangeName
) {
//
// Validate Args ...
if (source.IsNull ()) {
XException.InvalidArgs.Throw ();
}
//
// Validate Exchanges ...
if ((source.Exchanges.IsNull () &&
!source.AllowDynamicExchangess) ||
(!source.Exchanges.IsNull () &&
!source.Exchanges.Keys.HasChild () &&
!source.AllowDynamicExchangess)) {
//
Console.WriteLine ($"XEventService Exception: there isn't any configured Exchanges ...");
//
XException.InvalidArgs.Throw ();
}
//
// Check exchangeName not empty ...
if (exchangeName.IsNullOrEmpty ()) {
//
Console.WriteLine ($"XEventService Exception: Invalid ExchangeName ...");
//
XException.InvalidArgs.Throw ();
}
//
// Check exchangeName Exists ...
if (!source.Exchanges.IsNull () &&
!source.Exchanges.ContainsKey (exchangeName) &&
!source.AllowDynamicExchangess) {
//
Console.WriteLine ($"XEventService Exception: ExchangeName {exchangeName} not found ...");
//
XException.InvalidArgs.Throw ();
}
}
/// <summary>
/// Retrieve specific QueueArgs ...
/// </summary>
/// <param name="source"></param>
/// <param name="queueName"></param>
/// <returns></returns>
public static XQueueArgs GetQueueArgs (
this XEventServiceConfiguration source,
string queueName
) {
//
// Validate Queue Name ...
source.ValidateQueue (queueName);
//
var result = source.Queues
.IsNull () ? null : source
.Queues[queueName];
if (result.IsNull ()) {
//
// Prepare Default Queue ...
result = source.DefaultQueueArgs;
if (result.IsNull ()) {
result = new XQueueArgs () {
Name = queueName
};
} else {
result.Name = queueName;
}
}
//
return result;
}
/// <summary>
/// Retrieve specific ExchangeArgs ...
/// </summary>
/// <param name="source"></param>
/// <param name="exchangeName"></param>
/// <returns></returns>
public static XExchangeArgs GetExchangeArgs (
this XEventServiceConfiguration source,
string exchangeName
) {
//
// Validate Exchange Name ...
source.ValidateExchange (exchangeName);
//
var result = source.Exchanges
.IsNull () ? null : source
.Exchanges[exchangeName];
if (result.IsNull ()) {
//
// Prepare Default Queue ...
result = source.DefaultExchangeArgs;
if (result.IsNull ()) {
result = new XExchangeArgs () {
Name = exchangeName,
Type = ExchangeType.Fanout
};
} else {
result.Name = exchangeName;
}
}
//
return result;
}
/// <summary>
/// Create a Channel ...
/// </summary>
/// <param name="source"></param>
/// <param name="exchangeName"></param>
/// <returns></returns>
public static XChannel CreateChannel (
this XEventServiceConfiguration source,
string exchangeName = null
) {
//
// Validate ExchangeName and Retrieve ExchangeArgs if provided ...
var exchangeArgs = exchangeName
.IsNullOrEmpty () ?
null :
source.GetExchangeArgs (exchangeName);
//
// Create Connection ...
var connection = source.Connect ();
var channel = connection.CreateModel ();
//
// Create XChannel Model ...
var result = new XChannel (
exchangeName: exchangeName,
connection: connection
);
//
// Declaring Queue if Provided ...
if (!exchangeArgs.IsNull () &&
!exchangeName.IsNullOrEmpty ()) {
//
// Declaring Queue ...
result.Channel.ExchangeDeclare (
exchange: exchangeName,
type: exchangeArgs.Type,
durable: exchangeArgs.Durable,
autoDelete: exchangeArgs.AutoDelete
);
}
//
return result;
}
}
}
-44
View File
@@ -1,44 +0,0 @@
using System;
using System.Text;
using xCommons.Extensions;
namespace xEventService.Extensions {
public static class XEventModelExtensions {
/// <summary>
/// Convert an Object to bytes array for publishing
/// </summary>
/// <param name="source"></param>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static byte[] ToBody<T> (this T source) {
//
var json = source
.ToJSON ();
//
var result = json
.ToBytes ();
//
return result;
}
/// <summary>
/// Convert Recieved Bytes to Specific Type ...
/// </summary>
/// <param name="source"></param>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static T FromBody<T> (this ReadOnlyMemory<byte> source) {
//
var jsonString = Encoding.UTF8
.GetString (
source.ToArray ()
);
var result = jsonString.FromJSON<T> ();
//
return result;
}
}
}
+81
View File
@@ -0,0 +1,81 @@
using xCommons.Extensions;
using xEventService.Configuration;
using xEventService.Constants;
using xEventService.Models;
namespace xEventService.Extensions
{
/// <summary>
/// Extensions for Event Service ...
/// </summary>
public static class XEventServiceExtensions
{
/// <summary>
/// Validate Event Broker ...
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
public static bool IsValid(this XEventBroker source)
{
//
var result =
!source.IsNull() &&
source != XEventBroker.None;
//
return result;
}
/// <summary>
/// Validate Event Service Configuration ...
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
public static bool IsValid(this XEventServiceConfiguration source)
{
//
var result =
!source.IsNullOrDefault() &&
source.Broker.IsValid() &&
!source.Url.IsNullOrEmpty() &&
source.Url.IsValidUrl();
//
return result;
}
/// <summary>
/// Validate an Event Message ...
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
public static bool IsValid(this XEventMessage source)
{
//
var result =
!source.IsNullOrDefault() &&
!source.Action.IsNullOrEmpty() &&
!source.Sender.IsNullOrEmpty();
//
return result;
}
/// <summary>
/// Validate an Event Request ...
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
public static bool IsValid(this XEventRequest source)
{
//
var result =
!source.IsNullOrDefault() &&
!source.Topic.IsNullOrEmpty() &&
source.Message.IsValid();
//
return result;
}
}
}