LINQ-To-SQL DataContext and NullReferenceException

2009 June 13
tags: , ,
by r.claypool

LINQ classes generated by the VS designer (or sqlmetal.exe) are marked partial by default.

[System.Data.Linq.Mapping.DatabaseAttribute(Name="MyData")]
public partial class MyDataDataContext : System.Data.Linq.DataContext
{
    // blah ... blah ...
}
public partial class MyDataDataContext
{
    // My additional methods, constructors, etc.
}

As I’ve shown above, this allows you to extend a class without modifying the original code. The additional class definitions just live somewhere else, typically in another file, and the compiler integrates them as needed. That’s great for keeping your code out of the machine generated file, but you still need to be aware of what the machine generated code is doing before you extend it! One thing to check is the default (sqlmetal generated) constructor when you are planning to overload it with your own. In some cases, the default constructor will initialize dependencies, so if you write an overload, make sure it calls the default constructor too:

public partial class MyDataDataContext
{
    public MyDataContext() : this() // We must call the designer generated constructor because it contains initialization code.
    {
        // blah ... blah ...
    }
}

Notice how I used the constructor initialization keyword : this().
From the C# Specification Section 10.10.1

“An instance constructor initializer of the form this(argument-listopt) causes an instance constructor from the class itself to be invoked.”
If your DataContext is throwing a NullReferenceException “Object reference not set to an instance of an object”,  make sure you are calling the default constructor.
Don't forget to call the default constructor!

Don't forget to call the default constructor!

Happy Coding!

Let’s All Learn from Stackoverflow, Please

2009 June 4
by r.claypool

I tried to leave feedback on Soma’s most recent post, but apparently the comment system thought I was long winded.  After being redirected to the home page a few times (without any indication if the comment submission was successful),  I reentered most of my thoughts in multiple short segments.  Well, the end result was not exactly what I wanted, and I do own a blog, so here they are in full with a little extra elaboration.  somas-blog-logo

Hi Soma,
Thank you for msdn low bandwidth mode.

My recommendation for user content?  Copy Stackoverflow!

Forums such as http://forums.asp.net/ could really use an upgrade, otherwise the communities might just migrate to stackoverflow.com.  It is already a far better application than the MS hosted forums I’ve used.  Please take a serious look.

Maybe you could just buy the source and incorporate it into MS sites, or maybe it will go open-source. I’d love to hear your thoughts on this.

Developers spend a great deal of time digging through forums and A LOT of time is spent reading messages that are not helpful. Sometimes the threads are dated. (How many times have I found “a solution” only to realize that the discussion applied a beta version that is now RTM?)  Sometimes they are junk. (How much bad programming advice is scattered throughout every programming forum?)  Sometimes a great concept is poorly worded from a non-native English speaker.  Sometimes the thread jumps around from one topic to the next and the “real answer” is on page 5.  Sometimes the answer doesn’t come up in a web search because the forum is not SEO. Sometimes a meta-discussion (comments about the thread) clouds the real topic at hand. Etc, Etc.

The design at stackoverflow has solved many of these issues, and I wish MS forums would do the same.  Effective documentation needs effective organization and that is one thing that stackoverflow has really nailed when it comes to user generated content.

It’s great that we are seeing performance improvements in tools like VS 2010.  Better organization of user generated content and .Net community discussions could be an even bigger productivity boost for all of us.  Please spend just as much effort in this area.

Do you agree?

Thanks for the great work, Robert Claypool

stackoverflow_is_awesome

So what is so great about stackoverflow?

  • It’s CC-wiki (And now a data dump torrent)
  • It’s fast. (But they only use a few servers)
  • No sub-forums.  (Just tag your questions and everyone (including Google) will find them just fine)
  • Reputation earns privileges. (The site gains more administrators the more it is used)
  • True OpenID support. (Not a provider-only half implementation)
  • No stupid signatures at the bottom of every post.
  • No “please mark this as the accepted answer” posts.  (You’d be modded down for that)
  • Meta-discussion and clarifications have a clear home. (comments)
  • Good answers bubble up, bad ones are pushed down.
  • Other (trusted) people can clean up your post.  (Its a thing of good if u speak new to englissh)
  • and lastly …

