Live Blogging VSLive! Austin – Day 4
Just about to start a Post-Conference workshop entitled “Advanced C#: Moving up to LINQ, VS2008, and .NET 3.5” by Richard Hale Shaw. Richard is extremely knowledgeable and has a tendency to go into deeper detail than anyone else, so this should prove to be a challenging class. Watch for updates throughout the day.
Wow! I can’t wait to get my hands on VS2008 and .NET 3.5! Lots of stuff going on: we’ve been covering Custom Iterators again, which is pretty cool in its own right but has been around since 2.0. We’ve been covering them, though, in the context of Extension Methods which are HOT. Keep watching this site, you’ll see lots of stuff coming up in the future about Extension Methods.
I do have a couple quick bullets that came up:
- DataContext is the key object type in LINQ
- DataContext has a .Log method that will reveal the actual SQL that was executed
- LINQ for SQL only works against SqlServer 2000+ : more evidence that we need to switch to SqlServer
- Compiler Inference is very cool and reduces a lot of code. For instance: var allows the Compiler to infer the variable type from the return object and Anonymous Methods can be replaced with Lambda Expressions.
- var will only work for local variables
Lambda Expressions are something else I’ll be exploring and posting about in the near future. Essentially, a Lambda Expression is a syntax method of creating Action<T> and Predicate<T> objects (there is another but I don’t recall its name). These objects are used by LINQ and some other constructs. Without Lambda expressions, these could be created using anonymous methods or by outsourcing the method call signature. Trust me, for most applications Lambda Expressions will be much better. There is too much about them to show how they work here, so here are a few bullets with the caveat that if you have not seen Lambda Expressions they won’t mean much for you:
- Lambda Expressions can consume outer variables
- Lambda Expressions can contain embedded code
- Lambda Expressions can be used in LINQ or anywhere else that the return types are needed
- Very readable, once you understand the syntax
- Scope for Lambda variables is protected
- Here is a sample line using a Lambda: .Filter( f => f.Length > fileSize )
- In this case, “f” is a variable that lives only inside the (). “=>” is the Lambda Execution operator. “fileSize” is an outer variable (defined and populated elsewhere in the scope)
- Custom Iterators, and as such LINQ query objects, employ “Deferred Execution”. In other words, the actual IEnumerable<T> return value is not populated until the object is used (as opposed to when it is defined)
- Extension Methods can be used in LINQ alongside the system defined actions
- In VS2008, you can step through .NET Framework code in DEBUG (I believe there is an additional install to make this work)
- LINQ can create Anonymous Types: in other words, the resulting IEnumerable<T> of a LINQ statement can be of a Type and structure that is not a defined type. This is very cool!
- Properties in Anonymous Types can be based on Method call results
- Properties in Anonymous Types can be named as desired
- Properties in Anonymous Types can be other Complex Types
Lots and lots of details, and my seat is getting really sore, but I am here until the bitter end! Here are a few highlights from the last section:
- LINQ sequences (Richard’s name for the resulting IEnumerable<T> collections) can be combined using .Concat() as long as the resulting Types are the same
- This is even true for Anonymous Types: as long as they are defined exactly the same
- LINQ can group results: this ends up in a IGroupedEnumerable that holds a set of Key Value pairs of the group key and IEnumerable<T>
- The LINQ group feature can be based on an anonymous type (defined as part of the group statement)
- Not LINQ related, but Richard mentioned something about using Partial classes as a way to protect custom code from Code Regeneration – great idea that also solves a problem I’ve had in my own CodeGen of ensuring that variables were defined as protected instead of private (since my system inherits from the generated abstract class) Update: I spoke with Richard after the workshop about employing this as a thin abstraction layer for DAL objects (Data Access Layer) and he suggested this would not be a good approach for that application.? And I may have misunderstood what he was saying in the first place, but I do think it led me to the idea that Partial classes could be a solution to this problem.
The last section was mostly about XLINQ (LINQ to XML), and we have plenty more to cover. The new XML classes are very easy to use and intuitive:
- Essentially an OO wrapper around XML
- XElement is the key class and represents an XML element
- XElement has an “Elements” Collection of child XElement objects which is an IEnumerable<XElement>
- This means that we should be able to use LINQ over the Elements collection
Down the home stretch. These are definitely long days, but well worth every minute.
I want to clarify something from above: the classes I mention for XML are XLINQ. These classes are found in System.Xml.Linq. After the XML is read in to these objects, then LINQ to Objects can be used as before. So XLINQ is LINQ, it just doesn’t look like the other flavors of LINQ.
Also, Richard showed a nice method of creating and storing much cleaner XML code based on defined classes behind the scenes. Then the Read and Write methods are abstracted out using Extension Methods and Generics. Very clean and powerful, and overall a fairly straight forward and simple approach.
Well, this is the end of a great week. If you ever get a chance to attend one of these events, by all means you owe it to yourself to go. Here are the technologies I will be exploring in great detail as soon as I get back to the world:
- LINQ – this is the KING of all the new MS technologies
- WPF – simply awesome: a new paradigm in GUI design with great promise
- SilverLight – a whole new web experience, both for developers and consumers
- IIS 7.0 – looks substantially easier and more functional
- Entity Data Model – not ready yet but will be soon and another paradigm shift
- Visual Studio 2008 – should be RTMing this month. If you want to do any of the above, you’ve got to have the right tools.
I’m sure there’s more, but I am brain fried and butt dead. Check back often, I expect lots of new posts in the near future as I work through all this stuff.