Archive for the ‘.Net’ category

ASP.Net MVC 2 Is GA

March 12th, 2010

Scott Guthrie just posted on his blog that ASP.Net MVC 2 has been released.  Looks like I am going to have to buy a new ASP.Net MVC book, and I didn’t even finish the last one!

Code Snippet Editor

December 30th, 2009

I stumbled across this great Visual Studio code snippet editor and thought I should share the link.  I have already created a few snippets with it, and it my development time is benefiting from it.

The editor allows one to highlight existing code and export it into a snippet file.  This is a great launch pad for snippet development.  The editor runs in Visual Studio, and once a snippet is saved with a shortcut it is immediately available in the IDE.  The editor also allows the editing of existing snippet files.

C# 4.0 Goodness: The dynamic Type, The DLR, and The ExpandoObject Type

November 10th, 2009

I have been playing around with C# 4.0 Beta 2 and stumbled onto the ExpandoObject (in the System.Dynamic namespace, System.Core assembly).  The object is built to be used in the DRL (Dynamic Language Runtime).  The DLR is a way for strongly-typed code to play nicely with weakly-typed code like IronPython, and vice-versa.  In C# 4.0 the dynamic keyword is used to reference objects that are weakly typed, and the ExandoObject is a .Net class that pretends to be a weakly-typed object.

ExpandoObject allows for addition of member properties and methods are run-time similar to popular scripting languages like Javascript.  Yet ExpandoObject is a real .Net type, and implements a couple of  interesting .Net interfaces: IDictionary<string, object> and INotifyPropertyChanged.  A dictionary of members is provides through the dictionary interface, and property changed events are fired through the property changed interface.  The only way to really leverage the power of ExpandoObject is with the dynamic keyword/type, which skips compile-time checking.  Method and property calls are not shown in intellisense.  You don’t know if your code will execute property until run-time.  So to access the above mentioned interfaces, the ExpandoObject’s dynamic reference must be cast to the interface type.

Here is some very simple, sample code that illustrates the concepts discussed (runs in Visual Studio 2010 / .Net 4.0 Beta 2):


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Dynamic;

namespace DynamcMethodExample
{
    class Program
    {
        static void Main(string[] args)
        {
            dynamic person = new ExpandoObject();
            person.Name = "John";
            person.Gender = 'm';
            person.Age = 10;

            Console.WriteLine(person);
            Console.WriteLine(person.Name);
            Console.WriteLine(person.Gender);
            Console.WriteLine(person.Age);
            Console.ReadKey();

            person.MakeTacos = (Func<string>)(() => "Tacos are ready!");
            Console.WriteLine(person.MakeTacos());
            Console.ReadKey();

            ((INotifyPropertyChanged)person).PropertyChanged += (s, e) =>
                {
                    IDictionary<string, object> personAsDictionary = (IDictionary<string, object>)person;
                    Console.WriteLine("Property {0} changed to {1}.", e.PropertyName, personAsDictionary[e.PropertyName]);
                };

            person.Name = "Jane";
            person.Gender = 'f';
            person.Age = 11;
            Console.ReadKey();

        }
    }
}

New Types in .Net 4.0

October 29th, 2009

A lot of the hype around .Net 4.0 is surrounding new features such as the Dynamic Language Runtime (DLR), and C# 4.0 features such as the dynamic type and co/contra-variance.  Something not getting as much press, but in many ways more helpful is the inclusion of many new, useful types included in 4.0.  The BCL team has a blog article about many of these types.  Here are a couple of my favorites:

  • Enum.HasFlag(Enum enum)
    I have written this code a couple of times before.  It detects whether a bit is set in an enum Flag value.
  • Enum.TryParse<EnumType>(string value, out EnumType result)
    I have also had to write this.  I think every core value-type should have a TryParse, which leads to:
  • Guid.TryParse(string value, out Guid result)
  • String.Join and String.Concat now take IEnumerable<T>!  I have written string.Join(“,”, values.Select(v=>v.ToString()).ToArray()) more that a few times.

I am really looking forward to .Net 4.0.  If you want to get your hands on it now .Net 4.0 and Visual Studio 2010 are available for download here.

Unit Testing in Silverlight

October 26th, 2009

