[RIA Services] Use Shared class for metadatas

With
WCF RIA Services and DomainServices, the metadatas file is generated from
selected entities of the ADO.NET Entity Data Model. But if you want to add the “FullAddress”
property in the metadatas, this error appears:

“The associated metadata type for type ‘SharedInfos.Web.Customers’
contains the following unknown properties or fields: FullAddress. Please make
sure that the names of these members match the names of the properties on the
main type.”


Shared classes allow completing this metadatas with properties and/or
methods. This file has the .share.cs extension (or .shared.vb). Notice that the
namespace must be the same as the Web project.

namespace SharedInfos.Web
{
 
public partial class Customers
  {
    
public string FullAddress
     {
       get
       {
         
return Address + ” ” + PostalCode + ” ” + City;
       }
     }
  }
}


During the compilation, the file is duplicated from the server side to the client
side. And the new functionalities are both accessible from the client and the
server side.

 

Now the
new property “FullAddress” is  available :

These files with additional
methods make it, interesting to use CustomValidations in metadatas for example (to
be continued in a next post).

 

[WP7] AskMe! My application for the killer-apps Windows Phone 7 contest

Microsoft France organizes a
killers-app Windows Phone 7 contest
. The 10 selected applications will be
presented to a very impressive jury:

  • Steve Ballmer, CEO, Microsoft
  • Pierre Olivier Carles,
    CEO of Kipost and co-founder de Labotec, business angel.
  • Jacques Antoine Granjon, Founder of Vente-privée.com
  • Ouriel Ohayon,Co-founder
    of AppsFire and Co-founder of Isai found
  • Marc Simoncini, Founder and
    President of Meetic and of Jaïna found
  • Bruno Vanryb, Founder and
    President of Avanquest Software

To be
selected, you must make a Windows Phone 7 application, a video about it, post
this video on the “Developpeurs” Facebook page and obtain a maximum of like to
be in the top 10 list!

I develop a Windows Phone 7
application which name is AskMe! This is a big 8 pool ball, you ask it a
question, shake the phone to shake the ball and it answers you. Be careful,
answers come without any warranties; the ball may put itself on strike!

If you like my application and
you are on Facebook, please “like” my AskMe! video on this Facebook page :

http://tinyurl.com/askmeappwp7

Thanks
!!!

This is the video of my
application:

