Created
October 9, 2008 19:30
-
-
Save jezell/15855 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public abstract class SubscriptionPersistenceService : RuntimeService | |
{ | |
protected override void OnStart() | |
{ | |
foreach (Endpoint e in LoadEndpoints()) | |
{ | |
ListenerEndpoint le = e as ListenerEndpoint; | |
SubscriptionEndpoint se = e as SubscriptionEndpoint; | |
if (le != null) | |
{ | |
_managedEndpoints.Add(le); | |
} | |
else if (se != null) | |
{ | |
_managedEndpoints.Add(se); | |
} | |
else | |
{ | |
throw new InvalidOperationException("Invalid endpoint type encountered"); | |
} | |
} | |
} | |
List<Endpoint> _managedEndpoints = new List<Endpoint>(); | |
protected override void OnStop() | |
{ | |
// Clear out all the stuff we loaded | |
foreach (Endpoint e in _managedEndpoints) | |
{ | |
ListenerEndpoint le = e as ListenerEndpoint; | |
SubscriptionEndpoint se = e as SubscriptionEndpoint; | |
if (le != null) | |
{ | |
Runtime.RemoveListener(le); | |
} | |
else if (se != null) | |
{ | |
Runtime.Unsubscribe(se); | |
} | |
else | |
{ | |
throw new InvalidOperationException("Unexpected endpoint type encountered"); | |
} | |
} | |
} | |
protected internal override void OnListenerAdded(ListenerEndpoint endpoint) | |
{ | |
base.OnListenerAdded(endpoint); | |
CreateListener(endpoint); | |
} | |
protected internal override void OnListenerRemoved(ListenerEndpoint endpoint) | |
{ | |
base.OnListenerRemoved(endpoint); | |
DeleteListener(endpoint); | |
} | |
protected internal override void OnSubscriptionAdded(SubscriptionEndpoint endpoint) | |
{ | |
base.OnSubscriptionAdded(endpoint); | |
CreateSubscription(endpoint); | |
} | |
protected internal override void OnSubscriptionRemoved(SubscriptionEndpoint endpoint) | |
{ | |
base.OnSubscriptionRemoved(endpoint); | |
DeleteSubscription(endpoint); | |
} | |
/// <summary> | |
/// Load saved end points from the persistence store. | |
/// </summary> | |
/// <returns></returns> | |
protected abstract IEnumerable<Endpoint> LoadEndpoints(); | |
/// <summary> | |
/// Create a subscription in the persistence store. | |
/// </summary> | |
/// <param name="subscription"></param> | |
protected abstract void CreateSubscription(SubscriptionEndpoint subscription); | |
/// <summary> | |
/// Delete a subscription from the persistence store. | |
/// </summary> | |
/// <param name="subscription"></param> | |
protected abstract void DeleteSubscription(SubscriptionEndpoint subscription); | |
/// <summary> | |
/// Create a listener in the persistence store | |
/// </summary> | |
/// <param name="endpoint"></param> | |
protected abstract void CreateListener(ListenerEndpoint endpoint); | |
/// <summary> | |
/// Delete a listener from the persistence store | |
/// </summary> | |
/// <param name="endpoint"></param> | |
protected abstract void DeleteListener(ListenerEndpoint endpoint); | |
} | |
public class SqlSubscriptionPersistenceService : SubscriptionPersistenceService | |
{ | |
public SqlSubscriptionPersistenceService(ISubscriptionDB db) | |
{ | |
_db = db; | |
} | |
ISubscriptionDB _db; | |
protected override IEnumerable<Endpoint> LoadEndpoints() | |
{ | |
List<Endpoint> endpoints = new List<Endpoint>(); | |
endpoints.AddRange(_db.LoadListenerEndpoints().OfType<Endpoint>()); | |
endpoints.AddRange(_db.LoadSubscriptionEndpoints().OfType<Endpoint>()); | |
return endpoints; | |
} | |
protected override void CreateListener(ListenerEndpoint endpoint) | |
{ | |
if (!endpoint.Transient) | |
{ | |
_db.CreateListener(endpoint); | |
} | |
} | |
protected override void CreateSubscription(SubscriptionEndpoint subscription) | |
{ | |
if (!subscription.Transient) | |
{ | |
_db.CreateSubscription(subscription); | |
} | |
} | |
protected override void DeleteListener(ListenerEndpoint endpoint) | |
{ | |
if (!endpoint.Transient) | |
{ | |
_db.DeleteListener(endpoint.Id); | |
} | |
} | |
protected override void DeleteSubscription(SubscriptionEndpoint subscription) | |
{ | |
if (!subscription.Transient) | |
{ | |
_db.DeleteSubscription(subscription.Id); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment