如何连接在引用的解决方案上触发的事件

时间:2017-08-04 15:03:37

标签: c# events delegates eventhandler

ConnectionManager如何获得在AndroidPush类中捕获的expiredRegistrationId?

我这样做是错误的吗?

有关如何改进解决方案的任何建议?

我可以遵循任何模式吗?

解决方案:经理

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

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

    }
}

解决方案: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? 
            };

        } 



    }

1 个答案:

答案 0 :(得分: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
}