- Create Silverlight Application Project, enter project as MVVM_RIA_ICOMMAND_CUSTOMVALIDATION and solution name as MVVMRIAICOMMANDCUSTOMVALIDATION. Make sure that Enable RIA Service Link check box is selected.
- Create folder in Server application, with name Model.
- Before actually we start working with our example we will create a sample Data base and a table. Data base name will be MVVMDomainData and Table name is Employee. I am using SQL Server 2008 Express. Now we have environment ready for the development.
- Add a new ADO.NET Entity Data Model item to the model folder in the server project. Name it as EmployeeModel. Click on Add
- Select Generate from database and click on Next.
- Add new connection, as we create in ADO.NET. Select the database that we created. And click on Next. Select the check box "save entity connection string in We.Config as".
- The Dialog box comes up with all the Tables, View and SP that we have created in our DB, MVVMDomainData. Select the table that we created, Employee. And click on Finish.
- Let us look at the class that generated for us. This has Context and
Entities. Let us see the Entities where we have to understand what
exactly it is. The class inherits from the EntityObject. EntityObject
class inherits from the StructuralObject. StructuralObject implements
INotifyPropertyChanging, INotifyPropertyChanged interfaces. Out of all,
we need to understand purpose of the following methods. These methods
will track the change make to the data and in UX and keep update the
Context object so that the changes reflects in all the places where the
context object is being shared.
protected virtual void OnPropertyChanged(string property);
protected virtual void OnPropertyChanging(string property);
protected virtual void ReportPropertyChanged(string property);
protected virtual void ReportPropertyChanging(string property);
Going back to the generated entityonject which will have a Factory method that can used as to create new entity object and Properties for the each filed in the data base.
/// <summary>
/// No Metadata Documentation available.
/// </summary>
[EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)]
[DataMemberAttribute()]
public global::System.String FirstName
{
get
{
return _FirstName;
}
set
{
OnFirstNameChanging(value);
ReportPropertyChanging("FirstName");
_FirstName = StructuralObject.SetValidValue(value, false);
ReportPropertyChanged("FirstName");
OnFirstNameChanged();
}
}
private global::System.String _FirstName;
partial void OnFirstNameChanging(global::System.String value);
partial void OnFirstNameChanged();
- EntityKeyProperty --> tell that it is autogenerated or not.
- IsNullable --> tell us that is allows null in the DB or Not.
- DataMemberAttribute --> Each is field is decorated and mapped to DB table field.
- ReportPropertyChanged("FirstName"); OnFirstNameChanged(); ReportPropertyChanged("FirstName"); OnFirstNameChanged(); are used for tracking the changes happening to the field value.
- Right on the Model folder and click on add new item. Select the Visual C# on the left side and select Domain Service class on the left side. Name the class as EmployeeDomain.cs, and click add.
- Build the Solution (F6).
- The Model that we created will automatically picked up by the Visual Studio. Show all the tables that we have selected during the model creation. Select the Entities and Enable Editing Check boxes. By selecting Enable editing check box will generated the methods required for Delete, Update and Insert. Also select Generate associated class for metadata, which we will see later part of the example. And click On
- Build the solution
- Let us see the generated domain class.
[EnableClientAccess()]
public class EmployeeDomain : LinqToEntitiesDomainService<MVVMDomainDataEntities>
{
public IQueryable<Employee> GetEmployees()
{
return this.ObjectContext.Employees;
}
public void InsertEmployee(Employee employee)
{
if ((employee.EntityState != EntityState.Detached))
{
this.ObjectContext.ObjectStateManager.ChangeObjectState(employee, EntityState.Added);
}
else
{
this.ObjectContext.Employees.AddObject(employee);
}
}
public void UpdateEmployee(Employee currentEmployee)
{
this.ObjectContext.Employees.AttachAsModified(currentEmployee,this.ChangeSet.GetOriginal(currentEmployee));
}
public void DeleteEmployee(Employee employee)
{
if ((employee.EntityState == EntityState.Detached))
{
this.ObjectContext.Employees.Attach(employee);
}
this.ObjectContext.Employees.DeleteObject(employee);
}
}
- EnableClientAccess --> Marks Domain Service class as accessible to clients. We must apply theEnableClientAccessAttribute attribute to a domain service to make it accessible to the client application. A DomainContext class is generated for each domain service that is marked with theEnableClientAccessAttribute attribute. When you create a domain service by using the Add New Domain Service Class wizard, the wizard contains a check box called Enable client access that is selected by default. The EnableClientAccessAttribute is applied to the domain service when this check box is selected.
- GetEmployees() --> is of type IQueryable<Employee> which allows to apply LINQ queries in the client.
- Also we can see the methods for CRUD.
- Create ViewModel folder in Client Project. And then add EmployeeViewModel.cs class.
- Add reference System.ServiceModel.DomainServices.Client dll.
- Add following using statements
using System.ComponentModel; --> we will be sing INotifyPropertyChanged interface
using System.ServiceModel.DomainServices.Client;
using MVVM_RIA_ICOMMAND_CUSTOMVALIDATION.Web.Model; -- > Through RIA services the link will be available to Mode in the Server project - Implement the INotifyPropertyChanged interface in the view model
class. With that we will have following code in the Employee View Model
class
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged (string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged (this, new PropertyChangedEventArgs (propertyName));
}
}
- Declare a class scope variable for EmployeeDomain, is
located in the Generated_Code folder. This is hidden folder in the
client project. EmployeeDomain class provides access to its instance
members that are generated in the EmployeeDomainService class.
private EmployeeDomain _employeeDomainContext = new EmployeeDomain();
public EmployeeViewModel()
{
_employeeDomainContext.Load<Employee>(_employeeDomainContext.GetEmployeesQuery());
}
- What Next? remember anything that we need to bind to
the View from View Model should be a property in the corresponding View
Model class. So, first thing we need to bind is all employees to the
grid. So this, Employees, property will the data context for the view.
public EntitySet<Employee> Employees
{
get
{
return _employeeDomainContext.Employees;
}
}
-
Provide a property for the Selected Employee Recode in the View Model so
that, this will help in pointing to the selected in the Grid.
private Employee _employee = null;
public Employee SelectedEmployee
{
get
{
return _employee;
}
set
{
_employee = value;
RaisePropertyChanged("SelectedEmployee");
}
}
- Create a folder 'View' in the Client Application and add a new Silverlight User Control to It. Name it as EmployeeGrid.xaml
- Add Referance of the View Mode to the User Controls and then add the
DataGrid to the root Grid.
<UserControl x:Class="MVVM_RIA_ICOMMAND_CUSTOMVALIDATION.View.EmployeeGrid"
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"
xmlns:ViewModel="clr-namespace:MVVM_RIA_ICOMMAND_CUSTOMVALIDATION.ViewModel"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400"xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk">
<Grid x:Name="LayoutRoot" Background="White">
<sdk:DataGrid Name="employeeGridView"/>
</Grid>
</UserControl>
- Go to main page and add View Reference and then add
the Emplyee Grid view user control that we created.
<UserControl x:Class="MVVM_RIA_ICOMMAND_CUSTOMVALIDATION.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"
xmlns:Views="clr-namespace:MVVM_RIA_ICOMMAND_CUSTOMVALIDATION.View"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<Grid x:Name="LayoutRoot" Background="White">
<Views:EmployeeGrid x:Name="employeeGrid" />
</Grid>
</UserControl> - Now we will bind to the Employee Grid with the
Employee View Model. Bind the Property "Employees" that we created in
Employee View Model, in the View using ItemSource.
<UserControl x:Class="MVVM_RIA_ICOMMAND_CUSTOMVALIDATION.View.EmployeeGrid"
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"
xmlns:ViewModel="clr-namespace:MVVM_RIA_ICOMMAND_CUSTOMVALIDATION.ViewModel"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400"xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk">
<Grid x:Name="LayoutRoot" Background="White">
<sdk:DataGrid Name="employeeGridView" ItemsSource="{Binding Employees}"/>
</Grid>
</UserControl> - Coming back to the MainPage.xaml, Add the following
code to the MainPage.xaml.cs.
employeeGrid.DataContext =new MVVM_RIA_ICOMMAND_CUSTOMVALIDATION.ViewModel.EmployeeViewModel();
- That's it, Build and run your application. I know that you will get many questions now. Let us some of them.

Jim CroninPosted Dec 21, 2010, 5:12 PM
It's the only one I've found that shows how to bring the domain service through a view model and into a view. Thank you so much for writing it. If possible, could you explain how to bind the view model to a data form instead of a grid? I just can't seem to get that last concept. Many Thanks!
divya srinivasanPosted Dec 9, 2010, 11:33 AM
i registered only to say u thanks.. man u r genius..with ur step by step procedure..it was so easy to get it..as a beginer i was so happy to get this articles..thanks a ton... please post more articles on MVVM for people like us....u rock n thanks a ton man..thanks