Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Design pattern for windows service - c#

Tags:

c#

I am getting my feet wet with programming and my self learning brings me to this question. I have been reading about design patterns and wanted to know what a good recommendation for this would be.

My test application sends a message to MSMQ. I want to write a windows service which will listen to this MSMQ and for every message it receives, I want it to perform a simple database insert.

I looked at this articlelink text

which gave me some information i needed. In the example I could do most of my work within

 private static void MyReceiveCompleted(Object source, 
        ReceiveCompletedEventArgs asyncResult)
    {
        // Connect to the queue.
        MessageQueue mq = (MessageQueue)source;

        // End the asynchronous Receive operation.
        Message m = mq.EndReceive(asyncResult.AsyncResult);

        // Display message information on the screen.
        Console.WriteLine("Message: " + (string)m.Body);

        // Restart the asynchronous Receive operation.
        mq.BeginReceive();

        return; 
    }

But is there a specific pattern that is more viable candidate? From reading around I assume it could be the 'command' pattern but I am not sure if this is the best pattern. Any ideas or thoughts to help me get a better understanding of the patterns.

Books I read on patterns: Head First Design patterns - i know mixed reviews on this ordered a used copy of design patterns by gang of four(sp?)


1 Answers

This is not directly related to your question, but a key design pattern to developing services of any kind (web services, windows services, etc) is to seperate your functionality from the service host.

That is, have a class that actually performs the work. And have a service that calls the class.

This way you can re-host the service easily in multiple hosts, and have an easy way to test the functionality via console apps, winforms apps, or unit tests.

like image 123
Jason Coyne Avatar answered Dec 12 '25 17:12

Jason Coyne



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!