Posts Tagged ‘WCF’

Silverlight, WCF, Entity Framework 4 ALL IN ONE

May 13, 2010


About 2 months ago, I was so exciting when my boss asked me to have a talk in his room. I thought my salary would be increased but my boss gave me a task that is no less exiting than a higher salary (Wait, a higher salary is better, I think ^^). All I had to do is implementing a pilot project. The project is not special, just for managing documents in our company. Yet because I am working in R&D team, main purpose of this pilot project is for demonstrating what a RIA is. The pilot project, called Lighter, will tell you.

Because I had to finish this job in my own, I had some technology options to choose carefully. There was some choices available: Java FX, Adobe Flex, Microsoft Silverlight… At last, I decided to use Microsoft Silverlight to go.

What is ALL, What is ONE

At the time I was starting, Microsoft had been released Visual Studio 2010 beta 2 with Microsoft Silverlight 3.0. But I knew Silverlight by itself couldn’t shine as I need. I spent 1 week to complete the solution architecture of the project with the combination of Prism2 – Silverlight, WCF Services and Entity Framework 4 (of course beta 2). Let’s take a little deeper look at them.

Prism 2 – Silverlight

Silverlight on it own can make your website brilliant, but for building an application at enterprise level, we need a more powerful tool. I realized that Prism 2 can make it count. So what is Prism 2? Prism 2 is a Composite Client Application Guidance which designed to help you more easily build modular Windows Presentation Foundation (WPF) and Silverlight client applications. A composite client application is typically comprised of several modules, featuring multiple screens, rich, flexible user interaction and data visualization, and role-determined behavior. It uses loosely coupled, independently evolvable pieces that work together in the overall application.

For more information about Prism 2 and Composite Client Application, refer to

WCF Services

Windows Communication Foundation (WCF) is Microsoft’s unified programming model for building service-oriented applications. It enables developers to build secure, reliable, transacted solutions that integrate across platforms and interoperate with existing investments. It brings Lighter into SOA world. All business features of Lighter are implemented using WCF services.

For more information about WCF, refer to

Entity Framework 4

Entity Framework (EF) and Linq to SQL are 2 ORM tools come from Microsoft. While Linq to SQL is lightweight and applied for Microsoft SQL Server database only, EF is more powerful and can be used at enterprise level. At the time Lighter is kicked off, the latest version of EF is EF4 beta 2. It supports many compelling new features such as: Persistence Ignorance & POCO, Self-Tracking Entities & N-Tier support, lazy loading…

I decided to use EF4 to manipulate data in Microsoft SQL Server in Lighter project.

For more information about Entity Framework, refer to

ALL – in – ONE

Well, I combine them in one solution to implement Lighter project. Let’s take a look at the architecture overview.

Architecture Overview

Architecture Overview

In above diagram, there are 2 types of components: Library components and Application components. Library components are built-in framework components that be used in the solution. Application components are components that be developed by developers in charge in project.

Library components

  • Shell Infrastructure: Provides infrastructure to host a application shell and plug application modules. This component in Lighter is provided by Prism 2.
  • Service Locator: Provides the way for other components to lookup services. Services may be implemented as funtionality classes or web services or WCF Service. In this system, I use Microsoft Unity Container as a Service Locator.
  • Cross-cutting components: Utility components that commonly used in other components of application. Some cross-cutting features they provides are:
    • Security
    • Operation management
    • Communication
    • Logging & Tracing
    • Caching
    • Exception Handling…
  • ORM (Object Relational Mapping) Framework: A framework for converting data between incompatible type systems in relational databases and object-oriented programming languages. This creates, in effect, a “virtual object database” that can be used from within the programming language. Lighter uses Entity Framework 4 POCO as it database access component.

Application Components

  • View: GUI component that presents visual information to end user.
  • Presentation Model: Encapsulates presentation behavior and state and acts as a business façade for invoking services to accomplish requests from View (user).
  • Model: Presents in conceptually what system is. It contains all conceptual entities in system and reveal how these entities interact with others. To be noticed that these triad are come from Model-View-ViewModel pattern. I will describe it later.
  • Service: Provides business logic functionality. Because SOA approach is promoted and adopted, all services are implemented using Microsoft Windows Communication Foundation.
  • Data Access: Encapsulates logic of accessing and manipulating data in a data storage. This system uses Microsoft SQL Server as its underlying database.

The implementation

In this section, we are going to dig deeper in the solution and some challenges of the project. First of all, let me show you a list of technology and tools used by Lighter solution:

  • Platform: Microsoft .NET Framework 4.0 beta 2, Microsoft WCF, Microsoft Silverlight 3.0
  • IDE: Microsoft Visual Studio 2010 beta 2
  • Programming Language: C#
  • Storages: Microsoft SQL Server 2008 Express Ed., XML file system.
  • Frameworks: Microsoft Prism 2, Microsoft Entity Framework 4 beta 2, Microsoft Unity 2.0.

