Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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

```
C#
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
```
C#
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
```
C#
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
```
C#
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

```
C#
namespace Models
{
    public delegate void TransactionHandler(TransactionDetails transactionDetails);
}

```

Model
```
C#
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.
Posted
Updated 16-Dec-21 2:29am
v4
Comments
FranzBe 16-Dec-21 3:25am    
perhaps this one might help:
https://www.codeproject.com/Articles/5317666/Why-do-we-need-MediatR
Richard MacCutchan 16-Dec-21 3:57am    
"but it's not working."
1. No one here can guess what that statement is supposed to mean.
2. Without seeing the code and an explanation of the problem, no one here can easily offer suggestions.
Eugene Rutsito 16-Dec-21 7:11am    
Sorry @Richard MacCutchan, i have added my sample code
[no name] 16-Dec-21 11:05am    
Maybe you should ask yourself why you have 4 (class) libraries when the classes are related.
Eugene Rutsito 17-Dec-21 2:22am    
Actually this is the simplified version. In the real version, the projects are not related. However they depend on this message from the PublisherModule

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900