Archive for September, 2006
TypeMock is wonderful
TypeMock is absolutely awesome. No, no, it is sexy. It is dead sexy. TypeMock has opened up a whole new world for me in unit tests. Scott Hanselman’s podcast on mock objects came at the perfect time. It was literally that day, I was sitting there thinking “this is exactly what I need.”
Previously, part of the trickery around unit testing was that it could be heavily dependent upon the data in the database or on outside systems. Want to have a test that makes a post to CS? Run it twice in a row and could run into an issue with the duplicate post checker. Want to test some Mail Gateway processing? It may need to connect to an IMAP/POP3 server to get some mail. There may not be any there.
Enter TypeMock.
There are a number of mock frameworks out there, and a number of great free ones. But one recurring thing I’ve seen is that they need the classes to be interfaces, so anything I want to override needs to be an interface, or they often can’t access methods/properties that are static or something. Not with TypeMock.
I would definitely find needing to make everything an interface tedious. And on top of that, that wouldn’t work for external libraries. Take the ASP.NET Profile class. It is not an interface, and I can’t make it an interface. I could create our own Profile interface and change it all throughout CS, but is it worth it just for a unit test when there are easy alternatives? Perfect example of this was a unit test I wrote recently for testing processing of email templates. The template file it loads depends on the user’s language setting. I can do a straight user.Profile.Language = “en-US”, because the profile is read only be default, and there is a ton of work to make it writeable and everything. So, just mock it.
Mock profileMock = MockManager.MockAll(typeof (Profile)); profileMock.ExpectGetAlways("Language", "en-US");
Sweet. All done. Just create a mock and tell it to always return en-US for the Language.
In the same test, a part of the process is to queue the message, but that hits the DB and requires the DB connection all there. Don’t want to both with that. DB is in separate tests, just want to test the actual processing of the email template. So mock the DB call so it doesn’t actually go to it.
Mock queueMock = MockManager.MockAll(typeof (SqlEmailQueueProvider)); queueMock.ExpectCall("QueueEmail");
Writing a test on blogging by email and need to test it adding the post. But you don’t want it to actually go to the DB, however, it needs to return a PostID after the post is created. No problem.
Mock weblogDPMock = MockManager.MockAll(typeof (WeblogSqlDataProvider)); weblogDPMock.AlwaysReturn("AddPost", BlogPostResults.Success).Args(Check.IsAny(), Check.IsAny(), new Assign(1));
Simply beautiful. I can sit there and test all day without worrying about having the proper set of sample data in the DB beforehand, getting duplicate post issues, or anything.
Final reminder!
In case you’ve missed the postings by myself, Jose, Rob, and Dave, if you are in or around the Bay Area or Central Valley, feel free to join me tonight at the Sacramento .NET User Group for my talk on CardSpace.
As for Telligent folks, the Telligent CA Crew (Jose and myself) will be there. If you get there before the talk, we will likely be found downstairs in Quizno’s. If you stick around after the talk, Jose and I will be there for a while to talk about CardSpace and/or Community Server.
Lesser known CS goodies: ImageInfo class
Kevin came to me the other day and was asking about whether I knew of a good way to efficiently extract the dimensions of 100+ images. He said he was starting to look at the System.Drawing namespace and was curious what the best way was.
I immediately turned on my caps and said NO! DON'T DO IT! Don't get me wrong, the System.Drawing namespace is great, but when you just need the dimensions of an image, and you are processing 100 images, it is not very efficient. It will need to load up the entire image, just so you can access some metadata about it. Do that 100+ times on a given run, and it will be incredibly slow and CPU hungry.
I had encountered the same issue way back when we were developing Community Server v1.0. I had begun prototyping the FTP Gateway and one of my main things was that I wanted to queue up 1000 images to be sent in a row. It worked, but would have the CPU pinned at 80-100% the whole time. The problem was that the images would transfer so fast (since it was on the local network), and the slow down was where it accessed the image's dimensions and cached the EXIF data. Initially, we were using the System.Drawing namespace and was having to process the entire image when we were just accessing the metadata.
So I set out to find a way to access the image's metadata directly, by looking at the raw header instead of relying on accessing it as an image. The solution was ImageInfo. I found this class that did precisely what I wanted, just that it was written in Java. So I did what any determined developer would do and decided to port it. And in one night, I was able to get it ported over to C#, plugged into CS, and working in both the CS website and with the FTP Gateway.
This class has been a part of Community Server v1.0 around beta2 or 3. It is in the CommunityServer.Components project, with its full namespace being CommunityServer.Components.ImageInfo. Since it is in Components, it is accessible by all CS assemblies. It is also quite easy to use. Just take this little tidbit:
// Check to make sure it is a valid imageFileStream stream = File.Open("image.jpg", FileMode.Open);ImageInfo imageInfo = new ImageInfo(stream);string contentType;int width;int height; // If it is a picture, get the imagesizeif (imageInfo.Check()){ // Get the image's width and height settings contentType = imageInfo.MimeType; width = imageInfo.Width; height = imageInfo.Height;} stream.Close();
This is a simple and straightforward way to access some basic information about the image. Create a new ImageInfo object with the stream of the file. Then, call the imageInfo.Check() method to do the actual processing of the file. If it returns true, it is a valid image type of either JPEG, PNG, GIF, BMP, and a couple others. After that, you can access the properties that contain the info about the image. Overall, it gathers the following:
- Width/height
- Bits per pixel
- Whether the image is progressive
- The image format (an enum of either JPEG, GIF, BMP, etc)
- The content type (as a string)
- Color type
- Comments from the image (such as in JPEG files)
- Number of images (such as with animated GIFs)
- Physic width and height DPI
Use it, love it, and enjoy it. Joy me next time, as I may make this into a semi-regular series to expose developers and power users to some of the great utilities buried deep within CS.
Why don’t banks use RSS?
This is something that occurred to me the other day…
RSS is becoming pretty big. There is RSS capabilities built into Vista. You can get RSS feeds for sports scores. CNN has RSS on news feeds. On Craigslist, you can do a search and get an RSS feed for the results. There are even sites where you can subscribe to an RSS feed for UPS package tracking.
So why does my bank not have an RSS feed?
I use Microsoft Money to daily download new transactions from my bank. It works great. But it isn’t supported everywhere. Wells Fargo supports it, but not Washington Mutual. Why? Because of all the licensing bull. The means by which they synchronize is totally proprietary. Money has its way, Quicken has its own way. Some banks support both, some support one and not the other, and others support them but not everywhere (like BofA, which supports Money everywhere but CA and two other states).
Why does it have to be so arcane? RSS is free! It is cropping up everywhere! It would not take a rocket scientist to implement it! Then everyone can enjoy it! Would no longer need to charge users $6/month to cover the ridiculous licensing fees. If you don’t want the extra bloat/baggage of Money/Quicken, you could subscribe to it in your RSS reader and be able to easily check new transactions each day. It would be wondrous.
Or take a service like Yodlee. With Yodlee, you can create an account and set it up to link up with all your bank accounts to get a central list of new transactions. It is used by a lot of companies. When Microsoft Money can’t connect directly to your bank, it often supports WebConnect through MSN Money, which uses Yodlee. Why can’t Yodlee support RSS? If they had RSS support, you could monitor all your bank accounts, regardless of whether or not your individual banks supported it. Wouldn’t that be beautiful?
Will it ever happen? Maybe I should bug Scott Hanselman about it. And now that I check his site (after writing this), I see a post of his from Monday I hadn’t read yet that looks surprising close to this. He also hints on a CardSpace implementation I’d love to see… managed cards from VISA, MasterCard, or AMEX (actually using a mock-up of that idea in my CardSpace presentation).
Hot IDE
Today, Jeff Atwood posted the Fonts and Colors settings file for his Visual Studio color scheme, and I have to say, I love it! After seeing one of his older posts with a screenshot of his IDE, I’d started mimicking some of it, mostly the color of the line counter, background color, and selected text color.
One of the biggest improvements is simply the background color. Dropping it from full on white (255/255/255) to a slightly lighter shade (248/248/248) makes a world of difference. The harsh contrast takes away so much. The more subdued colors make it easier to look at and far less straining on the eyes.
I’ve learned the hard way how not fun it is to over strain your eyes. A couple of years ago, was working in a poorly lit office, staring at a monolithic CRT monitor for hours a day, and it jacked me up. I had to stay completely off the computer for nearly 2-3 days. If I looked at a monitor, even just for 15 minutes, I’d get a raging headache right behind my eyes. Was very light sensitive, motion sensitive, and could not focus well (ie, reading even averaged size print). No real permanent damage, and I’m sure others have had worse, but certainly not something I want to repeat. Hence why I use LCDs now, have better lighting in my home office, take breaks about every hour or two, and love Jeff’s color scheme.
Qgyen.AllCommentRss with Spam Filtering
Qgyen.AllCommentRss v1.0.60918
A while ago, I had released my module to add an RSS feed for comments across an entire blog, geared for blog owners. One of the frequently requested enhancements was for it to support filtering out of spam comments. Lots of people have been getting a ton of spam, and the spam rules within CS can be a huge help in tackling them, but they would still pollute the owner's comment RSS feed, keeping them from just seeing the actual comments made by their adorning audience.
So, over the weekend, I had some spare time and added in support for filtering spam from the comment RSS feed. The feed link included on the review comments page will now match the currently selected spam filter. So if the list of comments you are viewing is filtered to Not Spam, the feed link will be the Not Spam filtered feed. Also, any existing feed URL that doesn't include the spam filter segment will default to the Not Spam feed.
Total list of changes in this version:
- Added spam filtering
- Reworked in-feed commands (moderate/approve and delete) to work through the exist comments page, rather than by the feed's handler. A little bit cleaner, less duplicate code, and like the security around it more.
- Fixed possible cache issue with using the feed across many blogs.
Download (Requires on Community Server v2.1)
How many of you buy developer tools?
How many of you out there buy developer tools out of your own pocket? Could range from ReSharper, profiler (was checking that out a while ago, pretty cool), or a library like a set of charting tools? Or, do you try to have your employer pick up the tab, if it is for a project at work (*grin*)?
Just curious. I'll buy something myself if it is something that is really useful, that will greatly improve my productivity or that I will regularly use, for work and non-work projects. ReSharper was one of those… it was well worth it and has repaid itself many times. Though I was recently looking for some charting controls just for myself, to generate some little charts for some stuff, but none of the free ones looked that good, and most cost a couple hundred dollars. Perhaps I could just get one with a demo, do what I need, and then dump it. Often times, the libraries aren't priced for casual developers. Or something like dotTrace, which would be fun to screw around with and fine ways to tweak programs, but I don't need to spend $250 out of pocket… and it is probably one of the cheaper ones on the market.
It seems like a catch-22 though. I love to code all kinds of things, sometimes for no particular reason other than I saw it, it looked cool, and I wanted to write something that used it (talking about a library in this case). But for some cool things, it costs $$ to make use of it, so unless you got money to burn, it doesn't do much good.
Perhaps I should start up a "development fund"… like set aside $10/week to spend on developer tools and libraries. That wouldn't be too bad. And maybe only get things that I'd continually use, rather than one-offs.
Skype listens on port 80!
Last night, I was sitting down to do some casual work on my laptop while watching TV with my wife, when I encountered an interesting problem. I needed to setup a new virtual directory in IIS for the stuff I was working with, and when I opened the IIS admin tool, the Default Web Site was being shown as stopped. I scratched my head and went to try to start it, and it failed. Interesting. I hadn’t really changed anything on my laptop in a while, and it was working fine not too long ago.
So, I open up the Windows event log and find an entry for it that says it failed to started because something else was bound to port 80. At this point, I flip out. Nothing else should be using port 80. I figured I had some sort of malicious spyware on my system that allowed a backdoor through port 80.
I immediately disable the wireless card to cut off all network access. Then I open up a command prompt and run ‘netstat -b’ and find out that oddly enough, Skype is bound to port 80. I scratch my head like, why the heck is Skype using port 80? I exit Skype, go back to IIS, try to restart the site, and it loads just fine. Ok. Now why?
I re-open Skype and proceed to look through its settings and find that it has an option for “Use port 80 and 443 as alternatives for incoming connections”. Ok, now it makes sense. But then I look at my desktop and it has the same setting enabled, but yet I’ve never had a problem with Skype using port 80 on my desktop. Both Windows XP Pro SP2, but never happened on my desktop. I had just installed Skype on my laptop last weekend, so that is why it was happening just recently on the laptop, but still, I didn’t understand why my laptop and not my desktop. They don’t have the same port setting for inbound connections, to there isn’t an issue with the firewall. So I don’t know.
So to other developers out there (or just regular Skype users), beware of Skype taking over your port 80!
Finally all caught up
After many months of being behind on my RSS aggregator, I finally caught up over this past weekend. Yes, I, for once, had nothing new to read!
Since around July, I had started falling behind on my daily blog reading, and once it starts going up, it is like a down hill slope where you never get caught up.
Now, I finally am. Granted, I do have some flagged posts I need to get back to, but I’ll get to those eventually (wish FeedDemon could sync flagged posts between PCs when using NewsGator).
In order for me to get to this point, there were a few big decisions I had to make, including dropping a few feeds. Overall, I dropped Scoble, Lifehacker, and Channel9. For the most part, those sites simply post too much. The signal-to-noise ratio was too much. I like blogs that get posts regularly, but not multiple times per day. A post a day or every few days is good. But 4+ long posts (or Lifehacker, which has tons) is too much. As for Channel9, for some reason, posts would always remark themselves as new when people replied to them or something. That just got annoying.
Official posting of my CardSpace presentation at Sac .NET User Group
This morning I got the official announcement of my presentation at the Sacramento .NET User Group later this month. If you are in the area, be sure to check it out! I will be giving away the Community Server shirts I'd posted about before. In the next week I will likely be posting the code samples to try and get them out there before the presentation.
September 26, 2006: Ken Robertson
CardSpaceThe event is free for all registered members. Please RSVP for the meeting by sending an email to rsvp@sacdotnet.org. If you have difficulty reading this email you can view the agenda online.
When:
Tuesday, September 26, 2006 from 6:00 to 8.30 PM
Where:
UC Davis Extension, Room #209, Sacramento.
Sutter Square Galleria, 2901 K St., Sacramento, CA 95816Directions:
2901 K St. is between 29th and 30th and K&J Streets (Map & Directions)
Business I-80 northbound: Take the N Street exit. Continue north on 30th Street for three blocks and turn left on K Street. Turn right into the parking garage.
Business I-80 southbound: Take the J Street exit. At the first stoplight (immediately after the exit), turn left on J Street. Turn right into the parking garage.Speaker:
Ken Robertson
![]()
Ken Robertson is a Software Development Engineer with Telligent Systems, Inc. (www.telligent.com), a company focused on building software for online communities based on .NET. Ken is a core member of the development team on Community Server (www.communityserver.com) and has helped develop the software that runs sites like forums.asp.net, weblogs.asp.net, blogs.msdn.com, and www.netfx3.com. Ken has also worked closely with the CardSpace team to help develop the first implementation of CardSpace utilizing Community Server.
Topic:
CardSpace
CardSpace (formerly InfoCard) is a new digital identity system introduced as a part of the .NET Framework 3.0 (formerly WinFX). CardSpace represents a new de-centralized way to manage user’s online identities through a collection of unique Information Cards that the user is able to create. We will cover the basis of what CardSpace is and isn't, talk about what is required to utilize CardSpace, and show an example of a finished end-user experience using CardSpace. Finally, we will cover the process of integrating CardSpace into an application from beginning to end.
