May 2007 - Posts

Since I have Orcas installed, one of the first things I decided to experiement with is LINQ. 2.0 adds great support for generic collections, but often I find looking for a particular item in one a total pain in the ass. Traditionally, your only options are to iterate over each item or use an anonymous method or something. Thanks to LINQ, this is now an easy task.

Let's start by looking at this simple class below.

    public class MyClass
    {
        private int myInt;
        private string myString;

        public int MyInt
        {
            get { return myInt; }
            set { myInt = value; }
        }

        public string MyString
        {
            get { return myString; }
            set { myString = value; }
        }
    }

Then we'll add a few instances of this class to a generic collection.

List myList = new List();
myList.Add(new MyClass { MyInt = 1, MyString = "Blah 1" });
myList.Add(new MyClass { MyInt = 2, MyString = "Blah 2" });
myList.Add(new MyClass { MyInt = 3, MyString = "Blah 3" });
myList.Add(new MyClass { MyInt = 4, MyString = "Blah 4" });

Notice the use of the new object intiializer on the MyClass instantiation. So say you want to return all items where the value of MyInt is greater than or equal to 3. Before LINQ, you would probably be iterating manually through and emitting the contents into another list or who knows. Now, you can simply query it.

 var  = from myClass in myList
                        where myClass.MyInt > 2
                        select myClass;

Going line by line here. The first statement says where to get the data from, in this case the generic list myList. The value myClass is effectively an iterator and can be named anything (i.e.: i). The second line applies a where clause. MyInt (along with all other public properties) will be available for selection in IntelliSense through the use of reflection. The last line, tells it what to return. In this case, it returns the whole class. This could also be individual properties (i.e.: myClass.MyInt, myClass.MyString). In this case I am letting the return type be set implicilty using var. In reality the actual return type if IEnumerable.

Once you get the data queried, its easy to bind it to a grid or anything else.

GridView2.DataSource = neat2;
GridView2.DataBind();

Obviously this makes things easy. LINQ works on your own collections as well as directly against a database using LINQ to SQL. Is this necessarily the correct way to do things? Does it perform well? Will using LINQ deem you as a Cowboy Coder? Only time will tell.

Ok, well I finally got around to downloading all 6 GB worth of Visual Studio Orcas. I opted to do the regular install as opposed to the VPC image. As I have said in the past, I have used beta versions of previous Visual Studios and have never ran into any issues with compatibility or getting them uninstalled. Then again, some people aren't as daring as I am.

The only part I have really checked out so far is Web Applications. For the most part things are the same. After all there aren't any major changes in this version of the framework for ASP.NET aside from them adding AJAX natively. There are some minor differences in the IDE however. First thing that you will notice is when a page opens up, it is in the new Split View. This gives the ability to edit in either HTML mode or design mode at the same time. It automatically syncs up changes in either view as you are making them (although sometimes it doesn't and it wants you to click something). This I think is pretty neat. There is also a lot of new stuff for CSS but I have yet to really figure it out.

Another thing I had to try was the JavaScript IntelliSense. It works and it seems to work pretty well. This by far is a long awaited feature. This next feature may have been around in VS2005 but maybe not to this extent. You can now highlight a block of code and generate code metrics for just that block.

The last thing I want to point out is that when you create a new project/solution, there is now a dropdownlist where you select which version of the Framework to target (i.e.: 2.0, 3.0, or 3.5). This is very cool because you can take advantage of the new Orcas IDE feaures on your existing 2.0 projects.

I have just begun to take a look at the new features in the project. I will talk about more of them as I explore the product further.

In the past, to build a web part control in MOSS, you had no choice but to build a custom control inside a class library. That of course sucks because there is no design time support. Let's face it building complex composite controls using C# is a bad time. Well luckily, now there is something called Return of the SmartPart (not to be confused with CAB SmartParts). This nifty web part allows you to load any user control inside of a MOSS web part. On top of that, there is even one with ASP.NET AJAX support which automatically loads the necessry script manager.

Installation is pretty easy. Just follow the steps in the included document. Once installed you will be able to select it from the web part gallery. Then you just configure the properties to load the control that you want. One thing to note is that the SmartPart requires that the controls be located in a folder called UserControls off of the root. If the folder is not there, it throws an exception. A minor annoyance, but with the functionality it brings, that is tolerable.

So next time you need to build something in SharePoint go grab this and give it a try. Microsoft also plans on implmeneting something of their own, but now that this somebody else did the work for them, I don't think they are in a real hurry to do so.

Return of the SmartPart

Microsoft is finally working on an OR/M tool that looks pretty compelling called LINQ to SQL (previously DLINQ). Although it is data centric, out of the box, I think it supports some good features and is worth looking at. In the last week, Scott posted a series of articles on this, so I thought I would mention it here today. Instead of rehashing everything in those posts, I'll just point out a couple of features and you can go check it out for yourself. Then you can decide for yourself how it compares to other OR/M tools.

Out of the box it makes it really easy to design entity classes based upon an existing table structure. As expected, it automatically reads foreign keys to create relationships between your objects. It also has built in support for lazy loading. You can also easily specify your own stored procedures for doing inserts, updates, etc. Of course with all of this comes the ability to manipulate your entity classes using LINQ.

Take a look at the feature set and see what you think. It may be missing some things compared to other tools, but so far it looks extremely easy to use. Take a look at Scott's two posts on it and let us know your comments. My guess, is most people will like other tools better, but we'll see.

Using LINQ to SQL (Part 1)

Using LINQ to SQL (Part 2)

The final version of the IE Developer Toolbar has been released. Although this may not have quite everything that the developer toolbar does in Firefox, it is not bad. Specifically I like the way you select elements, can view the DOM, and see the styles applied (again yes I knew this was part of Firefox). By far the coolest feature I like is the Trace Style feature. After you select an element on the screen. Right click on any style and choose Trace Style. This will then take you to the exact place in the CSS where the style came from.

Go download it now. Chances are you will be using IE when developing something.

IE Developer Toolbar

So you finally got a project that is a good fit to use the ASP.NET 2.0 ProfileProvider for and you get all the entries needed in your web.config defined and now you are ready to use it. You open up the page where you intend to use your nice new profile class, start typing the letters P r o f only to realize the word Profile is not there. Why is this you wonder? You check the web.config and everything looks good, yet for some reason the Profile object is not there.

This is probably due to the fact that you are using a Web Application Project for whatever reason. The first thing to know about using WAPs is that they do not support dynamic compilation. The way the Profile object works behind the scens is by creating a ProfileCommon class dynamically using the properties you listed in the web.config. Since WAP doesn't support this, the Profile Object isn't there.

So what do you do? Obviously you can manually program against the ProfileInfo object, but who wants to do that. That removes half the fun of the new Profile functionality. Well after doing some research there is a workaround of sorts. There is a utility on gotdotnet (currently being phased out) called ASP.Net WebProfile Generator. This is a Visual Studio 2005 Add-in, that reads your properties in the Profile element of the Web.config and generates a class called WebProfile that you can use to access the Profile in a strongly typed manner. To create the class, simply right click on your Web.config and choose Generate WebProfile.

Imagine you have a Profile defined as the following.

<profile defaultProvider="MyProfileProvider">
      <properties>
        <add name="MyString" type="string" AllowAnonymous="true" />
        <add name="MyInt" type="int32" AllowAnonymous="true" />
      &/properties>
</profile>

In a web project, you would access the MyString property like this:

Profile.MyString = "Blah";

In a Web Application Project, you would access the MyString property like this:

WebProfile.Current.MyString = "Blah";

Here is the link download the ASP.NET Web Profile Generator.

ASP.NET Web Profile Generator

I have never really been a fan of the App_Code folder, but sometimes find it useful when prototyping something. Often however, I have wanted to know how to specify a type of something that is in the App_Code folder and did not know how. For example, maybe you have a custom ProfileProvider named MyProfileProvider sitting in your App_Code folder. How would you specify the path to that since there is no namespace probably and its not in an external assembly? Here is the syntax.

    <providers>
       <add name="MyProfieProvider" type="MyProfileProvider, __code" />
    </providers>

Last week Silverlight 1.1 Alpha was released and a key feature is that it allows you to use .NET code to interact with XAML. I immediately noticed that there was still a download for both Windows and MacOS. This of course made me wonder. Is it really allowing you to run .NET code on Mac OS? The answer is in fact, yes.

To quote Scott Guthrie on his post:

Silverlight 1.1 includes a cross platform version of the .NET Framework, and enables a rich .NET development experience within a browser. The total download size of the Silverlight 1.1 package (including all of the 1.0 features + CLR + a WPF and .NET FX library API subset + dynamic language support) is ~4MB - and it takes less than 20 seconds to install on a machine.

So there you have it. This is the beginning. I always thought it was a matter of time before we started seeing .NET stuff on other platforms. This is very interesting. It's not the complete framework, but it shows you it is possible and more than likely there will be more. Any thoughts on this? Anyone think they will bring Silverlight to Linux as well?

Scott Guthrie's Post on Silverlight

I am really surprised that it took me so long to make a post on this, but here it is. For the love of god, STOP USING HUNGARIAN NOTATION. You developers out there that still use it know who you are. For some reason you haven't noticed that your naming convention doesn't follow anything else that Microsoft is doing in their class libraries. Hungarian Notation has been considered bad form since the start of the .NET Framework and now is the time to learn the correct way to name variables.

To further prove my point, this article on MSDN states some common best practices. Guess what? "Do not use Hungarian notation" is listed right in the first paragraph. Other things that are recommended are using easily readable identifier names and to avoid using abbreviations or acronyms.

Lastly one thing to note is that they also recommend against using underscores and hyphens in variable names. I completely agree with this but I still often see private variables named in this manner in Microsoft code.

.NET Framework General Naming Conventions

I know not very many of you are using Vista to do your development work yet but I still highly recommend it for the simple fact that it allows you to have more than one web root now. Anyhow, If you have been working with Vista, you may have encountered the following error. "Unable to start debugging on the web server. An authentication error occurred while communicating with the web server. Please see Help for assistance." After searching through several blog posts, I finally found a solution that works, but it does make the site run in classic mode.

To resolve this, first make sure you have Windows Authentication installed in IIS (most likely you already do since you wouldn't have made it to this error likely in the first place). Next, go to your web site properties, click on Advanced Settings. Then click on your Application Pool and change the option to Classic ASP.NET AppPool. This should get you up and debugging in no time.

Also, if this error is not happening to you, as many other posts point out, be sure and run Visual Studio as an administrator (i.e. right click on icon and choose Run as Administrator).