Fun with LINQ

Comments ( 0 ) Posted August 17, 2009 in Blog by Tieson

Anyone who's read any amount of the content I've put on this site knows I love the C# language. It has a lot of capabilities I find really useful, like strong-typing, properties, generics, etc. I also like that C# has implemented one of the more interesting technologies Microsoft has developed recently: LINQ. LINQ stands for Language-Integrated Query, and is a technology we can use to simplify the processing of structured data within the .NET Framework.

My favorite use of LINQ is building database applications. With LINQ, you simply build a database in MS SQL Server, connect to it in the Server Explorer window in Visual Studio, and then drag and drop your tables from the Server Explorer to a special DBML file. DBML, which stands for Database Markup Language, is used by LINQ to build classes out of your table definitions. The database column types are converted to their nearest C# equivalent. The fun comes together if you developed your database correctly using primary and foreign keys. If your tables were properly associated in the database, LINQ builds those associations into the LINQ classes it builds from the DBML file.

The LINQ classes have database connections built right into them. So, you can modify an instance of your class that was populated from the database, and then call a special LINQ function called SubmitChanges(). LINQ will process the class object and determine what attributes have actually been changed. It then builds LINQ queries to execute against the database, AND it only does that for the values that NEED to be updated. This can save you a lot of bandwidth over time.

Now, you could do this all by hand with the database tools .NET has had pretty much since the beginning. But WHY? LINQ does it just as well, and in a lot of cases BETTER, not to mention QUICKER.

LINQ also comes in handy if you want to parse a structured document, like, say, an XML file. LINQ has an entire library devoted to manipulating and creating XML documents. With a good understanding of building data structures, you can use XML and LINQ to entirely replicate the kind of functionality you get with a database.

The only major beef I have with LINQ is that you don't get something as nice as DBML for automagically transforming your XML into a class object. Which really makes sense if you think about it, since XML just defines structure, so LINQ would really have no idea what types to apply to your XML data, although it could be argued that other languages are capable of inferring data types from anonymous data.

Anyway, I think that is enough rambling for now. I should have some LINQ examples up sometime soon, hopefully before the month is out...

Cheers!

:]

Tags for this entry:

LINQ, XML, C#

Social media links:

Submit to Reddit! Stumble it! Bookmark at del.icio.us! Digg this! Add us to your Technorati favorites Tweet this! Share on Facebook

Comments for this entry ( 0 )

Login or register to comment.

Updated: August 19, 2009 ↑Back to top

Shorthand Properties = Lazy Programming, IMHO

Comments ( 0 ) Posted July 28, 2009 in Blog by Tieson

During an in-class demo of some code I had to edit (and that was inherited from a couple classmates), I mentioned that I really disliked some of the code my classmates had written. Namely, I didn't like their use of shorthand properties. C# lets you do something like this:

 
public type Name{ get; set; }
 

I don't like that. To me, it violates the encapsulation aspect of object-oriented programming. Encapsulation (in a nutshell) means you "hide" the attributes of your class, and limit the access of external code to those specific accessors you create. In C#, we can easily encapsulate by making our member variables private and then create public properties.

Now, with shorthand property syntax, you can skip creating member variables. The property itself becomes the member variable.

Let me repeat what I said earlier: I DON"T LIKE THAT.

So one of the guys I inherited this code from asked my why I don't like that syntax. I explained my idea of encapsulation (pretty much verbatium of what I wrote above) and also made a few more points.

My biggest beef with shorthand properties is that you can't perform any validation on the input value. The only guarantee you have is that the data is type-compatible (since otherwise the compiler will not build).

I much prefer standard syntax, which lets you do something like this:

        private int? majorID;
 
        public int? MajorID
        {
            get { return majorID; }
            set
            {
                if (value.HasValue)
                {
                    if (value > 0)
                    {
                        majorID = value;
                    }
                    else
                    {
                        throw (new Exception("'Major ID' must be greater than zero."));
                    }
                }
                else
                {
                    throw (new Exception("'Major ID' must contain a value."));
                }
            }
        }

As you can see, I can do a little bit of error handing very easily with standard syntax. To me, that is more than worth the small amount of extra typing involved.

What do you think? Let me know...

:]

Tags for this entry:

properties, C#, rant

Social media links:

Submit to Reddit! Stumble it! Bookmark at del.icio.us! Digg this! Add us to your Technorati favorites Tweet this! Share on Facebook

