Make note that I define a line of business (LOB) application as:
An application needing centrally stored/processed data; using its own networking contracts, where requirements change rapidly.
This given as a counterpoint to some common consumer-focused applications such as:
- Angry Birds, games generally don't have changing requirements, only new levels
- Weather Applications, they do not store and/or process data, only pull data from simple web APIs
- Facebook applications, they process data, but don't have their own data contracts
So, with that in mind, I plan to cover all the points a normal LOB developer would need to cover, but I will use tricks, such as attached databases, that will speed development. In addition, I will not be covering items that are just good practice, such as using source control, even for yourself.
I am also not doing this from a clean machine, so if I am missing anything, such as a download link, contact me.
Finally, I am focusing on using ASP.NET MVC 3 and Windows Phone 7.5 (Mango). In the future I may make additions covering:
- Integrating the phone’s navigation into MVVM Light
- Adding and sharing code with Silverlight 5
- Adding and sharing code with ASP.NET MVC 3
- More complex data relationships (foreign keys, many-to-many, blobs)
- Porting the ASP.NET and database site over to Azure
- Publishing your application on the App Store
- Integration with my Offline (Occasionally Disconnected) Application demo
- Better “Design-Time Data”
Adding the Web Application
Ok, let’s start with the basics, open Visual Studio 2010 and create a new ASP.NET MVC 3 Web Application, I think you may need to download and install this package.
and on the next screen, select “Internet Application”. I personally don’t create a unit test project at this point, I think it is better to just build it yourself at a later point.

Creating your Database
Now, let's get a database into the mix. In your Solution Explorer (if you don’t see it, the hotkey is Ctrl + W, S), right click on App_Data and click Add and select New Item… and select SQL Server Database and give it a name like so:

Now, in the Solution Explorer, expand App_Data and double click on LobData.mdf to open it in the Server Explorer.
Now, right click on the Tables folder and select Add New Table:

Now, I am only planning on touching on the core point here, but you will ultimately want to create your schema as needed. Here is the one I am going to roll with for now:

But, it is a good idea to set a Primary Key, and Row GUID Column. To set the primary key, right click on your CustomerGuid column and select Set Primary Key. Now in the Properties window (F4), make sure you don’t have any columns selected and change the Row GUID Column to CustomerGuid:

Click Save, give the table a name, such as “Customer” and you should be done with the database for now.
Creating your Entities
Now I personally love using a more ‘enterprise’ framework, such as CSLA, for the sake of time, we'll just stick with Entity Framework. So, right click on your project and select Add and New Item… and choose the following:

and make sure to give it a name, such as LobModel.edmx in my case. Then choose from the following sets of options:



And you should be dropped right into the edmx Model Editor:

And that’s all you need for that step, but first, let's add some sample data. I like to plan my sample data to work two ways: Create it when I need it, or setup new sample data every time. To make this happen I like to add an AddSampleData method to my Global.asax.cs; let’s do that now:
- protected void Application_Start()
- {
- AreaRegistration.RegisterAllAreas();
-
- RegisterGlobalFilters(GlobalFilters.Filters);
- RegisterRoutes(RouteTable.Routes);
-
- AddSampleData(); // Add this
- }
-
- // Create this
- private void AddSampleData()
- {
- var MyContext = new LobDataEntities();
- if (MyContext.Customers.Count() != 0)
- return;
-
- MyContext.AddToCustomers(new Customer()
- {
- CustomerGuid = Guid.NewGuid(),
- FirstName = "First",
- LastName = "Customer",
- Level = 1,
- Created = DateTime.Now,
- LastUpdated = DateTime.Now,
- });
- MyContext.AddToCustomers(new Customer()
- {
- CustomerGuid = Guid.NewGuid(),
- FirstName = "Second",
- LastName = "Guy",
- Level = 2,
- Created = DateTime.Now,
- LastUpdated = DateTime.Now,
- });
- MyContext.AddToCustomers(new Customer()
- {
- CustomerGuid = Guid.NewGuid(),
- FirstName = "That",
- LastName = "Other One",
- Level = 2,
- Created = DateTime.Now,
- LastUpdated = DateTime.Now,
- });
- MyContext.SaveChanges();
- }
Now, if you want to have clean data every time, just remove the if statement.
Adding Your Windows Phone 7.5 (Mango) Project
First, I think you need to install this package. Once you have that (and restarted Visual Studio), right click on your solution and select Add and select New Project… and select the following:

