Category Archives: Object Oriented Design

Designing and implementing proper object oriented structure

Exchange Web Services Notifications

Something that I have always enjoyed working with is the exchange web services notifications engine.  This process not only allows you to jump in and fetch information from exchange through the exchange web services but it also gives you a nice subscriber model for responding to any exchange events.  This includes things like emails being received in a specified folder, or calendar entry changes, etc.  What I would like to show you is a quick overview of how the exchange notifications can be grabbed and utilized for whatever you so desire.  In my specific example I am grabbing email notifications and handling them with some business logic to assign them to cases in a help desk system.

Exchange Web Services Notifications – Fetching an event

Our first step is to create an exchange web service object that holds the URL to our service:

Dim excExchangeService As New ExchangeService(ExchangeVersion.Exchange2010)
excExchangeService = New ExchangeService(ExchangeVersion.Exchange2010)
excExchangeService.Url = New Uri(_mExchangeURL)

At this point we subscribe to the event that we awant to grab, and run a loop to hit it until we have events:

While True
  Do
    gerResult = plsSubscription.GetEvents()
    For Each ntfEvent As NotificationEvent In gerResult.AllEvents
      If TypeOf ntfEvent Is ItemEvent Then
        ..Handle notification event
      End If
    Next
  Loop While plsSubscription.MoreEventsAvailable
End While

Exchange Web Services Notifications – What do I do with an item once I have it?

Each event will be typed as either an item type specific to the folder or location that you setup your pull to pull from. In my specific example I setup a few timers to listen to email boxes and waited for the events to come in. When they come in I get an email item so I call the EmailMessage.Bind method to bind an Email object and read some information about the email.

A big push in .NET and software development in general is to move away from a service based process that reads every 5 minutes and sends updates and move into more of a reactionary event driven approach.  This is the best of both worlds, as you do not have to have an event driven model but at the same time your timer is not reading every certain time period and pulling in information.  By providing a real-time system that reacts to events we can tightly integrate our applications with the exchange processes.

Exchange Web Services Notifications provides not only a place for you to hook into great events from the exchange process, but an excellent object model for retrieving any information you need about exchange.

HOW TO: Use Events From An Inherited Class

When designing objects I will always think that not only will I be coding functionality in the object, but potentially someone else could be extending this functionality.  Almost every time I have written an object I have come back to it to expand it out to help usability.  One big subject that I have struggled a little with is how to expose events to descendant classes.  This becomes a point of focus when working in an Agile environment, because the requirements can shift and change and you want to be able to shift and change with them.  Designing a base class like a persistent Person object requires that you release as much control to the descendant while protecting any logic that is completely necessary.

Let’s take the following example of a person that notifies applications when today is their birthday:

VB.NET

Public Class Person

    Public BirthDate As Date
    Public Sub DateChanged(ByVal NewDate As Date)
        If NewDate = BirthDate Then
            RaiseEvent ItsMyBirthday()
        End If
    End Sub
    Public Event ItsMyBirthday()
End Class

C#

    public class Person
    {
        public DateTime BirthDate;
        public void DateChanged(DateTime NewDate)
        {
            if (NewDate == BirthDate)
                ItsMyBirthday(this,new EventArgs());
        }
        public event EventHandler ItsMyBirthday;
    }

You cannot implicitly call RaiseEvent for a base class event, so to work around this issue we create a method in the base that as always should be overridable.  Typically I will call this On<EventName>.  In this case I would call it OnBirthday and have the base method raise the event.  This change would look something like this:

VB.NET

    Public Overridable Sub OnBirthday()
        RaiseEvent ItsMyBirthday()
    End Sub

C#

    public void OnBirthday()
    {
        ItsMyBirthday(this, new EventArgs());
    }

Now you can call OnBirthday in your base class if you wanted to have two birthdays and inform your application of such.  Happy coding!

HOW TO: Use Await In Windows 8 Style Metro Coding

With the release of the new Visual Studio 11, and Microsoft 8, comes a new display and coding style targeting Metro. Metro is a display system that was modeled after a Swiss system of subway trains that allowed users to interact and purchase their tickets quickly and efficiently. A new very exciting keyword is being presented with the demos that are up called Await. What this keyword allows us to do is execute an asynchronous function or subroutine and move on with our code. This option is available very easily and does not require any timers or threading to handle any complex processes.
Here is a great example of how to use Await while reading a feed from a blog.

VB.NET

Private Async Function GetFeedAsync(FeedUriString As String) As Task
' Imports Windows.Web.Syndication
Dim Client As New SyndicationClient
Dim FeedUri As New Uri(FeedUriString)
Try
Dim Feed As SyndicationFeed = Await Client.RetrieveFeedAsync(FeedUri)
Dim FeedData As New FeedData
FeedData.Title = Feed.Title.Text

For Each Item As SyndicationItem In Feed.Items
Dim FeedItem As New FeedItem
FeedItem.Title = Item.Title.Text
FeedItem.PubDate = Item.PublishedDate.DateTime
FeedItem.Author = Item.Authors(0).Name.ToString()

If Feed.SourceFormat = SyndicationFormat.Atom10 Then
FeedItem.Content = Item.Content.Text
ElseIf Feed.SourceFormat = SyndicationFormat.Rss20 Then
FeedItem.Content = Item.Summary.Text
End If

FeedData.Items.Add(FeedItem)
Next

Me.DataContext = FeedData
ItemListView.SelectedIndex = 0
Catch Ex As Exception
' Log Error.
TitleText.Text = Ex.Message
End TryEnd Function

C#

private async Task GetFeedAsync(string feedUriString){
// using Windows.Web.Syndication;
SyndicationClient client = new SyndicationClient();
Uri feedUri = new Uri(feedUriString);

try {
SyndicationFeed feed = await client.RetrieveFeedAsync(feedUri);
FeedData feedData = new FeedData();
feedData.Title = feed.Title.Text;

foreach (SyndicationItem item in feed.Items)
{
FeedItem feedItem = new FeedItem();
feedItem.Title = item.Title.Text;
feedItem.PubDate = item.PublishedDate.DateTime;
feedItem.Author = item.Authors[0].Name.ToString();
if (feed.SourceFormat == SyndicationFormat.Atom10)
{
feedItem.Content = item.Content.Text;
}
else if (feed.SourceFormat == SyndicationFormat.Rss20)
{
feedItem.Content = item.Summary.Text;
}
feedData.Items.Add(feedItem);
}
this.DataContext = feedData;
ItemListView.SelectedIndex = 0;
}
catch (Exception ex)
{
// Log Error.
TitleText.Text = ex.Message;
}
}

Test Driven Development

Eureeka!

Fellow developers, I have had my TDD epiphany.  I have been a software developer for a very long time and never felt that I fully grasped the concepts behind Test Driven Development.  Today,  I came across a performance issue which involved a client complaining about an application taking 20 hours to process a certain number of records.  I was panicking and thinking of ways to reproduce the problem and how we can measure the performance so we can improve it, but what I did not have was a goal.  I started thinking, why is it a problem that it takes 20 hours to process these products?  Then it dawned on me.

What is the expectation of my code?

At this point in the development cycle, I had no tests that asked this very important question. I had only an expectation of myself of how well my code should perform.  I asked my boss during this performance discussion this very question, and we both kind of looked at each other without a clue.

This very question could have solved the development life cycle issue the entire time, and could potentially save this issue.  How many records should my application process?  What is the expectation of the client?  If we don’t know, we need to do research and find out.  This lead to a whole host of questions that will improve our application and rang the word through my ears, Eureeka!

By finding out what a client expectation, I can develop tests against my code to see if I am meeting that expectation, and if not I won’t release code to a client that does not meat their expectation.  This will result in the overall quality of my code to improve drastically.  To everyone who has ever tried to tell me about test driven development and had it fall on confused and deaf ears, I apologize.  Let’s get to work!