Archive for the ‘Code and Development’ Category

NUnitEx

July 22nd, 2009

NUnitEx is an extension to NUnit.  It makes the assertions more of a flow to write.

Instead of writing

string myString = MyMethod();
Assert.AreEqual( "selfelected", myString );

one writes

string myString = MyMethod();
myString.Should().Be.EqualTo( "selfelected" );

A good thing with the former approach is that the coloured syntax makes it easier to distinquish method calls from assertions.

But when one tests exception throwing nunitex is the way to go.

Originally one had to write one method per call that threw an exception.  Even as a happy typist I think it was too much.  Nunitex easily handles exception throwing calls almost as ordinary calls.

See the example code at http://code.google.com/p/nunitex/ for yourself.

Xml comment check

July 13th, 2009

In C#/VBNet there is a nifty thing called Xml comments.  I always use it to explain my methods and classes.  A good thing about Xml comments is that they pop up as intellisense.  Press ctrl-J in Visual studio at your method call and your comments are shown.  The comments survive project boundaries so your well crafted comments in the business layer pop up at the presentation method calls to aid the GUI programmers.

The comments don’t travel C#->VBNet though.

In VBNet you get squiggly lines when the method call and the Xml comment don’t match.  In C# you have to go through the Analyze->Run code analysis to get the differences.

There are more comments on commenting here and here.

Visual studio database edition – how to think to get started

July 13th, 2009

Visual studio database edition is part of the “normal” Visual studio.  One cannot be totally sure of this since Microsofts licences are as comprehensible as scientology.

I made two tries at it before I got it to work.  Then it failed me anyway, but that is another posting.

The way to think is to think about the database as source code.  You make changes, compare them to the checked in/commited code and let the machine do its magic merge and check in the result.

Note: it does not handle databases with data in them but truncates everything.  My presently preferred solution is here.

Attach to a process even faster

April 11th, 2009

In an earlier article (or here) I mentioned a time saver for web projects where I recommended attaching to the process instead of restarting every time.

Today I invested time in creating a macro for attaching to the Nunit process.  It was easy.

– Instead of learning all commands just record a new macro (menu->tools->macro->record temporarymacro) and connect to the process of choice.

– Then open the macro explorer (menu->tools->menu->macro explorer) which opens a toolbox.  There is a (new) module called RecordingModule.  Open this.

A new IDE opens with something like:

  Sub AttachToNUnitProcess()      ' MsgBox("AttachToNUnitProcess.Start")      Try          Dim dbg2 As EnvDTE80.Debugger2 = DTE.Debugger          Dim trans As EnvDTE80.Transport = dbg2.Transports.Item("Default")          Dim dbgeng(1) As EnvDTE80.Engine          dbgeng(0) = trans.Engines.Item("Managed") ' Can be "Native".          Dim proc2 As EnvDTE80.Process2 = dbg2.GetProcesses(trans, "MDG-VILTERSTEN").Item("nunit.exe")          proc2.Attach2(dbgeng)      Catch ex As System.Exception          MsgBox(ex.Message)      End Try      ' MsgBox("AttachToNUnitProcess.Slut")  End Sub

– Testrun your macro in Visualstudio just to make sure it runs properly.

– In the macro editor, create a new Module and copy the code you just created.  Rename module and method.  Save.

– Back in Visualstudio, Macro explorer, your new module should be visible.  “Should” – one might have to restart Visualstudio or the toolbox or something.  It should be runnable directly from the Macro explorer toolbox.

– Create a new menu (menu->tools->customize …) and attach your macro.  You find it under “Categories/Macros”, just drag it to your new menu or the menu bar.

Now attaching to a process is just a click or keystroke away.

Some more info is found here: http://msdn.microsoft.com/en-us/library/hdf2d1z8(VS.80).aspx.

Update: I debugged a web app for a while yesterday and Wow! – what a difference between 3 clicks and 1 click, or 3 keystrokes and 1 keystroke.  Besides being simpler it also connected faster when the GUI didn’t have to render and didn’t have to wait for me.  Why haven’t I done this years ago?  What else is there I should have done years ago?

