Friday, 30 September 2011

MVC model binding not setting values

As part of our push towards using the latest and greatest technologies, we have a couple of small web solutions using MVC rather than Webforms. A problem came up recently for one of our devs when trying to model bind a post from a form. The model being passed in to the action method on the controller was chock full of default values for the data types in question, rather than taking the values that the form was providing. Quite a bit of time was spent by him staring at the code trying to figure out why it didn’t work. This was followed by quite a big of time with me staring at his code. Googling for the issue didn’t seem to show up anything obvious either.

Wondering if he’d managed to break the routing setup or something else low-level and fundamental to MVC working correctly, I took a look at the log on code in the template MVC project and saw that it worked just fine. After more staring I finally realised what the problem was. The following code shows essentially the same very very basic model class, firstly with code that doesn’t model bind, secondly with code that does:

public class BadModel
{
    public int MyValue;
}
public class GoodModel
{
    public int MyValue { get; set; }
}

Pretty subtle huh? Trust me when I say that it is far subtler when you don’t have the good and bad code stacked together to see the blatant line length disparity that makes the property stand out. Granted, best practices say that we should always hide member variables behind property getters and setters so you’d hope not to have such a situation arise. However, in day to day usage with VB or C# property and field access does tend to feel identical. Yeah there are multiple methods made when compiled to IL, but Visual Studio hides these from us so it’s not hard for a dev to leave direct access to fields available. So remember, public fields are bad, mkay.

Sunday, 4 September 2011

Dolby Prolog-ic? It's day 2.

So, to begin with, day 2 of Prolog is still looking like very much the same logic engine rather than something that feels like a full programming language to me. We kick off with recursive rules for working out the ancestry of members of the Waltons. The recursion gives a nice flow to the whole process of defining such a rule, and it is a concept that I'm very used to from my day-to-day coding. Infact, I'm ashamed to say that when I attended J.P. Boodhoo's Nothin' But .Net course and was tasked with writing an app in our own time that could list the contents of a directory tree without using recursion it took me a looooong time to figure out how to do it, because recursion is such a nice and natural fit.

Next up is a demonstration of lists and tuples. The list is obviously a key data structure in my daily .Net (ahhh, List<T> how I love thee) and tuples get mentioned time to time in the more cutting edge sections of the blog-o-sphere, and I recall the term from my comp sci degree, but they're not something that I've had much use for. This was different to the usual examples in the book as rather than write a small app with some pop-culture theme, it was just bashing values into prolog to see what happens when you tell it (1, B, 3)=(A, 2, C). or [4, 5, 6] = [4, [Head | Tail]].

Then we were back to the more traditional programming of write a file and then use it. In this case it was using recursion to write routines to count the number of elements in a list, sum the elements in a list, and using those 2, work out the average value in a list. This is definitely different to the normal ways of coding, but it does feel a bit convoluted, especially in these post-Linq days where any old enumerable collection of data lets you get a count from an extension method, or even pre-Linq where a typical collection class would have a count that it could tell you easily.

The last lesson of this section takes a look at the Prolog method "append" and then walks through writing it from scratch. This ends up as merely a 2 line function, however it does get a bit mind bending with its use of recursion. My initial reaction is that I understand it, but I don't want to have to write it myself. The thing is, the next part of the chapter will get into more exercises, so I may have to...

And I was right to be suspicious. The first of the exercises was to write a function to reverse the elements in a list which leads to a very similar function. I actually managed to get to the right answer pretty quickly, but as a Prolog amateur I failed to put square brackets around a variable that needed them and the code didn't work. There was nothing obvious pointing to why it failed, so fixing it revolved around trial, error, and headbutting the desk. The first sample here doesn't work, the next one does. Simples.

rev([],[]).
rev([Head|Tail], Revd) :- rev(Tail, Revd2), append(Revd2, Head, Revd).

rev([],[]).
rev([Head|Tail], Revd) :- rev(Tail, Revd2), append(Revd2, [Head], Revd).

The next exercise asked us to find the smallest element in a list. I couldn't think of a way to do this without employing a conditional statement, which doesn't really seem very much like idiomatic prolog from what we've been shown so far, but a quick google lead me to a helpful post on stack overflow with this snippet "( condition -> then_clause ; else_clause )" after which it all fell into place. The last exercise was to sort a list. I stared at this for a while, started thinking of ideas that might work but would quickly get convoluted and huge, and rapidly lost enthusiasm. I decided to have a quick google and saw that other people were finding this one a blocker too, and that solutions they'd eventually come up with, or simply copied from elsewhere on the net were as nasty as I'd expected, either longwinded or mindbending, so I decided to cease my pursuit of that final goal there.

This chapter was a pretty heavy load of info, so again it didn't have the same fun vibe of some of the earlier writing, but it did flow well and didn't ram the americanisms down my throat that the previous chapter did, so it is approaching a return to form. Hurrah.

As an aside, whilst I'm doing this all on my mac lappy with the terminal, I'm getting to play around with Vim. It always takes me typing a word or two into a new document before I remember that the stupid thing has different modes for navigation and text entry. Bah. But once I've been reminded by the distinct lack of text appearing, or the end of a word after a vowel suddenly flitting onto the screen, I do like it :) I've tried using ViEmu in Visual Studio, but found that you need to use the escape key way too much in Studio so it clashed with the Vi functionality. So getting to use it a bit here is fun :)

 

 

 

Thursday, 1 September 2011

Visual Slowdio 2010

Recently I’ve been finding Visual Studio 2010 to be having something of a performance problem. We’re not just talking slow here, it would just hang for seconds at a time when doing seemingly trivial things, like clicking into the app to change some code after just looking in a web browser, or switching to a different file in the solution. You know, simple every-day stuff.

The first though when faced with such issues is to try and switch off any extensions that are installed and might be causing issues. I’m a big Coderush fan, but I’d rather keep that around for now. I’ve been playing with ViEmu, but turning that off had no effect. Next up I spotted an installation of TestDriven.Net that I’d installed purely for doing a demonstration to my team based on things I covered on the J.P. Boodhoo Nothin but .Net course earlier in the year. That seemed to have 2 different entries in the Add-in Manager, “TestDriven.Net 3.0 Personal” and “TestDriven.Net Reflector”. Turning both of these bad boys off has seeeeeemed to fix the problem.

I’m not trying to criticise TestDriven here, it may be a combination of MsTest, Coderush’s test runner and TestDriven all getting into fights over my code. I’ve had it installed and forgotten about for months and the problem only happened recently, so it may have been an update to Coderush or Studio that sparked it off. Anyways, so far it looks like I have a somewhat more responsive IDE again now. Hoorah :)

Whilst that has reduced the problems, there are still times that Studio decides to have a little nap, albeit less frequently. So I’m compiling a list of links that have tips for speeding it all up here as a bunch of resources to churn through when I have time: