gists, csharp, net comments edit

I’m looking at different ways to provide caching from within an application so I can read in some information from a file, keep it in memory, and have cache dependencies perform a callback to update the cache whenever the file changes.

This is simple to do in an ASP.NET app using the System.Web.Caching.Cache object that you find in HttpRequest. Just add something to the cache with a cache dependency and you’re set. But what about if you’re working in a console or Windows Forms app?

I did an experiment and it looks like you can access the cache from any application as long as you access it through HttpRuntime directly. I haven’t tested to see if this works on a machine that doesn’t have IIS installed, but I don’t see why it wouldn’t.

Below is my experiment code. It’s a console app that reads/writes the cache with a file dependency that constantly changes. Running it, you can see that the cache is doing what you expect, just like in a web app.

using System;
using System.Diagnostics;
using System.IO;
using System.Web;
using System.Web.Caching;

namespace ConsoleApplication1{
  class Class1{
    const string KEY = "mykey";
    const string FILENAME = "dependency.txt";

    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main(string[] args){
      WriteFile();
      InsertItem();

      for(int i = 0; i < 1000; i++){
        ReadItem();
        WriteFile();
      }
      ReadItem();

      if(Debugger.IsAttached){
        Console.ReadLine();
      }
    }

    static void InsertItem(){
      DateTime t = DateTime.Now;
      Console.WriteLine("Inserting item into cache: {0:m.s.ffffff}", t);
      HttpRuntime.Cache.Insert(
          KEY,
          t,
          new CacheDependency(Path.GetFullPath(FILENAME)),
          DateTime.MaxValue,
          TimeSpan.Zero,
          CacheItemPriority.Default,
          new CacheItemRemovedCallback(CallBack));
    }

    static void ReadItem(){
      object item = HttpRuntime.Cache[KEY];
      if(item == null){
        Console.WriteLine("Item is null.");
      }
      else if(item is DateTime){
        Console.WriteLine("Item is {0:m.s.ffffff}", item);
      }
      else{
        Console.WriteLine("Item is wrong type: {0}", item.GetType());
      }
    }

    static void WriteFile(){
      if(File.Exists(FILENAME)){
        File.Delete(FILENAME);
      }
      Console.WriteLine("Removing file.");
      FileStream stm = File.Create(FILENAME);
      stm.Close();
      Console.WriteLine("Wrote file.  File exists: {0}", File.Exists(FILENAME));
    }

    static void CallBack(string key, object value, CacheItemRemovedReason reason){
      Console.WriteLine("Callback invoked: {0}", reason);
      InsertItem();
    }
  }
}

If anyone finds a problem with it, do leave me a comment with reproduction info. Seems to work, though, which is pretty cool.

sql comments edit

I’m taking Microsoft training course #2734B: Updating Your Database Development Skills to Microsoft SQL Server 2005. I’ve got my MCSD; I really just want to see what’s new in SQL Server 2005 and how to use it. Rock on, right?

Day 1, we covered a general overview of SQL Server 2005 changes (enough to give you a general idea of what the course will cover, but not enough to really tell you anything), some of the T-SQL enhancements they’ve added, and a bit on how they’ve updated the handling of XML in the database.

The first module, the overview, was pretty good for the beginners but really didn’t tell me much.

The second module, the T-SQL updates, was neat. They added some stuff that should have been there all along (ALTER INDEX, anyone?) and put some really cool things in for partitioning. When we got to pivot tables… well, I won’t lie. I generally understand the concept of pivot tables, but I’m not a data analyst and really don’t care to be, so when we got to the use of PIVOT and UNPIVOT, I took it in from an academic standpoint but I can’t say I totally get it. The new ranking operators are cool, though, and I can see how they would be very useful. And how can I forget the TRY/CATCH they’ve added? Love that.

The third module, on handling XML, got a little more tricky. I feel pretty comfortable in my XML skin and worked with SQL Server’s OPENXML and SELECT...FOR XML in the 2000 version, so that stuff wasn’t new. The added ability to format the output of the XML to the level now available is very welcome, as is the ability to store XML as a native data type (either validated or not).

So what have I seen that I don’t like?

