For a system where a user can be a member or admin, users with the member role must either pay for it with a recurring subscription, or be given a complimentary access.
My current approach:
- Users have a
userdatabase table. - A
subscriptiontable includes a record for a user if they have a subscription. - A
subscription_eventtable records each billing or failed payment. I can query this to see if the last event was indeed a successful payment.
But how should I record if a user is given “complimentary” access?
- Have another table
complimentary_subscriptionwith the user ID as the foreign key? - Record a special “subscription” for them in
subscription? - Or add another column to their user row for columns like
is_complimentaryandcomplimentary_expires_date? - Add a more general
expirescolumn to the user row?
Question Review
As @leanne said, you’re modeling a
Subscriptionwhose specializations are, say,MonthlySubscriptionandComplimentarySubscription(to give them a name for this answer).You know that a subscription can expire:
MonthlySubscription, that happens when a user didn’t pay the current month’s subscriptionComplimentarySubscription, the expiration date is assigned when it’s assigned to the userAs you can see an
ExpirationDateis an essential attribute of anySubscription, but the way you store it is different in each case. If the first case you’ll have to calculate it based on the last event, in the latter you can retrieve it directly.Dealing with Inheritance in the Data Base
So, to map this sample model to a database schema, you could go with the Class Table Inheritance pattern described in Martin Fowler’s Patterns of Enterprise Application Architecture book. Here is its intent:
Basically you’ll have a table with the attributes shared in common between the classes, and you’ll be storing attributes specific to each class in a separate table.
Keeping this in mind, let’s review the options you proposed:
Having a separate table for storing
ComplimentarySubscriptionspecific details sound good, but if you don’t relate this one with thesubscriptiontable, you can end up with an user that has both aMonthlySubscriptionand aComplimentarySubscription. Its foreign key should point to thesubscriptiontable, which is the one that tells you if a user has a subscription or not (and you’ll have to enforce up to one subscription per user).Yes, you’ll have to record that a user has a subscription either monthly or complimentary. But if you’re thinking something like recording a special subscription whose amount is zero, you’re looking for a solution that matches your current design instead of searching for the right model for it (it might and it might be not).
Personally I don’t like this one because you’re putting information where it doesn’t belong. Taking that into account, where you will be storing the expiration date for complimentary subscriptions (remember that for monthly ones you are calculating it, and not storing the expiration date)? It seems that all that information is crying for its own “home”.
Also, if later you need to add a new type of subscription that table will begin to clutter.
If you do this, you’ll have to deal with data synchronization each time the
subscription_eventgets changed (in the case of a monthly subscription). Generally I try to avoid this data-duplication situation.Sample Solution
What I would do to favor extensibility when adding new types of subscription is to have the
subscriptiontable to store shared details betweenMonthlySubscriptonandComplimentarySubscription, adding atypecolumn key that’ll let you differentiate which kind of subscription a row is related to.Then, store details specific to each subscription type in its own table, referencing the parent
subscriptionrow.For retrieving data, you’ll need an object in charge of instantiating the right type of
Subscriptiongiven thetypecolumn value for asubscriptionrow.You can take a look at the pattern in the “Patterns of Enterprise Application Architecture” book for further assistance on how to define the
typecolumn values, how to use a mapper object to do theSubscriptioninstantiation and so on.01/03/2012 Update: Alternatives for defining and handling the
typecolumnHere’s an update to clarify the following question posted by @enoinoc in the comments:
It’s ok to have that information in the
Planstable, as long it´s not static information that doesn´t need to be edited. If that’s the case, don’t over-generalize your solution and put that knowledge in the correspondingSubscriptionsubclass.About the foreign key approach, I can think of some drawbacks going that way:
Subscriptionto use for each row in thePlanstable. If all you got is a foreign key value (say, an integer) you’ll have to write code to map that value with the class to use. That means extra work for you 🙂Proposed Solution
What I’d do is to put into the
typecolumn in thePlanstable. That column will hold the name of the class that knows how to build the rightSubscriptionfrom a particular row.But: why do we need an object to build each type of
Subscription? Because you’ll be using information from different tables (subscription_eventandcomplimentary_subscription) for building each type of object, and it´s always good to isolate and encapsulate that behavior.Let’s see how the
Planstable might look like:— Plans Table —
Id | Name | Type | Other Columns…
1 | Monthly |
MonthlySubscriptionMapper|2 | Complimentary |
ComplimentarySubscriptionMapper|Each
SubscriptionMappercan define a methodSubscription MapFrom(Row aRow)that takes a row from the database and gives you the right instance of theSubscriptionsubclass (MonthlySubscriptionorComplimentarySubscriptionin the example).Finally to get an instance of the mapper specified in the
typecolumn (without using a nastyiforcasestatements) you can take the class name from the column’s value and, by using reflection, create an instance of that class.