12/31/2009

ASP.NET MVC – Do I Really Need It?

As a professional software developer, particularly of the Microsoft flavor, you get bombarded with “new stuff”. If you do not instill in yourself a certain discipline, you are sure to be brought down by the sheer complexity of tackling every “new thing” that is sent your way.

I say this from personal experience; I do not mean to be negative in any way in saying this. It’s just a fact of life. There are so many “CTPs” and new technologies being thrown at you that it is easy to succumb to “Beta-itis” – the debilitating compulsion to download and play with every new thing, to the point where your productivity as a software developer begins to suffer.

It’s even worse if you’re an MVP because when you go to the Summit each year, they throw a whole truckful of even more sexy new “Stuff” at you. Sometimes, its stuff that nobody else has seen before and you had to sign an NDA saying you wouldn’t talk or blog about it. Talk about getting excited!

ASP.NET MVC is an example for me. About a year ago, I was working for an IT consultancy and we got a new client who insisted on using their existing codebase which was totally based on ASP.NET MVC. I had to learn not only MVC, but StructureMap (which I really like, thanks to Jeremy Miller) and other cool goodies whose names shall go unexposed for purposes of brevity.

But during this entire exercise, it became obvious to me that I could have done everything they were doing using my perfectly good “Classic ASP.NET” skills. I could have minimized ViewState by keeping it on the server (and I am certainly not the first guy to write about this, here’s a newer one). In addition, in developing a smartphone based web UI for it, it became obvious to us that a lot of the “MVCness” was actually an impediment.

You could make the case for unit – testability, and I would respond that most ASP.NET applications get smoke-tested locally to the satisfaction of the developers before deployment. Don’t get me wrong – I am a big proponent of unit tests and TDD – having helped develop some 700+ unit tests for over 280 DAO’s and Model classes in a current project I am working on.

Purists will tell you that classic ASP.NET doesn’t offer separation of concerns, etc., and they are absolutely right. But classic ASP.NET also works fine, as long as it is understood and not abused by developers.

I’m hip with ASP.NET MVC – MVP buddy Rick Strahl says that his Codepaste.net site (which I think absolutely rocks) was his first venture into ASP.NET MVC. I commend him for it. But the fact is, it could have just as well been coded in classic “ASP.NET” and it would have looked and acted pretty much the same. You want JQuery? You don’t need MVC to use JQuery.

The bottom line is, after a year or more of having been exposed to ASP.NET MVC – and especially seeing various developers putting out their own MVC implementations that do not even rely on the Microsoft engine, because they feel it is flawed – I still remain unconvinced. Read the forum posts and see how much pain the average developer goes through to adopt this -- to do something they already know how to do.

I think the lesson learned for me is to carefully consider new technologies and spend some time objectively determining if they are right for you, rather than “jumping on the latest bandwagon” due to peer pressure. You may decide that you do like a technology, but you want to wait a while before you jump on.

I use LINQ, Entity Framework, LINQ to SQL, lambdas, NHibernate, and much more of the new “stuff” because the benefits are easy to see and I’ve been able to make a determination in my own mind that the learning curve is worth the effort. I'm glad ASP.NET MVC is out there - it's a quality offering. But I’m still not using it. Flame me if you will, I remain unconvinced.

NOTE: Interestingly, Donn Felker, whose work I particularly admire,  has a more recent post that is very similar to this, featuring a real-life example of his own. I hope you’ll read it. Bottom line? Get the job done, get paid, and go home to spend time with your family. Even “Mr. ASP.NET”, Scott Guthrie, has something to say about the controversy. His bottom line? “Respect the choices other people make”.

WebForms isn’t going away anytime soon. As a framework for developing ASP.NET applications, it has evolved into a rich set of tools, controls and features that share more of the same “pieces” with ASP.NET MVC than many developers are aware of.  So if you’re an MVC purist and you feel that WebForms make you feel “dirty”, knock yourself out. You aren’t going to impress me.

Have a happy New Year, do good, and be productive.

12/24/2009

SQL Server 2008 Fix: SP1 Install Failure

 

For quite a while I could not install SQL Server 2008 SP1, as it reported a failed shared component installation – in this case, Books Online. Problem is I could not find the source MSI to fix it.

So here is how I solved the problem:  If any shared components installations result in a failed state, a Registry key is updated. SP1 reads these keys when it starts, and if the value is not “1” (in my case it was “3”) – it will stop. Seems ridiculous to me, but that’s how they do it.

And doing a repair may not fix it either – because you may have installed Books Online from an interim standalone MSI installer. if you try to find the original MSI to handle the repair, it might not be the correct one.

Edit this key (or the respective key for whatever your fail message says) and ensure that the value is 1:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\100\ConfigurationState\Sql_BooksOnline_Redist

That took care of that problem! Now I have a different issue, but I’ll tackle that one too. Hey! Its us against them!

Another little Gem Department

If you have ever used Alt-PrtScn to get a quick screen capture of an image for pasting into an article or blog post, you may have brought up MS Paint to CTRL-V paste it in there. The problem is, you get your image – but the canvas size may be much bigger. If you forget to drag the sides of the canvas in to match the size of the image before you save it, you may be very disappointed at the results!

12/13/2009

Why the Economic Stimulus Plan Doesn’t Work

When President Obama was elected, the economy was already sick and getting sicker.

If nothing was done, his economic team said, the unemployment rate would keep rising, reaching 9 percent in early 2010. But if the nation embarked on a fiscal stimulus of $775 billion, mainly in the form of increased government spending, the unemployment rate was predicted to stay under 8 percent. Obama bought into a classic Keynesian solution – and so did Congress.

Congress passed a huge fiscal stimulus that focused almost entirely on government spending. Yet things turned out worse than the White House expected. The unemployment rate is now in the 10 percent range — a full percentage point above what the administration economists said would occur without any stimulus!

So what should they  do now? The administration seems  determined to stay the course, although recently, the president showed interest in “increasing the dosage” – a bad prescription indeed. A better approach might be to rethink the entire strategy from the git-go.

When concocting its fiscal package, the Obama Administration relied on conventional economic models based largely on ideas of John Maynard Keynes. Keynesian theory says that government spending is more potent than tax policy for jump-starting a stalled economy.

The fallacies of Keynesian economics were exposed decades ago by Friedrich Hayek and Milton Friedman. Ronald Reagan's decision to dump Keynesianism in favor of supply-side policies—which emphasize incentives for investment—produced a 25-year economic boom.

According to  Christina  Romer, now chairwoman of the president’s Council of Economic Advisers, each dollar of tax cuts has historically raised G.D.P. by about $3 — three times the figure used in the administration’s recent report. That is also far greater than most estimates of the effects of government spending.

In an October study, Alberto Alesina and Silvia Ardagna  of Harvard looked at large changes in fiscal policy in 21 nations in the Organization for Economic Cooperation and Development. They identified 91 episodes since 1970 in which policy moved to stimulate the economy. They compared the policy interventions that succeeded —  those that were followed by robust growth — with those that failed.

What did they find? Successful stimulus relies almost entirely on cuts in business and income taxes. Failed stimulus relies mostly on increases in government spending.

These studies and others point toward tax policy as the best fiscal tool to combat recession, particularly tax changes that influence incentives to invest, like an investment tax credit.  This really should come as no surprise, since small and medium size businesses are the engine that creates 80 percent of American jobs. Throughout modern history, it is small business that fuels jobs and growth – not government.  Yet, the Administration and Congress’ knee-jerk spending reactions so far indicate that they “still don’t get it”. If Congress and the President really wanted to get the economy moving and create jobs, they could repeal the capital gains tax. But, they won’t.

There is a growing body of evidence to support that Keynesianism is not an effective way to “spend your way” out of a bad recession. It’s time for our legislators and the executive branch to stop the spending foolishness. Ronald Reagan got it. All we have now is simply a recipe to repeat the Japanese “Lost Decade” of the 1990’s.

12/09/2009

Don’t Break the Interface

Recently while contributing to a test suite that covers some 250 C# DAOs (Data Access Objects) I discovered a couple of issues:

1) There was an implementation of “PrimaryKey” which is defined as a nullable long (long? PrimaryKey) that returned a 0 (zero) if the nullable type had no value. Uh-Oh!

2) There was more than one implementation, e.g., this actually appeared in more than one base class, in different assemblies. Depending on which references you had set, you could get clobbered!

Needless to say we’re going to fix that quickly; there are objects that are “older code” that need to be updated to reflect the change.

Fortunately our test suite will show immediately which guys need to be updated,underscoring the importance of having robust unit tests.

But the real question is, “How did this creep in”? The answer: Most probably, "Stinkin' Thinkin'" by developers.

Nullable types are instances of the System.Nullable struct. A nullable type can represent the normal range of values for its underlying value type, plus an additional null value.

For example, a Nullable<Int32>, pronounced "Nullable of Int32," can be assigned any value from -2147483648 to 2147483647, or it can be assigned the null value. A Nullable bool can be assigned the values true or false, or null. The ability to assign null to numeric and Boolean types is particularly useful when dealing with databases and other data types containing elements that may not be assigned a value. For example, a Boolean field in a database can store the values true or false, or it may be undefined.

An example from the MSDN documentation:

Class NullableExample
{
static void Main()
{
int? num = null;
if (num.HasValue == true)
{
System.Console.WriteLine("num = " + num.Value);
}
else
{
System.Console.WriteLine("num = Null");
}

//y is set to zero
int y = num.GetValueOrDefault();

// num.Value throws an InvalidOperationException if num.HasValue is false
try
{
y = num.Value;
}
catch (System.InvalidOperationException e)
{
System.Console.WriteLine(e.Message);
}
}
}

The above will display the output:

num = Null

Nullable object must have a value.

Nullable types have the following characteristics:

  • Nullable types represent value-type variables that can be assigned the value of null. You cannot create a nullable type based on a reference type. (Reference types already support the null value.)

  • The syntax T? is shorthand for System.Nullable, where T is a value type. The two forms are interchangeable.

  • Assign a value to a nullable type in the same way as for an ordinary value type, for example int? x = 10; or double? d = 4.108;

  • Use the System.Nullable.GetValueOrDefault property to return either the assigned value, or the default value for the underlying type if the value is null, for example int j = x.GetValueOrDefault();

  • Use the HasValue and Value read-only properties to test for null and retrieve the value, for example if(x.HasValue) j = x.Value;

    • The HasValue property returns true if the variable contains a value, or false if it is null.

    • The Value property returns a value if one is assigned, otherwise a System.InvalidOperationException is thrown.

    • The default value for a nullable type variable sets HasValue to false. The Value is undefined.

  • Use the ?? operator to assign a default value that will be applied when a nullable type whose current value is null is assigned to a non-nullable type, for example int? x = null; int y = x ?? -1;

  • Nested nullable types are not allowed. The following line will not compile: Nullable<nullable> n;

The bottom line? Good communication between members of the developer team ensures consistency and that the interface won’t be broken. This includes the mistake of having base types or interface members declared in multiple places -the functional equivalent of "breaking the interface". And, it’s important to understand the “Nullable Object must have a value” exception and why it happens, cryptic as the exception may seem.