GeekSpeak comments edit

Something I’ve noticed as I work with developers of varying skill levels is that one of the key differentiating factors between the junior folks and the senior folks is the ability to think in the abstract - that is, patterns and concepts, not concrete implementations.

How many times have you been one of the people in this conversation?

Senior Developer: OK, so the task at hand is to create a Doorman that works in a hotel or apartment building. We know he’s going to hail taxi cabs, handle the door for people, make small talk, and do a few other things. Today we’re just looking at the door handling part.

Junior Developer: What kind of door is it?

SD: It really doesn’t matter, and we won’t really know until the Doorman gets placed somewhere, so we need to be flexible. What we probably should do is have an interface like “IOpenDoor” with an “OpenDoor” method. Then we can provide any number of implementations to that to handle different door types. (This is the strategy pattern.) The question on the table is, “Does the doorman need to do anything else with the door besides open it?”

JD: What if the door is heavy?

SD: It doesn’t matter - that’s an implementation detail. What we’re looking at here is the pattern. Like, we know the Doorman needs to open the door, but does he need to close it, too? Should it be “IDoorController” with “Open” and “Close” methods? Or do we consider all doors to be self-closing?

JD: If the door is glass and the Doorman kicks it closed, he might break the door.

SD: Um… OK… right… but really we need to look at the pattern here. Is it the right way to go? Are we missing something around the Doorman’s interaction with the door that we should know about?

Another Senior Developer: The door might be open due to some other action, but the Doorman might be requested to hold the door, so we might need a “HoldOpen” method on that interface.

SD: Good point. That’s a door state we didn’t consider.

JD: What if the door has a knob instead of a handle?

SD: Step into my office.

Now, I’m not saying that all junior developers are unable to think in the abstract, and I’m definitely not saying that all senior developers (or at least people with a “senior” title) can. What I’m saying is that the ability to think in the abstract is key to being able to step beyond simple coding and into the world of development.

gists, dotnet, build comments edit

Working on some localization stuff, I’ve come across the need for resource assemblies to be created from .resx files in a post-build step - I have the .resx files, but I don’t want them at all to be attached to my application. I just want them built into a tiny hierarchy of resource-only assemblies so I can get a ResourceManager on them and have string lookup behave as expected.

I also find that sometimes I need to recompile .resx without recompiling the whole app and I forget the command-lines to do all that.

The overall process:

  1. Convert your .resx files to .resources files using resgen.exe.
  2. Link your .resources files into assembly format using al.exe.

Again, what I’m showing here assumes that the assemblies you’re compiling are resource-only - which is to say, there is no code to compile and you’re not linking resources into existing assemblies.

For a culture-neutral (default) resource-only assembly, you’ll use:

resgen.exe "Path\To\Culture\Neutral\Strings.resx" "Path\To\Output\Folder\Strings.resources"
al.exe /embed:"Path\To\Output\Folder\Strings.resources" /out:"Path\To\Bin\Strings.dll"

The first command line generates the .resources file from .resx and puts it in an output folder. It doesn’t really matter where the output folder is; it’s a temporary place to store the .resources file before it gets compiled into an assembly. The second line links the .resource file into assembly format and puts it in your application’s “bin” folder, where you can start to consume it.

For a culture-specific resource-only assembly, you add culture-specific parameters and output locations into the mix:

resgen.exe "Path\To\Culture\Specific\Strings.es.resx" "Path\To\Output\Folder\Strings.es.resources"
al.exe /c:es /embed:"Path\To\Output\Folder\Strings.es.resources" /out:"Path\To\Bin\es\Strings.resources.dll"

In the above example, we’re compiling general Spanish resources, as noted by the “es” in there. If we wanted Spanish specific to Mexico, we’d use “es-MX” as the culture. The key differences to note:

  • In the .resx and .resources files, we specify the culture before the extension. While this isn’t mandatory, it is standard convention and helps you keep your source .resx and output .resources files organized.
  • In the al.exe command line, we add the /c: parameter to specify what culture we’re linking for and we put the output assembly in a folder under “bin” that is named after the culture we’re linking for. We also add “resources” just before the .dll extension so the ResourceManager knows it’s a resource assembly. (You don’t do that for culture-neutral resources, so you didn’t see that before.)

This is pretty easy to do in a Visual Studio project post-build step, too:

"$(VS80COMNTOOLS)..\..\SDK\v2.0\Bin\resgen.exe" "$(ProjectDir)\Strings.resx" "$(TargetDir)Strings.resources"
"$(WINDIR)\Microsoft.NET\Framework\v2.0.50727\al.exe" /embed:"$(TargetDir)Strings.resources" /out:"$(TargetDir)Strings.dll"
"$(VS80COMNTOOLS)..\..\SDK\v2.0\Bin\resgen.exe" "$(ProjectDir)\Strings.es.resx" "$(TargetDir)Strings.es.resources"
"$(WINDIR)\Microsoft.NET\Framework\v2.0.50727\al.exe" /c:es /embed:"$(TargetDir)Strings.es.resources" /out:"$(TargetDir)es\Strings.resources.dll"

Note we had to specify some paths to resgen.exe and al.exe because the post-build step isn’t run as part of a Visual Studio 2008 command prompt environment.

Obviously, change your paths and parameters as needed, but the above compiles and links a neutral and a generic Spanish “Strings” assembly in a post-build step.

gaming, playstation comments edit

Had a bit of a PS3 weekend this weekend. Realized I had the PS3 but really hadn’t played any games on it, using it primarily as a Blu-ray player, so thought it might be time to see how it went.

