I have four (4) class libraries in c#. Three (3) (InitiatorModule, TransactorModule, LoggerModule) of which are higher level and one (1) (PublisherModule) is a lower level. All the three higher level projects have project reference to the lower level project (PublisherModule) and no reference to each other. What i want is that, the InitiatorModule initiates a transaction and sends it to PublisherModule. PublisherModule then adds some extra information then publishes the message to TransactorModule and LoggerModule of which the PublisherModule is not aware of since they are higher level class libraries. Is there a way to do this using events or delegates or any other way?
I have four (4) class libraries in c#. Three (3) (InitiatorModule, TransactorModule, LoggerModule) of which are higher level and one (1) (PublisherModule) is a lower level. All the three higher level projects have project reference to the lower level project (PublisherModule) and no reference to each other. What i want is that, the InitiatorModule initiates a transaction and sends it to PublisherModule. PublisherModule then adds some extra information then publishes the message to TransactorModule and LoggerModule of which the PublisherModule is not aware of since they are higher level class libraries. Is there a way to do this using events or delegates or any other way?
Here is my code
InitiatorModule
```
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Models;
using Publisher;
namespace Initiator.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class InitializeTransaction : ControllerBase
{
private readonly ILogger<InitializeTransaction> _logger;
private readonly HandleIncoming _handleIncoming;
public InitializeTransaction(ILogger<InitializeTransaction> logger, HandleIncoming handleIncoming)
{
_logger = logger;
_handleIncoming = handleIncoming;
}
[HttpPost]
public void Post([FromBody] TransactionDetails transactionDetails)
{
_handleIncoming.Publish(transactionDetails);
}
}
}
```
PublisherModule
```
using Models;
using System;
namespace Publisher
{
public class HandleIncoming
{
public event TransactionHandler OutBoundTransaction;
public void Publish(TransactionDetails transactionDetails)
{
transactionDetails.Date = DateTime.UtcNow;
OutBoundTransaction(transactionDetails);
}
}
}
```
TransactorModule
```
using Models;
using Publisher;
using System;
namespace Transactor
{
public class TransactionExecutor
{
private readonly HandleIncoming handleIncoming = new HandleIncoming();
public TransactionExecutor()
{
handleIncoming.OutBoundTransaction += OnOutBoundTransaction;
}
public void OnOutBoundTransaction(TransactionDetails transactionDetails)
{
Console.WriteLine($"Send Amount: {transactionDetails.Amount} to Username: {transactionDetails.Recipient} \nDate: {transactionDetails.Date}");
}
}
}
```
LoggerModule
```
using Models;
using Publisher;
using System;
namespace MyLogger
{
public class TransactionLogger
{
private readonly HandleIncoming handleIncoming = new HandleIncoming();
public TransactionLogger()
{
handleIncoming.OutBoundTransaction += OnOutBoundTransaction;
}
public void OnOutBoundTransaction(TransactionDetails transactionDetails)
{
Console.WriteLine($"Username: {transactionDetails.Name} \nAmount: {transactionDetails.Amount} \nDate: {transactionDetails.Date}");
}
}
}
```
Delegate signature
```
namespace Models
{
public delegate void TransactionHandler(TransactionDetails transactionDetails);
}
```
Model
```
using System;
namespace Models
{
public class TransactionDetails
{
public string UserId { get; set; }
public string Name { get; set; }
public decimal Amount { get; set; }
public DateTime Date { get; set; }
public string Recipient { get; set; }
}
}
```
**Please note each module represent a different project**
What I have tried:
I have tried using a delegate to listen for an event in PublisherModule and subscribed to that delegate from TransactorModule and LoggerModule but it's not working.