Professional Programmer Notes

or just call this my soapbox

Archive for the ‘CSharp’ Category

Gestalt = Low-Lying Awesome

leave a comment »

Microsoft appears to be betting big on Silverlight.  When the Silverlight 1.0 bits were released in 2007, my initial thoughts were, “Yay, Flash for .NET developers.”  As Microsoft pushed forward with version 2, version 3, and now version 4 of Silverlight, those sarcastic thoughts have subsided to make way for more genuine curiosity.  How did that happen?

Well, for starters, Microsoft delivered real features.  Initially, Silverlight demos were all about media (music and video).  In addition, Microsoft touted the interopability between dynamic languages like vbx, c#, python, ruby, and javascript.  Then, that interopability was sidelined and Silverlight applications started to emerge.  Which was interesting.  In fact, Silverlight 2 had enough features to stir up debates in the enterprise over which RIA technology was best suited for enterprise applications: Flash 8 with Flex or Silverlight 2.   Then, Microsoft played their wildcard.  They made Silverlight play nicely with … ugh … Mac OSX.  Out-of-browser Silverlight applications made me raise my eyebrows for a technology that I had quickly written off as a “fad”.

That might still be the case.  I won’t make a claim either way.  But, Silverlight and RIA are spaces where Microsoft continue to innovate.  Gestalt is a very good example of that last statement.  Gestalt is built atop a foundation consisting of XAML, Silverlight, and dynamic languages.  It enables web developers to script their way to rich internet applications in a way that both Flash and Silverlight seemingly missed. 

With that said, it is difficult to pinpoint exactly what value Gestalt adds, but you feel it when you’re molding some python, ruby, or javascript hackery into a magical Silverlight-powered application that just works.

The technology appears to still be more of a proof-of-concept than a supported product.  But, it makes a strong case for embracing XAML and Silverlight. 

Checkout the website and the samples at http://visitmix.com/labs/gestalt/.

Written by curtismitchell

February 11, 2010 at 9:59 pm

Posted in .net, CSharp, Javascript, web

Tagged with , , , , ,

Seven Habits of Highly Effective ASP.NET MVC Developers

with one comment

This weekend I had the privilege of presenting two talks at the CMAP Code Camp in Central Maryland.  I gave a talk on the Spark View Engine and a new talk called “Seven Habits of Highly Effective ASP.NET MVC Developers.” 

Here are the slides for the latter talk:

Written by curtismitchell

November 9, 2009 at 11:31 pm

Posted in .net, CSharp

Tagged with , , , , ,

Richmond Code Camp 2009.2

with 2 comments

Wow! What an event!

This weekend, I joined ~400 others for the Richmond Code Camp and a good time was had. As others have noted, the hardest part of the day was choosing which talks to attend due to a schedule full of excellent topics and speakers.

I started off with Justin Etheredge’s talk on Linq Expressions. This was 75 minutes of great slides and polished demos of basic to advanced Linq concepts. I left that talk more educated and less scared of the power of Linq Expressions. Justin has an unbelievable understanding of how Linq works and an amazing ability to convey that to the layman with nothing more than a stock photo of a cat and a VM with Win 7 and VS 2010.

Second, I attended a talk that was missed from Raleigh’s Code Camp two weeks earlier. I went to John Feminella’s talk on Ruby for C# developers. John gave .NET developers a great introduction to the Ruby language using IronRuby (I thought that was brave at this point). To my surprise, John held up his end with great content and examples, and IronRuby held up its end with stability and support for most of the features of Matz Ruby (the original implementation of Ruby).

Next, I decided to checkout Open Spaces. The evening before, I jokingly suggested that the audience would convince Kevin Hazzard to present something on the DLR at Open Spaces since he was not officially presenting. Well, I guess they did! Kevin led a discussion on IronPython and the DLR that included some very nice demos. He also discussed C# 4.0’s new “Dynamic” type and how it actually works. I gained a lot of insight on when and where the DLR and Dynamic Languages on .NET are useful. And, while I love Ruby, IronPython is making the Python language very attractive to me.