All programming questions are fair game. Today you can ask “how to setup Tomcat 5.5 for ArcIMS 9.2″ and tomorrow you can ask “what’s the big deal about Dependency Injection” after answering a few questions about LINQ and C#.  No one will ever say “Excuse me, you are on the wrong forum and I’m moving (or closing, or ignoring, or down modding) your question. “

Maybe Microsoft shouldn’t try to keep up with stackoverflow.

Come to think of it, maybe Microsoft **shouldn’t** try to keep up with stackoverflow.  I’d much rather have those forums dry up and move to an open site like stackoverflow.  Now that would be awesome!

What do you think?

Deploying Silverlight 2 on IIS

2009 May 14

Silverlight 2 deployment on IIS 5.1 or 6 requires the addition of 2 MIME types in the web server’s configuration:

  • .xap application/x-silverlight-app
  • .xaml application/xaml+xml

Adding them to IIS is easy:

  1. Right click on the website and select “Properties”
    iis-default-website-properties
  2. Choose the HTTP Headers tab.
    iss-mime-mapping
  3. Click the “File Types…” button.
    (It is labeled “Mime Types…” in IIS v6.0)
    iss-mime-types
  4. Add the types.
    adding-xap-mime-type adding-xaml-mime-type

Accelerating Future, Part 2

2009 May 4

In part 1 of this series, I explained that exponential progress in computing has been going on for a long time.  Gordon Moore saw the trend over 45 years ago, and industry experts believe it will continue for at least another decade or even much longer than that.

I also made the case that this trend is not limited to computing. Progress in other areas has driven the worldwide average standard of living much higher today than just 50 or 100 years ago.  Humanity is better off [year after year] in just about every measurable material way.

Maasi using modern technology.  Used with permission by MarkKelley.

Modern technology in the 3rd world. Used with permission by MarkKelley.

We are at a significant point in history. Great advances in computing, nanotech, biotech, robotics, energy, artificial intelligence and communications are occurring every day.  Most do not make the mainstream news, but their cumulative effect will raise our standard of living more rapidly than ever before.  I do not predict a completely Utopian future (we will discuss the increased dangers in a subsequent post), but I do believe most people will have the chance to live in far greater prosperity if we avoid the biggest risks.

Beginning with this post, I want to point out real world examples of accelerating change.  I think you will see that Moore’s Law is not just an isolated trend confined to the world of geeks.  It is something that will affect your life on multiple fronts, regardless of who you are.


Human genome printed, Wellcome Trust

Human genome printed, used with permission by JohnJobby

The fusion of health care and information technology – i.e. biotechnology – is experiencing exponential growth.

The fusion of health care and information technology – i.e. biotechnology – is one of many industries experiencing rapid and (by some measures) exponential growth.

Biotech could be the most promising and dangerous technology for humanity, but how soon will such things as personal DNA analysis and real time biometric monitoring become commonplace?

Consider the Human Genome Project (1990-2003):   One billion DNA base pairs were sequenced in the first 4 years.  Another billion base pairs were sequenced in the next 4 months and 1.5 billion base pairs were sequenced in the last 4 weeks. [source]

By the end of this year, you can have your entire genome sequenced for just $5000.00 and results will be ready in a few days.  By 2014, genome sequencing is projected to cost less than a nice pair of jeans and results will be ready in about 8 hours.

years > months > weeks > days > hours

See the trend?

The first genome cost 2.7 billion US dollars.  This price dropped to $2,000,000 by 2007 and $1000 is coming in 2009 or 2010.

$,$$$,$$$,$$$ billions > $,$$$,$$$ millions > $,$$$ thousands > $$$ hundreds

See the trend?


Does the promise of a biotech revolution sound like science fiction?

Personal computers (that calculate billions of instructions in one second) and personal GPS devices (that pull data from satellites 12,000 miles above the earth) would have sounded like science fiction only a few decades ago.  Does the promise of a biotech revolution sound like science fiction today?

