The Rain and The Shade

August 5, 2011

Injecting Timer into Services

Filed under: Dependency Injection,Modeling — ovaisakhter @ 5:03 am

In the recent days while developing a project I had two services in my ApplicationController which were utilizing a Timer, i.e. they were instantiated with the Application and a timer was started when the services were instantiated and the services performed a certain action on the elapsed event.

I am using Dependency Injection in this project using Unity Container by Microsoft. I use Constructor Injection and define all dependencies in a BootStrapper class hosted present in my Application.

In my refactoring thoughts I came to a conclusion that the timer should be injected from the outside to the services as a Dependency. So I started modeling a Timer. Names like ITmer or ITimerService or even IKeepTime (recently I have been inspired by Udi Dahan’s way of naming interfaces like IDoThis and IHandleThat, fascinating stuff but more on that later). So ITimer was the winner in the end.

I started contemplating well it is merely a wrapper around the normal Timer class so it should just provide the features timer class provides. Then I thought but why should my service have the capability or even knowledge of the interval in which the Timer will elapse it only needs to know when to start the timer when to stop it and when it had elapsed. The Stop feature is only relevant so that the utilizing service can ask the timer to hold on while it is performing a action if it wants to Do one thing at a time (which is generally a good thing we should tell our Classes to do this also) Some may argue that why does Timer needs to know this it should just keep on banging on the door and our service should ignore any such notifications, I agree that this can be a way of thinking but read on I got it cleared for me later. Here is the final interface I came up with,

public  interface ITimer
      void Start();
      void Stop();
      event EventHandler Elapsed;

Now I am all happy made the implementation of the service all set for tomorrow when I will refactor my services to use my newly created model and called it a day. But then I started thinking about it again when I came home (Hope my wife doesn’t read my blog, but this blog is not about a new Chicken Biriayni  recipe so we are all set). I thought that for the services utilizing the Timer do not care that there is a timer running in the Timer service, for them this is a service that notifies them when the they should execute an action, and timer is just a concrete implementation of such a service they can even be a code listening to some event or even a UI sending a click of a button in which case being able to tell the service generating the notifications that the host Service is not entertaining any notifications at the moment may make sense. (may be disable a button during execution). Now let us call our newly thought of service INotifyToExecuteAction and let us change the names of the members to go with the new theme.

public  interface INotifyToExecuteAction
    void StartNotifications();
    void StopNotifications();
    event EventHandler Execute;

(by the way did I tell you that the renaming feature in Resharper is freaking awesome Ctrl R R on the method names of the interface rename them and it will change the method names in all the implementations also. How kool is that?)

so here we are then


Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

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

You are commenting using your 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

Create a free website or blog at

%d bloggers like this: