# Sunday, August 23, 2009

Last week I showed how to use the Telerik OpenAccess WCF Wizard on this blog. The wizard creates some of the boring WCF code for you automatically. You can create endpoints for WCF, Astoria, REST collection and ATOM Pub services. Our first demo used WCF, later we will show you the other services. This is what we did:

  • Created a data access layer automatically using OpenAccess
  • Created a WCF Service automatically using the wizard
  • Created a Silverlight 3.0 application that used WCF to databind the Customers table to a datagrid, enabling editing automatically

Here is a video by Peter Bahaa (aka Pedro), .NET ninja in training, using the wizard with the above scenario. You can get the code here. Enjoy!

Telerik OpenAccess WCF Wizard Part I from Stephen Forte on Vimeo.

posted on Sunday, August 23, 2009 7:34:48 PM (Eastern Daylight Time, UTC-04:00)  #    Comments [0] Trackback
# Wednesday, August 19, 2009

Microsoft’s Silverlight 3.0 is a great new platform for building line of business applications. With every new technology advancement, we always seem to lose something. With Silverlight we lose the dirt simple data access since Silverlight does not support System.Data.SQLClient and talking to a database directly. This forces us into a service oriented architecture and an asynchronous model. While this is definitely a best practice, it sometimes takes a little getting used to.

With Silverlight we have to wrap up our data access layer into WCF services. (Or Astoria, RIA Services, or something similar.) It is also pretty standard to use some kind of ORM like the Entity Framework or Telerik OpenAccess to map to your database tables and then expose those entities as part of your WCF service. Mapping tables to entities may save you time, however, the part that bothers me is that there is a lot of generic “plumbing” code that has to get written for the WCF service to function.

That is why Telerik created the OpenAccess WCF Wizard. The Telerik OpenAccess WCF Wizard is a tool that will automatically create the C# “plumbing” code and necessary project files for using OpenAccess entities with the following services:

  • Astoria (ADO .NET Data Services)
  • WCF
  • REST Collection Services (WCF REST Starter Kit)
  • ATOM Publishing Services (WCF REST Starter Kit)

Using the wizard is pretty easy. If you already have a project with OpenAccess mapped entities in it, all you have to do is point the wizard to that location and have it generate the scv and .cs files of the WCF service (or Astoria, REST Collection or ATOM Pub service) as shown below. This will eliminate the need for you to write all of the WCF plumbing code.

image

 

We’ll map the Northwind tables and run the wizard and then create a WCF service project. Once you create the service, replace the .cs file with the one generated by the wizard and then create a new Silverlight project (or any project that wants to communicate with your OpenAccess enabled WCF service.) Then set a Service Reference to the WCF service and you can start to consume the it. Your projects in the solution will look like this:

image

Where:

  • OA.WCFWizard.Demo.DAL is the project containing your OpenAccess entities (mapped to Northwind tables).
  • OA.WCFWizard.Demo.SL is the project containing your Silverlight project, including the service reference to your WCF service. (First arrow.)
  • OA.WCFWizard.Demo.WCFService is the project containing the WCF service created by the WCF wizard (second arrow.)
  • OA.WCFWizard.Demo.Web is the asp.net site hosting your Silverlight application

Writing some Silverlight code to consume the WCF service

Consuming the service is pretty easy once you have the service reference set up. We’ll start with some basic XAML, but I will spare you the major XAML details since I am not a XAML expert. Here is my datagrid in XAML, notice that I am using data binding and predefined columns to make life a little easier:

<data:DataGrid x:Name="dataGridCustomers" Grid.ColumnSpan="2" AutoGenerateColumns="False" ItemsSource="{Binding}">
     <data:DataGrid.Columns>
         <data:DataGridTextColumn Binding="{Binding Path=CompanyName}" Header="Company Name"></data:DataGridTextColumn>
         <data:DataGridTextColumn Binding="{Binding Path=ContactName}" Header="Contact Name"></data:DataGridTextColumn>
     </data:DataGrid.Columns>
</data:DataGrid>

When filled, it looks like this:

image

Pretty basic stuff, if you want more advanced looking grids and controls, talk to a designer. :)

Ok, so how can we easily consume this service and bring this data from Northwind via OpenAccess and WCF to the Silverlight grid? Once you have a service reference set up (NorthwindWCFService) in your Silverlight project, you have to do a few things to call it. I created a LoadData() method to contain the code to fill the grid. We can call this on the page load event as well as on the UI with a “refresh” button, or after any “save” method.

Inside of the LoadData() method, first you have to create an instance of the proxy or the “client” as shown in line 4 of the first code block below. Once you have this set up, you then have to register an event for the method you are going to call, as shown on line 6 below. This means that when you call your ReadCustomers method asynchronously the client_ReadCustomersCompleted event handler will fire when the ReadCustomers method is done. This is where you put your binding code (more on that in a second.)  Lastly we have to call our ReadCustomers method, we can only call the asynchronous version of this method as shown in line 9.

   1:  private void LoadData()
   2:  {
   3:      //ref to our service proxy
   4:      NorthwindWCFService.SampleWCFServiceClient wcf = new NorthwindWCFService.SampleWCFServiceClient();
   5:      //register the event handler-can move this up if you want
   6:      wcf.ReadCustomersCompleted += new EventHandler<NorthwindWCFService.ReadCustomersCompletedEventArgs>(client_ReadCustomersCompleted);
   7:      //make an async call to ReadCustomer method of our WCF serice
   8:      //get only the first 100 records (default)
   9:      wcf.ReadCustomersAsync(0, 100);
  10:  }

 

The code for the event handler is pretty straightforward, we just set the datagrid’s DataContext to the e.Result of the event handler. This will contain an ObservableCollection<Customers> that was defined automatically for you via the proxy when you created your service reference.

   1:  void client_ReadCustomersCompleted(object sender, OA.WCFWizard.Demo.SL.NorthwindWCFService.ReadCustomersCompletedEventArgs e)
   2:  {
   3:      dataGridCustomers.DataContext = e.Result; 
   4:  }

 

That is it! I will soon post a video here showing how to do this in more detail as well as an update method since our grid is editable (it is super easy.) Next week I will show some examples using ADO .NET Data Services (Astoria) where the wizard will also automatically create the IObjectScope OpenAccess and Astoria plumbing code for you.

posted on Wednesday, August 19, 2009 6:04:20 PM (Eastern Daylight Time, UTC-04:00)  #    Comments [0] Trackback
# Thursday, August 6, 2009

The reason why Apple products are so slick and cool is two fold, the first is that they are a great consumer product/design company hands down. But the second is an equally important reason, a reason that will potentially prevent them from massive long term market share (like Windows, Oracle, etc.) The second reason why Apple products look and work so nicely is that they have a tight control over the software installed on their devices. For example if you are a software developer and want to build an application for the iPhone or the Mac, you need to follow the strict Apple design guidelines and then get approval from Apple before you can sell (or give away) your software.

This is great, it gives the vendor (Apple) near complete control over what gets installed by the user base and the vendor (Apple) can control the user experience. This of course has some great benefits: less virus attacks, less crashes (windows is usually unstable due to people like me: software developers) and less hacks. You can also guarantee that there is no porn or politically sensitive content on your device.

This also has a negative side effect. Pissed off developers. Tech blog TechCrunch reports that many software developers are getting angry with Apple due to the rejection of their apps, random removal of their apps, or rejection of an upgrade of their original app from the Apple AppStore. This will only anger more and more devs and over time, will limit the developer community on that platform. As we all know, with the apps, there will be no platform.

I am not saying that this is happening (developers leaving in droves) now, but it can happen. A recent decision to pull an app may have started the revolution. Apple last week pulled the Google Voice application from its AppStoe. Google Voice is pretty cool and allows you to use VOIP to make and receive calls (among over cool stuff.) So free phone calls means less minutes AT&T (and other international carriers) can sell.

This caused a bit of an uproar in the tech world. Some major influencers including prominent Apple supporters decided to quit using the iPhone over the decision to remove Google Voice. Google’s CEO Eric Schmidt has decided to step down from Apple’s board due to the conflict of interest. Lastly, the US government has gotten interested, last week, the FCC launched an investigation about the removal of Google Voice.

Apple (and AT&T) should allow any application to be installed on the iPhone as well as any other phone for that matter. On the regular wired internet, I can buy a PC and then download and install anything I want, my ISP does not control what computer I can buy and what I can see and what I can’t see. Why is it different for phones? I can’t install everything I want, nor can I even buy whatever phone that I want, I have to buy the phone from the carrier in a “locked” mode.

There is a reason: the United States is large country by land mass. The government has let the carriers have some control due to them promising to wire the whole country-this is why you can get signal in remote places where there is not a lot of demand. But now years later we are paying the price. It is time to open up the airwaves and allow us to buy any phone and install any application. The Apple AppStore is a walled garden that eventually will fail. While Apple risks losing a few design points and some system crashes and AT&T will lose some revenue, over the long run, if they allow anyone to develop software and anyone to install it, we all will be better off.

posted on Thursday, August 6, 2009 5:14:53 AM (Eastern Daylight Time, UTC-04:00)  #    Comments [0] Trackback
# Friday, July 31, 2009

If case you did not know, Telerik released LINQ to M a few weeks ago, enabling you to use a LINQ style interface with M values of data. Mehfuz and I have written blog posts where you can see how to use the basics. 

There have been tremendous amounts of downloads and we have gotten tons of feedback. (Thanks, keep it coming!) One thing that stood out is that when you are building M values a lot of time you will have a named instance of your M code like this:

   1:  //People is the named "M" instance
   2:  People{
   3:                  {Id=1,Name="Stephen Forte",Age=37},
   4:                  {Id=2,Name="Mehfuz Hossain",Age=29},
   5:                  {Id=3,Name="Vassil Terziev",Age=31},
   6:                  {Id=4,Name="Nadia Terziev",Age=27},
   7:                  {Id=5,Name="Chris Sells",Age=37},
   8:                  {Id=6,Name="Todd Anglin",Age=27},
   9:                  {Id=7,Name="Joel Semeniuk",Age=37},
  10:                  {Id=8,Name="Richard Campbell",Age=42},
  11:                  {Id=9,Name="Kathleen Gurbisz",Age=31}
  12:   }";    

 

In the first version of LINQ to M, you get a runtime error saying that LINQ to M cannot parse “People”. The most current refresh of LINQ to M fixes this problem and you can run your LINQ queries against M values that have a named instance. You can get the refresh here. Enjoy!

posted on Friday, July 31, 2009 12:25:00 PM (Eastern Daylight Time, UTC-04:00)  #    Comments [0] Trackback
# Friday, July 10, 2009

I was interviewed about what I will be doing this summer on Bytes by MSDN. It was fun, and you can watch lots of others such as Scott Hanselman and Billy Hollis too.

posted on Friday, July 10, 2009 8:43:09 AM (Eastern Daylight Time, UTC-04:00)  #    Comments [0] Trackback
# Thursday, July 9, 2009

I chat it up with Richard and Carl about SQL Server, ADO .NET, the Entity Framework, the Boston Red Sox, LINQ to SQL, ORMs, Silverlight, Astoria, and RIA Services. Listen here.

posted on Thursday, July 9, 2009 7:38:44 PM (Eastern Daylight Time, UTC-04:00)  #    Comments [0] Trackback
# Wednesday, July 8, 2009

I have been using Microsoft “Oslo” for some time and have been pretty excited about the ability to model an application and store it in the repository. Other developers have also gotten very excited about the ability to create domain specific languages (DSLs). DSLs are cool, because if you build a DSL on top of your application, you can abstract away some of the hard to understand stuff. You can also put in a layer on top of your standard communication (DALs, web services, etc.) For example, let’s say you work at Expedia and you want to give your providers (the airlines) a way to enter flight data to your site. You will most likely have a data entry screen with lots of boxes and drop downs. An alternative of course is a Web Service as well as a CSV text import. But another alternative is to provide a DSL, so if someone wants to go in and make a quick change, they can type:

Delta flight 280 on Friday’s new price is $780.

Then using Oslo, you can transform this to an M Value format:

Flights

{

{Carrier=”Delta”, Flight=280, DepartDate=”July 10, 2009”, Price=780}

}

This is one area where you can use Oslo with .NET today-you can call the M DSL DLLs from .NET and perform the transformation in C# or VB. The problem is that the M Value format is difficult to work with in .NET; parsing the M values can be a challenge.

Mehfuz Hossain over at Telerik built a LINQ Extender available on codeplex. Building on top that, Telerik has released a LINQ to M implementation. It is pretty easy to use. After you download it and set a reference to it in your application, you then use standard LINQ statements against M Values.

For example, let’s say that your application has some M values that looks like this:

   1:   {                
   2:           {Id=1,Name=""Stephen Forte"",Age=37},
   3:           {Id=2,Name=""Mehfuz Hossain"",Age=29},
   4:           {Id=3,Name=""Vassil Terziev"",Age=31},
   5:           {Id=4,Name=""Nadia Terziev"",Age=27},
   6:           {Id=5,Name=""Chris Sells"",Age=37},
   7:           {Id=6,Name=""Todd Anglin"",Age=27},
   8:           {Id=7,Name=""Joel Semeniuk"",Age=37},
   9:           {Id=8,Name=""Richard Campbell"",Age=42},
  10:           {Id=9,Name=""Kathleen Gurbisz"",Age=31}
  11:   }

 

You could have gotten this M code from the results of a DSL or some other process. For our purpose, we will just put it into a constant and query against it:

 

