March 2009 Entries

Know thy framework System.xml.XmlConvert

Posted Friday, March 27, 2009 5:55 PM | Feedback (1),

How to use System.Xml.XmlConvert

ASP.NET MVC 1.0 Released!

Posted Wednesday, March 18, 2009 11:04 PM | Feedback (1),

Microsoft has just released the 1.0 version of their ASP.NET MVC platform. Go get it at http://www.microsoft.com/downloads/details.aspx?FamilyID=53289097-73ce-43bf-b6a6-35e00103cb4b&displaylang=en

Overview from the site:
ASP.NET MVC 1.0 provides a new Model-View-Controller (MVC) framework on top of the existing ASP.NET 3.5 runtime. This means that developers can take advantage of the MVC design patterns to create their Web Applications which includes the ability to achieve and maintain a clear separation of concerns (the UI or view from the business and application logic and backend data), as well as facilitate test driven development (TDD). The ASP.NET MVC framework defines a specific pattern to the Web Application folder structure and provides a controller base-class to handle and process requests for “actions”. Developers can take advantage of the specific Visual Studio 2008 MVC templates within this release to create their Web applications, which includes the ability to select a specific Unit Test structure to accompany their Web Application development.
The MVC framework is fully extensible at all points, allowing developers to create sophisticated structures that meet their needs, including for example Dependency Injection (DI) techniques, new view rendering engines or specialized controllers.
As the ASP.NET MVC framework is built on ASP.NET 3.5, developers can take advantage of many existing ASP.NET 3.5 features, such as localization, authorization, Profile etc.”

Grouping with Linq Igrouping

Posted Friday, March 13, 2009 8:13 AM | Feedback (5), Filed Under .NET C# developement Linq

Now a days no one uses arrays any more, the reason why, because they are a pain to work with and seriously, why would you when we have the generic List<T> collection.
If you are using object collections that implement IEnumerable like List<T>, List(Of T)  in VB, you are in a world of joy. And if you are using Linq then you are in a world of joy in Christmas!

If there is one technology that have had an impact  on my coding then it would be Linq. Things that where a pain to do like parsing an XML file and doing loops and if statements for selecting elements from an array have been reduced to some lines of code.

A simple example of this would be to select a subset of objects from a list. Say we have an instantiated collection of car objects from our car database, (why always cars??).

 

  1. public List<Car> GetBySoldYear(DateTime year){   
  2.     var cars = from c in GetCarList()   
  3.         where c.YearSold.Year == year.Year   
  4.         select c;   
  5.     return cars.ToList();   
  6. }  

 

This snipped returns all cars that where sold a specific date. So instead of iterating the collection doing if checks and copying the matching results to a new list, we simply express our feelings in a nice SQL like syntax. Well the hole select statement is in reverse but you get it. The reverse thingee is so that the compiler has a chance to serve you with some nice intellisense as you type. That you don’t get from the SQL query editor!

Now for another scenario, this is maybe more uncommon but when you get there, Linq is to the rescue. I’m talking about when you want to get a grouped result, like, group the cars by year sold.
Now when doing this, if you are used to the SQL results from a group by query, this is nothing like it. The result from a group by is a flat table. With Linq grouping you get a IEnumerable<IGrouping<Key,T>>, where IGrouping<Key,T> is a collection all by it’s self. The Key is the value you did the group by on and T the actual object that matched the criteria. 
in this case the result is a IEnumerable<IGrouping<DateTime, Car>>. The code for this query looks like this

 

  1. public IEnumerable<IGrouping<DateTime, Car>> GroupedByYear(){   
  2.     var carlist = GetCarList();   
  3.     var grouped = from car in carlist   
  4.                   group car by car.YearSold;   
  5.     return grouped;   
  6. }  

 

 

If you would like to get the result of this you simply iterate through the IGrouping<DateTime, Car> collection, like so:

  1. public void PrintModelsPerYear()   
  2. {   
  3.     foreach (IGrouping<DateTime, Car> cars in GroupedByYear()){   
  4.         Console.WriteLine("YearSold: {0} - Count {1}", cars.Key, cars.Count());   
  5.     }   
  6. }  


Remember that the Key will be the value of the filtered group, so the output will look like this:
YearSold: 2009-01-01 00:00:00 - Count 2
YearSold: 2005-01-01 00:00:00 - Count 2
YearSold: 1985-01-01 00:00:00 - Count 1

Here you can download the code for this post

Technorati-taggar: ,,,,

Free Nerd dinner MVC tutorial E-Book

Posted Wednesday, March 11, 2009 6:06 PM | Feedback (2), Filed Under .NET ASP.NET MVC MVC

Yesterday Scott Gu announced the finishing and release of the new book for ASP.NET MVC 1.0, This book was written by Scott Hanselman, Rob Conery, and Phil Haack. Scott Gu’s contribution to the book is a 185 pages long startup project Walkthrough that he is releasing free of charge and with a Creative Commons with no derivatives! You can also download the source code from CodePlex.

The book is just printing so you will have to wait some time. Hope it’s not going to be one of those lengthy I-like-my-own-voice type of book, but with a 185 pages for chapter 1 that isn't looking too promising. We'll just have to wait and see.

Nansen at The EPiServer day conference

Posted Tuesday, March 10, 2009 8:42 PM | Feedback (1),

EpiServer (one of the most used Content Management Systems used in Sweden) is holding a customer event today. Our company Nansen is one of the sponsors, Michael Jäderlind, CEO at Nansen, will be talking about how companies can do a better job when gathering requirements for to be able to get the most out of their projects.

We will also be blogging from the conference at the Nansen blog, follow the event and post comments.

Windows Command shell cheat sheet

Posted Tuesday, March 10, 2009 8:22 PM | Feedback (1), Filed Under developement scripting windows

From time to time I have the need to get to some of the windows specific folders like, Program Files or the System folder. Some of them are easy to learn but I don’t use them that often. Now I just had one of those moments, but this time I found a nice Command shell overview page at TechNet containing all those nice to have shortcuts, nice!

A chance to contribute

Posted Saturday, March 07, 2009 5:21 AM | Feedback (1),

If you ever have wanted to contribute to an open source project but didn’t know how this is your chance.
Join the Open Source Club! The peeps at CodePlex have started an initiative that promotes one project each month that will be open for any one that wants to help. Check out more at the CodePlex weblog

This months project is Rawr. The project is a program for comparing and exploring gear for the different characters in World of Warcraft.

Technorati-taggar: ,