[View:http://www.youtube.com/watch?v=iwPa0272ftc:550:0]

[WP7] Add images in the Windows Phone 7 Media Library

The Windows Phone 7 emulator included in the beta developer
tool doesn’t contain
any images in the Media Library. It’s not very easy to test an
application that
asks the user to choose an image to use the application.
This
is
a few lines of code which helps you to add images in the Media Library
emulator. Please note that images are not being saved when you close the
emulator !

When you
look the media library in the WP7, there is nothing:

You must
add in the project the assembly Microsoft.Xna.Framework.Media. It allows you to
access the Media Library and to save images with the SavePicture method :

using
Microsoft.Xna.Framework.Media;

Create
an “Images” folder in the project, and add images (or photos) in this folder.
For each images, change the following properties as the following screenshot:

And use
the following lines of code to add the images in the Media Library:

MediaLibrary myMediaLibrary = new MediaLibrary();
Uri myUri = new Uri(@”Images/Jellyfish.jpg”,
UriKind.Relative);

System.IO.Stream photoStream =
App.GetResourceStream(myUri).Stream;
byte[] buffer = new byte[photoStream.Length];

photoStream.Read(buffer, 0,
Convert.ToInt32(photoStream.Length));

myMediaLibrary.SavePicture(“Jellyfish.jpg”, buffer);

When you have added several images, this is what you can see in the WP7 emulator:

[OData] Open Data Protocol Visualizer Extension for Visual Studio 2010

As
seen before
, I used OData in a Silverlight 4 project. Adding a data feed was
possible through a Service Reference. Several files were created after the
adding, including an edmx, but when you want to see it in Visual Studio 2010
the following error message appears :

A
Visual Studio 2010 extension is available to visualize exposed OData feed entities
as a diagram. This extension is the Open Data Protocol Visualizer Extension for
VS2010. The Extension Manager (in the Tools menu) allows its installation in Visual Studio 2010.

A
window opens and lists all available extensions. Choose Open Data Protocol
Visualizer and click on the “Download” button. When the installation is over,
click on the “Restart now” button, and Visual Studio 2010 restarts to activate
the installed extension:

From now,
when the edmx file is opened with the extension, you can see that :

A summary informs us on the different elements of the diagram. Click on
“add all” for the Namespaces to allow visualizing them. Then, we obtain all the entities and
its associations:

It’s
possible to visualize the entire diagram or just a piece of it. You can drag
and drop entities from the Open Data Protocol Model Browser to the designer
like Customers and Orders entities for example.

With a right-click and a click on “View Generated Code” on a specific
entity from the diagram, it’s possible to see the generated code in the
“reference.cs” file. It’s possible to export the diagram to a XPS file.

For more information, this is a video from the ODataPrimer channel:

[View:http://www.youtube.com/watch?v=y8hkb9ZYIA4&:550:0]

[SketchFlow] Convert SketchFlow Feedback into TFS Work Item

Within
Expression Blend 4, SketchFlow enables to build (with WPF or Silverlight) application
prototypes to exactly define, in collaboration with the customer, the
navigation map and the functionalities for the future application.

When
the SketchFlow application is executed, the customer can write on each screen,
and add feedbacks to notify what must be added, updated and deleted. A file
with all the feedbacks can be exported by the customer. Then it’s forwarded to
the development team. This file can be imported on Blend to list all the
customer feedbacks.

Each
feedbacks can be convert into TFS Work Item through a right-click on each
feedback items and a click on « Convert Feedback into TFS Work Item » :

 

The Work
Item type must be selected (« Task » in our example) :

 

It’s possible
to complete information about this new Work Item with the following interface :

 

And
when the Work Item is saved, a dialog box appears with its ID number and a link
to visualize it :

 

Finally in
TFS, the Work Item, created by the customer SketchFlow Feedback, can be
visualized :

[Silverlight] Three new Silverlight applications templates

Three
new templates
are available for Silverlight applications, both Silverlight
Business Application and Silverlight Navigation Application. Furthermore style
elements of these new themes are available to download, you can use them in any
Silverlight applications. Finally these new themes include the new WCF RIA
Services templates.

These three
new templates are :

 

There
are downloadable at this address, and these templates can easily be installed
in Visual Studio 2010 :

And
Expression Blend 4 :

[OData] Use OData data with WCF Data Services and Silverlight 4

As seen before, OData (Open Data Protocol) is a protocol on top of HTTP, with a REST architecture and provide XML formatted data (e.g.: ATOM).
WCF Data Services, included in Silverlight 4,will help us to handle data with
OData.

The purpose is to build a Silverlight master-detail
application composed of a ComboBox control to choose a customer and a DataGrid
Control to see all the Customer orders. We will use Customers and Orders tables
from Northwind OData feed which is at this address : http://services.odata.org/Northwind/Northwind.svc/

Add Service Reference

We create a Silverlight 4 application and we add, in the
Silverlight project, a reference to the service which have the Northwind
database OData feed. We will use WCF Data Services. Right-click on the
Silverlight project, and a click on “Add Service Reference”, a dialog box
appears and the address of the OData feed must be written in the textbox
“Address”. And finally, click on the “Go” button to verify the data exposed by
the service.

Add Service Reference

After clicking the “Ok” button, you will find, in the
hidden files of your solution, that WCF Data Services added 3 new files :

  • Reference
    datasvcmap : it’s a configuration file which contains all information about
    your service.
  • Reference.cs
    : it’s an Entity Data Model file which contains all data information about your
    database.

Add Service Reference

Then an XAML interface is built to display data with a ComboBox control and
a DataGrid control :

<UserControl.Resources>
  <DataTemplate x:Key=”DataTemplateComboBoxItelCustomers”>
    <TextBlock Text=”{Binding ContactName}”/>
  </DataTemplate>
</UserControl.Resources>

<Grid x:Name=”LayoutRoot” Background=”White”>
  <Grid.RowDefinitions>
    <RowDefinition Height=”40″/>
    <RowDefinition Height=”*”/>
  </Grid.RowDefinitions>
  <ComboBox Grid.Row=”0″ ItemTemplate=”{StaticResource DataTemplateComboBoxItelCustomers}” Name=”cbCustomers”     
  
SelectionChanged=”cbCustomers_SelectionChanged” />
  <sdk:DataGrid AutoGenerateColumns=”True” Grid.Row=”1″ Name=”dgOrders” />
</Grid>

From the
code-behind point of view, the following members have to be declared to use the
data from your service :

private DataServiceCollection<Customer> _customers;
private DataServiceCollection<Order> _orders;
private Customer _selectedCustomer;
private NorthwindEntities _context;

Then,
in the Loaded event of your User Control, the context must be initialized with your
service and the objects must be initialized to cache Customers and Orders data,
through DataServiceCollections of WCF Data Services. A Linq request will select
all the Customers and sort them by ContactName. Finally, the LoadSync method is
called to collect the data from the Linq request:

_context =new NorthwindEntities(new
Uri(“http://services.odata.org/Northwind/Northwind.svc/”));

_customers =new DataServiceCollection<Customer>();
_customers.LoadCompleted +=
new
EventHandler<LoadCompletedEventArgs>(_customers_LoadCompleted);

_orders =new DataServiceCollection<Order>();
_orders.LoadCompleted +=
new
EventHandler<LoadCompletedEventArgs>(_orders_LoadCompleted);

var query = from g in _context.Customers
            orderby
g.ContactName
            select g;
_customers.LoadAsync(query);

 

When the LoadAsync is called, the LoadCompleted event will be raised when
the data loading is finished. You then just need to bind the data to your controls
:

void
_customers_LoadCompleted(
object
sender, LoadCompletedEventArgs e)
{
 
if (_customers.Continuation !=null)
  {
     _customers.LoadNextPartialSetAsync();
  }
 
else
  {
     cbCustomers.ItemsSource
= _customers;
     cbCustomers.UpdateLayout();
     cbCustomers.SelectedIndex
= 0;
  }
}

void _orders_LoadCompleted(object sender, LoadCompletedEventArgs e)
{
 
if (_orders.Continuation !=null)
  {
     _orders.LoadNextPartialSetAsync();
  }
 
else
  {
     dgOrders.ItemsSource
= _orders;
     dgOrders.UpdateLayout();
  }
}

Then
in the SelectionChanged Customers’ ComboBox, when the user chooses a Customer,
the CustomerID is used to select the corresponding orders in the Linq request.
The LoadAsync method allows to load the data:

_orders.Clear();
_selectedCustomer
=
cbCustomers.SelectedItem
as
Customer;

if (_selectedCustomer !=null)
{
   var query
= from
g
in _context.Orders
              
where g.CustomerID == _selectedCustomer.CustomerID
              
select g;
   _orders.LoadAsync(query);
}

 

Finally,
there we are :

Add Service Reference

[OData] Create and expose your own OData flow

OData (Open Data Protocol) is
a protocol on top of HTTP, based on REST architecture, that allows remote edition and use for different
clients (RIA,
Web, Mobile, etc…). So one can use Linq to request data from a client interface.

With WCF RIA Services, it becomes possible to
expose your own data easily. When you add a DomainService, you must check the box
“Expose OData endpoint” and the selected tables will be exposed. In our
example, we will expose the Customers and Orders tables from Northwind
database.

 

Checking the box brings two important modifications to our code:

  • In the DomainService,
    now each parameterless methods have a specific tag :

[Query(IsDefault = true)]
public
IQueryable<Customers> GetCustomers()
{
  return this.ObjectContext.Customers;
}

  • In the Web.Config, an “endpoints” is added in the
    domainServices area :

<system.serviceModel>
  <domainServices>
      <endpoints>
         <add name=”OData”
             
type=”System.ServiceModel.DomainServices.Hosting.ODataEndpointFactory,
                   
System.ServiceModel.DomainServices.Hosting.OData,
                   
Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35″

/>
     
</endpoints>
  </domainServices>
</system.serviceModel>


Now you can compile, run the application and query in a browser the
following URL (which is built like that):

http://localhost:1234/SolutionName-Web-DomainServiceName.svc/OData/

N.B : if the application name and/or the DomainService name contains dots
“.”, you must replace them by dashes “-“.

In our example the correct URL is: http://localhost:2321/RIAOData-Web-NorthwindService.svc/OData/

At the end, we can see all the tables we had selected before.

 

If you want
to consult the data from a specific table, Customers for example, you can just
complete the URL with the same table name:

http://localhost:1234/SolutionName-Web-DomainServiceName.svc/OData/CustomersSet/

The data have been formatted like for ATOM, the browser tries to display it
like a RSS feed. So with a right-click in the browser page, you can select the
option “View Source” to see the Customers data:

 

If one
wants to practice, it can use the OData website where a lot of exposed data can
be found. A feed list is available on the OData website.

My new English blog !

Hi everybody !

This is my first post on my new English blog ! I
really want to say thank you to Matthieu, Kathleen
and Susan who give me that
chance to open this blog here. I’m a MVP Client Application Development,
so this blog will talk about RIA and RDA, specially Silverlight, WPF,
Surface, WCF RIA Services and OData. I’m French, so sorry in advance for
my English, but promised I will try to make my best ! 🙂

See you soon !