Something I am a really big fan of is Unit Testing.  I was a big evangelist of unit testing at my last company.  The code my team wrote unit tests for was, on average, much better code than code that was written without unit testing.  Code-testing coverage via unit testing creates solid code.  It allows developers to thoroughly test their code in an automated fashion.  Unit tests give the developer confidence that they have introduced few or no regressions with new functionality added to old code.  All one needs to do is re-run the old unit tests to ensure everything still works.  Unit tests are a great form of documentation.  When a developer maintains code written by another they can inspect unit tests to see how the code is used, and can run these unit tests to ensure they aren’t breaking functionality they may not understand.  Ultimately unit tests act like a code specification.  In fact there is a style of coding called Test Driven Development (or its sexy new name, Behavior Drive Developer) that mandates that the tests be written first, and then the code is not considered complete until it satisfies the tests.  And even when unit testing is not take to that extreme, developing code that works well with unit tests usually ensures well-designed code from an object-oriented standpoint.  Code that is easily tested by a unit test is usually adequately decoupled and uses OO principles like inheritance, encapsulation and polymorphism in the right way.  Finally, writing code with unit tests can be faster.  Running tests can take less time than standing up an application and entering in the correct criteria to test the code in question.

At my current employer I have found myself working with a very mature (and very good) code-base that does not lend itself to unit testing.  It has probably been a year since I have written a unit test at work.  So when I started diving back into Silverlight I made it a goal to write our new framework to be as compatible with unit testing as possible.  This will help lesson the burden on our QA team, which has already written a lot of UI testing for our current ASP.Net web application.  We will also receive all the benefits mentioned above.

To start off I needed a unit testing framework.  NUnit is my old unit-testing friend and was my first thought.  However, NUnit and other unit testing frameworks don’t work in Silverlight-land, since Silverlight relies on a slightly different set of core .Net assemblies.  I did discover that there is a Silverlight version of NUnit and I considered it.  I also found the Silverlight Unit Testing Framework from Microsoft thanks to a post over on Scott Gu’s website.  The Silverlight Unit Testing Framework is based on the Microsoft library for normal .Net (which is also based on NUnit) but runs in Silverlight, in the browser.

Ultimately I chose the Microsoft library for a couple of important reasons.  First, there seems to be more acceptance and more example of the Microsoft library.  Second, the Silverlight Unit Testing Framework does one thing that is very unique compared to other unit testing frameworks: it runs the tests in the UI thread.  Jeff Wilcox, one of the authors of the framework, has an excellent post here about why Microsoft chose to do this, and it really makes sense.  Silverlight is a graphical library, and bugs will best be found if testing is written from a user-interaction standpoint.  Many things run on the UI thread, or are marshaled back and forth.  The Microsoft Silverlight Unit Testing Framework can work both as a unit testing framework and as a kind of integration testing framework.  The framework makes it very easy to test off the UI thread, and test asynchronously.  I found this excellent post on using the async features of the Silverlight Unit Testing Framework.  It really boils down to the testing environment being the same as the real run-time.  I have run across enough idiosyncrasies in the way Silverlight handles UI thread interaction to know that testing without it will miss a lot of bugs.

I have developed some “test” tests on business objects.  I also tested a thread manager I wrote using the built-in async functionality.  With the experience so far, I feel happy with the results.  My next step is to look at pluging in the unit tests into the automated build at work…

Visual Studio 2010

September 1st, 2009

Scott Guthrie has a very informative series about Visual Studio 2010 over on his web site.  Today’s article is about enhanced multi-monitor support in VS 2010.  I have only had a chance to use VS 2010 in a virtual machine, and have not had a chance to take the multi-monitor stuff mentioned in his article for a test drive.  It does look very cool.

Something I am particularly interested in is support for varying numbers of monitors.  At work I have a dual 19″ monitor display rig.  At home I remote into my desktop from my single-monitor 17″ laptop.  I occasionally have trouble with windows displaying correctly when I remote in. particularly using Visual Studio 2008 with tool boxes that are on the second monitor.  In VS 2010, window settings are kept in the local user file for the project, so if nothing else one should be able to move off-screen windows on-screen with a little text editing.  I am interested to see what kind of built-in support VS 2010 will have to detect that the second monitor is not present in this type of situation.  If it doesn’t auto-detect, it might now be easy to build a tool to edit the user file, or swap out user files.

First Impressions of IronPython

July 31st, 2009

