Archive for March, 2009

Linq Part 1 – What is Linq?

Tuesday, March 24th, 2009
This entry is part of a series, Linq»

Linq is an abbreviation for Language Integrated Query.  The idea of it is that you can write a query on a source of data inline as part of your code.  It is particularly clever because Linq allows different providers to be used so that different types of data can be queried.  There are Linq providers that are part of the .NET Framework for querying anything based on IEnumerable (including Lists, Collections, Arrays, etc.), XML, SQL Server databases (using Linq to SQL). 

There are also providers available for querying Oracle, MySQL, various webservices (one of the earliest ones I came across was Linq to Amazon).  It is a very powerful system with lots of flexibility.

Another point about Linq is that you can build queries on top of queries and depending on the provider, it only compiles and executes the final query when you try to access the data.  For example, you could do a query like

Dim people = From p in DB.People Where p.FirstName=”John”

people = From p in people Where p.Surname=”Smith”

Once you try to access people, (eg. by looping through it), Linq will execute an SQL query something like

SELECT [ID], [FirstName], [Surname] FROM [Person] WHERE [Person].[FirstName]=’John’ AND [Person].[Surname]=’Smith’

.NET 3.5 SP1 in Windows 2000

Monday, March 23rd, 2009

I’ve posted a couple of times earlier on .NET 3.5 in Windows 2000.  The .NET framework 3.5 sits on .NET 2.0 SP2, which is only available to install on Windows XP and onwards.  However, from experimentation, this does not mean that the same tricks don’t work.  As long as you have .NET 2.0 SP1 installed, you can get away with copying most of the DLLs in with no trouble.  It also seems to work with some of the newer ones.

Assembly Build and Revision Numbers

Monday, March 23rd, 2009

I hadn’t actually realised until last week that assembly build numbers are not automatically incremented on each build. I remember at some point in the past going looking for where Visual Studio was storing them, and being very confused as to how it managed to increment the build number without checking out any files in source control.

Last week I discovered that build numbers are actually based on the date – it is the number of days since a date specified in the registry (I think 1st Jan 2000 by default). For example, today’s build number with the default date is 3369. Since this behaviour can be a little annoying if you release multiple builds per day and your users refer to the build number, people have come up with various different solutions. I’ve found this one on CodeProject, but I haven’t actually tried it.

Revision numbers, on the other hand are related to the time of day.  I’ve heard various different reports that they are the number of seconds since midnight, or half that, or a random number.  Experimentally, they do seem to be related to the time, but I haven’t played around for long enough to work out the exact relationship.  I’m also not sure what time zone is used to work this out – it may be the computer clock or it might be UTC or something else.  Anyone who has bothered to actually do this, please feel free to post and let us know!

XAML, Microsoft Silverlight 2.0 and WPF

Sunday, March 22nd, 2009

Whenever new technology comes about, there is an automatic reluctance to spend time looking into it.  I think this is probably quite a sensible thing, since you can’t possibly keep up with everything going on at once.  When Silverlight came out, I had a brief look at it and thought “oh, it looks like Microsoft are trying to compete with Flash.”  I thought it wasn’t really worth worrying about at the time, as Adobe have pretty much got the market cornered as far as browser animation plugins go, and I didn’t think people would really want to bother installing another one.

More recently, I’ve had a bit more of a look at Silverlight, and I’m wondering whether to invest some time learning it.  It does seem to be getting a bit more popular.  One of the things that I’ve heard (I don’t know whether this is true), is that Silverlight will work off the same XAML documents as WPF, which means that you can create a single XAML file and use it as an interface for both web and Windows.  

Granted, I don’t actually use WPF yet either – from what I’ve heard of that, it still needs some work before it is a full replacement for Windows Forms, and I have heard that it can cause speed issues.  On the other hand, the idea of being able to create a single user interface which can be used for both web and windows is very appealing.

Let me step back for a minute and explain.  XAML (Extensible Application Markup Language) is Microsoft’s XML based language which is used for defining user interface.  I think they invented it for WPF (Windows Presentation Foundation), which is supposed to eventually replace Windows Forms.  The idea is that you can define a user interface in an XML document (much as you do in HTML), and then it gets rendered.

You may ask “why not just use HTML?”  The answer is that while HTML has plenty of extensions which allow you to manipulate the DOM (document object model) via AJAX (asynchronous Javascript and XML) or similar, it is messy.  XAML, on the other hand, is designed to be a user interface for applications, whereas HTML was just designed for static pages.  This means that you don’t need to spend so much time mucking about with code to manipulate the DOM when you want to make changes to a page.  Of course, AJAX frameworks will do a lot of that for you, but there are usually some complications at some point along the way, especially when things go wrong.

One of the big issues of course is browser compatibility.  Apparently (according to Wikipedia), XAML can be rendered in some browsers (Internet Explorer and Firefox at least) with no plugin, as long as .NET 3.0 is installed on the PC with the browser in.  For other browsers, the Silverlight plugin is available to deal with the XAML.  On non-Windows platforms, Microsoft have released a plugin called Moonlight to deal with it.  

I’d be interested to have comments from what other people’s experiences of Silverlight, XAML and WPF have been like and whether you’d like me to post more about any or all of these subjects.

Akismet WordPress Spam Filter

Sunday, March 22nd, 2009

I must say that I’m very impressed with the automatic anti-spam filter that comes with WordPress – it is called Akismet.  At this point in time, it has blocked 715 spam comments, and every one that I’ve checked it has been 100% correct on (and I’ve checked most of them)!  I’m thinking of looking into how it works (if it is open source, which I imagine it is).  I wonder if a similar system can be written for use on email spam and whether it would be as effective.

ASP.NET Sessions Part 6 – Abandoning a Session

Friday, March 13th, 2009
This entry is part of a series, ASP.NET Sessions»

If you want to terminate your session and effectively wipe out all variables in it, call Session.Abandon().  Although you can reset your session variables back to Nothing, this will also notify ASP.NET to clear the session up and call the event handler in Global.asax to deal with the session end (I think that this is the only way that this ever actually gets fired, but I could be wrong).

Windows 7 Beta vs Vista or XP

Friday, March 13th, 2009

Well, I’ve finally managed to get a look at a beta copy of Windows 7.  My initial thoughts are that I quite like the look of it.  I don’t use Vista on any of my own PCs – what I have seen of it made me want to avoid it – it seems unnecessarily flashy and annoying.  It seems to get in the way of what you want to do and asks everything that you might want to do.  I can understand why it was made that way, but I think it is a pain in the neck.

Windows 7, on the other hand seems to have been designed to be very unobtrusive.  As far as I have seen so far, it tries to put what you want in the easiest places to get to it, and lets you get on with what you are trying to do without getting in your way.  I can’t help thinking that Microsoft might have been taking notes from the way Google Chrome has been designed in some respects.

Comparing it to Windows XP, Microsoft have ditched the classic user interface and start menu (I think they actually did in Vista), which I have always turned on (one of the reasons I have avoided Vista).  I think I’ll finally have to get used to a new shell interface for the start menu, taskbar etc., but it looks fairly easy to use and find your way around so that should be ok.  

I like some of the little things like windows locking to the side if you drag them there, and the new “show desktop” section on the task bar.  I also noticed the ability to have multiple clocks, which I haven’t tried, but it looks useful if you work with people in different time zones.

I’m not sure about the way that Control Panel has been reorganised, but it doesn’t look too bad – I think I prefer it to the Windows XP default.  They’ve probably done a good job of it, but it is hard to overcome people’s natural resistance to change.

I’m going to have to have more of a play with it when I get the chance, but that was my initial review.

ASP.NET Sessions Part 5 – How Long Does a Session Last?

Thursday, March 12th, 2009
This entry is part of a series, ASP.NET Sessions»

Bearing in mind what I posted in earlier parts of this series of posts on sessions, you may find that other factors cut short your users’ sessions – see the earlier posts about this.

You can set the timeout of your sessions using the “timeout” attribute of the “sessionState” element in web.config.  This is a value set in minutes, and is the idle time before a session expires.  That means that the time is measured from the last request (not sure if it is measured from when the request is started, completed or some random point in the middle).  It isn’t measured from the start of the session or anything. 

The maximum value is 525,600, which is the number of minutes in a year (assuming it is a non-leap year).  This maximum value apparently only applies to the in process and state server modes (I’ll be amazed if anyone manages to keep a session going that long in either of these modes without dedicating a server to it!).  

The default value is 20 minutes.

Note that changing this has absolutely no effect on classic ASP sessions – they operate completely independently.  I’m pretty sure nothing in web.config will ever really affect them.

Char/Character Class Shared Functions/Methods

Thursday, March 12th, 2009

Not everyone realises that many of the base types in .NET have their own shared (or static for C#ers) methods which you can use as utility functions.

For example, the Char class has some very useful ones, including…

  • IsControl (is the parameter a control character)
  • IsDigit (I think this is numeric, or decimal point and possibly minus sign)
  • IsLetterOrDigit
  • IsLower (lower case – small letters as opposed to capitals)
  • IsNumber
  • IsPunctuation
  • IsSeparator (I think this is punctuation or whitespace – stuff that isn’t part of a word)
  • IsSymbol (dingbats style stuff I think)
  • IsUpper (capitals)
  • IsWhitespace (space, tabs, non-breaking (nbsp) etc)

These are fairly easy to call – if you have a Char variable called “c”, you just do eg.

If Char.IsWhitespace(c) Then MsgBox(“Is whitespace”)

Happy First Blogday!

Thursday, March 12th, 2009

This blog is now 1 year old!