When we got into the trickier parts of the XML stuff - updating values of attributes in stored XML fields, for example - the book got a bit sparse. The lab would instruct you to do something like “change the ‘foo’ attribute in the document stored in field ‘bar’ in the first row in the table to have the value ‘val’” and when you went to look up the syntax for that… well, good luck with that. I ended up opening the solution and seeing how they did it (at which point it finally made sense).

The biggest issue, though, is how arbitrary some of the syntax has become. It turns out that in some cases, T-SQL requires semicolons to end statements and in other cases it doesn’t. That makes it difficult, but I figured out that they don’t penalize you for ending every statement in a semicolon, so maybe that’s the new habit to get into. I also don’t like the inconsistency of the syntax - sometimes you specify options in parentheses, sometimes you don’t; sometimes the parameters for a method are in [square brackets] and sometimes ‘single quotes.’ The worst bit is that they seem to intermingle it all without any rhyme or reason, so you might call a function like FUNCTION_CALL MODIFIER([param1], 'param2') 'param3', [param4] even if all of the parameters refer to database objects. Consistency! Pick something and stick with it! It feels very much like all this was tacked onto the end of T-SQL and they did their best not to alienate previous T-SQL users but couldn’t quite make it happen.

Today, day 2, we’re looking at the native SQL Server 2005 service oriented architecture provisions. I’m liking it so far. Better than SQLXML, anyway. It occurs to me that you could potentially replace BizTalk with SQL Server 2005. I wonder if that’s what they were going for.

media, movies comments edit

Saw the latest installment of Harry Potter this weekend, and it rocks. I am so pleasantly surprised to see them get back to the books rather than taking too much “creative license” or whatever and ruining the thing the way they did with the last one. In all honesty, I wasn’t looking forward much to this one because I was afraid they’d butcher it like they did with the last one. Not so - this is definitely the best since the original movie.

I won’t review the plot, since there’s a whole book on it and you can find out about that online. The question is, “Should you go see it?” The answer is an unequivocal yes.

A lot happened in the book, and I don’t want you to think that they didn’t cut anything, because they did. The beginning, in particular, was chopped down. Nothing to be too disappointed about because from a plot perspective, you’re not missing much - a bit of character development and background, but nothing that would translate well to the screen. That said, the things that could be shown were, and they did a fantastic job with them.

The effects continue to impress me. With each episode, they get better and better with the effect quality and the level of sheer cool they throw out there. The magic, the creatures… it all totally looks exactly the way you imagined it, and it’s brilliance to see it happening.

What I’m most pleased with, though, is that the screen translation of the book retained the awkwardness of growing up that was so important to the ongoing character development. I was afraid that since it wasn’t a driving plot point they would nix it, but they carried it off well - the characters’ expressions and actions reflecting the whole time the changes that their lives are undertaking. It totally captures that stage of adolescence where you’re discovering dating and the social difficulties that ensue. Great in the book; just as great in the movie.

All the actors did a fantastic job, as usual, and the new faces… perfect casting. Love it. I don’t want to give too much away - you’ll just have to see.

Long story short - check it out. You’ll be glad you did.

I already can’t wait for the next one.

coderush, vs comments edit

Previously the CR_Documentor plugin made its way into the Visual Studio Hacks site in an article called “Ask The Pros: Visual Studio .NET Addins.” Today it makes it in with its own dedicated article. That totally rocks!

I noted in the article that one of the downsides of CR_Documentor mentioned is the lack of support for included files. Not to worry; I’ve been noodling on how to make this work in a performant fashion. Not forgotten, just not quite here yet. There are other things I want to add, too, like the little gray box that shows the method signature in the doc and so forth. We’ll see what we can do.

Something the article doesn’t mention is the context menu that gets added to the code editor with CR_Documentor. It adds a lot of helpful commands that allow you to expand/collapse the XML comment blocks in your document, convert text blocks into XML comments, XML encode selected text, and insert XML comment block templates (among other things).

If you haven’t already, check out CR_Documentor.

aspnet comments edit

I just got a copy of Professional ASP.NET 2.0 and now I’m thinking it’s time to put some of the skills I’ll be learning from it to use. Stu and I have talked about writing our own blog software because none of the packages out there really do all the things we want, so maybe this is the time to step up and learn this stuff. It’s always better to learn when you have a project you’re working on (otherwise it’s just aimless and nothing seems to get done).

Time to start reading, eh?