private void InvokeMethodOnRefresherInstance <T>(ICacheRefresher refresher, MessageType dispatchType, Func <T, object> getId, IEnumerable <T> instances) { if (refresher == null) { throw new ArgumentNullException("refresher"); } LogHelper.Debug <DefaultServerMessenger>("Invoking refresher {0} on single server instance, message type {1}", () => refresher.GetType(), () => dispatchType); var stronglyTypedRefresher = refresher as ICacheRefresher <T>; foreach (var instance in instances) { //if we are not, then just invoke the call on the cache refresher switch (dispatchType) { case MessageType.RefreshAll: refresher.RefreshAll(); break; case MessageType.RefreshById: if (stronglyTypedRefresher != null) { stronglyTypedRefresher.Refresh(instance); } else { var id = getId(instance); if (id is int) { refresher.Refresh((int)id); } else if (id is Guid) { refresher.Refresh((Guid)id); } else { throw new InvalidOperationException("The id must be either an int or a Guid"); } } break; case MessageType.RemoveById: if (stronglyTypedRefresher != null) { stronglyTypedRefresher.Remove(instance); } else { var id = getId(instance); refresher.Refresh((int)id); } break; } } }
/// <summary> /// Executes the strongly typed <see cref="ICacheRefresher{T}"/> on the local/current server /// </summary> /// <typeparam name="T"></typeparam> /// <param name="refresher"></param> /// <param name="messageType"></param> /// <param name="getId"></param> /// <param name="instances"></param> /// <remarks> /// Since this is only for strongly typed <see cref="ICacheRefresher{T}"/> it will throw for message types that are not by instance /// </remarks> protected void DeliverLocal <T>(ICacheRefresher refresher, MessageType messageType, Func <T, object> getId, IEnumerable <T> instances) { if (refresher == null) { throw new ArgumentNullException("refresher"); } LogHelper.Debug <ServerMessengerBase>("Invoking refresher {0} on local server for message type {1}", refresher.GetType, () => messageType); var typedRefresher = refresher as ICacheRefresher <T>; switch (messageType) { case MessageType.RefreshAll: refresher.RefreshAll(); break; case MessageType.RefreshByInstance: if (typedRefresher == null) { throw new InvalidOperationException("The refresher must be a typed refresher."); } foreach (var instance in instances) { typedRefresher.Refresh(instance); } break; case MessageType.RemoveByInstance: if (typedRefresher == null) { throw new InvalidOperationException("The cache refresher " + refresher.GetType() + " is not a typed refresher."); } foreach (var instance in instances) { typedRefresher.Remove(instance); } break; default: //case MessageType.RefreshById: //case MessageType.RemoveById: //case MessageType.RefreshByJson: throw new NotSupportedException("Invalid message type " + messageType); } }
//public void PerformNotify(ICacheRefresher refresher, object payload) //{ // if (servers == null) throw new ArgumentNullException("servers"); // if (refresher == null) throw new ArgumentNullException("refresher"); // Deliver(refresher, payload); //} #endregion #region Deliver protected void DeliverLocal <TPayload>(ICacheRefresher refresher, TPayload[] payload) { if (refresher == null) { throw new ArgumentNullException(nameof(refresher)); } Current.Logger.Debug <ServerMessengerBase, Type>("Invoking refresher {RefresherType} on local server for message type RefreshByPayload", refresher.GetType()); var payloadRefresher = refresher as IPayloadCacheRefresher <TPayload>; if (payloadRefresher == null) { throw new InvalidOperationException("The cache refresher " + refresher.GetType() + " is not of type " + typeof(IPayloadCacheRefresher <TPayload>)); } payloadRefresher.Refresh(payload); }
//public void PerformNotify(IEnumerable<IServerAddress> servers, ICacheRefresher refresher, object payload) //{ // if (servers == null) throw new ArgumentNullException("servers"); // if (refresher == null) throw new ArgumentNullException("refresher"); // Deliver(servers, refresher, payload); //} #endregion #region Deliver protected void DeliverLocal(ICacheRefresher refresher, object payload) { if (refresher == null) { throw new ArgumentNullException("refresher"); } LogHelper.Debug <ServerMessengerBase>("Invoking refresher {0} on local server for message type RefreshByPayload", refresher.GetType); var payloadRefresher = refresher as IPayloadCacheRefresher; if (payloadRefresher == null) { throw new InvalidOperationException("The cache refresher " + refresher.GetType() + " is not of type " + typeof(IPayloadCacheRefresher)); } payloadRefresher.Refresh(payload); }
/// <summary> /// Executes the non strongly typed <see cref="ICacheRefresher"/> on the local/current server /// </summary> /// <param name="refresher"></param> /// <param name="messageType"></param> /// <param name="ids"></param> /// <param name="json"></param> /// <remarks> /// Since this is only for non strongly typed <see cref="ICacheRefresher"/> it will throw for message types that by instance /// </remarks> protected void DeliverLocal(ICacheRefresher refresher, MessageType messageType, IEnumerable <object> ids = null, string json = null) { if (refresher == null) { throw new ArgumentNullException(nameof(refresher)); } Current.Logger.Debug <ServerMessengerBase, Type, MessageType>("Invoking refresher {RefresherType} on local server for message type {MessageType}", refresher.GetType(), messageType); switch (messageType) { case MessageType.RefreshAll: refresher.RefreshAll(); break; case MessageType.RefreshById: if (ids != null) { foreach (var id in ids) { if (id is int) { refresher.Refresh((int)id); } else if (id is Guid) { refresher.Refresh((Guid)id); } else { throw new InvalidOperationException("The id must be either an int or a Guid."); } } } break; case MessageType.RefreshByJson: var jsonRefresher = refresher as IJsonCacheRefresher; if (jsonRefresher == null) { throw new InvalidOperationException("The cache refresher " + refresher.GetType() + " is not of type " + typeof(IJsonCacheRefresher)); } jsonRefresher.Refresh(json); break; case MessageType.RemoveById: if (ids != null) { foreach (var id in ids) { if (id is int) { refresher.Remove((int)id); } else { throw new InvalidOperationException("The id must be an int."); } } } break; default: //case MessageType.RefreshByInstance: //case MessageType.RemoveByInstance: throw new NotSupportedException("Invalid message type " + messageType); } }
void ProcessRefreshJson(Notification notification, ICacheRefresher refresher) { //Tricky As IJsonCacheRefresher Is Marked As Internal var refresh = refresher.GetType().GetMethod("Refresh", BindingFlags.Instance | BindingFlags.Public, null, new[] { typeof(string) }, null); if (refresh == null) throw new NotSupportedException("Cache Refresher Does Not Implement IJsonCacheRefresher"); refresh.Invoke(refresher, new object[] { notification.Payload }); }
protected void InvokeMethodOnRefresherInstance(ICacheRefresher refresher, MessageType dispatchType, IEnumerable <object> ids = null, string jsonPayload = null) { if (refresher == null) { throw new ArgumentNullException("refresher"); } LogHelper.Debug <DefaultServerMessenger>("Invoking refresher {0} on single server instance, message type {1}", () => refresher.GetType(), () => dispatchType); //if it is a refresh all we'll do it here since ids will be null or empty if (dispatchType == MessageType.RefreshAll) { refresher.RefreshAll(); } else { if (ids != null) { foreach (var id in ids) { //if we are not, then just invoke the call on the cache refresher switch (dispatchType) { case MessageType.RefreshById: if (id is int) { refresher.Refresh((int)id); } else if (id is Guid) { refresher.Refresh((Guid)id); } else { throw new InvalidOperationException("The id must be either an int or a Guid"); } break; case MessageType.RemoveById: refresher.Remove((int)id); break; } } } else { //we can only proceed if the cache refresher is IJsonCacheRefresher! var jsonRefresher = refresher as IJsonCacheRefresher; if (jsonRefresher == null) { throw new InvalidOperationException("The cache refresher " + refresher.GetType() + " is not of type " + typeof(IJsonCacheRefresher)); } //if we are not, then just invoke the call on the cache refresher jsonRefresher.Refresh(jsonPayload); } } }