Noah Blumenthal's Blog

January 15, 2009

DotNetPays — get paid for your work

Filed under: Uncategorized — noahblu @ 3:10 pm

I recently (well, a couple months ago) launched DotNetPays (http://www.dotnetpays.com).  I launched this more for myself than anyone else but I figured some others might want access so I made it available online to anyone.

If you’re a freelancer you might have a hard time recouping payment for work you do sometimes.  Well DotNetPays gets referenced in your Global.asax and checks the server every once in a while.  If the client doesn’t pay, you can have a warning displayed on every page (floating div style) and if they still don’t pay you can shutdown the app altogether.  One nice feature is that after they DO pay, DotNetPays will create a license file so that the app is not forever dependant on DotNetPays servers.

I was actually really excited when I released this because I emailed Scott Hanselman (www.hanselman.com) and he twittered a short hey that’s cool response which ended up creating thousands of pageviews over the next day.  Obviously that died down (I guess not that many people actually found it useful or felt comfortable using it), but it seems that there are a couple users.  Anyway it’s a nifty little product for those types of clients.

January 14, 2009

MVC ModelBinders, complex types, Enumerables, and partial binding

Filed under: ASP.NET MVC — noahblu @ 5:15 pm

If you’ve got an object that’s got some sort of enumerable, you’re going to have to write your own ModelBinder.

For example, suppose you have a Person object with a List<string> containing friends’ names.  Now suppose you have an HTML page that has 10 textboxes and the user can add 10 friends at a time (better yet, give them a JS widget that adds textboxes as necessary).  You could post back 10 or 20 friends at a time, right?

Now the Person object might just have a FirstName & LastName (which are strings) and maybe a phone number (also a string), and, aside from this list of friends, be nicely updated using the default ModelBinder included in MVC.  However, you really want these friends to be updated with the Person object, right?  Well here’s an easy way to do it.

I’m going to call this partial binding even though that’s NOT what it is.  I mean it’s partial binding relative to my programming effort — I’m only going to bind part of my Person object and someone else (ie the MVC framework’s built in binders) will bind the rest.

Here goes:

internal class PersonModelBinder : IModelBinder
{
public ModelBinderResult BindModel(ModelBindingContext bindingContext)
{
ModelBinderResult result = ModelBinders.DefaultBinder.BindModel(bindingContext);
Person p = result.Value as Person;

// What we just did was bind the Person’s non-complex types: e.g. the string of FirstName, LastName, and PhoneNumber
// all the stuff MVC would do anyway without any intervention on our part.


// Now all you have to do is add logic to populate the list here

return result;
}
}

Ok, so that’s easy.  How about populating the list?  Well one way of doing it would be this:

Suppose your textboxes were named like “txtFriend_1”, “txtFriend_2”, “txtFriend_3”, etc, then you could easily loop through the form variables:

foreach (System.String key in bindingContext.HttpContext.Request.Form.AllKeys)
{
if (System.Text.RegularExpressions.Regex.IsMatch(key, @”^txtFriend_\d+$”, System.Text.RegularExpressions.RegexOptions.IgnoreCase))
{
p.Friends.Add(bindingContext.HttpContext.Request.Form[key]);
}
}

You don’t have to use regular expressions of course.  Pretty easy, huh?

Stephen Bohlen rocks!

Filed under: Uncategorized — noahblu @ 4:41 pm

If you haven’t checked out Summer of NHibernate (http://www.summerofnhibernate.com/) or Autumn of Agile (http://www.autumnofagile.net/) you are really doing yourself a disservice. Stephen Bohlen runs through a great deal of topics and there’s something to learn for everyone even if it’s just seeing how another company (Microdesk) handles certain development tasks.

NHibernate Generic Dictionary Mapping

Filed under: Uncategorized — noahblu @ 4:38 pm

“NHibernate: have I told you today how much I love you?”

The other day I had a good reason to use a generic dictionary and I though hey, I wonder how I’ll get that working with NHibernate…  It’s very simple to persist Generic Dictionarys with NHibernate as I found out.  Let’s see an example…

.NET PSEUDO CODE

Take a person and his favorite or least favorite deserts:

First we have a Person object

class Person
———–
int PersonId
string FirstName
string LastName

And a desert object

class Desert
————
int DesertId
string Name

Now we want to associate a Person with all the deserts and rate how much he likes them (I know, it’s contrived, but it’s simpler than the business case I did this with yesterday).  So first we might create an enum for storing how one feels about a particular dessert (we’ll call it Rating):

enum Rating
———————–
Love,
Like,
Impartial,
Dislike,
Disdain

And then we might add a generic dictionary to the Person object like so:

class Person
———–
string FirstName
string LastName
Dictionary<Desert, Rating> DesertRatings

Looks good.  Now how would we use NHibernate to persist this?  First off, let’s make a slight change to the Person object because NHibernate actually uses IDictionary, so that last line of the Person object would look more like:

IDictionary<Desert, Rating> DesertRatings

DATABASE TABLES

To persist this data we first create the tables in our database.  Assuming we used the code above, I would probably create 3 different tables: 1 to store the Persons, 1 to store Deserts, and 1 to store the association between Persons and their Desert ratings.  The tables might look like this:

PEOPLE
PersonId, int
FirstName, varchar
LastName, varchar

DESERTS
DesertId, int
Name, varchar

PEOPLEDESERTRATINGS  (PersonId and DesertId will be the primary keys since 1 person can only have 1 rating for a specific desert)
PersonId, int
DesertId, int
Rating, int (this will store the integer representation of the enum)

NHibernate Mappings

Now behold the magic of NHibernate.  Let’s take a look at what the Person.hbm.xml file might look like:

<?xml version=”1.0″ encoding=”utf-8″ ?>
<hibernate-mapping xmlns=”urn:nhibernate-mapping-2.2″ assembly=”MyAssembly” namespace=MyNamespace”>
<class name=”Person” table=”People”>
<id name=”PersonId” column=”PersonId” type=”Int32″ unsaved-value=”0″>
<generator class=”native”></generator>
</id>
<property name=”FirstName” column=”FirstName” type=”String” not-null=”true” />
<property name=”LastName” column=”LastName” type=”String” not-null=”true” />
<map name=”DesertRatings” table=”PeopleDesertRatings”>
<key column=”PersonId” />
<index-many-to-many class=”Desert” column=”DesertId” />
<element column=”Rating” type=”MyNamespace.Rating” not-null=”true” />
</map>

</class>
</hibernate-mapping>

That’s all there is too it.  There’s a lot that I didn’t cover here, and this is NOT meant to be a tutorial on how to design your objects or tables.  However, I think I’ve shown how amazingly easy it is to persist this kind of thing using NHibernate.  I didn’t find anything online about this before I tried it and it worked the first time, but I would have felt more comfortable trying it had I seen someone else do it first.  So hopefully this post helps someone feel comfortable diving right in.

January 13, 2009

Review: Balsamiq Mockups

Filed under: Uncategorized — noahblu @ 4:11 pm

I’m not a designer, I can barely dress myself.  However, it is sometimes often necessary to create a UI mockup in order to communicate with the client.  We’ve all been there — the client gives you a pretty clear picture of how they want a widget to function and then they are disappointed when you build it to spec.  I think it’s usually that the client doesn’t have a clear vision, but let’s be nice and call it a problem with communication.  Creating UI mockups is a great way to show the client, in clear imagery, how exactly this widget will behave.

People typically do mockups in one of three types of applications — a drawing application (like Photoshop, Gimp, Paint.NET and a ton of others), a diagramming tool like Visio, or a tool specifically for UI design of which there are very few.  My feeling is that Photoship and its ilk are mad overkill for this type of thing and you need to do too much work to get anything worthwhile: how long does it take to create a tab bar in photoshop?  Now add another tab, how long did that take?  Well, you get the point.  Same goes for Visio.

I recently downloaded Balsamiq Mockups (http://www.balsamiq.com/) and have worked with it for a couple weeks.  Before this I was working with a 30 day trial of Mockup Screens (http://www.mockupscreens.com).  Overall I think it’s a Balsamiq is a terrific tool and it’s now my defacto UI mockup app. I have to give a disclaimer that I got a free license and so I have a monetary interest in using Balsamiq over Mockup Screens, but I really am being honest that it’s a sweet toy.

First let me introduce you to UI mockup applications in general, and then I’ll talk about what I like and dislike about Balsamiq vs Mockup Screens which seem to be the only 2 contenders in the inexpensive (less than $100) UI mockup space (both Balsamiq and Mockup Screens sell for $79).  The awesome thing about using either of these programs is that you can just drag and drop items (window, button, textbox, label) right into your design space.  From there you can re-size them and change their text.  Things like tables are handled very well in both programs where you can just type free-form text separated by a tab into it and the text will automatically be laid out as a table — what would this require in Photoshop?

Now onto the differences.  I really like the Balsamiq UI — it seems based on the idea that UI design is really easier done on paper.  The window has all the tools on top and then what looks like a pad of paper upon which you drag the UI elements:

Balsamiq UI

Balsamiq UI

(Image taken from their website)

Mockup screens is more computer-program-ish with a work area in the center, commands on top, UI elements on the left and element properties on the bottom:

275screen3small(Image taken from their website)

I found Balsamiq to be smoother and more natural to work with.  Mockup Screens is a little clunky in the way it organizes your mockups too.

One thing I find a little annoying about Balsamiq though is that it doesn’t have any right-click functionality (yet?).  In order to modify layers (move one back, etc) you have to mouse on over to your floating properties dialog.  That’s more mouse moving than I really want to do — give me right-click please!

Anyway, I would highly recommend either of these programs to any developer or designer.  They both have free trials so you can give them a try and see which one you like the most.  They are inexpensive and can save tons of time interacting with clients.  Now when a client asks me for a quote on a widget, I first send them a mockup before doing anything else.  There is inevitably something missing or not as they expected.

Blog at WordPress.com.