Below is a capture of the solution in Visual Studio .NET

The solution

The solution

Lighter architecture contains 3 layers: Presentation, Business Services, Data Access.


Presentation layer adopts Prism 2 Silverlight as it main framework. Let’s see how Presentation is organized.




Prism 2 promotes us a modular architecture style. That is you have a Shell project with a Bootstrapper to start the Shell and load all modules that are plugged into the Shell. Each module is responsible for a business usecase. We also have a Web project to load Silverlight shell in browser.


As you can see, Shell project contains 2 main important pieces: Shell and Bootstrapper.

Shell is nothing but a placeholder for Layout module to inject the main GUI layout  onto it. I will describe Layout module later. The placeholder is also called “Region”. This is a term that coined by Prism 2.

Bootstrapper is responsible for several starting stuffs:

  • Initializes a IOC container
  • Initializes all necessary stuffs for system
  • Loads all plugged modules
  • Initializes and shows the Shell to end user.

Prism 2 provides several comfortable ways for loading modules

  1. Loading modules by code.
  2. Loading modules in configuration file.
  3. Loading modules in XAML.
  4. Loading modules in a directory.

Because Lighter is using Silverlight, so I picked the method number 3 and you may know why ModulesCatalog.xaml is existing in Shell project.

Layout Module

Layout Module

This module contains only one View is main layout frame of system. I use MVP pattern to implement this View. The main layout contains a header and a content panel.

  • Header: Displays Lighter application title and  defines regions for Search & Profile areas.
  • Content: defines a region for main content of Lighter application.

Other modules will inject Views and other GUI elements into these regions.

Management Module

Management Module

This module implements business usecases of Lighter application. I use Model-View-ViewModel pattern (MVVM) as the dominated pattern for organizing workitems in this module. (You can refer to or for MVP pattern and for MVVM pattern).

There are some important parts in this module

  • Module Bootstrapper
  • Models
  • Views
  • ViewModels
  • Controllers
  • Events and Commands

Module Bootstrapper

Each module has 1 bootstrapper, it is an implementation of interface Microsoft.Practices.Composite.Modularity.IModule. Shell bootstrapper when loading plugged module, it arquires for module bootstrapper and invokes method Initialize() of it.

Thus, a module bootstrapper is typically responsible for

  • Initializes all services and registers them into system IOC container.
  • Initializes all controllers and registers them into system IOC container.
  • Initializes all Views – ViewModels and registers them into system IOC container.
  • Injects specific Views into appropriate regions, necessarily required by business logic.


This contains model classes of Presentation layer only. They are defining the data to be displayed or otherwise acted upon in the user interface. So they need be utilizing interface INotifyPropertyChanged and class ObservableCollection to take advantages of Data Binding mechanism of WPF and Silverlight.


A View is an interface that displays data (the model) and routes user commands (events) to the ViewModel to act upon that data.


A ViewModel is a “Model of the View” meaning it is an abstraction of the View that also serves in data binding between the View and the Model. It could be seen as a specialized aspect of what would be a Controller (in the MVC pattern) that acts as a data binder/converter that changes Model information into View information and passes commands from the View into the Model. The ViewModel exposes public properties, commands, and abstractions. The ViewModel has been likened to a conceptual state of the data as opposed to the real state of the data in the Model.


In Presentation layer, controllers play the role of a interacting bridge between ViewModels and Services, between ViewModels and between Modules using Events.

Events and Commands

Events and Commands are the ways for communication between 2 modules and 2 piece-of-logic workitems. Because modules in a Composite Application are independent, events and commands are methods for 2 independent modules talking with each other. Inside a module, they can be used similarly. For example, a View can send action from User to a ViewModel using Comamnd. Prism2 supports a rich bunch of classes and interfaces for us to work with events and commands.

Business Services

Now we go to the second part of the system. All services in EMDS system are implemented using WCF. Because Presentation is working with Services by Service Contract interfaces. So Business services can be implemented by classes or standard Web services.

Another important part is DTO classes. In the early stage, I used EF4 POCO model classes as data contract of WCF services. It led to many complicated problems. Then I found a solution for this. That is Lighter solution has 3 types of model classes

  • EF4 POCO model classes: will be described later in Data Access section
  • WCF data contract model classes
  • Presentation model classes: already described in Presentation section.

Data Access

Data of EMDS is stored in Microsoft SQL Server and XML files. For accessing XML files, I use Linq to XML. For accessing database, I use EF4 beta 2 and applying its new feature POCO support. I want to say thank you to I want to say thank you to Daniel Wertheim, the man providing us very helpful framework of EF4 POCO (


This is not a real world application. Yet it successfully demonstrates the way you putting Silverlight – WCF – EF4 POCO in a business application.

I host it at

Your feedback is very important.

Nguyen Minh Dung.