-2

How can ConnectionManager get expiredRegistrationId that was captured in AndroidPush class?

Am I doing this the wrong way?

Any suggestions on how I may improve my solution?

Is there any pattern I could follow?

Solution : Managers

public class ConnectionManger
{
    private readonly IPushManager pushManager = new PushManager();

    public void NotifyAppUser(List<PushNotificationSubscription>  regIds, Alert alert)
    {
       pushManager.PushNotification(regIds, alert);
       var expiredRegistrationId = ??

    }
}

Solution : PushNotification

 public class PushManager : IPushManager
    {

        public void PushNotification(List<PushNotificationSubscription> registeredPhone, Alert alert)
        {

            AndroidPush androidPush = new AndroidPush();
            androidPush.Push(alert, registeredPhone);

        }     

    }


    public class AndroidPush : IPushNotificationStrategy
    {

        public void Push(Alert alert, List<string> registrationIds)
        {

            // Wait for GCM server
            #region GCM Events
            gcmBroker.OnNotificationFailed += (notification, aggregateEx) =>
            {

               var expiredRegistrationId = aggregateEx.OldId;
               Q: How do i pass expiredRegistrationId to ConnectionManager class? 
            };

        } 



    }
Hasta Tamang
  • 2,205
  • 1
  • 18
  • 17
  • Ad a failed property to the class ConnectionManager so it can be set by the event handler. – jdweng Aug 04 '17 at 15:21
  • if i add a failed property to ConnectionManager, AndroidPush cannot set this property as it is being referenced by ConnectionManager and causes circular reference. – Hasta Tamang Aug 04 '17 at 15:40
  • You need to use an instance of the class. – jdweng Aug 04 '17 at 17:29
  • Your question lacks context, a good [mcve], and is far too vague. It's not clearly why you don't just subscribe to the `OnNotificationFailed` event from the manager class. _Some_ sort of design like that would be the right way to solve this, i.e. don't expect the notification project to know anything about the manager project, but instead use C# events to allow the manager project to respond to specific things that occur in the notification project. You need to improve the question significantly if you want a good answer. – Peter Duniho Aug 04 '17 at 17:32
  • @PeterDuniho I have removed unnecessary code, and simplified it. I hope it makes a bit more sense, what I am trying to get at? – Hasta Tamang Aug 04 '17 at 21:59
  • Simplifying is good, but there still needs to be a _complete_ code example. It's not clear what `gcmBroker` is here and why the manager class can't subscribe to the event directly. ... – Peter Duniho Aug 04 '17 at 22:14
  • ... That said, assuming that's really not possible, the next option is for `AndroidPush` to declare an event the manager class _can_ subscribe to, and then raise that event when the `gcmBroker` event is raised. See https://stackoverflow.com/questions/3612137/is-it-possible-to-expose-events-of-a-member-object-of-a-class-to-the-outside-in and https://stackoverflow.com/questions/3310661/exposing-events-of-underlying-control for details about how to do that. – Peter Duniho Aug 04 '17 at 22:14
  • Possible duplicate of https://stackoverflow.com/questions/3612137/is-it-possible-to-expose-events-of-a-member-object-of-a-class-to-the-outside-in – Peter Duniho Aug 04 '17 at 22:23
  • Possible duplicate of https://stackoverflow.com/questions/3310661/exposing-events-of-underlying-control – Peter Duniho Aug 04 '17 at 22:23

1 Answers1

0

You have .NET as common playground so there are several options

Assuming:

  • you by solutions mean separate dll´s
  • you need to return a list of strings ( from the original question )
  • your code is keeping gcmBroker alive so the OnNotificationFailed event can fire

then this should work:

Change your signature in the IPushNotificationStrategy interface to

List<string> Push(Alert alert, List<string> registrationIds)

Add this event to your IPushManager interface:

event Action<List<string>> ExpiredRegs;

Implement and invoke this event from PushManager:

public event Action<List<string>> ExpiredRegs;

// call this when Push returns some expiredRegs :
void OnExpiredRegs(List<string> expiredRegs) => ExpiredRegs?.Invoke(expiredRegs);

Subscribe to the event in ConnectionManger:

pushManager.ExpiredRegs += OnExpiredRegs;

void OnExpiredRegs(List<string> expiredRegs)
{
    // whatever you need to do
}
noontz
  • 1,782
  • 20
  • 29