And select Windows Phone 7.1 from the New Windows Phone Application dialog. Now, if I have learned anything from being a Program Manager at Microsoft, it is that when you create new applications all the time (I am up to “SilverlightApplication52” on this computer), learn to LOVE NuGet. Let me show you how… Come on, don’t fight it.
Right click on your Silverlight project and select Manage NuGet Packages… and enter MVVM Light into the “Search Online” box in the upper right hand corner and click the Install button on the first item that shows up:

Now, there are a lot of MVVM frameworks, and trust me, I have tried them all (or at least it feels like it). I can say I am impressed with MVVM Light, BxF, and Caliburn Micro, but for the purpose of creating an application in 5 minutes or less and avoiding as much magic as possible, MVVM Light by Laurent Bugnion is awesome.
While we are at it, let’s get the Silverlight Toolkit for Windows Phone. Search for “windows phone toolkit” and click Install:

Building the Solution
Right click on your solution and select Build Solution. Yup, not joking, this is a step. If you skip this step, Entity Framework will not generate the code that we will need for the next step. Please don’t skip it.
Connecting Your Client And Server
To get the data from the web server to the phone and back again, we are going to use oData.
Getting the Server Ready
This might be a good time to talk about your options, but if you want to do this fast and easy, you really don’t have any. The first step is to wrap our entities as a DataService<T>.
First, add a reference to System.Data.Services, System.Data.Services.Client and System.ServiceModel DLLs that should be in your GAC. Then create a new folder in the root of you web project called Services and create a new WCF Data Service like so:

And replace the contents of the LobEntities.svc.cs file with the following:
- using System.Data.Services;
- using System.Data.Services.Common;
- using System.ServiceModel;
-
- namespace LobApplication.Services
- {
- [ServiceBehavior(IncludeExceptionDetailInFaults = true)]
- public class LobEntities : DataService< LobApplication.LobDataEntities >
- {
- public static void InitializeService(DataServiceConfiguration config)
- {
- config.SetEntitySetAccessRule("*", EntitySetRights.All);
- config.DataServiceBehavior.MaxProtocolVersion =
- DataServiceProtocolVersion.V2;
- }
- }
- }
and if you F5 your web application and go to the following address:
http://localhost:[WhateverYourPortIsGoesHere]/Services/LobEntities.svc/
you should see the following:

Ok, I know, not that impressive, but we will get there.
Silverlight (or WP7) and oData, Friends Forever!
If you want to get data back and forth in a modern application, I can’t see myself going with anything other than oData for all but the most hard-core enterprise applications. It seems to be supported on everything, pretty much everywhere, and works great with Azure.
Hell, did you know that SQL Server 2008 R2 can host a database as oData without any other tools? Spin up SQL Server Management Studio, right click on a database, select Tasks and Register as Data-tier Application… and you will get a nice wizard walking you though the process. More data can be found here.

Ok, back to the walkthrough:
Go to your Windows Phone project, right click on Service References and select Add Service Reference.

Click Discover and it should automatically find your service. Give it a name and click OK.
You're done, the data is there.
Don’t trust me? Do we need to prove it to you. Ok, fine, but I want to let you know that that hurts.
Making Your First ViewModel
On the Windows Phone project, the MVVM Light NuGet package creates a ViewModel\MainViewModel.cs class for you. Let's go there, it looks something like this (with the comments removed).
- using GalaSoft.MvvmLight;
-
- namespace LobApplication.Silverlight.ViewModel
- {
- public class MainViewModel : ViewModelBase
- {
- public MainViewModel()
- {
- ////if (IsInDesignMode)
- ////{
- //// // Code runs in Blend --> create design time data.
- ////}
- ////else
- ////{
- //// // Code runs "for real"
- ////}
- }
- }
- }
So, let’s get our data into the mix.
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Data.Services.Client;
- using System.Linq;
- using GalaSoft.MvvmLight;
- using LobApplication.Silverlight.LobEntitiesService;
-
- namespace LobApplication.Silverlight.ViewModel
- {
- public class MainViewModel : ViewModelBase
- {
- private Uri _DataUri = new Uri("http://localhost:35772/Services/LobEntities.svc/");
-
- private IEnumerable<Customer> _Customers;
- public IEnumerable<Customer> Customers
- {
- get
- {
- if (_Customers == null)
- return new ObservableCollection<Customer>();
- return _Customers;
- }
- set
- {
- if (_Customers == value)
- return;
- _Customers = value;
- RaisePropertyChanged("Customers");
- }
- }
-
- public MainViewModel()
- {
- if (IsInDesignMode)
- {
- // Code runs in Blend --> create design time data.
- }
- else
- {
- var MyContext = new LobDataEntities(_DataUri);
- var MyData = new DataServiceCollection<Customer>(MyContext);
- MyData.LoadCompleted += (o1, e1) =>
- {
- // This block of code runs when we get results.
-
- if (e1.Error != null)
- throw e1.Error;
-
- if (MyData.Continuation != null)
- MyData.LoadNextPartialSetAsync();
- else
- Customers = MyData;
- };
- var MyQuery = from c in MyContext.Customers
- select c;
- MyData.LoadAsync(MyQuery);
- }
- }
- }
- }
Connecting Your ViewModel to XAML
In our MainPage.xaml, we connect our ViewModel to our page’s DataContext, using MVVM Light’s ViewModelLocator to help make this easy. To do this, put the following attribute into your PhoneApplicationPage element, and then just bind a list box to your ViewModel’s Customer property.
- <phone:PhoneApplicationPage
- x:Class="LobApplication.Silverlight.MainPage"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
- xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
- xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
- xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
- mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
- FontFamily="{StaticResource PhoneFontFamilyNormal}"
- FontSize="{StaticResource PhoneFontSizeNormal}"
- Foreground="{StaticResource PhoneForegroundBrush}"
- SupportedOrientations="Portrait" Orientation="Portrait"
- DataContext="{Binding Source={StaticResource Locator}, Path=Main}"
- shell:SystemTray.IsVisible="True">
-
- <Grid x:Name="LayoutRoot"
- Background="Transparent">
- <Grid.RowDefinitions>
- <RowDefinition Height="Auto"/>
- <RowDefinition Height="*"/>
- </Grid.RowDefinitions>
-
- <StackPanel x:Name="TitlePanel"
- Grid.Row="0"
- Margin="12,17,0,28">
- <TextBlock x:Name="ApplicationTitle"
- Text="LOB Application"
- Style="{StaticResource PhoneTextNormalStyle}"/>
- <TextBlock x:Name="PageTitle"
- Text="Customer View" Margin="9,-7,0,0"
- Style="{StaticResource PhoneTextTitle1Style}"/>
- </StackPanel>
-
- <ListBox Grid.Row="1"
- ItemsSource="{Binding Customers}">
- <ListBox.ItemTemplate>
- <DataTemplate>
- <Grid Margin="40 4 0 0">
- <Grid.RowDefinitions>
- <RowDefinition Height="Auto"/>
- <RowDefinition Height="Auto"/>
- </Grid.RowDefinitions>
- <Grid.ColumnDefinitions>
- <ColumnDefinition Width="60px" />
- <ColumnDefinition Width="*" />
- </Grid.ColumnDefinitions>
-
- <TextBlock Grid.RowSpan="2" Text="{Binding Level}"
- Style="{StaticResource PhoneTextTitle1Style}"/>
- <TextBlock Grid.Column="1"
- Text="{Binding FirstName}"
- Style="{StaticResource PhoneTextNormalStyle}"/>
- <TextBlock Grid.Row="1" Grid.Column="1"
- Text="{Binding LastName}"
- Style="{StaticResource PhoneTextNormalStyle}"/>
- </Grid>
- </DataTemplate>
- </ListBox.ItemTemplate>
- </ListBox>
- </Grid>
-
- </phone:PhoneApplicationPage>
Finally, Results��
And when you run your Silverlight application, you should get results like this:

And that, I think a good place to stop for now. If you followed the steps, you have a great framework that combines ASP.NET MVC 3, WCF Data Services (oData), Windows Phone Mango, MVVM, and SQL Server Express.
That’s not a bad place to start your Silverlight journey.