Update: I created a new menu and items for every macro I use.  So Fast to use!
There is something that bothers me though.  Some menus dissappear after I restart VSNet, I don’t know why.

The macros are easy to start also without having created a menu for them.  Just alt-F8 and then arrow keys to find the right macro and start it with return.

Update: If I only use 1 macro I can use “run last macro” with shift-ctrl-P.  I am using it right now because I am too lazy to create a new menu and stuff.

Macro for attaching to a process

April 11th, 2009

When debugging aspnet solutions a lot of time is wasted on restarting the debugged application.  A real time saver is to connect to the process to avoid restarting.

This is done like so:

– When a new bug is found don’t stop the web browser, instead detach from the process (menu->detach->detach all).
– Update the code to correct the bug.
– Set a breakpoint.
– Connect (menu->attach to process->[find the process, it is called something like iis or aspnet]->attach) to the process.
– Reload the web browser.

There are some caveats.  Like when using Webform the viewstate is tightly connected to the controls on the form so it is not always possible to connect.

But for the most times this is a great time saver.

—-

When you have done this a couple of times even this takes long time.  So fire up the macro recorder and do the recipe above.  Then run the temporary macro.  Running the macro is way faster than doing it manually.  I guess it is all the GUI stuff that takes time.
Or store the macro in VS and create a button and/or a shortcut.

—-

Update: use shift-ctrl-P to run last macro.  Nice if one is too lazy to create a menu.

truefalse

March 22nd, 2009

Personally I write xml comments on all my methods even though I am a fan of method names  enough describing that comments really are unnecessary.  By descriptive method names I mean that others also understand them.

In a project I stumbled upon this method:

private void ShowHideControl( bool truefalse ){…

Since I had the source code it didn’t take much effort to find out what it did.

But a question remains – how do I present the problem for the original programmer without being rude?

LINQ examples

March 22nd, 2009

Linq is sooo good.

The next time you loop a list or array to find a value or two; consider spending 15 minutes to learn some Linq; it is well invested.  The syntax is somewhat like SQL but more straight forward.  For instance the keywords are written “the normal” way.  First what you have (from), then the filtering (where) and finally what you want (select).

var query =
from c in myCustomerList
where c.Age < 42
select c;

If you are looping two lists and compare them to get a new list, you should really look into Linq.  It will save you lots of time.

A tip for writing Xml comments in Csharp

March 20th, 2009

There is something called xml comments in the dotnet world.

It looks something like

/// <summary>
///
/// </summary>
/// <param name=”name”></param>
/// <returns></returns>
public User FindUser( string name )
{…

The trick here is to write the comments on the very first line. Like:

/// <summary>This method finds and returns a User by its name.
/// </summary>
/// <param name=”name”></param>
/// <returns></returns>
public User FindUser( string name )
{…

When the code is folded it then looks like

/// <summary>This method finds and returns a User by its name. …
public User FindUser( string name )…

Visual Studio “suggests” writing the comments on line two, like

/// <summary>
/// This method finds and returns a User by its name.
/// </summary>
/// <param name=”name”></param>
/// <returns></returns>
public User FindUser( string name )
{…

but then after folding it looks like

/// <summary> …
public User FindUser( string name )…

(this is not a problem in vbnet)

More commenting comments are found here and here.

Sortable IBindinglist

February 9th, 2009

Datagridviews in dotnet work well with datasets.  They work less good with “normal” objects; for instance sorting is much more of a hassle to implement.

The sorting problem is solved nicely in this http://groups.google.co.uk/group/microsoft.public.dotnet.languages.csharp/msg/2b7528c689f9ef84 piece of code.  It is more or less just to copy the code and violà, a sortable object list.

Select Distinct is considered bad

September 15th, 2008

Correction; Considered a warning.

Here is a good rule-of-thumb: If you have to write an SQL with Select Distinct, do adjust your chair, get a cup of coffee, walk around the office but above all talk to someone else because Select Distinct is a symptom of something being wrong, either in the database or in your solution.

/* Select Distinct is normally not needed when your database is lagom normalized. But if you do need a Select Distinct then this is a very good example of a situation that needs to be documented.*/