I stumbled across a book sale a few days ago and bought a few programming books. One of them was IronPython in Action.  I had played with Python a number of years ago; I believe I was first introduced to the language in late 2001.  I evaluated it as a language for a local Unix shop to use to replace a couple of proprietary languages.  I found it quite appealing at the time and we ended up recommending it over Perl, Ruby and Tcl.  And then I proceeded to not use Python for anything over the last 8 years.  Its kinda like the time I learned Pascal just so I could help a girl that I liked that was in a Pascal class, but that is a different story and it didn’t work out anyway.

IronPython, as you may or may not know, is an implementation of Python built on top of the .Net Common Language Runtime.  The level of integration I have found between Python and the .Net Framework is uncanny.  I had a Windows Forms window with controls up on the screen in 5 minutes of programming.  On top of the actual .Net integration, the Visual Studio integration is also quite good.  Designing a Windows Form is about the same as it would be for a C# or VB.Net application.

I am only a couple of chapters into the book, but so far I have found IronPython to be a join to work with and I am already looking forward to a chance to use it professionally.  I will post a book review when I am finished.

Silverlight Command Line Arguments.

April 17th, 2009

I have been developing a fairly rich Silverlight application for the last couple of months that is meant to ultimately replace some of our existing UI.  This project has evolved from prototype to framework authoring to actual wire-frame implementations.  I have had a chance now to step back and create some small charting components for one of our existing pages.  A change of pace can be refreshing, and this one allows me to stay in Silverlight land yet spend about a week writing some production code.

There are a number of charting components available, many of them free.  I am going to take this opportunity to write my own from scratch since I really only need some simple pie charts.  I want to keep it small and simple, and yet learn from the experience.

I have run into a new situation with these small components that I had not encountered with the large application.  I need to pass “command line arguments”.  Or rather, I need to embedd some data in the HTML page for the Silverlight to pick-up and render.  This data will be the charting data.  As it turns out this is dead simple.  In the Silverlight object tag I simply include the initParams parameter with my data, comma-delimited:

  <object type="application/x-silverlight" width="100%" height="100%">
    <param name="source" value="ClientBin/MyChartingApp.xap"/>
    <!-- Command Line Arguments -->
    <param name="initParams" value="datum1=1,datum2=2,etc" />
  </object>

Then I read that data in during the application’s Load event:

  private void Application_Startup(object sender, StartupEventArgs e)
  {
    foreach (var key in e.InitParams.Keys)
    {
      var datum = e.InitParams[key];
      //do something with the datum
    }
  }

Pretty simple!  Now I just need to write some ASP.Net code to render out the desired HMTL, and I am all set.  Oh, that and write the Silverlight component.

ASP.Net MVC, IE 8 (Yawn) Released

March 19th, 2009

The first tech news coming out of Mix this morning was the release of IE 8. While its nice that they are still trying to do nice things with IE, I currently have 3 browsers installed that I prefer over IE: Firefox, Safari and Chrome. Firefox is my everyday browser. Its much faster than IE, and its plug-in ecosystem is great.  All IE 8 does is make testing of our current release more difficult.  Now we have to support 3 versions of IE.  Such is progress.

There is something cool coming out of Mix today and that something is ASP.Net MVC.  I played around with ASP.Net MVC a couple of months ago and really, really liked it.  For creating a modern web site I don’t think it gets much better.  Think Rails done right on top of a managed, strongly typed, compiled language married to great tools.  Download page here.  Microsoft has been making a lot of great moves in the web development space recently with their adoption of jQuery, release of Silverlight 2 last year and upcoming release of Silverlight 3, and now ASP.Net MVC.

Some More New Features in Silverlight 3 Announced at #Mix09

March 18th, 2009

Scott Gu and Co. have announced a few more items that are, well, kick ass.

  • A new data tier technology that will allow developers to write data services, decorate those services with a client attribute, and your Silverlight project will automatically generate proxies for those data services.  This eliminates a lot of custom code I have written to do my WCF plumbing.
  • Silverlight on the desktop on Mac and Windows!  Its pretty obvious they are going after Adobe Air here.  This gives Windows developers and easy-in to Mac desktop development.
  • A Silverlight developer plugin for Eclipse on Windows and Mac.
  • Blend 3 can import from Photoshop.  It provides granular control over importing layers from the Photoshop document.  Very nice!
  • Blend 3, in general, looks like a huge improvement over Blend 2.