const string MGraphCode = @"

            {

               

                {Id=1,Name=""Stephen Forte"",Age=37},

                {Id=2,Name=""Mehfuz Hossain"",Age=29},

                {Id=3,Name=""Vassil Terziev"",Age=31},

                {Id=4,Name=""Nadia Terziev"",Age=27},

                {Id=5,Name=""Chris Sells"",Age=37},

                {Id=6,Name=""Todd Anglin"",Age=27},

                {Id=7,Name=""Joel Semeniuk"",Age=37},

                {Id=8,Name=""Richard Campbell"",Age=42},

                {Id=9,Name=""Kathleen Gurbisz"",Age=31}

            }";

Now you need to load the M code into a QueryContext object so you can work with it in LINQ:

   1:  var personM = QueryContext.Instance.Load(MGraphCode);

There is not a ton you can do with it just yet, but you can bind it to an ASP.NET data grid:

   1:  GridView1.DataSource = personM;
   2:  GridView1.DataBind();

This alone will save you some time, but if you want to do typed queries and have the cool IntelliSense experience, you have to strongly type your LINQ statement. To do this, create a class that has the same shape as your M data, so we will create a Person class:

   1:  public class Person
   2:  {
   3:  public int Id { get; set; }
   4:  public string Name { get; set; }
   5:  public int Age { get; set; }
   6:  }

 

Now it gets fun. Let’s create a simple LINQ statement that will query just the 37 year old people not named Joel Semeniuk:

   1:  var persons = QueryContext.Instance.Load<Person>(MGraphCode);
   2:   
   3:  var result = from person in persons
   4:                where person.Age == 37 && person.Name != "Joel Semeniuk"
   5:                orderby person.Name ascending
   6:                select person;

This will return just Chris and Stephen. You can see that we are using the standard LINQ statements FROM, WHERE, ORDERBY, and SELECT. (Hey wait a minute, aren’t those SQL operators, but I digress…. )

Let’s do some aggregation, this query will aggregate a list all of the ages and count how many people are that age, but we will exclude from our query any age that only has one person:

   1:  //Using a Group By and SUM
   2:   var result1 = from person in persons
   3:                 group person by person.Age into g                         
   4:                 where g.Count() > 1
   5:                 orderby g.Count() descending
   6:                 select new { Age = g.Key, Count = g.Count() };

Your results will look like this:

 

Age

Count

37

3

31

2

27

2

Lastly, just for the true geeks, here is how to use a LAMBDA expression:

   1:  GridView3.DataSource = QueryContext.Instance.Load<Person>(MGraphCode)
   2:                         .Where(p => p.Name == "Mehfuz Hossain");
   3:  GridView3.DataBind();

 

Mehfuz and I are pretty excited about this. There are some limitations; the M Values that you pass in has to be shaped without a name as you can see above. (Don’t worry we will change that later on.) But what you can do so far is pretty darn cool. Enjoy and drop a comment to me or Mehfuz about what you want to see in the next release.

Download it here.

posted on Wednesday, July 8, 2009 11:31:05 AM (Eastern Daylight Time, UTC-04:00)  #    Comments [0] Trackback
# Tuesday, July 7, 2009

Thursday, July 16, 2009
Project Stonehenge for .NET

Subject: 
You must register at https://www.clicktoattend.com/invitation.aspx?code=139381 in order to be admitted to the building and attend.
Project Stonehenge is an open source project created to build reference applications that demonstrate the use of WS-* Standards in real-world project implementations. Project Stonehenge is sponsored by the Apache Software Foundation, a leader in the Open Source community. Project Stonehenge’s flagship application the StockTrader Application currently has interoperable layers in .NET, PHP (unmanaged-C), and JAVA.
This session will provide an overview of the WS-* standards, a history of Apache Stonehenge and the StockTrader application, an demo of the StockTrader web services interoperability, and walk-thru of the StockTrader .NET WCF code and configuration.

Speaker: 
Ben Dewey, twentysix New York
Ben Dewey is a Senior Software Developer at twentysix New York and is a committer for the Apache Incubator StoneHenge project. He has over 10 years of web development experience and has recently worked on projects using WebServices, REST, WCF, jQuery, Silverlight, and WPF. Ben strives to create connected system with well defined roles without sacrificing the richness of the user experience.

Date: 
Thursday, July 16, 2009

Time: 
Reception 6:00 PM , Program 6:15 PM

Location:  
Microsoft , 1290 Avenue of the Americas (the AXA building - bet. 51st/52nd Sts.) , 6th floor

Directions:
B/D/F/V to 47th-50th Sts./Rockefeller Ctr
1 to 50th St./Bway
N/R/W to 49th St./7th Ave.

posted on Tuesday, July 7, 2009 7:48:33 AM (Eastern Daylight Time, UTC-04:00)  #    Comments [0] Trackback