The cornerstone to this approach is an abstract class that
all derived, implementation-specific providers will inherit. Although not
every method in the class is abstract, the methods that are the most important
for the operation are abstract and will be overridden in the derived class. If
you look through the abstract methods, they deal with the adding, deleting,
updating, and retrieving, and checking the existence of newsletters and
subscribers.
Listing 1
public abstract class NewsletterProvider : Nucleo.Web.Providers.ProviderBase
{
public abstract void AddNewsletter(string newsletterName,
string description);
public abstract void AddSubscription(string subscriberEmail,
string newsletterName);
public abstract string[] FindSubscriber(string emailToMatch,
string newsletterName);
public abstract string[] GetAllNewsletters();
public abstract string[] GetNewslettersForSubscriber(
string subscriberEmail);
public abstract string[] GetSubscribers(string newsletterName);
public abstract bool NewsletterExists(string newsletterName);
public abstract void RemoveNewsletter(string newsletterName);
public abstract void RemoveAllSubscriptions(string subscriberEmail);
public abstract void RemoveSubscription(string subscriberEmail,
string newsletterName);
public abstract bool SubscriberExists(string subscriberEmail);
public abstract bool SubscriptionExists(string subscriberEmail,
string newsletterName);
protected virtual void ValidateNewsletterName(string newsletterName)
{
if (string.IsNullOrEmpty(newsletterName))
throw new ArgumentNullException("newsletterName",
"The newsletter name must be provided.");
}
protected virtual void ValidateSubscriberEmail(string subscriberEmail)
{
if (string.IsNullOrEmpty(subscriberEmail))
throw new ArgumentNullException("subscriberEmail",
"The subscriber email must be provided.");
}
}
The validation is stored in the abstract class and used
directly, because it will be handled the same way in all derivatives. If for
some reason it isn't, these methods declare virtual in the definition, so they
can be overridden.
By inheriting from this abstract class, any derived class
can be used directly through this class. This is the benefit of inheritance:
all derived classes coming from the same base class can be referenced as the
base class. So in this instance, any derivatives of the NewsletterProvider
class can be referenced as NewsletterProvider, which is an important part of
object-oriented programming as you will see soon.