Learn WCF RIA Service: Day 4

Learn WCF RIA Service: Day 1

Learn WCF RIA Service: Day 2

Learn WCF RIA Service: Day 3

Adding Custom Query in Domain Service Class

On Day 3 we went deep in generated Domain Class. Now we do have an understanding on what all methods are there in the Domain Service Class.

First question we need to ask ourselves is that, “why we need to add custom query in domain service class.

We add custom query in Domain Service class to achieve certain application logic. When to meet certain business requirement we add custom query as application logic in Domain Service Class , that gets available in both presentation layer and middle layer.

Let us go ahead and try to achieve below business task

  1. Passing parameter to query. Such as we want to fetch a particular person of a given person id.
  2. Retuning multiple and single results from query

We will see how we could add query methods to the generated Data Context class.

To return a single object, you need to make query iscomposable attribute to false. Any method returning single object should be attributed as below,

clip_image001

To filter out person on a particular PersonID you can use lambda expression as below,

SchoolRIAService.cs


[Query(IsComposable=false)]
        public Person GetPersonById(int PersonID)
        {
            return this.ObjectContext.People.SingleOrDefault(p => p.PersonID == PersonID);
        }

Above code would fetch you a single person of given person Id. At the presentation layer in Silverlight client you can call above custom query as below,

MainPage.xaml.cs

using System.Windows.Controls;
using DemoDay2.Web;
using System.ServiceModel.DomainServices.Client;
namespace DemoDay2
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
            SchoolRIAContext proxy = new SchoolRIAContext();
            LoadOperation<Person> loadPerson = proxy.Load(proxy.GetPersonByIdQuery(1));
            grdRIADataGrid.ItemsSource = loadPerson.Entities ;
        }
    }
}

If you want to add custom query as of your requirement returning multiple entities, you can add a method in Domain Service Class.

SchoolRIAService.cs

</span>
<pre>
public IQueryable<Person> GetPersonByStartName(string startchar)
        {
            return this.ObjectContext.People.Where(s => s.FirstName.StartsWith(startchar));
        }

Above method will return all the people with given start name. At the presentation layer in Silverlight client you can call above custom query as below,

MainPage.xaml.cs


using System.Windows.Controls;
using DemoDay2.Web;
using System.ServiceModel.DomainServices.Client;
namespace DemoDay2
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
            SchoolRIAContext proxy = new SchoolRIAContext();
            LoadOperation<Person> loadPerson = proxy.Load(proxy.GetPersonByStartNameQuery("d"));
            grdRIADataGrid.ItemsSource = loadPerson.Entities ;
        }
    }
}

Today we saw how we could add custom query to Domain Service Class matching our business requirement. We can add any application logic in Domain Service Class to be shared in between presentation layer and middle layer.

See you on day 5 Smile

Learn WCF RIA Service: Day 1

Learn WCF RIA Service: Day 2

Learn WCF RIA Service: Day 3

Learn WCF RIA Service: Day 3

Learn WCF RIA Service: Day 1

Learn WCF RIA Service: Day 2

Overview of Domain Service Class

On day 2 we ran through creating our first line of business application using RIA services. We did not look deeper much into generated classes. Today we will take a step back and examine Domain Service class. Better understanding on Domain Service class would give us more efficiency to create, share and focus on application logic in between presentation layer and middle layer.

If you go ahead and open solution explorer of project we created on day 2, you will get two projects, one Silverlight and other hosting web project there.

image

If you remember we added Domain Service class to the web project and we enabled metadata for domain services as well. Due to that you are seeing there is a metadata.cs class in solution explorer.

Open SchoolRIAService.cs and first thing you would notice would be as in below image.

image

  1. Attribute EnableClientAccess tells this domain class would be visible to the presentation layer residing at client side.
  2. The class is derived from LinqToEntitiesDomainService. This is generic abstract class and it is part of WCF RIA Service framework.
  3. Generic class is taking data model class as input to create domain service.

Next you move further in the class you will find different CRUD methods for different entities from data model. If you have four entities in data model then you will have 16 methods here. For each entity there would be four methods.

Below four methods would perform CRUD operation for Course entity.

image

In this way you will find CRUD methods for other entities like Departments, OnlineCourses etc

If you remember at time of creating Domain Service class, we checked the check box against entities class to enable editing.

image

Besides different generated methods you can plug application logic in this Domain Service class. You can modify existing application logic also for example modifying logic behind GetCourse() method.

I will conclude day 3 with assumption that by now you have an overview of Domain Service Class.

See you on day 4 Smile

Revised Windows Azure Portal: 2nd August 2011

Microsoft Azure team has come nicely with revised Windows Azure Portal. Some of the new features of portal are as below,

1. The portal is now can be worked with 11 different languages. Supported language are listed on left top before billing.

 

clip_image001

 

2. Performance improvement by increasing isolated storage quota. When you open Azure portal, you will be asked to increase the size of quota for better performance.

 

clip_image003

 

3. Running deployment can be deleted without stopping. Earlier as a developer you had to stop the deployment before deleting that.

 

4. Context menu in Hosted Service and CDN pane

 

clip_image004

I am very happy seeing revised portal Smile Kudos to Azure team

 

Learn WCF RIA Service: Day 2

Learn WCF RIA Service: Day 1

Building a simple line of business Application using RIA Services

Last day we had enough of theory on RIA Services. Today let us get our hand dirty and launch visual studio. We will keep it simple and easy in flow since it is our first exercise.

Essentially we will fetch data from Person table and bind it to grid view on Silverlight page. Flow diagram of the application we are going to make would be as below diagram.

image

Building Silverlight Application

Start with creating a Silverlight application.

image

Next you will get prompted to choose

  1. Web site to host Silverlight application
  2. Select Silverlight target version

And if you have WCF RIA services installed then you will get a check box to instruct Silverlight whether you want to enable WCF RIA Service for this particular Silverlight application or not ? You want to enable WCF RIA service so check the checkbox Enable WCF RIA Services.

image

We have instructed Silverlight application while creating that we want RIA Service enabled and next we need to create Data Model to be exposed and share data through this model in between presentation layer and middle layer.

Building Data Model

To create data model right click on web project [project with extension .web] and choose option add new item from context menu.

image

Next from Data tab you need to choose ADO.Net Entity Model template. We are going to use ADO.Net Entity Model ORM to create data model. ADO.Net Entity Model creates model with extension edmx. Change name of created edmx file to SchoolModel.edmx.

image

After clicking on add button, you will be prompted to choose the source of data model. Either you can create empty data model or choose a database as a source to create data model. We are going to choose School database residing on my local server as a source of data model. So select Generate from database option and click on next button

image

Now you will be prompted to choose database object. If school database is not listed in drop down then you can create a New Connection else you can choose from the dropdown. Let us proceed with creating a new connection

On clicking of New Connection button, you will be prompted with dialog box to specify database connection properties. Specify different values as of below image and click on Test Connection. After getting successful message click on Ok button.

image

In Data connection dialog box if you want you can change the name of connection setting in web.confg. However I am leaving the default name.

image

Click on next button to get last dialog box to conclude data model creation. Here you need to choose database objects you want to put as mart of data model. You can have tables, views, and stored procedures from database as part of data model. I am selecting all tables and stored procedures as part of data model. If you want you can change namespace name but for sake of simplicity at this point leave everything else as default.

image

On click on Finish button you will get data model created and you will get data model file open in designer surface. Here you can notice mapped between the entities.

image

In solution explorer you will find SchoolModel.edmx file has been added under the web project.

clip_image001

Building Domain Service

To create data model right click on web project [project with extension .web] and choose option add new item from context menu.

image

Next from Web tab you need to choose Domain Service class template. Change name to SchoolRIAService.cs and click on Add button.

image

In next dialog box go ahead and

  1. Select DataContext or ObjectContext class from the dropdown. You will get SchoolEntities context class in drop down value to choose.
  2. You can edit Domain Service class name. For simplicity leave it as it is.
  3. You have check box to specify Enable Client Access. Check this check box.
  4. You have check box to specify whether you want to expose ODATA Endpoint. For this time uncheck it.
  5. You will get all the entities from the context class listed. You can choose from them to be exposed as RIA Service. I am selecting all the entities and enabling all entities for edit.
  6. Check the box generates associated class for metadata.

image

Click on OK button and give a second to examine web project in solution explorer. You will find three classes added there. We will examine and talk more on these classes in coming days. Today we are just going to see there uses at the presentation layer in Silverlight project.

image

Using Domain Service in Presentation layer or Silverlight client

You have,

  1. Created data model
  2. Create domain service

Now build the web project and add below namespace on MainPage.xaml.cs file.

image

After adding namespaces open MainPage.xaml and drag and drop a data grid on that. Give name of the data grid as grdRIADataGrid. Xaml would be as below,

MainPage.Xaml


<UserControl xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"  x:Class="DemoDay2.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">

    <Grid x:Name="LayoutRoot" Background="White">
    <sdk:DataGrid x:Name="grdRIADataGrid" />
    </Grid>
</UserControl>

At this point designing of page has been completed. You need to bind data from person table to data grid. For that

1. Create instance of domain service class

clip_image001[5]

2. Load all people to the context of client side proxy

clip_image003

3. Fetch the entities from loaded list of entities and bind as item source of data grid

clip_image004

Code behind would look like as below,

MainPage.xaml.cs


using System.Windows.Controls;
using DemoDay2.Web;
using System.ServiceModel.DomainServices.Client;
namespace DemoDay2
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
            SchoolRIAContext proxy = new SchoolRIAContext();
            LoadOperation<Person> loadPerson = proxy.Load(proxy.GetPeopleQuery());
            grdRIADataGrid.ItemsSource = loadPerson.Entities ;
        }
    }
}

Now you run Silverlight project and in data grid you will get the entire person from school database using RIA Service.

clip_image002

Today we followed a simple path and fetched data from database and bind to data grid on at Silverlight client using RIA Service. We have not gone deeper into different aspect of context class. We will discuss then in further days. I will conclude day two with assumption that by now you know,

  1. Why WCF RIA?
  2. Creating simple line of business application using RIA Services

See you on day 3 Smile

Learn WCF RIA Service: Day 1

Learn WCF RIA Service: Day 1

Why WCF RIA Service ?

It is said that, “If you want to have better understanding on a technology; first find why you need that technology? “ And to adhering above saying, we will start our journey of WCF RIA learning with a discussion on “Why WCF RIA Service? “

Most developers are very much accustomed to work with layered architectures. Usually there are three layers;

 

image

 

It is presentation layer talks to business layer and yield UI output to the client. In normal web application UI output at the client talks to presentation layer over Internet. There is extremely close proximity between presentation layer and business layer and so in between business layer and data layer. Somehow UI rendered at client side does not contain any business specific code and takes instruction from presentation layer and render the html output.

When you use Silverlight to create N-tier Rich Internet Application, presentation layer reside at the client side and it talks to middle layer over the Internet.

 

image

 

In Silverlight application portion of business logic such as validations may reside at middle layer and presentation layer or may be at both layers. In most of the time business logic to and fro in between middle layer and presentation layer

 

image

 

There are astronomical amount of data and many business logics involved in creation of large line of business application. There are many clients pulling and pushing data to the server, authorizing and authenticating with server, validating users and data in the server and to accommodate all these as a developer you will have to write complex services and plug them with the clients. Majority of development time goes in creation of service, managing service, configuring endpoints and then working with proxies at the client side. Imaging if all service related work is being automated then as a developer you could give all your time to application logic. WCF RIA Service does the work of automation for you.

To create powerful Silverlight line of business application, application logic must be shared in between middle layer and presentation layer. WCF RIA Service does this for you without duplicating the code in both layers.

 

image

 

WCF RIA Service shares application logic between view [Presentation layer] and data access layer within a trust boundary. WCF RIA Service eliminates WCF or web service to share the application logic between the layers over Internet. WCF RIA Service acts as a plug in between presentation layer and middle layer. It creates a programming view between these layers to work with application logic.

As name suggests WCF RIA Service is based on WCF Service. It abstracts many complexity of WCF Service from you to focus on business or application logic. However it does not restricts you to use powerful features of WCF in your line of business Silverlight application.

We can summarize now that why we need WCF RIA Service as

  1. It helps line of business application to connect with data and of course without writing WCF or Web services.
  2. It helps application logic to be shared in between entities residing in both presentation layer at the client and middle layer at the server and of course without duplicating application logic code.
  3. It helps in tracking changes in entities at both sides.
  4. It helps in avoiding creation of complex services, management of endpoints and proxy management at client side while connecting data from client in rich line of business application.

 

With hope I am concluding day 1 that you have an understanding on WCF RIA Service. And even if you have some doubt let us directly jump to the code and create a basic application on day 2 and see if you are still having doubt in your mind. See at day 2  Smile