The first game I picked up was Echochrome. It’s sort of like “MC Escher: The Game.” The idea is that you’ve got this little mannequin that walks around on a sort of wireframe-looking level that obeys certain “laws of perspective” like “perspective existence” - if you can’t see an obstacle, it doesn’t exist. It’s sort of hard to explain - watch the video.

What I like about it is that it makes you think in a different way. A level that looks impossible to traverse is actually pretty simple if you think in perspective. It’s definitely worth the $10 I paid for it.

Darth Vader in Soul Calibur
IVThe second game I got was Soul Calibur IV. I debated whether I should get this on Xbox 360, where the “special character” included is Yoda, but decided that the PS3’s special character, Darth Vader, would just be cooler to fight with. Yoda’s neat and all, but I don’t really think “bad ass” when I see Yoda. It’s more… novelty value. Darth Vader, though, I’m thinkin’ that boy’s gonna put the beat down on someone.

Jenn and I put a few rounds in here and, while I was pretty decent at Soul Calibur III, Jenn kicked my ass like nine out of ten rounds just by button mashing. (She’s really good at button mashing.) I’ll have to bust out the guide and practice up again. It’s been a couple of years since I played SC3 so I’ve gotta get back in the groove. Still, tons of fun, and totally great graphics and sound. A total step up from SC3 and a good way to get myself into the PS3 game world.

One thing Jenn and I noticed while playing SC4: On PS3, when you turn it on, you can “sign in” to the Playstation Network with an account you create, which then associates purchases, etc., with your “profile.” This seems very similar to the Xbox Live account sort of thing, where you sign in and then play games and achievements get associated with your profile. On Xbox, though, when you play two people at the same time, both of you are signed in - I get signed in on my control, Jenn gets signed in on hers, and when we play, each of us can unlock achievements.  PS3 doesn’t have achievements (there’s a notion of “trophies” that’s still pretty new) and it turns out you can’t sign multiple people in at the same time, so we ended up playing with only me signed in even though Jenn has her own account. We both thought that was sort of weird. Hopefully they’ll fix that in the future.

gists, dotnet comments edit

I’m working on some custom FxCop rules and one that I want to do is to catch people who try to call Dispose() on objects deriving from System.ServiceModel.ClientBase<T> because they didn’t implement IDisposable in a safe manner.

So you have ClientBase<T> which looks, in a very abbreviated fashion, like this:

public abstract class ClientBase<TChannel> : ICommunicationObject,
          IDisposable where TChannel : class
{
  // Other stuff... and then
  void System.IDisposable.Dispose()
  {
    this.Close();
  }
}

Later, I might have a class that derives from that. Maybe a special type of client, and I might implement my own safe IDisposable version:

public class CustomClient : ClientBase<IMyService>,
          IDisposable where TChannel : class
{
  // Other stuff... and then
  void System.IDisposable.Dispose()
  {
    try
    {
      this.Close();
    }
    catch
    {
      this.Abort();
    }
  }
}

Try not to get hung up on the hokey implementation there, just stick with me - you have a sort of “overridden” Dispose() call. The thing is, if I put my CustomClient in a using statement, it’s the “overridden” Dispose() that executes, not the one in ClientBase<T>.

I want my FxCop rule to catch people who put something deriving from ClientBase<T> in a using block, but if you’ve got an override like in the CustomClient class there, I want it to let it go.

How do you detect that?

I’ve been all over the System.Reflection namespace and I can’t find anything. If you do Type.GetInterfaces() or Type.GetInterface() it shows that you implemented IDisposable either way because it gets all of the interfaces you implement all the way through the inheritance chain. Type.GetInterfaceMap() only returns the base implementation - the one from ClientBase<T> - in all cases. It ignores the derived class’s “override.” The only thing I can figure out that seems to work, but feels really bad, is this:

public static bool OverridesDispose(Type runtimeType)
{
  // For brevity, we're assuming the incoming Type isn't null and
  // implements IDisposable. I've omitted those checks here.
  MethodInfo info = runtimeType.GetMethod(
    "Dispose",
    BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic,
    null,
    new Type[] { },
    null);
  if (info == null)
  {
    info = runtimeType.GetMethod(
      "System.IDisposable.Dispose",
      BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic,
      null,
      new Type[] { },
      null);
  }
  if (info == null)
  {
    return false;
  }

  Type declaringType = info.DeclaringType;
  if (
    declaringType.IsGenericType &&
    declaringType.GetGenericTypeDefinition() == typeof(ClientBase<>)
  )
  {
    return false;
  }
  return true;
}

See what I’m doing? I basically query for an implicit interface implementation, then if that’s not found, I get the explicit interface implementation. If neither are found, I figure there’s no override. If one is found, then I ask what the declaring type of the method is, and if it’s the ClientBase<T> type, it’s not overridden, otherwise it is.

But the code smell! Ugh!

Am I missing some easier way to do it?

Last week was #32 for me. I had a great time the weekend before last, checking out The Dark Knight and hitting the driving range.

On the day of my birthday my parents came over and brought dinner - Mom made this super-tasty Mexican meal. I ate far too much of that and had some cheesecake when it was all done. They also got to visit the new kittens, who are starting to get big but no less wild. Jenn got me a red and black Nintendo DS with Guitar Hero: On Tour. My parents got me a case for all that and Mario Kart DS.

This past weekend, we went to the Washington County Fair on both Friday night and Saturday. I rode one of the rides called “El Niño” with Jenn and our friends Angela and K. It was pretty crazy and probably one of the better traveling fair rides I’ve been on. We also saw The Pirate’s Parrot show and it was great as usual, even though I got a pretty decent sunburn out of it. I hope they come back again next year.

In the end, it was a pretty great birthday. It’s going to be a while before I reach my next power of 2, so I’m going to have to savor this one.