Click here to Skip to main content
Click here to Skip to main content

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

By , 17 Mar 2013
 

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    About the Author

    Weidong Shen
    Software Developer (Senior)
    United States United States
    Member
    Weidong has been an information system professional since 1990. He has a Master's degree in Computer Science, and is currently a MCSD .NET

    Sign Up to vote   Poor Excellent
    Add a reason or comment to your vote: x
    Votes of 3 or less require a comment

    Comments and Discussions

     
    You must Sign In to use this message board.
    Search this forum  
        Spacing  Noise  Layout  Per page   
    GeneralGreet WorkmemberSaif8411 May '13 - 8:23 
    thanks a lot , I like this Architectural but I can't deploy component in the Server and Client.How to deploy the application on the server, can you give me an architectural or method for publishing and installing BL and EF on Server?
    GeneralRe: Greet WorkmemberWeidong Shen12 May '13 - 10:07 
    If you can only deploy application on the server side, not on client side, this architecture is definitely not a fit for you. You may need to check about ASP.NET MVC[^].
     
    Thanks!
    GeneralRe: Greet WorkmemberSaif8413 May '13 - 1:02 
    I want deploy application on the server side, and client side but i dont know how install the server component on server and component Client on computers client , your architecture focuses on architecture Client server , view and view model and business logic and model on Client side and than install business logic and other validation and WCF Service and EF and DB on server computer , but how do this ? thanks a lot .
    GeneralRe: Greet WorkmemberWeidong Shen13 May '13 - 15:45 
    You can create a new web setup project to include the output of project "SchoolSample.Wcf", and that setup project can install the server components for you. Then you can create another setup project to include the output of project "SchoolSample", it will install the client components.
     
    Thanks,
    GeneralRe: Greet WorkmemberSaif8413 May '13 - 19:14 
    Thanks a lot for your help. Smile | :)
    GeneralMy vote of 5memberCyclingFoodmanPA30 Apr '13 - 5:12 
    As ususal Weidong, your articles and samples rock. Clear, succinct and well designed and coded.
    GeneralRe: My vote of 5memberWeidong Shen1 May '13 - 2:35 
    Thank you so much! Smile | :)
    GeneralMy vote of 5memberMazen el Senih29 Mar '13 - 6:09 
    Very very great !
    GeneralRe: My vote of 5memberWeidong Shen29 Mar '13 - 14:19 
    Thanks!
    GeneralMy vote of 5memberzhangzq7119 Mar '13 - 17:27 
    This sample is very good for MVVM and EF, thank you very much!
    I want to know where to define the progress bar when retrieving data from WCF?
     
    Ok, I found it, it is BusyIndicator.
    GeneralRe: My vote of 5memberWeidong Shen20 Mar '13 - 13:01 
    多謝!
    QuestionMy vote of 5!memberjim191018 Jan '13 - 14:22 
    What a nice job! The sample code covers most of the wpf and ef issues for beginners. Do you have more complicated sample codes? For example, multiple objects with different types on one screen. How can we do update/insert for different entities at the same time and return information selected from different tables? Appreciate your help. Jim
    AnswerRe: My vote of 5!memberWeidong Shen8 Feb '13 - 17:26 
    Thanks! Just updated the SchoolSample for .NET 4.5 at the source code section of the project site[^]. This updated sample adds new features for filtering, sorting, and paging functionality. Take a look if you are interested, and I will write about it later.
    GeneralRe: My vote of 5!memberjim191011 Feb '13 - 14:56 
    Thanks.
    I ended up creating new objects ( entities and collections of entities). One screen in my case consists of tens of different different entities.
    Any documents regarding to ClientQuery and why needs it (instead of simple and convenient LINQ)?
    GeneralRe: My vote of 5!memberWeidong Shen11 Feb '13 - 15:14 
    You can only run LINQ to Entity query against ObjectContext on the server side, not from the client side. ClientQuery class gives you the capability to create a query on the client side, and it can be serialized and deserialized over the wire and applied against the ObjectContext object.
     
    The other class, ClientFilter, gives you the capability to build a filter and sort condition dynamically and then merges as part of the ClientQuery.
     
    Thanks,
    GeneralMy vote of 5memberChristian Amado14 Aug '12 - 12:05 
    Well done!
    GeneralRe: My vote of 5memberWeidong Shen14 Aug '12 - 14:11 
    Thanks! Big Grin | :-D
    GeneralRe: My vote of 5memberSureshChandran13 Mar '13 - 21:02 
    Please help me!
     
    This is the error i get from VS12. Compilation and execution is done but i click instructor/student/course tab i received one pop up window Error details...
     
    Server stack trace:
    at System.ServiceModel.Channels.ServiceChannel.ThrowIfFaultUnderstood(Message reply, MessageFault fault, String action, MessageVersion version, FaultConverter faultConverter)
    at System.ServiceModel.Channels.ServiceChannel.HandleReply(ProxyOperationRuntime operation, ProxyRpc& rpc)
    at System.ServiceModel.Channels.ServiceChannel.EndCall(String action, Object[] outs, IAsyncResult result)
    at System.ServiceModel.Channels.ServiceChannelProxy.InvokeEndService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
    at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)
     
    Exception rethrown at [0]:
    at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
    at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
    at SchoolSample.WCFService.SchoolService.ISchoolService.EndGetDefaultFilterByScreenName(IAsyncResult result)
    at SchoolSample.WCFService.SchoolService.SchoolServiceClient.EndGetDefaultFilterByScreenName(IAsyncResult result) in c:\Users\spcsc_000\Desktop\Release for Visual Studio 2012\v2.1\Sample\SchoolSample.NET4.5\SchoolSample.WCFService\Service References\SchoolService\Reference.cs:line 874
    at SchoolSample.Model.SchoolModel.<>c__DisplayClass49.b__48() in c:\Users\spcsc_000\Desktop\Release for Visual Studio 2012\v2.1\Sample\SchoolSample.NET4.5\SchoolSample.Model\SchoolModel.cs:line 1178
    GeneralRe: My vote of 5memberWeidong Shen14 Mar '13 - 13:39 
    This demo requires you to have a local SQL Server running, and you also need to run the script "School.sql" to set up the database.
     
    Thanks!
    GeneralRe: My vote of 5memberSureshChandran17 Mar '13 - 7:36 
    Thank you Sir!

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

    Permalink | Advertise | Privacy | Mobile
    Web01 | 2.6.130516.1 | Last Updated 17 Mar 2013
    Article Copyright 2012 by Weidong Shen
    Everything else Copyright © CodeProject, 1999-2013
    Terms of Use
    Layout: fixed | fluid