Guest User

Untitled

a guest
Feb 20th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.97 KB | None | 0 0
  1. public abstract class SubscriptionPersistenceService : RuntimeService
  2. {
  3. protected override void OnStart()
  4. {
  5.  
  6. foreach (Endpoint e in LoadEndpoints())
  7. {
  8. ListenerEndpoint le = e as ListenerEndpoint;
  9. SubscriptionEndpoint se = e as SubscriptionEndpoint;
  10.  
  11. if (le != null)
  12. {
  13. _managedEndpoints.Add(le);
  14. }
  15. else if (se != null)
  16. {
  17. _managedEndpoints.Add(se);
  18. }
  19. else
  20. {
  21. throw new InvalidOperationException("Invalid endpoint type encountered");
  22. }
  23. }
  24. }
  25.  
  26. List<Endpoint> _managedEndpoints = new List<Endpoint>();
  27.  
  28. protected override void OnStop()
  29. {
  30. // Clear out all the stuff we loaded
  31. foreach (Endpoint e in _managedEndpoints)
  32. {
  33. ListenerEndpoint le = e as ListenerEndpoint;
  34. SubscriptionEndpoint se = e as SubscriptionEndpoint;
  35.  
  36. if (le != null)
  37. {
  38. Runtime.RemoveListener(le);
  39. }
  40. else if (se != null)
  41. {
  42. Runtime.Unsubscribe(se);
  43. }
  44. else
  45. {
  46. throw new InvalidOperationException("Unexpected endpoint type encountered");
  47. }
  48. }
  49. }
  50.  
  51. protected internal override void OnListenerAdded(ListenerEndpoint endpoint)
  52. {
  53. base.OnListenerAdded(endpoint);
  54.  
  55. CreateListener(endpoint);
  56. }
  57.  
  58. protected internal override void OnListenerRemoved(ListenerEndpoint endpoint)
  59. {
  60. base.OnListenerRemoved(endpoint);
  61.  
  62. DeleteListener(endpoint);
  63. }
  64.  
  65. protected internal override void OnSubscriptionAdded(SubscriptionEndpoint endpoint)
  66. {
  67. base.OnSubscriptionAdded(endpoint);
  68.  
  69. CreateSubscription(endpoint);
  70. }
  71.  
  72. protected internal override void OnSubscriptionRemoved(SubscriptionEndpoint endpoint)
  73. {
  74. base.OnSubscriptionRemoved(endpoint);
  75.  
  76. DeleteSubscription(endpoint);
  77. }
  78.  
  79. /// <summary>
  80. /// Load saved end points from the persistence store.
  81. /// </summary>
  82. /// <returns></returns>
  83. protected abstract IEnumerable<Endpoint> LoadEndpoints();
  84.  
  85. /// <summary>
  86. /// Create a subscription in the persistence store.
  87. /// </summary>
  88. /// <param name="subscription"></param>
  89. protected abstract void CreateSubscription(SubscriptionEndpoint subscription);
  90.  
  91. /// <summary>
  92. /// Delete a subscription from the persistence store.
  93. /// </summary>
  94. /// <param name="subscription"></param>
  95. protected abstract void DeleteSubscription(SubscriptionEndpoint subscription);
  96.  
  97. /// <summary>
  98. /// Create a listener in the persistence store
  99. /// </summary>
  100. /// <param name="endpoint"></param>
  101. protected abstract void CreateListener(ListenerEndpoint endpoint);
  102.  
  103. /// <summary>
  104. /// Delete a listener from the persistence store
  105. /// </summary>
  106. /// <param name="endpoint"></param>
  107. protected abstract void DeleteListener(ListenerEndpoint endpoint);
  108.  
  109. }
  110.  
  111. public class SqlSubscriptionPersistenceService : SubscriptionPersistenceService
  112. {
  113. public SqlSubscriptionPersistenceService(ISubscriptionDB db)
  114. {
  115. _db = db;
  116. }
  117.  
  118. ISubscriptionDB _db;
  119.  
  120. protected override IEnumerable<Endpoint> LoadEndpoints()
  121. {
  122. List<Endpoint> endpoints = new List<Endpoint>();
  123. endpoints.AddRange(_db.LoadListenerEndpoints().OfType<Endpoint>());
  124. endpoints.AddRange(_db.LoadSubscriptionEndpoints().OfType<Endpoint>());
  125. return endpoints;
  126. }
  127.  
  128.  
  129. protected override void CreateListener(ListenerEndpoint endpoint)
  130. {
  131. if (!endpoint.Transient)
  132. {
  133. _db.CreateListener(endpoint);
  134. }
  135. }
  136.  
  137. protected override void CreateSubscription(SubscriptionEndpoint subscription)
  138. {
  139. if (!subscription.Transient)
  140. {
  141. _db.CreateSubscription(subscription);
  142. }
  143. }
  144.  
  145. protected override void DeleteListener(ListenerEndpoint endpoint)
  146. {
  147. if (!endpoint.Transient)
  148. {
  149. _db.DeleteListener(endpoint.Id);
  150. }
  151. }
  152.  
  153. protected override void DeleteSubscription(SubscriptionEndpoint subscription)
  154. {
  155. if (!subscription.Transient)
  156. {
  157. _db.DeleteSubscription(subscription.Id);
  158. }
  159. }
  160. }
Add Comment
Please, Sign In to add comment