bc-vnt Posted October 12, 2012 Report Posted October 12, 2012 IntroductionIn 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. 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 class implementation for all entity classes that provides client-side LINQ queries for filtering, paging and sorting. 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 ApplicationThe 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 (Dev - Pro): Vittorio Bertocci: 9780735627185: Amazon.com: BooksSystem Requirementsn order to build the demo application, we need:Supported Operating Systems: Windows 7, Windows 8, Windows Server 2008 R2, or Windows Server 2012Other requirements: Microsoft SQL Server 2005, 2008, 2008 R2, or 2012 Microsoft Visual Studio 2012Self-Tracking Entity Generator for WPF and SilverlightMVVM Light Toolkit V4 (included in the sample solution)InstallationAfter downloading the demo application source code on your local disk, we need to complete the following two steps:First, we need to download Self-Tracking Entity Generator for WPF/Silverlight Setup from the Downloads section of the Self-Tracking Entity Generator for WPF and Silverlight 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 DatabaseTo 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 SampleAfter 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="data source=localhost;initial catalog=SCHOOL;integrated security=True;multipleactiveresultsets=True;App=EntityFramework"" 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:ArchitectureSolution Structurenside 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 data access 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 data access 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 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 as follows: MainPageViewModel StudentPageViewModel InstructorPageViewModel CoursePageViewModelProject SchoolSample is the main client-side project, and also hosts all the UI logic.Project SchoolSample.Data.WcfNext, let us take a closer look at the server-side data access layer 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.DataProject 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 data access 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 of what actually has been done when you click the update button, here is the list of tasks: For client project SchoolSample.Data, verify whether model class folder and validation files folder are different Verify whether the resource namespace is valid or not Add a minimum set of required references to client project SchoolSample.Data If necessary, create the validation files folder for server project SchoolSample.Data.Wcf If necessary, create the resource files folder for server project SchoolSample.Data.Wcf If necessary, create the model class folder for client project SchoolSample.Data If necessary, create the validation files folder for client project SchoolSample.Data If necessary, create the resource files folder for client project SchoolSample.Data Add conditional compilation symbol "WPF" to client project SchoolSample.Data Run custom tool on T4 template files Create T4 template file links from server project SchoolSample.Data.Wcf to client project SchoolSample.Data Create validation file links from server project SchoolSample.Data.Wcf to client project SchoolSample.Data Create resource file links from server project SchoolSample.Data.Wcf to client project SchoolSample.Datahttp://www.codeproject.com/Articles/440703/Building-WPF-Applications-with-Self-Tracking-Entit Quote