Comments for this entry ( 0 )

Login or register to comment.

Updated: ... ↑Back to top

W3Schools is teh awesomeness

Comments ( 0 ) Posted July 21, 2009 in Blog by Tieson

It is either really sad, or really stupid (and maybe both) that I spent most of one afternoon Googling like crazy to try and find a good partial-page-refresh script example, and the place I wound up actually finding the most useful tutorial was the last place I thought to look: w3schools.com

I've benefited greatly from the HTML and CSS tutorials w3schools has, but for some odd reason it never crossed my mind that they might just have what I needed. I had even remarked to someone else that they also had AJAX tutorials, but it never crossed my mind to check there first.

D'oh.

Anyway, I did find a damn fine tutorial, and it even included working code! All I had to do was change a few values in the JavaScript file, point it at a PHP script I had built, and viola! Partial-page-refresh! Wooooo.

The best part is that this script seems to work on every major browser (except, of course, IE6 and lower) (EDIT: It seems a png fix script I was trying out was what was keeping the partial-page-refresh from working. It does actually work on IE6), and (even better!) behaves almost identically in every browser I can test.

Those that are curious can visit my portfolio site to see my implementation, and visit the source page to see what the original looked like.

Cheers!

Tags for this entry:

AJAX, W3Schools, PHP

Social media links:

Submit to Reddit! Stumble it! Bookmark at del.icio.us! Digg this! Add us to your Technorati favorites Tweet this! Share on Facebook

Comments for this entry ( 0 )

Login or register to comment.

Updated: July 23, 2009 ↑Back to top

falcon1986 is my hero...

Comments ( 0 ) Posted July 11, 2009 in Blog by Tieson

I've been looking for ways to make this site more responsive (especially the aggregate pages, like Articles, Tutorials, etc.). Some of these pages can get pretty long, so anything that would optimize how they get from me to you is most welcome.

So I stumbled onto a page from the Yahoo! development team where they talk about compressing pages to save bandwidth. "Gee," thinks I, "that would probably help out a lot." So I go a-googling to see how hard it is to enable any sort of compression on a dynamic website.

Aaaaannnnd... I find lots of links to people complaining about how my host and others like it disable the compression functions on their Apache modules, since it's so CPU intensive.

"Poop!" says I. "There goes that idea..." (sadface).

Then I see this article. Hmm. Innntereeesting. Maybe this will work....

And it did! I modified the code I found there a bit to match what I am already using, uploaded my modified php.ini and .htaccess files, and, viola!, the site takes off!

We're talking compression ratios of over 75 percent, people. 75 percent!!! That means you dial-up users don't have to twiddle your thumbs (as much), and everyone else is getting a page that has an almost audible snap! as it loads...

So I'm very happy right about now.

:]

Tags for this entry:

optimization, Apache, mod_rewrite

Social media links:

Submit to Reddit! Stumble it! Bookmark at del.icio.us! Digg this! Add us to your Technorati favorites Tweet this! Share on Facebook

Comments for this entry ( 0 )

Login or register to comment.

Updated: July 13, 2009 ↑Back to top

Rolling, rolling, rolling...

Comments ( 0 ) Posted July 9, 2009 in Blog by Tieson

...Keep them blogs a'rolling, BLOGROLL!

Heh. Yeah, I'm a dork. What do you want? Shakespeare? NOT.

Anyway. What's been on MY mind lately is "how does one make an effective blogroll?" I had ideas, but I wanted to see how the rest of the blogosphere does it first. So, of course I fired up Google, thinking I should find some good articles relatively easily....

NOPE.

You would think something that is so common to so many web sites would have some pretty good resources. And, let's be fair, if you're using a "commercial" CMS package like Wordpress, Joomla!, or Drupal (to name a few), you are in luck. If, like me, you are writing your own CMS, it's not so nice. I only found one decent tutorial. Fortunately, it does prove the old adage that "quality" beats "quantity" every time, since it's a darn good tutorial.

So, look for the GNS blogroll to change sometime soon, once I've worked the bugs out of the new script. I think you'll like it.

:]

Tags for this entry:

blogroll, Google, scripts

Social media links:

Submit to Reddit! Stumble it! Bookmark at del.icio.us! Digg this! Add us to your Technorati favorites Tweet this! Share on Facebook

Comments for this entry ( 0 )

Login or register to comment.

Updated: July 9, 2009 ↑Back to top