Suddenelfilio’s Weblog

Icon

Passionate about .net

Babysteps: LINQ To SQL

Okay I had some free time to spend on LINQ  To SQL and because I’m working on a new personal project for the future (God only knows if I’ll ever finish it ;-) ). I need in my database a table “Countries”. Because I don’t want to create a script to load this table  and I certainly don’t want to fill it manually I started looking for a web service that could provide me the data I need. I want to keep the country’s Name and its ISO code.
For this I found a web service on http://www.oorsprong.org [WSDL] which does exactly what I need.

The web service has got a web method – ListOfCountryNamesByCode – that returns all the countries with their ISO codes. Just perfect! :-D

Knowing where to get the data, we need to create the table that holds the data I’m getting from the web service. For this I only need a very basic table:

CREATE TABLE

[dbo].[Countries]
(
[ID] [int]
NOT NULL IDENTITY(1, 1),
[ISO] [nvarchar] (3)
NULL,
[Name] [nvarchar] (50)
NULL,
[InsertDate] [datetime]
NULL
) ON [PRIMARY]

Okay I admit maybe it’s not the best table structure ever, but it will do the trick.

So now the table is ready to store the data gotten from the web service all we need is to get the data.

First thing you need is to add a LINQ To SQL Mapping file (dbml file). Doing this is very simple. Just click somewhere in the solution explorer’s related project and select “Add new item”.

add new item - LINQ to SQL

 From the list you can select LINQ To SQL Classes. When the dbml file is created you just drag-n-drop the tables from the server explorer onto the designer’s surface.

dbml designer

That’s all you need to do for the object relational mapping for the Countries table. Also notice that when you drop the Countries table onto the surface of the designer it is smart enough to rename the autogenerated object to Country because it will represent the Country entity that is stored in the Countries table.

Next you need to fetch the countries using the web service. So just add a web service reference to your project, create an instance and call the necessary method, in this case it’s the ListOfCountryNamesByCode which will return an array of tCountryCodeAndName instances.

org.oorsprong.webservices.CountryInfoService serv =    new org.oorsprong.webservices.CountryInfoService();
org.oorsprong.webservices.tCountryCodeAndName[] countries =    serv.ListOfCountryNamesByCode();

To work with the generated Country entity you will first need to create a database context. The designer already generated all the necessary code for you. All you need to do is create an instance like this:

CountryTableDataContext db = new CountryTableDataContext();

Be honest it couldn’t be much easier ;-)

The instace db contains a property called Countries which is a Linq table for Country entities. Because we have a type difference from what is returned by the web service and what needs to get stored in the database we will convert the array of tCountryCodeAndName instances to an IEnumerable of Country entities. For this we can use the LINQ to Objects technology together with anonymous typing and object initialization expressions.

 var ctr = from c in countries
           select new Country() { ISO = c.sISOCode,            Name = c.sName, InsertDate = DateTime.Now };

As you can see we don’t define the type of ctr it will be implicitly defined as whatever is returned from the LINQ query. The part after the equals sign is the same as writing a foreach statement.
The second part of the code is where we create a new Country instance and assign it’s values using object initialization expressions. For example the Country entity has got an ISO property so you can say ” new Country() { ISO = c.sISOCode} ” to assign it the value of the property sISOCode on the tCountryNameAndCode instance c.

In C# 2.0 the previous code would look like this:

 List<Country> countryList = new List<Country>();
  foreach (tCountryCodeAndName c in countries)
  {
      Country ctr = new Country();
      ctr.ISO = c.sISOCode;
      ctr.Name = c.sName;
      ctr.InsertDate = DateTime.Now();

      countryList.Add(ctr);
  }

Of course there are some other techniques in the new way of coding that I didn’t explain like local type inference, lambda expression (behind the scenes)…

Now that we have got an IEnumerable of Country entities “ctr” we can add them to the database context very easily:

db.Countries.AddAll(ctr);

That’s it, just do this last line of code below and all the countries with their respective ISO code will be stored nicely in the database. Isn’t that cool ?!?

db.SubmitChanges();

If you have any questions on this post you can use the comment option to contact me about this.

Filed under: .net 3.5, LINQ, Visual Studio .Net "Orcas"

Download alternative for Visual Studio .Net 2008 beta 2

Most of you know the problem with downloading big files from the microsoft site. at least I do. Most of the time it’s not very fast and downloads also get interrupted frequently. I’m not saying this only due to the microsoft site so don’t get me wrong here ;-)

Now microsoft is testing a new piece of software (still in ctp) called Microsoft Secure Content Downloader:

The Microsoft Secure Content Downloader (MSCD) is a peer-assisted download manager capable of securely downloading specific files. MSCD is intended for consumers who are downloading from a home PC, or business users whose computers are not behind a corporate firewall. If you use MSCD from behind a corporate firewall, you may be unable to download content, and may adversely affect other clients’ ability to download content.
Main features of the MSCD are:

  • Secure content description
    • Each file available for download has a secure description, ensuring the content you download is exactly what the publisher published.
  • Scalable performance
    • MCSD is a peer-assisted technology. Each client downloads content by exchanging parts of the file they?re interested in with other clients, in addition to downloading parts from the server.
      • No matter how great the internet?s demand for the file, you will always be able to make progress downloading.
      • MSCD lets you download content more quickly than possible without peer assistance.

Filed under: Visual Studio .Net "Orcas", Visual Studio .Net 2008, msdn

How to ignore a blogging career ?

That’s simple buy a house that needs to be finished in the meantime switch jobs and on top of that move out of your current appartement. :-)

I’ve not been writing as much as would like, but I can’t seem to find the time. Lately I’ve been watching more closely to LINQ, Nhibernate, EDM, Windows Server 2008 and Visual Studio .net Codename ”Orcas”… I know that’s a lot but I can’t keep up and I would like to explore as much as humanly possible under current conditions.

Anyways my findings in short are:

LINQ: nice
NHibernate: same as LINQ = nice + advantage current support .net 2.0 and older versions.
EDM: yeah well… nice as well :-)
Windows Server 2008: mmm still beta, should say enough.
VS.Net Orcas: surprisingly “stable”.

Filed under: .net, .net 2.0, .net 3.0, .net 3.5, Visual Studio .Net "Orcas", Windows

Get a history of the evolution to LINQ

 Anson Horton wrote a very nice article for the MSDN magazine June 2007 edition. It gives you an overview of how LINQ (Language Integrated Query) has evolved from the idea of the concept to an actual technology. A nice extra on this article is that he shows a lot which new language features in C# 3.0 made it actually possible to come to version on LINQ we have now. It’s thanks to Lambda Expressions, Extension Methods, Anonymous Type, Implicitly Type Local Variables, Object Intitializers and Query expressions that LINQ can offer you a very nice language enhancement to query for example objects, dataset, xml, …

Check it out at:
http://msdn.microsoft.com/msdnmag/issues/07/06/csharp30/default.aspx

Filed under: .net, .net 3.0, .net 3.5, LINQ, Visual Studio .Net "Orcas", Visual Studio .Net 2005

Dynamic Data Controls (ASP.NET Futures CTP)

If you want a small introduction for the dynamic data controls that exist in the ASP.NET Futures CTP. You can watch the webcast below:

Filed under: .net, Asp.net, Visual Studio .Net "Orcas", Visual Studio .Net 2005

Get started with Expression Blend and Design

logoF or those that like to get started with the new Microsoft products Expression Blend and Expression Design. You should check out these links below. They give you access to tons of FREE video tutorials hosted by lynda.com

Links:
- Getting started with Expression Blend

In Getting Started with Expression Blend , Lee Brimelow covers all the basics that every designer and developer of WPF and Silverlight content needs to know. He starts with an overview of how Expression Blend fits into WPF and Silverlight workflows, then guides viewers through the process of creating and manipulating objects, building timeline-based animations, and exporting compositions into XMAL for use in Visual Studio.

- Getting started with Expression Design

From basic vector-based drawings to professional three-dimensional graphics built with Live Effects, Ted LoCascio covers the full range of possibilities in Getting Started with Expression Design. He starts by explaining Expression Design’s interface and how to work with documents, then moves on to cover working with objects and applying fills, strokes, and effects for best results. Exercise files accompany this training.

Enjoy!

note: the lynda.com logo is protected by copyrights. Suddenelfilio.net is not in any way affiliated with lynda.com or any of its partners.

Filed under: .net, .net 3.0, Expression Blend, Expression Design, Visual Studio .Net "Orcas", Visual Studio .Net 2005, WPF

Installing Microsoft Codename "Astoria"

Since I’m going to take a further look at the Astoria project I’ve installed the necessary pieces to get started. Here I will give a brief overview on what to install.

The pre-requisites are:

- Visual Studio .Net Codename “Orcas” beta 1 Standard, Professional or Team Suite edition.

note: The Visual Web Developer Express Codename “Orcas” edition will work as well but the experience is not optimal.

- .Net Framework 3.5 (Pre-Release version)

- Sql Server 2005 database

- Microsoft Codename “Astoria” May 2007 CTP

- Virtual Pc or Virtual Server 2005 R2 (if you are going to set up a virtual machine )

1. Visual Studio .Net Codename “Orcas” Bèta 1

For this it’s up to you to decide on which way to go. You can either download the bèta software and install it directly on your machine or install it as a virtual machine.

Since most of the software isn’t final yet I prefer to install it as a virtual machine. So I downloaded the VSCTPBASE file which is a pre-installed virtual machine by Microsoft you can use for testing purposes. The advantage is that you can re-use this image becuase it’s set up in such a way that it contains the basic needs for a virtual machine and you can install it together with other images that contain the specific technologies that you want. In this Case I downloaded the Visual Studio .Net Codename “Orcas” Bèta 1 Team Suite VPC. This is download is seperated in 9 files you can merge after download.