Another talk that I was able to attend was by Chris Love. He talked about building quality ASP.NET applications faster. I know Chris to be a very experienced developer. He just completed an updated version of a book I found to be very practical when I was getting into more advanced ASP.NET concepts, ASP.NET 3.5 Website Programming: Problem – Design – Solution. His talk drew off of his experiences building applications and sites for his clients. He talked about architecture as well as development practices. I recommend his talk to anyone doing ASP.NET development that is looking for practical advice on how to manage it all from start to finish.

In the last time slot of the day, I presented Spark, an ASP.NET MVC View Engine, to a great audience. This was essentially the same talk that I gave a couple of weeks earlier at Raleigh’s Code Camp, but I made some modifications for the Richmond crowd. Here are the slides from that talk:

Enjoy!

Written by curtismitchell

October 5, 2009 at 3:04 pm

Posted in .net, CSharp

Tagged with , , , , ,

Rails-like ASP.NET Development Assets

with 2 comments

Written by curtismitchell

May 31, 2009 at 11:12 am

Debug an IE Add-On in Visual Studio 2008

leave a comment »

I’m working on a project that involves creating an IE Add-On in C#.  It is an inherited project that was created in Visual Studio 2005 targeting the .NET Framework 2.0.

Well, times have changed.  I am now doing most of my development in Visual Studio 2008, and (when lucky) I am using the features of the .NET Framework 3.0 or higher.  When I first made the switch, I tried to use 2008 to continue my work on the toolbar, but I failed miserably.  I was shocked to find out that my process of registering my assembly in a post-build action and launching IE as a Start Action did not work in 2008.  At the time, I quickly got over it, and preserved 2005 just for that project.

Luckily, in a more recent attempt to use 2008 for this type of development, I was able to solve the problem.  Check it out.

Problem:

Whenever I tried to debug my IE Toolbar project in Visual Studio 2008, I would get a nasty exception that says, “Error while trying to run project.  Unable to start debugging

image

My project is configured to register the assembly using regasm in a Post-build step.  Then, as you can see below, the VS Debugger is configured to start Internet Explorer (and subsequently attach to the process).

image1

Well, that was the wrong way of doing it.  I was able to get it to work successfully by modifying the Start Action like such:

image

And it worked!

UPDATE:  I still had issues on a separate computer.  Adding “about:blank” to the command line arguments fixed the issue.

Written by curtismitchell

May 6, 2009 at 3:25 pm

Improve page load performance in your ASP.NET MVC site

with 3 comments

As ASP.NET MVC become more popular in the enterprise and behind high-traffic commercial web sites (like Dimecasts.net), developers will look for ways to increase performance.

One well known way to increase performance of any website is to combine files to minimize HTTP requests (see Yahoo! Performance Rules). The reason is that most modern browsers use two threads to load a given page and all of its assets (css, js, images, etc.). This post will give a quick walk-through of an ASP.NET MVC implementation of a CSS consolidator.

In typical MVC fashion, I will give a quick breakdown of the model, the view, and the controller.

Let’s start with the controller.

My controller has one need: it provides a pretty url for me to point my tag to retrieve passed-in stylesheets. I implemented a controller called StaticController and gave it an action called CSS. Here is a snapshot of the action:

        public ActionResult CSS(string id)
        {
            if (String.IsNullOrEmpty(id)) return View();

            string[] filenames = id.Split(',');
            string path = HttpContext.Request.ServerVariables["APPL_PHYSICAL_PATH"];
            path = String.Concat(path, @"Content\"); // path to css files

            string cssData = Models.StaticCSS.GetContent(filenames, path);
            ViewData["css"] = cssData;

            return View();
        }

Quick explanation of the above code:
The action uses the default MVC routes (for simplicity). So, the id parameter is used to pass a comma-delimited string of stylesheet names. The path to the Content directory (the default folder for stylesheets) is derived, combined with each file name in the id parameter and passed to the model.

Let’s see how our model works.

The model is named StaticCSS and here are the methods:

        public static string GetContent(string[] filenames, string cssDir)
        {
            StringBuilder sb = new StringBuilder();
            if (filenames.Length == 0 || String.IsNullOrEmpty(cssDir))
return String.Empty;

            foreach (string f in filenames)
            {
                string filepath = (f.EndsWith(".css")) ?
String.Concat(cssDir, f) : String.Concat(cssDir, f, ".css");
                sb.Append(@"/* Start CSS file */");
                sb.Append(Environment.NewLine);
                sb.Append(GetFileContents(filepath));
                sb.Append(@"/* End CSS file */");
                sb.Append(Environment.NewLine);
            }

            return sb.ToString();
        }

        public static string GetFileContents(string file)
        {
            try
            {
                using (TextReader textReader = new StreamReader(file))
                {
                    return textReader.ReadToEnd();
                }
            }
            catch(FileNotFoundException)
            {

            }

            return String.Empty;
        }

Quick explanation:
The public static method, GetContent loops through a list of files, combines their contents into a string, and return the string. The GetFileContents method opens the file and returns the contents.

If we look back at the remaining code in our action:

            ViewData["css"] = cssData;

            return View();

We are adding the css file contents to the ViewData hash and calling the view.

Here are the important parts of the view:

<%
    Response.ContentType = "text/css";
    Response.ContentEncoding = Encoding.UTF8;
    Response.Write( Server.HtmlDecode(ViewData["css"].ToString()) );
%>

We are setting the content-type to “text/css”, which is important in standard-compliant browsers (not IE). We’re setting the encoding dumping the content to the page. These three lines essentially turn the view into a standard css file.

Finally, to use this code I would simply add the following line to any view requiring css files:

<link href="/Static/CSS/cssfile1,cssfile2,cssfile3" rel="stylesheet" />

This would download cssfile1, cssfile2, and cssfile3 in one HTTP request instead of three.

This is a simplistic approach that doesn’t care (yet) about the amount of memory used to load very large css files. But, if you choose to follow this model, I would love to hear how it performs for you.

Written by curtismitchell

March 17, 2009 at 10:42 pm

Posted in .net, CSharp

Tagged with , , ,

(Reminder) How to reset my environment settings in Visual Studio 2008

leave a comment »

A co-worker once asked me how to reset his environment settings. His environment was set to Visual Basic, but he wanted to change it to C#. Both of us are very experienced .NET programmers, but neither of us could remember how to do it. Here is a reminder to us all:

Goto Tools > Import and Export Settings > Reset all settings

Goto Tools > Import and Export Settings > Reset all settings

Written by curtismitchell

January 30, 2009 at 8:16 am

Posted in .net, CSharp

Tagged with , ,

Not really a Fan, but still kind of Groovy

with 2 comments

Fan is a very young programming language with some promise.  The features of the language are more pragmatic than theoretical.  This means, Fan, like Python and Ruby, should allow you to be productive in a concise manner.  The syntax of Fan left a lot to be desired.  It is not the prettiest language, but hopefully, syntactic sugar will be added in the future to facilitate Fan’s adoption.The killer feature that Fan promises to deliver is the interoperability with both the Java Virtual Machine and the .NET Runtime.  Currently, one or the other are required to compile Fan source code.  However, Fan currently does not offer much in terms of interoperability with Java and/or any of the CLR languages.  That is disappointing for now, but the language is extremely young.  Powerful features like that take time.

While a more developed Fan will find its niche audience of developers, I wonder about the true utility of such a language.  The JVM already has the well-known scriptingly awesome language known as Groovy.  I do not qualify as a Java developer, but I hold Groovy in high regard because of its performance, language features, and *gulp* use of the JVM.  Java reservations aside, Groovy is a language I look forward to learning.

.NET is not absent of nice scripting languages either.  Boo is one such language, noted for its Python-like syntax. Oh, and did I mention IronPython?  It is also noted for its Python-like syntax, but .NET-like performance.  Perhaps, the most awaited .NET language is IronRuby, a dynamic language with the features of ruby and the power of the .NET Framework.  It is going to be great.

There is one other .NET language that I think deserves mention.  It is called Nemerle.  Nemerle is a static language, much like C#.  However, Nemerle implemented features like type inference long before the release of C# 3.0.  Since the latest release of C#, Nemerles advantages have become less pronounced.  But, it still differentiates itself from C# with features like Macros that allow you to customize the syntax of Nemerle (think domain specific language).

Nemerle is a pretty mature language.  It has some integration with Visual Studio 2005.  Also, the Nemerle compiler is written in Nemerle.  That could be a sign of a pretty usable language.

Nemerle is open-source.  I don’t know if it is actively developed today.  However, I recently came across a blog post that sums up some of the features of Nemerle.  That post was the inspiration for this post actually.  Onur went as far as to call Nemerle “C# glorified.”  I’m not sure I agree with that.

However, I believe Groovy is definitely “Java glorified.”   Maybe Nemerle is a little Groovy.

Written by curtismitchell

August 3, 2008 at 4:14 pm

SnippetCompiler is great, but…

with 2 comments

…we don’t have to use it for quick compilable chunks of .NET goodness.  As detailed on marlongrech.wordpress.com, we can use TestDriven.Net to run small parts of code in the Visual Studio.NET debugger.

The code is not required to be a test case for use in a unit testing framework.  It can be an arbitrary method that just does some processing.  The key to using this technique, is to make sure you set a breakpoint.  That way you can manipulate parameters and actually get feedback about what the method is doing when it executes.

This is yet another reason we should buy TestDriven.Net.  It can pay for itself within a day or two.

In addition to SnippetCompiler and TestDriven.Net, Powershell is very useful for this kind of snippet testing as well.  But I’ll have to defer that post for another time.

Written by curtismitchell

July 2, 2008 at 6:04 pm

Fix Visual Style of Tab Control in .NET

with one comment

I was having an issue with my tab contols looking very ugly:

Ugly Tab

These controls did not look anything like other fancy tab controls that I have seen recently in XP and Vista.    My tab control sucked.  Looking at it took away my desire to use my application.

I made several attempts at remedying the issue.  I tried one combination of property changes after another to no avail.  I was on the verge of creating a new owner-drawn tab control ( I had created a new project in Visual C# Express and everything) when I stumbled upon a CodeProject article.

I didn’t follow the article all the way through because it seemed to be written for an older version of the Framework and IDE.  But, it reminded me that there is an Application.EnableVisualStyles() method*.

I swiftly made sure I called that method before any controls were drawn, saved my hard work, and clicked the VS “Play” button with the excitement and anticipation of a child in line at Chuck E. Cheese.  That all died quickly when I received a screen containing the same ugly tabs staring back at me.

I checked the return value of the Application.RenderWithVisualStyles property and discovered my application was not using visual styles.  Thanks to the MSDN documentation, I quickly found the problem.  I had XP’s Appearance set to Windows Classic style.  Doh!!!

After changing XP’s Appearance back to Windows XP style, I had beautiful tabs:

Beautiful Tabs

To recap the solution:

  1. Call Application.EnableVisualStyles() before any controls get rendered.  In Program.cs just before Application.Run is a good place.
  2. Make sure you are allowing your OS to use visual styles.
  3. Don’t judge me for being absent-minded most of the time.  I’m trying to get it out of the way so that I won’t be absent-minded when I’m older.

* I probably saved over that memory with a Mitch Hedberg joke or my wedding anniversary date.

Written by curtismitchell

May 17, 2008 at 9:38 am

Posted in .net, CSharp, vb.net

Tagged with , , , ,