I’m excited about the rapid price drop in genome sequencing, and I’m especially looking forward to the breakthroughs in medicine that DNA analysis will bring.  Once millions of people can afford this technology, we will have vast amounts of data to find new treatments on everything that ails us.  But what about the risks?  How will we deal with this information getting into the wrong hands?  How will we protect our privacy and how will we prevent genetic discrimination?  These issues have not been widely discussed, but you can bet the $100 genome will get lots of people talking.


UPDATE: May 11, 2009 — Singularity Hub is reporting on a real time, biometric monitoring device that is now FDA approved.  Niche market today… mainstream tomorrow!

UPDATE: August 10, 2009 — Another benchmark has been reached : http://arstechnica.com/science/news/2009/08/human-genome-completed-using-one-machine-for-four-weeks.ars

Accelerating Future, Part 1

2009 March 4
by r.claypool
IBM-PS1, modified by r.claypool and used with permission by Kungfoocow369

A typical IBM-PS1 could store 713 million bits and process 8.5 million instructions per second.

My family brought home our very own IBM PS/1 in 1992.  This small box could store 713 million bits and calculate 8.5 million instructions per second.  It was 10x faster than enormous supercomputers built in the 1960′s, and no computer this compact and powerful was available in the 1970′s and early 80′s.  Like any other “appliance”, we thought this machine would last at least a decade.  After all, the sales associate said it was “the first and last computer we would ever need”.

Half a dozen computers later, I wonder what ever happened to that old PS/1. We really didn’t keep it or any other computer around for more than a few years. Something always pushed us into getting a replacement.  Something made them useless enough to warrant another big purchase on another new system.  Was it too much dust in the keyboards? Faulty hardware? Break-ins from envious neighbors? No. The answer has everything to do with Mr. Gordon Moore.


In 1965, IBM co-founder Gordon Moore predicted that integrated circuits would double in complexity about every two years. His prediction went relatively unnoticed at the time, but it is now considered one of the most significant statements in the history of information technology. That’s because Moore’s prediction has held true for 45 years with no signs of decline for at least another 20 years (2029).

Moore’s prediction has held true for 45 years with no signs of decline for at least another 20 years.

The original prediction (also called Moore’s Law) was strictly about integrated circuits, but we now use the term to describe exponential growth in all areas of technology:  Everything from Internet bandwidth to the efficiency of solar panels, DNA sequencing to location services, robotics, and nanotech have followed an exponential growth trend. Many of these technologies are still in their infancy, but that does not mean they are 75 or 100 years from making a profound impact on your life.  Exponential growth is explosive once the doubling factor is noticeably large.

Consider this graph.  It shows the growth of processing power for general purpose computers since the early 1970′s.

mips-chart
It appears that little or no progress was made for the first 30 years, but in the last 10 years we jumped from almost nothing to over 70 thousand million instructions per second.

Now look at the very same data on another graph.
The y-axis is set to display exponential data in a linear fashion using log base10.

mips-chart-log-base10
This view shows that the first 30 years did indeed have steady growth. We doubled from 1 to 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, … to over 70 thousand — in about 35 years.

There are 3 important points we should notice about this:

  1. The gains we have made in computing are not something new.
    – We have made steady progress for at least 35 years (and actually much longer that that).
  2. This growth is a much bigger deal today than it was 10, 20 or 30 years ago.
    – Every 2 years we gain as much as all previous years combined.
    – Every doubling from this day forward brings an increase of at least 70 billion instructions per second.
  3. Progress in computing is not isolated.
    – These are real gains that have a real impact on our standard of living.

Where might this lead us in the next 20, 30 or 40 years?

Where might this trend lead us in the next 20, 30 or 40 years? It is a difficult question that most people have not even begun to ask, but that is exactly what I will address in subsequent articles.

I will give you a new perspective that is based on solid data and reasoning that most people completely miss.  You will see (among many things) why expecting oil demand to double by 2050 is pretty unrealistic.  You will see that most predictions about 2050 are completely wrong just because people don’t foresee the second half of the chessboard.  It is a simple mistake.  We tend to expect the next 20 years to change about as much as the last 20, but in truth it will change exponentially more.

Stay tuned for some very interesting commentary on this accelerating future, and in the meantime, check out a video that shows exactly what I mean.

This work by Robert Claypool is licensed under a Creative Commons Attribution 3.0 United States.