May 25, 2008

Microsoft Source Analysis

First question: do you use FxCop?  Do I...not really.  But I try not to tell too many people that either.

So there is a new tool out there for all of you who need help keeping your source code up to snuff, similar to what FxCop does for your API...just at the source code level.  It is called the Microsoft Source Analysis for C# (sorry VB). BTW, there is also a blog.

I happen to have a project that I am in the midst of doing some spring cleaning anyway, so I decided to try it out.  You right click on the project or class you want to check, and select "Run Source Analysis".  Then in a tool window you will see a list of your failings as a developer.

Right off the bad I had a few issues with the list of failures.  Every code file has to have a header.

Here is my "new" standard header to make it happy:

   1: // <copyright file="Axis.cs" company="DiamondB Software">


   2: //     Copyright 2008, DiamondB Software. All rights reserved.


   3: // </copyright>




Lucky for me ReSharper can make that easy to add, but the ReSharper standard "File Header Text" in the ReSharper options does not allow you to have macros, or that would have been a lot easier.



Also, the Source Analysis does not like underscores on field names.  So this will get a red 'x'





private string _name;




and that is just not right.  In fact, I want just the opposite. (Note: although, this will make one cranky old dutchman that I know very happy -- he doesn't like underscores).



But lucky for me, it is easy to turn things off, which I do.  Right-click on your project file and select "Source Analysis Settings" and you can change anything you want.  The bad news, it is on a project by project basis.  The good news is that the files are merge-able.



The other thing I like about it: it detects Hungarian notation (a prior sin that I am currently making penance for).  "SA1305: The variable name 'oElement' begins with a prefix that looks like Hungarian notation."  But, if you like that sort of thing, back in the options is a way to tell the tools which Hungarian prefixes are acceptable.



But this could get ugly



OK, there are a few other concerns looming here.  I'm calling it Dueling Source Officers.  One tool will say two spaces between methods, the next will say one space.  One tool like spaces, the other likes tabs.  (for the record, Source Analysis HATES tabs, and thinks multiple lines between methods is plane evil).  The other officer: ReSharper Code Analysis and ReSharper Code Reformat.  Lucky for us ReSharper's tools are very configurable...but it could take a while to get the two in sync.



Not throw in FxCop (now integrated into the Team Systems version of Visual Studio) and it can get very interesting.



Next steps



The next steps are simple:



1. Learn how to write your own rules (I want my underscores)

2. Figure out how to integrate into Team City and Cruise Control (Code Analysis ships with an MSBuild task), and get something meaning full out of it.

May 21, 2008

WPF startup vs NCover

An interesting issue I've been trying to figure out lately, why is WPF so blasted slow on my laptop?  How slow?  It takes 90 seconds to open a Hello World WPF app.  Heck, it takes 90 seconds to load a WPF window with no "Hello World" or anything else for that matter.  But to make life more interesting, if I was to load a WPF app that actually does something, like on of the Prism samples, it also takes 90 seconds to load.

I talked with one of my buddies who is a budding WPF expert who told me I was crazy and needed to get a psychologist.  He is probably right.  But something was telling me that this was a system problem, not a WPF problem.  But for the life of me I wasn't seeing it.

Every programmer needs a toolbox.  Turns out there are several performance analyzing tools for WPF that ship with the Windows SDK.  One of them is the WPF Performance Suite.  While the tool is really cool...it didn't help at all.  If you want to check the performance of your WPF application while it is RUNNING, this tool will be great.

My problem was start up.  Here was the breakdown: 60 seconds before I saw a WPF window, 30 seconds before anything displayed.  After that, everything ran fine.  Time for a different tool.  Namely: Process Explorer from SysInternal (via Microsoft.com).

I was able to find my Hello World WPF application in Process Explorer (on my system, all .Net processes are colored yellow), then displayed the lower pane to display the DLLs used by the process.  (BTW: I just confirmed that Windows Live Writer is a .Net application).

I found one interesting dll that was referenced: NCover.  A while back I had installed a trial version of the commercial product (this was not the open source version). I have no idea what it was doing there, but it was there.  In fact, it was referenced in every WPF application that I loaded -- including WPF applications that I did not write, like the Subscription Center.

Long story made only slightly longer, I uninstalled NCover and now everything loads in about 10 seconds (a nine fold improvement), but I still don't know what NCover was doing.  If you know, please let me know. 

May 19, 2008

ReSharper 4.0 goes Beta (Candidate)

I just saw this on the wire: ReSharper 4.0 has entered Beta a Beta Candidate now as of build 804. If you want it, go get it here: http://www.jetbrains.net/confluence/display/ReSharper/ReSharper+4.0+Nightly+Builds

If you just want to see what is new: check out this page:
http://www.jetbrains.net/confluence/display/ReSharper/ReSharper+4.0+EAP+Notes

Finally, there is another deal going on at JetBrains.  DotTrace has gone to version 3.1.  For a limited time you can now buy the Personal version of DotTrace for $199.

Note: Ilya Ryzhenkov corrected me, this is just a Beta Candidate.

May 06, 2008

Using JQuery DatePicker in ASP.Net

I've had a number of uses for a more configurable date picker on my current application.  I needed something that looked good, and I one that I could set constraints for (Min and Max dates).  That last problem basically made the AJAX Toolkit CalendarExtender out of the running.

Luckily I found the JQuery DatePicker.  This used be a separate download, you can find the original here, but it is now part of JQuery.UI.

But first, there are a few tricks to using it (or any other JavaScript control for that matter).  First things first, this is not a separate server control that replaces the text box with a date friendly text box.  This is a javascript library that extends a standard text box and gives it date functionality.

So the first thing you have to do is put a text box on your form.

<asp:TextBox ID="DateTextBox" runat="server" />




Next on my list is to add the DatePicker JavaScript file, ui.datepicker.js.  I still use the Microsoft AJAX Library which has the nice ScriptManager control, so typically I use that control to add my JavaScript files to the page.





<link rel="stylesheet" href="~\Scripts\ui.datepicker.css" type="text/css" media="screen"  charset="utf-8" />


<asp:ScriptManagerProxy ID="ScriptManagerProxy1" runat="server">


    <Scripts>


        <asp:ScriptReference Path="~/Scripts/jquery.js" />


        <asp:ScriptReference Path="~/Scripts/ui.datepicker.js"  />


    </Scripts>


</asp:ScriptManagerProxy>




Note: I also included a link to the CSS file used to "skin" the calendar control.



Now everything is in place, we still  have to tell the javascript which text box control (DateTextBox) to put a Calendar control on.  I typically make a quick little JavaScript function that I can call to hook them up.  It looks like this:





function LoadDateControls() {


    $('#<%= DateTextBox.ClientID %>').attachDatepicker(); 


}




This code block asks the ASP.Net page for the ClientId of the text box control (very handy when dealing with Master Pages or User Control), and then attaches the date picker to the text box.



OK, the final bit of hooking up we have to do is call this function once the page is loaded and ready.  If we are using the Microsoft AJAX Library then you can use the pageLoad method like this:





function pageLoad(sender, args) {


    LoadDateControls();


}




If you want to stick with "pure" JQuery then you can do this instead:





$(document).ready(function() {


   LoadDateControls();


 });




Either way works, it is up to you which you like better.



JQueryCalendarSo lets review where we are now.  We have added the JavaScript to our page, and pointed it at a text box to display a calendar.  If everything has gone according to plan, you should see something like what you see to the right when you click on the text box.



I like it.  And as far as I know all of the colors and fonts are configurable via css.



OK, that covers most of my requirements, but there was one other requirement that isn't here yet: Min and Max dates.



This one is a little harder.  First we have to know what Min and Max dates to use, and that will have to come from the code behind.  Assuming you know that, you can call this C# code:





protected string MinDate;


protected string MaxDate;


 


private void UpdateDateRangeContols(DateTime minDate, DateTime maxDate)


{


    MinDate = EncodeJsDate(minDate);


    MaxDate = EncodeJsDate(maxDate);


}


 


public string EncodeJsDate(DateTime date)


{


    return date.ToString("U") + " UTC";


}




Now I have a MinDate and MaxDate fields that I can access from my aspx page.  To do that I'm going to rewrite my LoadDateControls function to look like this:





function LoadDateControls() {


 <%


    Response.Write("$.datepicker.setDefaults({dateFormat:'m/d/yy', minDate: new Date('"+MinDate+"'), maxDate:new Date('"+MaxDate+"')});");


 %>


    $('#<%=DateTextBox.ClientID %>').attachDatepicker();


}




One side effect of that bit of code is that I also use it to specify the date format.



But wait, did I really just use a Response.Write in my ASP.Net file?  Isn't that old school and lame?  Old school: yes.  Lame: no (if used in moderation).  I'm note writing an entire web site using Response.Write, just setting some JavaScript.  Besides, if you want to be one of the cool kids and start using the MVC framework, you had better get used to Response.Write again.  You're going to need it.



In my code behind, I set my calendar controls to only allow date entries that go back 2 weeks.  Take a look at what we get.



JQueryCalendar2



Today's date is May 6 (highlighted in pink), so my date range is April 28 to May 11.  When you are on the month of May, the Next button is disabled.  And, if I hit the drop downs for Month I only see April and May, and for Year I only get 2008.  Same for April, I can't go to March.



OK, so what if all of this is too much for you.  This is too much code, to much work, all you want is darn server control to use.  You are in luck.  Head over to West Wind Technologies and pick up Rich Strahl's JQuery Calendar ASP.Net Control.  Also subscribe to his blog, he is usually worth the read.

Using JQuery DatePicker in ASP.Net

I've had a number of uses for a more configurable date picker on my current application.  I needed something that looked good, and I one that I could set constraints for (Min and Max dates).  That last problem basically made the AJAX Toolkit CalendarExtender out of the running.

Luckily I found the JQuery DatePicker.  This used be a separate download, you can find the original here, but it is now part of JQuery.UI.

But first, there are a few tricks to using it (or any other JavaScript control for that matter).  First things first, this is not a separate server control that replaces the text box with a date friendly text box.  This is a javascript library that extends a standard text box and gives it date functionality.

So the first thing you have to do is put a text box on your form.

<asp:TextBox ID="DateTextBox" runat="server" />




Next on my list is to add the DatePicker JavaScript file, ui.datepicker.js.  I still use the Microsoft AJAX Library which has the nice ScriptManager control, so typically I use that control to add my JavaScript files to the page.





<link rel="stylesheet" href="~\Scripts\ui.datepicker.css" type="text/css" media="screen"  charset="utf-8" />


<asp:ScriptManagerProxy ID="ScriptManagerProxy1" runat="server">


    <Scripts>


        <asp:ScriptReference Path="~/Scripts/jquery.js" />


        <asp:ScriptReference Path="~/Scripts/ui.datepicker.js"  />


    </Scripts>


</asp:ScriptManagerProxy>




Note: I also included a link to the CSS file used to "skin" the calendar control.



Now everything is in place, we still  have to tell the javascript which text box control (DateTextBox) to put a Calendar control on.  I typically make a quick little JavaScript function that I can call to hook them up.  It looks like this:





function LoadDateControls() {


    $('#<%= DateTextBox.ClientID %>').attachDatepicker(); 


}




This code block asks the ASP.Net page for the ClientId of the text box control (very handy when dealing with Master Pages or User Control), and then attaches the date picker to the text box.



OK, the final bit of hooking up we have to do is call this function once the page is loaded and ready.  If we are using the Microsoft AJAX Library then you can use the pageLoad method like this:





function pageLoad(sender, args) {


    LoadDateControls();


}




If you want to stick with "pure" JQuery then you can do this instead:





$(document).ready(function() {


   LoadDateControls();


 });




Either way works, it is up to you which you like better.



JQueryCalendarSo lets review where we are now.  We have added the JavaScript to our page, and pointed it at a text box to display a calendar.  If everything has gone according to plan, you should see something like what you see to the right when you click on the text box.



I like it.  And as far as I know all of the colors and fonts are configurable via css.



OK, that covers most of my requirements, but there was one other requirement that isn't here yet: Min and Max dates.



This one is a little harder.  First we have to know what Min and Max dates to use, and that will have to come from the code behind.  Assuming you know that, you can call this C# code:





protected string MinDate;


protected string MaxDate;


 


private void UpdateDateRangeContols(DateTime minDate, DateTime maxDate)


{


    MinDate = EncodeJsDate(minDate);


    MaxDate = EncodeJsDate(maxDate);


}


 


public string EncodeJsDate(DateTime date)


{


    return date.ToString("U") + " UTC";


}




Now I have a MinDate and MaxDate fields that I can access from my aspx page.  To do that I'm going to rewrite my LoadDateControls function to look like this:





function LoadDateControls() {


 <%


    Response.Write("$.datepicker.setDefaults({dateFormat:'m/d/yy', minDate: new Date('"+MinDate+"'), maxDate:new Date('"+MaxDate+"')});");


 %>


    $('#<%=DateTextBox.ClientID %>').attachDatepicker();


}




One side effect of that bit of code is that I also use it to specify the date format.



But wait, did I really just use a Response.Write in my ASP.Net file?  Isn't that old school and lame?  Old school: yes.  Lame: no (if used in moderation).  I'm note writing an entire web site using Response.Write, just setting some JavaScript.  Besides, if you want to be one of the cool kids and start using the MVC framework, you had better get used to Response.Write again.  You're going to need it.



In my code behind, I set my calendar controls to only allow date entries that go back 2 weeks.  Take a look at what we get.



JQueryCalendar2 Today's date is May 6 (highlighted in pink), so my date range is April 28 to May 11.  When you are on the month of May, the Next button is disabled.  And, if I hit the drop downs for Month I only see April and May, and for Year I only get 2008.  Same for April, I can't go to March.



OK, so what if all of this is too much for you.  This is too much code, to much work, all you want is darn server control to use.  You are in luck.  Head over to West Wind Technologies and pick up Rich Strahl's JQuery Calendar ASP.Net Control.  Also subscribe to his blog, he is usually worth the read.