jump to navigation

The Logical Layer (mlists) November 6, 2006

Posted by _netoLopez in .net, c#, database, mlists, mssql, programming, projects, web2.0.
trackback

…or business logic tier, or middle tier/layer…, what I mean to say is that the code on the MailingList class that I’ll present is not tied to any GUI/display functions, meaning it may be used in a web environment, or a desktop application just fine.

Namespaces
Let’s start by viewing and explaining the required .net namespaces:

MailingList namespaces

The System namespace is always used.
The System.Data will supply us with generic data objects like DataTable and DataRow.
System.Configuration we use because we rely on a configuration file (same for web or desktop) to store certain values like connectionStrings.
System.Data.SqlClient is used to connect to an SqlServer2oo5 database instance (or file in our case because of express edition).
And last but not least, the System.Net.Mail namespace we use to send email messages thru SMTP (this is new in .net 2.0 as previously we had to use System.Web.Mail).

Properties
This is how I implemented the properties, all of them are read-only…

MailingList properties

Constructors
I have specified a public constructor that accepts a code parameter to initialize the object properties based on it, and hidden the default constructor from public use; making it private thou, I can still use it on my static methods shown later.

MailingList Constructors

Basically, the public constructor will use the supplied code to get the rest of the property values (name and description). Recall the table definitions on previous post. Notice the use of the helper database method GetDataTable().

Static methods (no instantiation needed)
I implemented two static methods that do not require MailingList to be instantiated to be used.  These are Create() and GetActiveLists().

Create() is used to “add” a new list to the application.  GetActiveLists() will return an array of list codes.

MailingList static methods

Create() method: there are some validations pending, but left them as TODOs to keep the code sample short.  I did do validation to check if the code already exists, and threw an ApplicationException if it in fact exists… Else if the provided code is valid, a new list is created!

GetActiveLists() method: This will query the database and get the list codes that have not been expired.

Object Methods

MailingList expire

When called, Expire() will remove the list from active lists, setting a datetime value on the [expires] field.

MailingList subscribe

The Subscribe() method, will add an email address to the currently instantiated mailingList. Notice it is first activating the email address by “unexpiring” it (putting null on [expires]);  and if this doesn’t work (variable i will be zero), it’ll insert a new row.  I did this because on the mlists_entries table, I placed a uniqueness constraint on the [list_id]+[inbox] columns.

MailingList unsubscribe

This one will do the opposite, it’ll remove an email address from the active list of addresses in the current list.

MailingList update

the Update() method is our way to edit the name and description properties of our mailingList instance.

MailingList getEntries

GetEntries() will retrieve an array of active email addresses from the current mailingList object.

MailingList publish

And the Publish() method, will send an email message to each of the emails retrieved by the GetEntries() method.  This method got a little messy… First, I’m getting some values from the web.config file, being senderInboxDomain and smtpServer.  The senderInbox variable, used to set the From property of the MailMessage instance, is composed of the code ([list_id]) of the mailingList plus the senderInboxDomain.  Second, I am appending stuff to the htmlContent (used as the main body of the MailMessage instance), I recommend appending the following two things to a mailingList message: a disclaimer & a way for the recipient to remove themselves from the list.  Third, the method is relying on the GetEntries() method to get the list of email addresses, and then looping thru them sending the message to each one 😀 .

Wrapping up the MailingList class
This is all the code I placed on the MailingList class. It covers all the requirements listed in the first post of this series (mlists).  My next entry will touch some activities I did when implementing the user interface portion of the project.

Advertisements

Comments»

No comments yet — be the first.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: