Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Building WPF Applications with Self-Tracking Entity Generator and Visual Studio 2012 - Project Setup

0.00/5 (No votes)
17 Mar 2013 2  
This article describes the project setup of building a WPF sample application with Self-Tracking Entity Generator and Visual Studio 2012.

Contents

Introduction

In this article, we introduce a new version of the "Self-Tracking Entity Generator for WPF/Silverlight" built as a Visual Studio 2012 Extension, and we will look into how to build a demo application SchoolSample with this Extension. Please note that this article is based on a previous article on Self-Tracking Entity Generator for Visual Studio 2010 with updates on all the new features and enhancements. Also, we are assuming that we are building n-tier applications with the following architecture:

Before we start, let us first take a look at the Extension's main features:

  • Auto-generate IClientChangeTracking interface implementation for all entity classes that provide client-side change tracking through each entity object. The interface includes methods and properties such as AcceptChanges(), RejectChanges(), HasChanges, and GetObjectGraphChanges() etc.
  • Auto-generate INotifyDataErrorInfo interface (for .NET 4.5) or IDataErrorInfo interface (for .NET 4.0) implementation for all WPF entity classes and/or auto-generate INotifyDataErrorInfo interface implementation for all Silverlight entity classes.
  • Auto-generate Display attributes and auto-generate validation attributes for validation logic on both property and entity levels.
  • Optionally auto-generate ClientQuery and ClientFilter class implementations that provide the capability to dynamically build client-side LINQ queries for sorting, paging and filtering.
  • Optionally auto-generate IEditableObject interface implementation for all entity classes that provides functionality to commit or rollback changes to an object that is used as a data source.

With these auto-generated functionalities, along with authentication and authorization through Windows Identity Foundation (WIF), we should be able to build WPF LOB applications much more quickly.

This, however, does not mean that we can develop any type of WPF applications with self-tracking entities. First, using self-tracking entities usually means that we need to develop both client and server assemblies with Microsoft .NET 4.0 and beyond. For non-.NET clients, it is a big obstacle to create change-tracking behaviors and consume WCF services built with self-tracking entities.

Another limitation is that we need to share the full entity classes on both client and server sides. In cases where we do not want to send all of an entity's properties to the client, we may have to develop an application with part of the data model using self-tracking entities, and the remaining part using DTO classes.

The Demo Application

The demo SchoolSample is a WPF application built with MVVM (MVVM Light Toolkit), Self-Tracking Entity Generator for WPF/Silverlight, and MEF. This simple application allows users to add/delete/update a student, an instructor, or a course and its enrollments. The Save button saves changes to the current selected item, and the Cancel button cancels any change made to that item. The Save All button loops through the whole list and saves changes for every item, and the Cancel All button loops through and cancels changes for all items. This sample does not show how to do authentication and authorization using WIF. For information about WIF, you can check Vittorio Bertocci's book Programming Windows Identity Foundation.

System Requirements

In order to build the demo application, we need:

Supported Operating Systems:

  • Windows 7, Windows 8, Windows Server 2008 R2, or Windows Server 2012

Other requirements:

Installation

After downloading the demo application source code on your local disk, we need to complete the following two steps:

Installing the VS2012 Extension

First, we need to download Self-Tracking Entity Generator for WPF/Silverlight Setup from the Downloads section of the project site. Extract its content, and run the installation package.

This Visual Studio Extension Installer adds the following Extension to Visual Studio 2012, and you can verify that by going from "TOOLS" -> "Extensions and Updates..." as shown below.

The Visual Studio Extension, C# Self-Tracking Entity Generator for WPF and SL, is a C# Entity Framework project item that generates self-tracking entity classes for WPF/Silverlight applications.

Installing the Sample Database

To install the demo database, please run SQL script School.sql included in the download source code. This script creates the SCHOOL database schema needed for our demo application.

Building and Running the Sample

After completing the two installation steps above, we are now ready to start building and running the demo application. Please double check that the connectionStrings of the Web.config file in project SchoolSample.Wcf is pointing to your newly created database. Currently, it is set as follows:

<connectionStrings>
<add name="SchoolEntities" connectionString="metadata=res://
*/EntityModel.SchoolModel.csdl|res://*/EntityModel.SchoolModel.ssdl|res://
*/EntityModel.SchoolModel.msl;provider=System.Data.SqlClient;
provider connection string=&quot;data source=localhost;initial catalog=SCHOOL;
integrated security=True;multipleactiveresultsets=True;App=EntityFramework&quot;" 
providerName="System.Data.EntityClient" />
</connectionStrings>

When the demo compiles and runs successfully, we should be able to see this WPF application running like the screen shot below:

Architecture

Solution Structure

Inside the demo's solution file, projects are organized into several solution folders. The Client folder comprises all the projects that eventually builds and runs as client-side WPF demo application, while the Server folder consists of all the projects that provide WCF Services and run inside a web server environment. Next, let us briefly go over the main functionality of each project:

For the projects inside the Server folder:

  • Project SchoolSample.Data.Wcf contains the server-side auto-generated entity classes, resource files and any custom validation logic for each entity class.
  • Project SchoolSample.Wcf is the main server-side project with WCF Service classes, which query and update the SCHOOL database through the entity classes from project SchoolSample.Data.Wcf.

For the projects inside the Client folder:

  • Project SchoolSample.Common contains all the common classes and interfaces that are shared among other client projects.
  • Project SchoolSample.Data is the client-side counterpart of project SchoolSample.Data.Wcf, and stores file links to the auto-generated self-tracking entity classes, resource files and any custom validation logic from project SchoolSample.Data.Wcf.
  • Project SchoolSample.WCFService contains the service proxy class for the class SchoolService from project SchoolSample.Wcf.
  • Project SchoolSample.Model defines class SchoolModel that needed for all ViewModel classes of this demo application.
  • Project SchoolSample.ViewModel keeps all ViewModel classes, such as:
    • MainPageViewModel
    • StudentPageViewModel
    • InstructorPageViewModel
    • CoursePageViewModel
  • Project SchoolSample is the main client-side project, and also hosts all the UI logic.
  • Project SchoolSample.Data.Wcf

    Next, let us take a closer look at the server-side project SchoolSample.Data.Wcf.

    The folder EntityModel hosts the ADO.NET Entity Data Model EDM file along with three T4 template files created by Self-Tracking Entity Generator for WPF/Silverlight. The Validation folder includes all custom validation logic defined on entity classes, and the folder Resource keeps resource files needed by both entity classes and validation functions.

    To add the three T4 template files, we first open file SchoolModel.edmx, and then right-click and select "Add Code Generation Item...".

    Next, we will be prompted with the "Add New Item" dialog box. Select "Self-Tracking Entity Generator for WPF/Silverlight", type "SchoolModel.tt" in the Name field, and hit the Add button.

    This will lead us to the second dialog box where we need to enter the entity class namespace "SchoolSample.EntityModel", choose whether to generate optional features, and whether to generate code for WPF, Silverlight, or both. After clicking OK, three T4 template files will be created, and these T4 template files will auto-generate all the self-tracking entity classes based on SchoolModel.edmx.

    Project SchoolSample.Data

    Project SchoolSample.Data contains the file links to the auto-generated self-tracking entity classes, resource files and any custom validation logic from project SchoolSample.Data.Wcf.

    In order to set up project SchoolSample.Data, we first open file SchoolModel.edmx of project SchoolSample.Data.Wcf and select its design surface. From the Properties windows, choose "STE Settings" as shown below:

    Next, we will see the "STE Settings" dialog box where we can choose all the settings needed to set up the client-side project SchoolSample.Data:

    After making our selections on the dialog box, the "Update" button will become available, and one click of that button completes the set up for project SchoolSample.Data.

    If you are curious about what actually has been done when you click the update button, here is the list of tasks:

    1. For client project SchoolSample.Data, verify whether model class folder and validation files folder are different
    2. Verify whether the resource namespace is valid or not
    3. Add a minimum set of required references to client project SchoolSample.Data
    4. If necessary, create the validation files folder for server project SchoolSample.Data.Wcf
    5. If necessary, create the resource files folder for server project SchoolSample.Data.Wcf
    6. If necessary, create the model class folder for client project SchoolSample.Data
    7. If necessary, create the validation files folder for client project SchoolSample.Data
    8. If necessary, create the resource files folder for client project SchoolSample.Data
    9. Add conditional compilation symbol "WPF" to client project SchoolSample.Data
    10. Run custom tool on T4 template files
    11. Create T4 template file links from server project SchoolSample.Data.Wcf to client project SchoolSample.Data
    12. Create validation file links from server project SchoolSample.Data.Wcf to client project SchoolSample.Data
    13. Create resource file links from server project SchoolSample.Data.Wcf to client project SchoolSample.Data

    Wrapping Up

    In this article, we introduced the Visual Studio 2012 Extension "Self-Tracking Entity Generator for WPF/Silverlight" as well as all the necessary steps to set up an environment to build and run the SchoolSample demo application.

    I hope you find this article useful. Please rate and/or leave feedback below. Thank you!

    History

    • August, 2012 - Initial release
    • October, 2012 - Source code update
    • February, 2013 - Update for version 2.1.3
    • March, 2013 - Update for version 2.1.4

    License

    This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

    A list of licenses authors might use can be found here