The Rain and The Shade

August 11, 2011

My name is Ovais and ICodeInCSharp , IDoPhotography,IWriteBlogs

Filed under: C# Coding,General Software Archiecture,Modeling — ovaisakhter @ 2:11 pm


I am a big fan of interfaces in a object oriented language like C#. I have been using dependency injection (IoC) in most of my previous projects and using DI your model contains many interfaces. I always try to spend some time in naming my model elements be it the name of interfaces, classes, methods or even the variables. When I look at code by other people one thing I usually notice is how they are naming their model elements.

Some days back I was looking into NServiceBus the things I noticed while doing that other than shear brilliance was the way different interfaces were named. for example IHandleMessages or IContainSagaData. This was a bit different way of naming then what I usually use as normally I would use something like IMessageHandler. I thought this is an interesting way of naming something like a class is saving my Name is MessageHandler and I Handle Messages. Sounds more natural more understandable.

I thought I should give this way of naming a try. In one of my projects, I tried to use similar naming.

The first place I need to create Interfaces are the repositories. So I started with names like IStoreData (instead of IRepository sounds boring doesn’t it?), IStoreDataInSql and went on and making names like IStoreStatistics.

Next place was the application controller where normally the interfaces for the services are named as IStatisticsService or IConfigurationService etc the names now became a bit different i.e. ILogStatistics, IManageSiteSurveryConfiguration, INotifyToExecuteAction INotifyWhenSiteSurveyConfigurationChanges.

Thought it was fun way of naming interfaces in some cases it become as stagnant as the old approach like IProvideThis and IHandleThat but in some cases the names become very interesting like INotifyWhenSiteSurveyConfigurationChanges and INotifyToExecuteAction.

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

Blog at