Tip: Use a download manager to download all these files because I’ve lost connection a lot during the downloads.

Okay when you’ve downloaded the files you can get working:

- Extract the base image (VSCTPBASE.exe) to a folder of your choice. I took D:\VM’s\Base.
- Extract the 9 files containing the bèta vs.net. For this I took D:\VM’s\vsnet orcas beta 1
- Create a new folder Astoria Environment. I picked D:\VM’s\Astoria Environment
- Copy the files from step 1 and 2 into the newly created folder. This way you won’t mess up the images in step 1 and 2 and it enables you to use them again if you want to test something else in another vm.

Issues with Differencing Disks (I’m using Virtual Server 2005 R2)
The Orcas image is a differencing disk which means that it needs a base disk to operate properly. The problem lies in the fact that the differencing disk has got it’s base disk hardcoded in the configuration and thus probably cannot find the base image because the paths you are using will differ from the ones the creator of the images was using. You should change that path using the configuration of the differencing disk

Differencing disk properties.

When starting the virtual machine for the first time you will probably run in the same problem as I did. What the hell is the user’s password ??? Well this is mentioned on the download pages of the images.

<<TODO insert image of MSDN download page showing login passwords>>

Finally it might be a good idea of installing the Virtual Machine addon for an optimal experience. To do this follow these steps:

- Log into the Guest Os.
- Go to the administrator site.
- Go to the configuration page of the virtual machine.

- Click on the Virtual Machine Additions.
- In the new screen you check the “Install Virtual Machine Additions” and press OK. (remember that the vm must be running.)

Okay now you are logged in and it you can proceed to part 2 of the installation. The .net framework 3.5 installation.

2. .Net Framework 3.5 Pre-Release installation

If you are using the virtual machines like I do you can skip this step because the .Net Framework 3.5 Pre-Release is already installed in the image.

If you are installing it directly onto your machine it shouldn’t be more than just executing the installer.

3. Microsoft Codename Astoria

Finally the “fun stuff” :-) installing astoria. This shouldn’t be too difficult because it’s just a MSI.

1. Execute the installer file and check the License Agreement checkbox of course.

2. Press the install button and the installation will begin and after installation press the Finish button.

Well euhm to be honest, that’s it… really the effort worthy to write an article about it eh?

Okay maybe 1 last thing, check if it’s installed correctly:

If you need to install the northwind database you can download it from MSDN

Filed under: .net 3.5, Astoria, Visual Studio .Net "Orcas"

Astoria video

On the Mix 07 website you can download the session that introduced the Astoria project. It was presented by Pablo Castro. The nice thing is that you can view as a Silverlight plugin, download the WMV or if you have a Zune download on there.

& wmv & zune

Silverlight video

Filed under: Astoria, Silverlight, Visual Studio .Net "Orcas"

New technology to play with

I was reading through my feeds when I found a post of Bart De Smet that summed up some of the newly announced “stuff” at Mix 07 .

One announcement in particular caught my eye it was the project codenamed Astoria . Astoria is a technology that will expose a data store through an ADO.NET Entity model using HTTP as transport technology.
It’s called a data service which can be consumed by web clients. The advantage is the HTTP protocol that is being used. This means that everywhere where you can access the internet you can also access the data service. It will pass through proxies, firewalls and so on…

Another nice thing about it is that Astoria not only allows “Read” functionality using the HTTP GET verb, but also “Create” using HTTP POST, “Update” using HTTP PUT and finally “Delete” using HTTP DELETE.

There are 3 formats in which the data is being sent over the wire:

- XML
- RDF & XML
-JSON

The main target groups are the “new” web technologies, like SilverLight & AJAX, but you can also use it in regular desktop applications. All you need is an internet connection.

Currently they released a first CTP that is meant to give you an idea on how they see the concept of Data Services. You can download it already at the site http://astoria.mslivelabs.com . There are some other downloads as well as an online service that demos the technology in a limited form (only reading is possible).

Some links to get you started:

Whitepapers: http://astoria.mslivelabs.com/resources.aspx
Downloads: http://astoria.mslivelabs.com/downloads.aspx

FAQ: http://astoria.mslivelabs.com/faq.aspx

I’m currently busy downloading the prerequisits like the Visual Studio .Net “Orcas” bèta 1. I’m downloading the VCP images.

Filed under: Astoria, Silverlight, Visual Studio .Net "Orcas"

Visual Studio Orcas Beta available !!!

I saw the notice on Informationweek that Microsoft released a beta of the VS.net Orcas.

Find out more here.

Filed under: .net, .net 2.0, .net 3.0, .net 3.5, Asp.net, Asp.net 2.0, Asp.net Ajax, General, Visual Studio .Net "Orcas"