Object mentor podcasts

Sunday, April 26th, 2009

http://www.objectmentor.com/videos/video_index.html

Concatenating strings with SQL Server

Wednesday, April 22nd, 2009

This is a bit of an odd request but I had to pre-pend a string to a result (a returned ID) so I looked at doing this in the query. It is dead easy - all you have to do is make sure that the return type is correct so you may have to convert:

select ‘40′ + CONVERT(varchar(12), intValueID) as whatIwantTheOutputToBeCalled ..blah rest of query WHERE blah

http://msdn.microsoft.com/en-us/library/ms187928.aspx

Sending email from you local machine using an O2 connection

Tuesday, April 21st, 2009

I have just swapped my Broadband provider from Pipex (who are RUBBISH) to O2 - so far so good. All email from my local dev sites stopped to any addresses in the outside world.

It was not any firewall issues and I am sure that lots of other people will have this problem so here is the solution: use their email server and the details are as follows:

host: relay.o2broadband.co.uk

port: 25

An example from a .NET app with the settings in the web.config:

<system.net>
<mailSettings>
<smtp>
<network host=”relay.o2broadband.co.uk” port=”25″ />
</smtp>
</mailSettings>
</system.net>

I hope that saves someone 30 seconds.

lucene.net and using luke

Thursday, April 16th, 2009

I am currently tweaking one of my sites that have a lucene search. After upgrading to a newer version I now have a static method on QueryParser to escape all of those reserved characters: QueryParser.Escape(”query string”) - great I can get rid of my naff regular expression that look ugly to do this! (http://www.javalobby.org/java/forums/t86124.html)

Also just so you do not forget - when using Luke and trying to build queries do not forget to change the search analyser - when searching for terms that have numbers in them. The default ‘SimpleAnalyzer’ does not work, change it to the ‘StandardAnalyzer’ - yippy now it works (a give away was looking in the parsed window - no numbers!)

Now on with adding a spell checker!

Useful list of algorithms

Wednesday, April 15th, 2009

There are many times when you need an algorithm to sort a problem - think of an easy problem such as shuffling a list. A good place to start looking is here:

http://en.wikipedia.org/wiki/List_of_algorithms

MSMQ - it seems too easy to get messaging working so here is an example

Thursday, April 9th, 2009

There are many cases where you might need to use a queue. For example within a web application you may want to send 1000 emails for example (not spamming but I am sure you can think of an instance). Anyway - you probably do not want to do this within the request timespan - well you don’t - that would be stupid - so a solution would be to whack each message on a queue so that they can be sent at any time a consumer can accept them.

Ok so lets have a dead simple example. Firstly you will need Message Queuing to be installed. If it is not already there (on your XP install) get there original install disk out and add additional components. I assume this is easy to do for the reader.

Ok so now all we have to do is write a bit of code to stick things on a queue. First of all add a reference to ‘System.Messaging’.

Ok so lets see some code:

MessageQueue mq;
if (MessageQueue.Exists(@”.\Private$\MailingListQueue))
{
mq = new MessageQueue(@”.\Private$\MailingListQueue);
}
else
{
mq = MessageQueue.Create(@”.\Private$\MailingListQueue);
}

for (int i = 0; i < 100; i++)
{

var tm = new TestMessageObject();
tm.Id = i;
tm.Message = _messageBody;
var mm = new Message(tm, new BinaryMessageFormatter());

mq.Send(mm);
}

Ok So what is all that then? Firstly we create a variable to hole the message queue called ‘mq’ we then check if it already exists and either create it or load it up. Notice the path for the message, the ‘.\’ means that it is local, the ‘Private$’ states that the list is private. It can also be Public$ this would mean remote machines could access it - I won’t go into detail - just google.

Ok - so we create an object (this is just a sample object) and then we add it to the queue. In this case we just loop over a few times to add lots of messages to the queue. Notice that we give it a Formatter. This is so that it can be de-serialized when taken off the queue at the other end (the consumer). There are a few built in formatters but in my case this was just the ticket.

Ok so now we have some messages on a queue - what do we do with them? Firstly run the code and check in the mmc Computer management snap in that you have a queue with your given name in there and some messages.

Right now we can consume these messages. Create a console app or a service - whatever you fancy and simply grab the messages from the queue:

MessageQueue mq;
if (MessageQueue.Exists(_messageQuePath))
{
mq = new MessageQueue(_messageQuePath);

if (mq.Peek() == null)
{
Console.WriteLine(”No Messages Found”);
}

while (mq.Peek() != null)
{
var mes = mq.Receive(new TimeSpan(0, 0, 3));
mes.Formatter = new BinaryMessageFormatter();
var recievedMessage = (TestMessageObject) mes.Body;
Console.WriteLine(recievedMessage.Message);
}
}
else
{
Console.WriteLine(”No Message Que Found”);
}
Console.ReadLine();

What we see here is that we are using the Peek method to check if there is anyhting on the queue. Peek takes a message from the queue without actually taking it off the queue so we can check to see if there is anything there.

If you leave this console app running and add more to the queue the consumer will be notified and will start running again - how cool is that!

Just think of the fun you can have being able to de-couple important functions - just think of auditing for example you could simply keep sending audit messages to the queue and even if the consumer was down (a database for example) the messages would not get lost and the app would not just fall over. When the consumer came back up then the audit messages would simply be consumed.

I am looking forward to getting some of this in action!

MassTransit next….

Googles auto complete form

Wednesday, April 8th, 2009

http://www.google.com/support/toolbar/bin/answer.py?hl=en&answer=47972

testdriven.net

Sunday, April 5th, 2009

After looking into Sonar for Java and getting more and more into the test driven ways (which is great) and after reading ‘Clean Code‘ (a must read for any programmer) I had a look for something to do code coverage for C# and found NCover.

NCover is great but does not integrate into Visual Studio (well the community edition doesn’t) so I went looking for something better and found TestDriven.net that integrates NCover for you.

After installing and then uninstalling the original version of NCover I installed TestDriven.net but every time I tried to get some results from NCover it could not generate:

NCover couldn't create a coverage report

Whatever I tried it did not work - it did however work on my work computer and on my laptop. I contacted Jamie Cansdale who is the person behind TestDriven and to my amazement got back to me first with a different version to try and after that did not work used copilot to connect to my machine and fix the problem for me right in front of my eyes! Needless to say I was very impressed and most appreciative.

My hat goes off to a great product and amazing support! Thanks Jamie (http://weblogs.asp.net/nunitaddin/)!

http://www.testdriven.net/

co pilot - very handy when you need to help someone out!

Sunday, April 5th, 2009

https://www.copilot.com/