Sie sind auf Seite 1von 478

p

DevForce

DevForce 2010 Developer Guide

Contents
DevForce, Enterprise Applications, and the ADO.NET Entity Framework .............................. 7
Introduction ...........................................................................................................................................................7 The Problem ..........................................................................................................................................................9 Object Mapping Technology ............................................................................................................................... 10 The Microsoft ADO.NET Entity Framework ...................................................................................................... 11

Using DevForce with the Entity Framework ...................................................................................... 14


Advantages of Using DevForce ........................................................................................................................... 14

DevForce in More Detail ....................................................................................................................... 16


Advantages of Using DevForce (Revisited) ........................................................................................................ 17 More DevForce Advantages ................................................................................................................................ 29

Conclusion .............................................................................................................................................. 37

Hello, DevForce ........................................................................................................................... 38


DevForce Application Architecture - The Big Picture ........................................................................................ 38 DevForce and the ADO.NET EntityModel ......................................................................................................... 40 Your First DevForce Application: a Walk-Through ............................................................................................ 41 Understanding the App.Configs .......................................................................................................................... 71 Monitoring Activity ............................................................................................................................................. 72 Appendix: Listings of Sample App.Config Files ................................................................................................. 74 Appendix: Probing Sequence for the App.Config File ........................................................................................ 75

DevForce Entity Model (EDMX) Designer Enhancements ....................................................... 76


Entity-Model-Level DevForce-Specific Properties ............................................................................. 77 Type-Level DevForce-Specific Properties ........................................................................................... 79 Property-Level DevForce-Specific Properties .................................................................................... 80

Customizing the DevForce Code Generation Template ............................................................. 82 Property Interceptors ................................................................................................................... 87
State of the Release Candidate Documentation ................................................................................................... 88 Code Snippets in This Document ........................................................................................................................ 88 Attribute Interception........................................................................................................................................... 88 Named vs. Unnamed Interceptor Actions ............................................................................................................ 91 Interceptor Chaining and Ordering ...................................................................................................................... 92 Multiple Attributes on a Single Interceptor Action ............................................................................................. 94 Defining an Interceptor to Act Against Multiple Types ...................................................................................... 95 The EntityPropertyNames class ........................................................................................................................... 96 PropertyInterceptorArgs and IPropertyInterceptorArgs ...................................................................................... 96 PropertyInterceptor Attribute Discovery ........................................................................................................... 104 Alternative PropertyInterceptor Attribute Method Signatures ........................................................................... 106 Dynamic Property Interception and the PropertyInterceptorManager. .............................................................. 107 EntityProperties and Property Interceptors ........................................................................................................ 109 PropertyInterceptor Keys ................................................................................................................................... 109 Mechanics of Property Interception ................................................................................................................... 109

Business Object Persistence....................................................................................................... 110


State of the Release Candidate Documentation ................................................................................................. 114 Note: Code Snippets in This Document............................................................................................................. 115

Object Persistence Overview .............................................................................................................. 115


The Big Picture .................................................................................................................................................. 115 2|P age

DevForce and the ADO.NET EntityModel ....................................................................................................... 116 Support for POCOs (Plain Old CLR Objects) ................................................................................................... 118 Persistence Management Capabilities ............................................................................................................... 119

Entity Queries and Entity Navigation ............................................................................................... 126


Entity Queries .................................................................................................................................................... 127 Entity Navigation ............................................................................................................................................... 150 The Null Entity .................................................................................................................................................. 160

Asynchronous Communication with the Business Object Server ................................................... 160


Asynchronous Queries ....................................................................................................................................... 161 IAsyncResult Asynchronous Pattern ................................................................................................................. 163 Asynchronous Fulfillment of Navigation Property Queries .............................................................................. 163 Canceling Pending Operations........................................................................................................................... 164

The EntityListManager ...................................................................................................................... 164 Entity Caching ..................................................................................................................................... 168


All Business Objects are Cached ....................................................................................................................... 168 Queries, Navigation, and the Cache ................................................................................................................... 171 Query Workflow ................................................................................................................................................ 174 Query Strategy ................................................................................................................................................... 176 Span Queries ...................................................................................................................................................... 185 Cached Entity Lifespan...................................................................................................................................... 188 Saving the Cache Locally .................................................................................................................................. 188 The TraceViewer: Watch What Data Is Being Loaded, and How ..................................................................... 189

Creating Business Objects .................................................................................................................. 202


When Not to Create ........................................................................................................................................... 202 The Business Object Create Method ................................................................................................................. 203 Auxiliary Business Object Class Methods ......................................................................................................... 208 Adding and Removing Related Objects using Add() and Remove() ................................................................. 208 Business Object Creation Review...................................................................................................................... 211

Saving Business Objects...................................................................................................................... 211


EntityState of an Object ..................................................................................................................................... 211 Undo .................................................................................................................................................................. 212 Validation .......................................................................................................................................................... 212 Temporary Id Fix-up ......................................................................................................................................... 212 Life Cycle Events .............................................................................................................................................. 213 Saves and Transaction Management .................................................................................................................. 215 Re-query After Save .......................................................................................................................................... 216 When Save Fails ................................................................................................................................................ 217 Data Source Concurrency .................................................................................................................................. 219 Saving the Dependency Graph ....................................................................................................................... 226 Dependency Graph Retrieval ............................................................................................................................. 229 Workflow For a Save ......................................................................................................................................... 232 Saving the Cache to a Local Disk File ............................................................................................................... 233 XML Serialization of Business Objects ............................................................................................................. 234

Business Object Persistence Advanced .................................................................................. 237


State of the Release Candidate Documentation ................................................................................ 239 Getting Information About an Entity Type with GetEntityMeta() ................................................ 239 Access Both Local and Remote Data Sources In the Same N-tier Application ............................ 240 Stored Procedure Queries................................................................................................................... 241
SQL Server Stored Procedure Queries .............................................................................................................. 243 3|P age

Stored Procedure Entity Navigation.................................................................................................. 246 Forced Re-fetch ................................................................................................................................... 246 Lost Connection During Query .......................................................................................................... 248 Query Cache ........................................................................................................................................ 249
EntityManager.RemoveEntities Overload Preserves Query Cache ................................................................... 249

MergeStrategy In More Detail ........................................................................................................... 251 The EntityManager.AttachEntity Method........................................................................................ 255 Filtering Queries .................................................................................................................................. 258 Query Inversion in More Detail ......................................................................................................... 260 Transactional Queries ......................................................................................................................... 265 DevForce and Data Sources Deep Dive .......................................................................................... 266
DataSourceKeys, DataSourceKeyResolvers, and DataSourceExtensions ......................................................... 267 EntityManagers and DataSourceExtensions ...................................................................................................... 268 Tenant Extensions.............................................................................................................................................. 271 Multi-Part Extensions ........................................................................................................................................ 271 Extensions and EntityServers ............................................................................................................................ 272 Dynamic DataSourceKeys and the DataSourceKeyResolver ............................................................................ 273

Multiple Application Environments .................................................................................................. 275 Multiple EntityManager Instances .................................................................................................... 276 Multi-Threading in a DevForce App ................................................................................................. 277 Batching Asynchronous Tasks ........................................................................................................... 279 Service Oriented Architecture ........................................................................................................... 281 POCO Support in DevForce............................................................................................................... 283
Examples of POCO Classes ............................................................................................................................... 284 Examples of a POCO Service Provider Class .................................................................................................... 285 Example of a Client-Side Class Containing Extension Methods for the EntityManager................................... 288 Obtaining an EntityAspect Property on Your POCO Object ............................................................................. 289 Data Contract Serializer (DCS) versus .NET Data Contract Serializer (NDCS) .............................................. 290 POCO Save mechanisms ................................................................................................................................... 293 Summary Things to Remember When Using POCOs in Your DevForce App .............................................. 298

Validation Through Verification ............................................................................................... 299


Introduction ....................................................................................................................................................... 299 DevForce Verification ....................................................................................................................................... 300

Getting Started .................................................................................................................................... 301


Validation-Related Settings In the Entity Data Model Designer ....................................................................... 301 Generated Property Code ................................................................................................................................... 302

A (Very) Brief Overview of Verification Mechanics ........................................................................ 306 VerifierOptions .................................................................................................................................... 306 Verification in the User Interface ...................................................................................................... 311
Verification in WPF and Silverlight .................................................................................................................. 311 Verification and WinForm User Interfaces ........................................................................................................ 311

Validation Through Verification - Advanced ........................................................................... 316


4|P age

Introduction ....................................................................................................................................................... 316

Verification Types Overview .............................................................................................................. 317


Main Verification Classes.................................................................................................................................. 317 Verifiers ............................................................................................................................................................. 319 VerifierResult .................................................................................................................................................... 322 Triggers.............................................................................................................................................................. 324 VerifierEngine ................................................................................................................................................... 326 PropertyValueVerifiers ...................................................................................................................................... 328

Verification Deep Dive ........................................................................................................................ 332


Verifiers ............................................................................................................................................................. 333 Verifier Result ................................................................................................................................................... 338 Triggers.............................................................................................................................................................. 341 VerifierEngine ................................................................................................................................................... 350

Invoking Verification .......................................................................................................................... 355


Instance Verification .......................................................................................................................................... 357 Trigger Verification: BeforeSet and AfterSet .................................................................................................... 358 Monitor Execution with the VerifierBatchInterceptor ....................................................................................... 362

DevForce Silverlight Apps ......................................................................................................... 365


Overview - What is DevForce Silverlight? ........................................................................................................ 365 Creating a DevForce Silverlight Application .................................................................................................... 366 Silverlight Deployment Steps ............................................................................................................................ 366 Questions and Answers...................................................................................................................................... 367 Troubleshooting ................................................................................................................................................. 368

Web Applications........................................................................................................................ 372


State of the Release Candidate Documentation ................................................................................................. 372 Introduction ....................................................................................................................................................... 372 The DevForce ASPDataSource Component ...................................................................................................... 372 Using the ASPDataSource in Development ...................................................................................................... 373 Overridable Methods for Select, Update, Insert, and Delete ............................................................................. 373 The EntityAdapterManager Class ...................................................................................................................... 374 The Configure Data Source Wizard ................................................................................................................... 375 Parameter Collection Editor .............................................................................................................................. 375 Retrieving Schema Information ......................................................................................................................... 376 Third Party Support ........................................................................................................................................... 376

Business Object Server............................................................................................................... 377


Business Object Server Architecture ................................................................................................................. 377 EntityService Startup and Shutdown ................................................................................................................. 381 EntityServer Startup and Shutdown ................................................................................................................... 381 Remote Service Method Call (RSMC) Methods ............................................................................................... 382 Push Notification ............................................................................................................................................... 384

BOS Hosting Details ............................................................................................................................ 385


The DevForce Client ......................................................................................................................................... 389

Vista Setup ........................................................................................................................................... 391


Vista setup requirements for the ServerConsole or ServerService .................................................................... 391 Vista setup requirements for IIS ........................................................................................................................ 391

Troubleshooting ................................................................................................................................... 392


Worked in 2-Tier, Strange Errors in n-Tier ....................................................................................................... 392

Disconnected Applications......................................................................................................... 394


State of the Release Candidate Documentation ................................................................................................. 394 5|P age

Introduction ....................................................................................................................................................... 394 Running Offline ................................................................................................................................................. 396 Securing Offline Data ........................................................................................................................................ 409

Security ....................................................................................................................................... 416


State of the Release Candidate Documentation ................................................................................................. 416 Introduction ....................................................................................................................................................... 416 Authentication ................................................................................................................................................... 417 Authorization ..................................................................................................................................................... 420 Query and Save Interception ............................................................................................................................. 422 Encryption ......................................................................................................................................................... 425 ASP.NET Security Integration .......................................................................................................................... 425

Deployment ................................................................................................................................. 430


State of the Release Candidate Documentation ................................................................................ 430 Document Overview ............................................................................................................................ 430 DevForce And the App.Config File.................................................................................................... 431
Creating and Editing a Configuration File ......................................................................................................... 431 IdeaBlade DevForce Configuration Editor ........................................................................................................ 432 DevForce Elements in App.Config .................................................................................................................... 433 Configuration File Location .............................................................................................................................. 434 Client and Server Versions of App.Config ........................................................................................................ 435 Probing in DevForce .......................................................................................................................................... 436

EntityServerQueryInterceptor ............................................................................................... 438 Server ......................................................................................................................................... 438


Data Server Deployment ..................................................................................................................... 439 Deploying a DevForce Silverlight Application.................................................................................. 439
Deploying to IIS Version 6 ................................................................................................................................ 439 Deploying to IIS Version 7 ................................................................................................................................ 443 Troubleshooting ................................................................................................................................................. 447 Resources ........................................................................................................................................................... 448

Deploying a DevForce WinClient Application.................................................................................. 448


Overview ........................................................................................................................................................... 448 Deploying a Single-Tier WinClient Application ............................................................................................... 450 Deploying Two-Tier (Client-Server) WinClient Applications .......................................................................... 450 Deploying N-Tier (Smart-Client) Applications ................................................................................................. 450 Building Blocks ................................................................................................................................................. 451

Troubleshooting ......................................................................................................................... 473


State of the Release Candidate Documentation ................................................................................ 473 General Troubleshooting .................................................................................................................... 473 Troubleshooting Silverlight Apps ...................................................................................................... 473 Contacting Support ............................................................................................................................. 476
Identifying your DevForce version .................................................................................................................... 477

Upgrading Your Software .................................................................................................................. 478

6|P age

DevForce, Enterprise Applications, and the ADO.NET Entity Framework

DevForce, Enterprise Applications, and the ADO.NET Entity Framework .............................. 7


Introduction ...........................................................................................................................................................7 The Problem ..........................................................................................................................................................9 Object Mapping Technology ............................................................................................................................... 10 The Microsoft ADO.NET Entity Framework ...................................................................................................... 11 Using DevForce with the Entity Framework ....................................................................................................... 14 Advantages of Using DevForce ........................................................................................................................... 14 DevForce in More Detail ....................................................................................................................................... 16 Advantages of Using DevForce (Revisited) ........................................................................................................ 17 More DevForce Advantages ................................................................................................................................ 29 Conclusion .............................................................................................................................................................. 37

Introduction
DevForce is a framework for building and operating multi-tier, data-driven enterprise applications.
By enterprise application we do not mean simply a big application, or an application for a big company. Rather, we refer to an application with the following specific characteristics: Its users devote many hours to its use, performing tasks essential to conducting the organizations business; It requires a rich and responsive graphical user interface, dense with sophisticated controls; User interactions are complex; task and context switching is common; It presents data that are complex in themselves, and deeply interrelated; The data are stored centrally and shared with other users.

Supply chain, customer relationship management (CRM), and asset-tracking applications are typical examples. User productivity is critical. That puts a premium on the applications ability to provide a highly responsive, richly featured user experience the kind of experience typical of a desktop application running directly on a client machine. We expect people to get work done at any time from anywhere. Those people may be employees or they may be valued partners. In either case, security matters. Accordingly, we often need to deploy and

7|P age

operate enterprise applications over a wide area network preferably over the internet with undiminished productivity and security. DevForce is especially suited to building and running applications that require a rich user experience delivered to remote, Internet-connected clients. While DevForce contributes at many levels of the enterprise application architecture stack, its Data Services, Object Relational Mapping (ORM) technologies, and object-oriented approach to data management draw most of the attention. Microsoft has stepped into this arena with the Language Integrated Query (LINQ) and the ADO.NET Entity Framework. The Entity Framework is a robust ORM solution; the developer can retrieve data as entities by writing LINQ to Entities statements in her preferred .NET programming language. DevForce delegates to the Entity Framework the mapping between object and relational database schemas, as well as the database persistence operations (queries and saves). These are important and challenging tasks that the Entity Framework handles well. There is much more to an application than how it handles raw data. There is the business object layer that encapsulates the data and governs those data with business rules. There are higher layers that address the application workflow and user experience. All of this is outside the purview of the Entity Framework.
If we concentrate only on data management, we still find enterprise application requirements untouched by the Entity Framework. Chief among them are: Centralized services, n-tier and Internet support, distributed transactions, performance, security, scalability, and Silverlight support - needs best met with an intelligent middle-tier server. Highly responsive client UIs that exploit caching to avoid redundant, slow trips across the wire. Object models mapped to multiple data repositories Objects mapped to non-relational data sources Proper support for a business object layer with business rules.

DevForce satisfies these requirements even as it relies on the Entity Framework for basic object mapping and query facilities. The key components of DevForce include: the EntityManager, which includes a queryable client-side cache; the Business Object Server (BOS) for services in a middle tier; a provider for the LINQ language that permits LINQ queries to be used with both the client-side cache and remote data sources object mapping enhancements which extend the Entity Framework designer and generate DevForce entity code; business object enhancements such as dynamic validation/verification, localization, object lifecycle events, binding support, and other extensions that simplify your code.

This chapter explores the key data management issues for .NET enterprise application developers. It introduces the LINQ and the Entity Framework, explaining what they do and where they leave off. It then describes how DevForce fills in the critical gaps.
8|P age

The Problem
Every business application is an extended dialogue between a user and the business objects that fulfill the applications purpose. Those business objects are behavioral objects first and foremost. They are the embodiment of the customer stories that describe what the application does and how it does it. A few behaviors may be stateless; financial calculations come to mind. But there is usually data somewhere in those business objects. An order has a customer and a delivery date and line items describing quantities of goods sold for a price. There is no escaping the data aspect of business objects and all of that data must be managed. While the application is running, the data are held in session in some form. In an object-oriented system they are held in fields and exposed as properties of a class instance. But because the data are long-lived longer-lived than any one session they have to be saved between sessions. And because we share our data with others, we have to save the data in permanent storage accessible over a network. Shuttling data between storage and the application session is one of those necessary but dirty jobs, a job completely unrelated to the applications purpose. Developers long ago discovered three data management problems. First, the way we store data is not the way we use data in an application. Money, for example, is both an amount and a currency (dollars, euros). The two aspects require separate slots in storage; from the application perspective, its just one thing: money. An order in the context of an application session may be seen as one thing with a customer, a shipper, line items, etc. When we store that order in a relational database, the order, customer, shipper and line are five different things. So the best representation of stored data often is not the best representation for session data. Second, session data are governed by rules. We must know the customer for an order before we can deliver the ordered goods. The date of the order should precede the delivery date. Some other part of the application may need to be alerted when the order is actually delivered. The application is more maintainable and easier to understand when the rules (behavior) and the data are bound together as business objects or entities. Such rules are largely irrelevant when the data are tucked safely away in storage. Third, there are many mechanical matters surrounding saving and retrieving data that have nothing to do with the applications purpose such as opening and closing connections, composing SQL, detecting concurrency violations, converting raw data into Data Transfer Objects, and managing transaction boundaries. Getting the application dialogue right is hard enough without these distractions. Yes, the application still has to ask for data and stow them away. But there should be a way to express our intent simply and entirely in terms of the application entities. Ordinary operations should make no mention of databases, connections, tables, or columns. The profound differences between stored data and session data lead developers to expend enormous energy moving and translating between stored and session representations. This is wasted energy from

9|P age

the perspective of the application customer who could not care less about our implementation problems. It is also wasted energy from the developers perspective because this problem has been solved by object mapping technology.

Object Mapping Technology


An object mapping technology maintains two views of the data. There is a conceptual model for representing the data within the entities used by the application and there is a storage model that defines how the data are stored in the repository. These two models have completely different characteristics, as we have seen. The conceptual model could include a conceptual order, an order entity, as it is understood by the application. The storage model describes how the order entitys data values are held in the data repository. If the repository is a relational database, many of the order entity data values its state are likely held in columns of a table. The value of a DeliveryDate property of an Order entity might be stored in the [DeliveryDt] column of an [OrderHeader] table row. The correspondence between the conceptual order entity and the table row is obvious and strong in this example. Even so, the correspondence is not literal; there is Order and DeliveryDate on one side; OrderHeader and DeliveryDt on the other. Therefore, the object mapping technology maintains a map of the correspondence between entities of the conceptual model and the table rows in the storage model so that it can transform one representation into the other. The Order entity has a related Customer entity and related OrderDetail entities. These additional entities might correspond to Company and OrderLineItem tables in a relational database. Relational database tables dont have relationships. They have foreign key constraints that imply these relationships. Accordingly, the object mapping technology also maintains a map of the associations between entities and the foreign key constraints in the database. The map records the pairing of the relationship between Order and Customer with the foreign key constraint between the OrderHeader and Company tables. This order example is especially simple. Other mappings could be enormously complex, with values changing shape (type), entities splitting among multiple tables, and relationships weaving through intermediate association tables. Without an object mapping facility, the application developer would have to be constantly aware of these correspondences as she wrote instructions to retrieve and save application data. Small changes in the actual storage schema or in the application entity model could easily break the code in a hundred places.

10 | P a g e

Without an object mapping facility, the application would become vulnerable and brittle as it grew and aged. Productivity would fall as developers devoted increasing effort to keeping the conceptual and the storage models aligned.

The Microsoft ADO.NET Entity Framework


The Microsoft ADO.NET Entity Framework is quickly becoming the standard for database access in .NET applications. DevForce builds upon the Entity Framework, so we introduce the Microsoft technology here before explaining DevForces added value.
Read more about the Entity Framework at http://msdn.microsoft.com/en-us/library/bb399572(VS.100).aspx

Entity Data Model (EDM)


The Entity Framework supports an Entity Data Model (EDM) that describes data from the application perspective. The EDM does not include the actual business object classes that contain those data; rather it defines certain of the data and data relationships within those classes in an implementation-agnostic language of its own. Concretely, the EDM is an XML schema file that defines a conceptual data model. That schema is accompanied by two other XML schema files: one describing how the data are stored (the storage model) and another that maps the conceptual model to the storage model. The Entity Framework uses this chain of descriptions to move data between the data-laden objects in memory and the actual data repositories. For this to work at runtime, the conceptual schema (the EDM proper) refers to entity classes of the application while the storage model gets matched up, via configuration, with a real database running on a server somewhere.

The Entity Data Model Designer


Most developers prefer to use a tool to work with XML rather than edit XML by hand. EDM XML is dense and forbidding so a tool is a practical necessity. The Entity Data Model Designer is a Visual Studio design tool that provides the developer with a graphical, drag-and-drop EDM design experience. The designer enables simultaneous development of all three related schemas the conceptual, storage, and mapping schemas. Most applications are predicated on a pre-existing database. This database cannot be ignored; the conceptual model must ultimately come to terms with it. Most developers find it convenient to confront this fact early and will prefer to generate the conceptual data model and associated schemas using the Entity Data Model Wizard. The wizard produces the EDM schemas which then can be viewed and edited in the designer.

11 | P a g e

Entity Object Layer


The Entity Framework business object layer consists of the classes that implement the application business objects. The Entity Framework includes an entity class generator that uses the EDM to produce class code that defines the business object data fields and their accessor properties. It also generates the navigation properties that enable the application to traverse from one object to its related objects (e.g. from an order to its customer). The EDM describes only the business object data and their relationships. The Entity Framework knows nothing about the business object behavior that applies to the data so there is no business logic in the generated code. The application developer writes business logic separately in a companion class file. The two files the developers business logic file and the generated object data management file combine to form a single definition of the business object, the business object class.
Technically, each file defines a .NET partial class. The compiler knits the two together, resulting in the complete business object class.

Entity Persistence
The Entity Framework includes components responsible for moving business object data between the application and the database. The ObjectContext is the most visible of the components. The application uses ObjectContext to retrieve, hold, and save entities. The ObjectContext maintains a cache of all the entities it manages. The developer writes queries and submits them to the ObjectContext, which retrieves the selected entities and adds them to its cache before returning them to the caller. The developer creates new objects and adds them to the ObjectContext. The ObjectContext tracks changes adds, modifications, deletes to entities in its cache. A save command tells the ObjectContext to write the changed entities to the database. The Entity Framework handles all of these relational database persistence operations without troubling the developer with details. The Entity Data Model and a few guiding parameters are all it needs.

LINQ to Entities
Earlier we described three problems for the developer who needs to represent data in the application as business objects. The third problem was how to retrieve and save business objects using a language that hid the underlying mechanisms and stayed true to the entity-oriented paradigm. While the mechanics of saving business object data are challenging, it has never been difficult for developers to express their intent. It is usually sufficient to tell some service class to save and the service knows what to do. Getting data is a different story. It is not easy to say precisely which data you want, and in what form, using a general purpose programming language. Its harder still to write queries in a strongly-typed
12 | P a g e

manner and stay within an entity-oriented paradigm. Until recently, object mapping vendors offered their own object query languages (OQLs) which were, in fact, merely special purpose classes with strangled interfaces. OQL queries were clumsy to write and repugnant to read. With its release of the .NET 3.5 Framework, Microsoft added new language facilities for finding and accessing data in a general purpose, object-oriented way, without exposing the details of data storage and retrieval. Chief among the new features is LINQ, an abbreviation of Language Integrated Query. A LINQ query looks much like an SQL query. Most programmers have long experience with SQL so, while SQL itself may be tortured, most programmers are accustomed to it and find LINQ expressions familiar:

C#

IQueryable<Product> products = from prod in anObjectContext.Products where prod.ReorderLevel > 100 select prod; foreach (Product aProduct in products) {}

VB LINQ defines a set of query operators for interrogating arbitrary sources of data. Anything that can be enumerated can be queried with a LINQ expression. We can use LINQ to select items from a list, nodes from an XML file, file names from a file folder, or records from a database. LINQ itself does not know how to do any of these things. LINQ defines the query operators and patterns for writing query expressions. The operators and expressions are meaningless until they are married to an implementation that is specific to a domain. Thus there is a LINQ implementation for querying inmemory objects (LINQ to Objects), an implementation for querying XML structures (LINQ to XML), an implementation for querying relational databases (LINQ to SQL), and so on. Microsoft provides some of these implementations but third parties can develop their own and Microsoft encourages them to do so. The LINQ facility provides the expressiveness we need for querying entities. What we need is a LINQ implementation that supports an object mapping technology. Microsofts LINQ to Entities is that implementation for the Entity Framework.

Entity SQL
The Entity Framework supplements LINQ to Entities with its own query language called Entity SQL. Entity SQL is a storage-independent dialect of SQL that works directly with the conceptual model. An Entity SQL query refers to entities, properties, and associations (e.g. Order and Order_Customer) rather than the database elements in the storage model. The particulars of data storage remain hidden in the object-oriented data design.

13 | P a g e

Entity SQL queries are strings as seen in this example:

C#

string queryString = @"SELECT VALUE Product FROM Products + AS Product WHERE Product.ReorderLevel > 100"; ObjectQuery<Product> products = new ObjectQuery<Product>(queryString, anObjectContext); foreach (Product result in products) {}

VB One significant drawback: Visual Studio will not detect even simple mistakes because the query string wont be evaluated until runtime.

Using DevForce with the Entity Framework


Microsofts Entity Framework is a solid foundation for object relational mapping and relational database persistence operations. LINQ to Entities is a huge advance over SQL string commands and proprietary object query languages. We covered this same territory in our earlier, .NET 2.0 version of DevForce; we are pleased now turn over some of these responsibilities to the Entity Framework.
DevForce integrates directly into the Entity Frameworks editor in Visual Studio and provides extensions that enable DevForce specific functionality by augmenting the generated code and by giving the developer more granular control over the generated class and property code.

DevForce relies upon the Entity Framework for the persistence operations that target relational databases. The Entity Framework prepares and issues the actual vendor SQL. The Entity Framework issues all insert, update, and delete commands and employs optimistic concurrency techniques to detect collisions between updates of the same object by different users.

Advantages of Using DevForce


The Entity Framework does a good job of handling relational database mapping and persistence operations for client / server applications. However, most enterprise applications need better data management and better support for developing the business objects that encapsulate the relational data. DevForce provides essential improvements in such critical areas as: Infrastructure for n-tier applications Security Client application performance Model design and code generation Multiple data sources Intermittently connected and offline apps

We summarize each point in the balance of this section.

Infrastructure for Multi-Tier Applications


14 | P a g e

The Entity Framework only supports a 2-tier architecture in which the client machine speaks directly to a relational database server. This wont work for many enterprise applications, especially those that Connect to servers over the Internet, a wireless, or a wide area network. Require rigorous security. Must scale to support many users, especially external partners and customers. Offer applications On-Demand (Software-as-a-Service). Will deploy as a Silverlight application in a browser.

Such applications require the performance, security, and scalability of an intelligent middle tier server that mediates between client machines and such server-side resources as databases and web services. DevForce implements an end-to-end, multi-tier (n-tier) architecture whose middle tier component is called the Business Object Server (BOS).

Security
The Entity Framework has no intrinsic security features. Because of it two-tier approach, the security burden falls entirely on the network and the database. That may be sufficient for simple applications with few users who are always connected within the company LAN. But we will need a better answer when authentication and authorization schemes become fine grained and application specific, when the number of users grows, and when some of those users are reaching in from outside the company walls. The DevForce n-tier solution supports a rich variety of standard and custom authentication techniques and provides encryption and authorization points on both client and server.

Client Application Performance


Data access is the number one performance killer. Large volumes of data are deadly. Frequent trips to the server are worse. And its really bad if the UI freezes while waiting for data. Responsiveness and user productivity improve dramatically when we eliminate unnecessary trips, reduce the size of data traveling over the wire, and retrieve data asynchronously. None of this is easy to implement. The Entity Framework is a purely 2-tier architecture in which the client talks SQL to the database, a chatty conversation with few means to shrink the data. It doesnt remember previous queries, we cant query its primitive entity cache, and we cant query asynchronously. A DevForce application deployed in n-tier mode represents business object data in a compact form and compresses the data before sending it resulting in smaller payloads over the wire. Smaller payloads, faster app. Most applications ask for the same data over and over. DevForce has a query-able entity cache and a query cache. We can ask the entity cache any question, including questions weve never asked before.

15 | P a g e

The query cache remembers previous database queries so repeated questions dont cause redundant server visits. In fact, we use DevForce to Entities, a LINQ-based query language, to pose questions that can search the cache, search the data source, or search both as we wish. Finally, DevForce offers asynchronous queries that can hide the actual cost of a remote query as perceived by the end user. The UI continues to function and we can occupy the users attention with an initial set of data while the balance is retrieved in background.

Multiple Data Sources


The Entity Framework supports just one database per Entity Data Model. But many application data models draw from data storied in multiple data sources. In a supply chain application, orders may be stored in an inventory database while ledger entries are captured in an accounting database. Orders and ledger entries have keys that, conceptually, enable navigation between them even though a cross database query is not technically possible. In DevForce we can define a single model that holds both orders and ledger entries and the code generator can produce navigation properties for seamlessly navigating between them. DevForce handles the SQL for simulating the cross database join. Order and ledger updates must be saved transactionally. DevForce can perform such distributed transaction; the Entity Framework, knowing only one database, cannot.

Non-Relational Data Sources


The Entity Framework can only map entities to relational databases. DevForces POCO (Plain Old CLR Object) support allows any data source that can be exposed as an enumeration to be used as a data source, which enables developers to work with a uniform, consistent object mode, regardless of the backing store.

Intermittently Connected and Offline Applications


Entity Framework applications are vulnerable to temporary connection failures. There is no effective way to recover from a query or save that fails because the connection or server is unavailable. There is no intrinsic solution to the airplane problem the application that must be able to launch and run offline as when working while in flight. DevForce applications have the means to survive transient connectivity and to thrive offline.

DevForce in More Detail


We highlighted the most significant DevForce differences in the previous section. Here we explain them in greater detail and cover some of the other important DevForce features that improve application design and developer productivity.
16 | P a g e

Advantages of Using DevForce (Revisited)


Multi-Tier Applications
The Entity Framework is a client / server technology. Its ObjectServices component, which is responsible for querying and saving data to the database, executes in the same process as the client business object layer. Database SQL commands and raw data flow over the wire.

This works just fine when there are relatively few clients, all connected to a secure, high speed LAN. Performance becomes a serious problem when the traffic goes up or when going over a wide area network. Theres a lot of back-and-forth talk when SQL passes over the network and the data are verbose. With reduced bandwidth and increased latency, those frequent roundtrips for data that no one noticed before become serious problems and the user experience slows to a crawl. Furthermore, in order for a two-tier application to work over the internet, you would have to expose your database directly to the world. This opens up the possibility of someone stealing the connection string and browsing or changing your database without authorization.

The DevForce n-Tier Solution


The DevForce n-tier solution, with its Business Object Server (BOS) deployed in a middle tier, overcomes all of these obstacles.

17 | P a g e

The Entity Framework has relocated from the client arena to the Business Object Server where it now functions purely as an object mapping technology, translating persistent data between entity and storage representations. The client application hosts the DevForce Entity Manager, a component responsible for holding business objects in cache and communicating with the BOS. The business objects and the Entity Manager itself are completely decoupled from the Entity Framework. There are no references on the client to any of the Entity Framework assemblies. Nor do clients talk to the database. Instead, the Entity Manager sends commands to the BOS and receives business objects in return. Commands may be expressed in a variety of formats including the new LINQ to DevForce query language. The BOS translates a LINQ to DevForce query into a LINQ to Entities query and submits it to the Entity Framework. The Entity Framework returns simple entities to the BOS which forwards them to the client. DevForce on the client turns them into business objects and caches them in the Entity Manager. The BOS and the client DevForce Entity Manager exchange data in a serialized binary form that passes easily through firewalls and over the Internet. The BOS compresses the data before sending them to the client. These smaller payloads reduce network traffic and improve client performance. The BOS is effectively stateless. It retains no essential information about client sessions between requests. Each client request resolves to a method call running on a new thread; the call holds onto entity data just long enough to fulfill the request after which it is discarded. Such statelessness makes it easy to distribute requests among multiple BOS servers for scalability and fault tolerance.

Remote Services
Some applications require services that must execute in a centrally hosted environment, perhaps because they involve proprietary logic or because they crunch volumes of data that would swamp the network if transmitted to clients. A client can make a remote service call to the BOS, which will invoke custom server side methods to perform or delegate these hosted services. The BOS can watch for server-side events such as data updates or network notifications, and publish corresponding events to subscribing clients through its push service.

DevForce Silverlight
Microsoft Silverlight enables deployment of .NET applications within a browser. There is no application to install, no client footprint, and no compromise of the client machines security. The door is open to deliver applications to consumers and locked-down enterprise environments securely. Data access remains a challenge. Data-driven Silverlight applications need access to the same data as their desktop equivalents. A Silverlight application can only reach data resources over the Internet and, as weve seen, the Entity Framework cannot move data over the Internet. But a DevForce Silverlight application can.
18 | P a g e

Also, the same DevForce object model can be used to drive multiple UI front-ends, whether its Silverlight, WPF, WinForms, or ASP.NET.

In Summary
With the DevForce n-tier capability, The Entity Framework becomes an n-tier platform Business object data can travel through firewalls and over the Internet Data are compressed and encrypted for fast, secure transport The client can request non-data services to be executed on the server and subscribe to server events. A software vendor can offer software-as-a-service to its Internet customers.

With DevForce Silverlight, you get all of the above capabilities in a tool that permits you to develop Silverlight applications that use the Entity Framework in the same way that WPF Windows clients do.

Secure Services
The Entity Framework only supports a two tier architecture in which the client talks directly to the database. There are not intrinsic capabilities for authenticating users, authorizing access, or encrypting data. This architecture relies entirely on coarse grained network and database measures to secure the application and requires extra care to protect the client machine from theft or intrusion. This level of security is not good enough in many environments. There may be tough corporate or legal mandates to protect sensitive data from unauthorized access. A client machine could fall into mischievous hands. Any .NET program is easily disassembled. A determined malefactor could discover the client-side application security measures, develop counter measures, and attempt unauthorized persistence operations.

Connection Security
The trouble begins with the database connection string. In a two-tier world, each client must provide the Entity Framework ObjectContext with a database connection string before it can access the database. The database is easily compromised if the string contains a user and password. Encrypting the string until the moment of use certainly helps if you remember to do so but still amounts to security-byobfuscation. It is much safer to rely on the operating system to authenticate the user to the database via the Security Support Provider Interface (SSPI) as when the MS SQL Server connection string specifies Integrated Security=SSPI;.
Moreover, each database connection is unique, defeating the performance advantage of connection pooling.

This technique works but there are problems. The IT management burden grows heavy when there are many application users scattered across a widespread corporate network. New users must be added both to network directories and to the databases own list of authorized users. Departing employees should be removed from all directories. The application administrator rarely maintains the network and database logons so there are communications breakdowns that lead to mistakes.
19 | P a g e

In a DevForce n-tier deployment, the Business Object Server (BOS) stands between the client and the database. The client must login to the BOS before the BOS makes any requests on the clients behalf. After login every transmission from client to server is accompanied by an encrypted session token that identifies the client.
Domain Server / Active Directory authentication and impersonation are viable alternatives for LAN users and can be combined with alternative login mechanisms when users access the application from outside the corporate network.

Clients no longer access the database directly. They dont hold a connection string nor issue vendor SQL calls. They dont know where the data physically reside. Instead they ask the BOS to fetch and save data on their behalf and only commands and object data travel over the wire. The BOS, running on a secure machine, connects to the database with its own private connection string. The BOS performs all database operations.

Authorization
The Entity Framework has no authorization mechanisms. In most cases, the application relies upon authorization settings in the database settings which operate crudely at table levels and do not reflect more detailed business rules. Application-specific authorizations can only be enforced in the client. The ability to limit order approval or restrict access to a patient record depends entirely on business logic executing in the client. With the DevForce BOS in place every query and save operation is subject to inspection. The BOS invariably calls certain customizable secured operation methods, passing along the clients Principal so each method can identify the client user and his assigned roles. The method can determine if the user is allowed to perform the requested operation and what action to take if permission is denied. Every step in this process, from login to security check can be tailored to meet the particular needs of the application. There is nothing that client can do to thwart these measures. The BOS will execute them like clockwork and the client has no access to the server, no ability to inject malicious code.

Encryption
The developer is free to engage the kind of encryption that is most appropriate. SSL is typical but other methods can be inserted in the pipeline. DevForce prefers to use Microsoft Windows Communication Foundation (WCF) for client-to-server communications; the WCF security-related configuration options are all available.

Client Performance with DevForce Caching


Fulfilling a request for data with a trip to the database is thousands of times slower than satisfying the same request from local memory. The trip is longer still when the database resides across the network.

20 | P a g e

Thats why responsive, data-intensive business applications cache entity data locally. If weve asked for the data before, we should not have to ask for the same data again at least not immediately. Do applications ask the same question twice? Yes they do. Users are always cycling among several active tasks; each time they return to a task underway, the application re-issues a query. The developer might take pains to cache such queries herself - but thats a lot of bookkeeping code write and maintain.

DevForce Caching and LINQ to DevForce


The DevForce Entity Manager maintains a query-able, client-side entity cache. By query-able we mean that we can always apply a LINQ to DevForce query to the in-memory cache. LINQ to DevForce is a LINQ implementation that enables queries to both the entity cache and to remote data sources. Lets look at an example. We want to see the orders of star sales rep, Nancy Davolio. We compose a LINQ query that searches for orders of the rep whos first name = Nancy and whose last name = Davolio. The first time we run it, the Entity Manager realizes that the query is new and sends the query over the wire to the BOS. The results come back after a fraction of a second or several seconds, depending upon the amount of data, the load on the database, and the speed of the network. A minute later we ask for Nancys orders again. The Entity Manager recognizes the repeat query and looks only in the local cache. It returns with the results immediately. Behind the scenes the DevForce Entity Manager maintains both a cache of entities and a cache of queries. The query cache is the memory of queries run against the database. When DevForce executes a LINQ to DevForce query it checks this query cache first. If it finds the query it assumes the query can be satisfied by the entity cache. It then translates the LINQ expression tree into search operations against that cache. The developer can inspect, add, remove, clear, and update the contents of both the entity and query caches.

Responsiveness with Asynchronous Queries


Responsiveness is subjective. The application is fast or slow if the user thinks it is. Users worry if the application freezes for more than a second. A prolonged delay when the application launches or a heavy screen loads is a common cause for complaint. Initialization queries or big data transfers are often the source of the problem. You can alleviate the pain by fetching the data in background with asynchronous queries. The Entity Framework does not support asynchronous queries. DevForce does. It is easy to fire off a series of async queries before displaying a form on screen. The form appears immediately and fills as the data arrive.

21 | P a g e

Some entities are more volatile than others. The list of provincial and city tax rates is probably constant during a particular session. Inventories, on the other hand, are changing constantly and screen full of quantities on hand should probably be refreshed every few minutes (or seconds perhaps). DevForce async queries on a timer can keep that screen current without stalling the UI while the application polls for changes. There is always the danger of a runaway query the query that pulls down so much data that it either freezes the UI for agonizing minutes or times out. Fortunately, its easy to use the LINQ extension method Take() to pull down sequential sections of a collection of entities. The following query, for example, will bring down the first 100 customers, ordered by the name of their company:

C#

var query =_mgr.Customers.OrderBy(c => c.CompanyName).Take(100);

We can make the application appear extremely fast by combining a Take() query that requests a small set of data with an asynchronous query that requests a larger set. Suppose, for example, that the user requests several thousand orders. We dont know for sure hell do so, but weve seen it before. So we take defensive measures. We first compose the users order query in the usual manner. We then suffix it with a call to Take() that limits the request to a safe maximum of 3,000 orders. We submit this one as an asynchronous query because we know from experience that it will take several uncomfortable seconds to return. We follow immediately with the same query, also suffixed with a call to Take(), this time limited to 100 orders. This one we submit synchronously1; were willing to wait a half second for this one. It returns as a list and we present the first 100 orders. The original request for 3,000 eventually arrives; the call-back method fills the list. On screen, the order grid magically grows from 100 to 3,000. The user is delighted. Note that there is also a Skip() extension method that can be used if you want something other than the first n members of an ordered result set. The following query will bring down the next 100 customers: C#
var query = _mgr.Customers.OrderBy(c => c.CompanyName).Skip(100).Take(100);

Model Design and Code Generation


Architects are increasingly convinced that we should design business objects with a blind eye to the way their inner state are stored. Our job is to interpret the user stories, to tease out the logic and data

In Silverlight applications all queries must be asynchronous, so in that case we will have to do both of our queries the larger one and the smaller one asynchronously. In a Silverlight app, we might choose to tie up the user interface of our application by other means (such as displaying a child window) while waiting for the smaller query to return.

22 | P a g e

necessary to support those stories. A business object model gradually emerges and from that model we later discover the storage scheme that fits best. This approach is called Behavior Driven Development (BDD) because it encourages us to start from the required application behavior and work toward the implementation rather than leap directly to data design (as most of us old folks have done our entire careers). If a user story says the order date must precede the delivery date, it is clear well need two date fields. When the story says the user enters an order date and the user enters a delivery date, we will know enough to give our Order class properties to get and set these dates. On the other hand, our Order class wont have an approval date, a credit checked date, a status changed date or any other date unless another user story calls for them. No peeking to see if these fields are in the Order table! We wont worry just yet about how or where the order and delivery dates are stored. BDD says we should wait to the last responsible moment before committing to a storage scheme. Meanwhile, we can code and test our Order class now. As storage blindness is rarely possible in real life, we should at least hang a curtain to hide the storage details and peer behind that curtain as little as possible. The Entity Data Model helps by separating the conceptual data model from the storage schema. There is no mistaking the fact that the conceptual data model remains a data model well short of a business object layer whose members combine behavior and state. Moreover it exists for one reason only: so that we can move values between business objects and storage when that time inevitably arrives. So it is actually a model of the state within the business objects rather than a model of the business objects. Nonetheless, we should be able to maintain the pretense that our state is purely conceptual and could be moved to any form of storage. We only commit to a storage scheme when were in a different frame of mind. This kind of design separation is extremely difficult to accomplish by hand. There is a lot of tedious programming for each business object, most of it concerning access to the fields of persisted data. An object mapped to a table row of twenty columns could yield a couple of pages of code. The slightest change to the storage schema necessitates a revision of this code. We wont do it without adequate tools and code generation. We would simply lack the patience and discipline.

Entity Framework Development


The Entity Framework designer presents a visual canvas upon which to draw entity classes, the relationships among them, and the mapping to the storage schemas.

23 | P a g e

The Entity Framework code generator produces a partial class file with properties to access persisted data fields. It also inscribes navigation properties that return related entities; the Order.Customer property returns the Customer object associated with a given Order instance. Because a business object is more than data and needs more logic more behavior than just data access properties, the generated class file needs a companion partial class file. The design tool cant generate the companion file only the developer knows what belongs there. The developer creates this file and pours her custom business object behavior into it. The compiler combines the two files, yielding a complete class with both business logic and data management capabilities. This two-part, bicameral file structure is effective in keeping developer and generated code in separate rooms. In principle the generator can be run repeatedly rearranging the generated code room without disturbing the furniture in the developers room.
The Entity Framework code generator falls short in several respects: The designer does not give the developer adequate control over the generated code The generated properties are not adequately extensible, limiting the developers ability to abstract out the business logic shared across business objects. The code generator blocks introduction of base classes into the inheritance hierarchy, limiting the developers ability to inherit common business object behavior.

Rigid Code Generation The Entity Framework code generator grants the programmer only limited control over the generated class code. For example, it emits public properties for all mapped data values, even those you dont want exposed. And it always generates properties with both getters and setters. This is a reasonable default but is not desirable in every case. The primary key value is usually immutable; its property should be read only if it can be read at all. Anemic Data Properties The bicameral approach works fine when we can locate the business logic in the developers custom partial class file. Its easy to put calculations and workflow rules there when they concern the entire object. For example, this is the place to augment the order object with an InvoiceTotal property that sums the cost of all item details. But a great deal of business logic is only effective when it executes inside the data access properties and these properties reside in the generated file. Suppose we want to constrain the transition from one order status value to another; perhaps the status proceeds from new to approved to shipped to delivered. We should reject any attempt to transition directly from new to shipped. Maybe we should block unauthorized users from changing the status at all. The critical place to catch validation and security violations is inside the OrderStatus property itself. The Entity Framework did not generate the OrderStatus property with these capabilities. We cannot add them to the generated property code ourselves; the designer will overwrite our change the next time we use it as we surely will in response to changing application requirements.
24 | P a g e

The generated code must have adequate extension points mechanisms that enable the developer to inject behavior into the properties without touching the code itself.
Unfortunately, the Entity Framework generates anemic property accessors. Here is another example:
[EdmScalarPropertyAttribute()] public string SocialSecurityNumber { get { return _socialSecurityNumber; } set { OnSocialSecurityNumberChanging(value); ReportPropertyChanging("SocialSecurityNumber"); _socialSecurityNumber = value; ReportPropertyChanged("SocialSecurityNumber"); OnSocialSecurityNumberChanged(); } }

The getter is not extensible. It simply returns the social security number field value. What if the user is not authorized to view that number? There is no way to block the attempt to read this value or to mask it so the user sees only a safe portion of it (e.g., the last four digits). The setter has a few extension points. There are reporting methods that could alert the application to changes.
The ReportPropertyChanging and ReportPropertyChanged methods defer to an Entity Framework ChangeTracker object that monitors current and original property values. It could be useful to a watching application component (e.g., for data binding support).

There are the partial methods, OnSocialSecurityNumberChanging and OnSocialSecurityNumberChanged, with which the developer can implement some limited logic specific to Social Security Numbers. Observe that the incoming value cannot be transformed before it reaches the field; we can complain (i.e., throw an exception) but we cannot heal. We are out of luck if we need generalized property logic that works across multiple properties. We shouldnt have to manually implement an OnChanging or OnChanged method for every property we want to validate. We should have a model-wide solution to validating changes that centralizes validation rules and manages them as resources as we do in DevForce. And remember: validation is but one example of logic we could manage as metadata and introduce dynamically into the property. Missing Inheritance The Entity Framework supports inheritance hierarchies but only if each class in the hierarchy is mapped to a physical database table. The only base class that isnt mapped is the Entity Frameworks own Entity class. There is no room to insert a class into the hierarchy that provides pure behavior. This is a serious omission. Years of real world application building confirm the wisdom and necessity of at least one base class that provides behavior that all business objects have in common. This is the application model base class, not Microsofts or IdeaBlades. Such a class could
Manage persistent auditing fields such as LastModifiedBy and LastModifiedDate. 25 | P a g e

Generate separate audit trail objects during save. Implement data binding interfaces such as IDataErrorInfo. Cache broken validation rules. Provide access to the applications Dependency Injection or Service Locator facilities.

It is not uncommon to introduce similar classes elsewhere in the hierarchy. We might want an Inventory class in support of several distinct types of inventory, each mapped to its own table; we shouldnt have to have an Inventory table too.

DevForce Code Generation


The code generation in DevForce addresses each of these deficiencies. The developer can specify an abstract base class that will never have a corresponding member in a data repository and insert this class anywhere in the business object class hierarchy. Its easy to set an application base class from which all new business objects derive by default.
Like the Entity Framework, DevForce generates a partial class file covering the persistent data, leaving the developer to write custom business logic in a companion file. But DevForce gives the developer better control over the generated code. For example, using DevForce she can Decide which properties to make public and which to hide Make any property read only Include or exclude DevForce value verification Impose a required-value requirement on a property mapped to a nullable column

Property Interceptors
DevForce provides a mechanism to intercept and either modify or extend the behavior of any .NET property. This interception is intended to replace, and expand upon, the technique of marking properties as virtual and overriding them in a subclass. This facility is a lightweight form of what is termed Aspect-Oriented Programming. Interception can be accomplished either statically, via attributes on developer-defined interception methods, or dynamically, via runtime calls to the current instance of a PropertyInterceptorManager. Attribute interception is substantially easier to write and should be the default choice in most cases. You can learn about property interceptors in the Property Interceptors section of the Learning Resources.

Multiple Data Sources


An Entity Data Model maps all of its entities to tables in a single database. This is unrealistic for the many enterprise applications whose conceptual data models integrate information from multiple resources. Its not uncommon for an application to draw upon data resident in three or four different databases.

26 | P a g e

Consider, for example, a custom ERP application that keeps order information in one database and accounting information in a separate database under the control of a third party accounting package. Business requirements are such that a contract for a new order stimulates a cascade of credits and debits. The ledger entries refer back to the order number and it must be possible to navigate from an order to its entire ledger history. Both databases must be updated when saving the new order. It would be a catastrophe if the order was added but not the ledger entries. We require a distributed transaction which means that the changes to both databases must either all succeed or all fail. This is an extremely difficult scenario for the Entity Framework. The two databases require two Entity Data Models and two sets of entity classes. An Entity Framework ObjectContext can only manage entities from a single model so well need at least two ObjectContexts at runtime. Our scenario calls for the ability to navigate from an order to ledgers and from a ledger entry back to an order. That will be tricky because the related objects live in different ObjectContexts. Entity instances dont know about other ObjectContexts so an order wont know which ObjectContext holds its companion ledger entries. The developer will have to create some clever infrastructure to make this work. Saving changes to orders and ledger entries is no picnic either. We have to save orders and ledger entries separately. The developer will have to be aware of the issue, set up a distributed transaction, and make sure that the Entity Framework properly enlists both save operations in that transaction. By contrast, DevForce supports multiple data sources. The Order and LedgerEntry classes can reside in a single model so there is no need for a separate interface assembly. DevForce will generate the navigation properties to walk from order to ledger entry and back again. And DevForce takes care of setting up the distributed transaction and enlisting the save operations within that transaction. Our example looks a bit like this:

We see that DevForce is relying on the Entity Framework for object mapping and persistence operations while shielding the developer from unpleasant implementation complexities. The critical factor is the introduction of the DevForce business object model as a construct separate from the Entity Frameworks own conceptual data model. In effect, DevForce provides a higher level abstraction over the Entity Framework object mapping abstraction.

27 | P a g e

Lost Connections and Offline Applications


The Entity Frameworks ObjectContext must always be able to connect to the database. If the application cannot connect, any query will throw an exception. The ObjectContext cache is unstable and unusable while the connection is broken so it is dangerous to continue even if something appears to work. Many applications operate in environments with unreliable connectivity. Mobile applications and wireless laptops are vulnerable to sudden outages. Without DevForce, the developer must work hard to protect against connectivity failures. The users pending changes could all be lost. A DevForce application can be immune to these problems. The application can recover from an outage and continue to process queries against the cache alone until the connection is restored. The cache preserves unsaved changes, including newly added objects, so the user can continue working, albeit constrained to the world of entities presently in cache. A DevForce application can encrypt and save the entity cache to a local file with just a few commands. Later, with a few more commands, the application restores the cache from that file. A bullet-proof application might automatically store a users pending changes locally every few minutes just in case. If the application crashes or the battery dies, the user could re-launch later and recover her work. We use this same mechanism to develop applications that operate offline intentionally. The user preloads the cache, preserves the cache locally, shuts down, re-launches while disconnected, does work, saves that work locally, and finally saves the pending changes to the database when reconnected. Someone may have saved changes to the same data while this user was offline. It happens while online too but the risk is greater when the time from change to save is prolonged. The response is essentially the same: DevForce detects the concurrency violation and the application resolves it, perhaps with the users help.

28 | P a g e

More DevForce Advantages


Weve seen the DevForce capabilities that are most critical for enterprise application development. There are other ways in which DevForce improves upon the Entity Framework. They may not be as critical in the majority of applications but they can significantly enhance developer productivity and code quality and are worthy of comment here.

Entity LifeCycle Events


The business object and upper application layers often need to know what the persistence layer is doing. The Entity Framework functions silently most of the time. It raises a SavingChanges event but wont tell you when the save operation succeeds or what entities were saved. There is no easy way of knowing when it reaches out to the database or returns with data. DevForce provides pre- and post- events or interception points for all significant moment in the lifecycle of an entity. Client side events include Creating and Created, Fetching and Fetched, Saving and Saved, Deleting and Deleted, Removing and Removed. There are also life-cycle extension points on the server-side (BOS) . These include the ServerSaving and ServerSaved methods so that developers can add custom processing immediately before and immediately after the save transaction. The ServerSaving method has access to the entities to be saved; the method can manipulate these entities, add to them, and remove them, before turning the final list over to DevForce for the save operation. The ServerSaved method knows if the transaction succeeded or failed and can invoke another server-side process as appropriate. Such a process might send a message to another service running in the hosted environment.

Lazy Load by Default


(The material in this section applies to DevForce WinClient but not to DevForce Silverlight, where all data retrieval is asynchronous.) DevForce navigation properties return a result if possible. The expression Order.Customer returns the orders customer if it has one. If the customer is already in cache, DevForce returns it. If the customer is not in cache, DevForce fetches it from storage. The behavior is the same if the navigation property returns a collection. The expression Order.OrderDetails returns the orders line items, retrieving them from storage if they are not found in cache. The Entity Framework takes a contrary approach. The navigation property it generates for Order.OrderDetails returns an empty list if the line items are not already in cache. Object-oriented guidelines say a property should return quickly. A database query is not a fast operation. Therefore the team reasoned it should return nothing rather than return what the caller clearly expected: the list of line items.
29 | P a g e

We agree with the rule in general. But we can think of no use case in which returning an empty list from Order.OrderDetails is the right thing to do. It only punishes the caller who will now have to write several lines of defensive code to satisfy the guardians of object-oriented propriety. IdeaBlade decided to break the rule and provide useful behavior.

The Null Entity Pattern


DevForce scalar navigation properties always returns a business object. The expression Order.Customer always returns a customer object. Of course the returned customer is the orders real customer entity if the order actually has a customer. If the order doesnt have a customer, DevForce returns a placeholder object called the Null Entity. The same navigation property if generated by the Entity Framework would have returned null. Null values greatly complicate the developers life. She has to be on constant alert for null reference exceptions. Data binding to a property that can return null is pure hell. A null reference exception thrown during data binding results in an ugly red bullet on screen and an error message that baffles the poor user. A customer null entity has all the properties of a real customer. The programmer can distinguish a null entity from the real thing when she has to but she doesnt have to litter the code with null value tests. Data binding survives nicely; a UI widget bound to a null entity displays a conveniently vacant value of the developers choosing. The null entity pattern spares developers many hours of pain both in writing and reading code.

Proper Merge Strategies


When the Entity Framework fetches data from the database it must decide how to merge those data into its cache. What happens if the retrieved entities match entities already in the cache? What if some of those entities have pending unsaved changes or are scheduled for deletion? By default the Entity Framework only adds unmatched entities. That leaves modified entities untouched. But it also means that stale data are not refreshed. Inventory levels wont be updated. The user wont know about depletions or replenishments unless she is lucky enough to try saving a change to one of the adjusted products; the save will fail with a concurrency exception and shell know to refresh the inventory level. An Entity Framework query with the overwrite option with refresh the unmodified inventory level and wipe out the users pending changes to other inventory objects. An Entity Framework query with the preserve changes option seems to do the right thing. It updates the unmodified inventory level and preserves the users changes. Unfortunately, it obscures the fact that the changed inventory item is out of sync with the database. Suppose there was one item left in stock when the user fetched the inventory level. The user allocates it to her customer. Meanwhile, a
30 | P a g e

different user sold the item to his customer, reducing the stock level to zero. After this user refreshes her cache with preserve changes she still believes there is one item in stock. There is not indication otherwise. She saves, intending to sell the item to her customer. The save succeeds and now the same item has been silently sold to two different customers? The DevForce offers equivalents to the Entity Framework merge strategies; they have their place. But the DevForce preserve changes option also preserves the pending concurrency conflict. The other user sold the item first and DevForce will prevent her from selling it twice.

The DevForce Verification Engine


DevForce provides a robust Verification Engine for validating the correctness of business objects. The developer can code custom verification rules and apply rules to objects by decorating properties with attributes, specifying the rules programmatically in the business object, or by reading them from metadata and adding them to the engine at runtime. While the application could suspend business object validation until just before save, users prefer to be alerted immediately when they enter invalid data. Validation should be performed in the business object rather than the UI. Business object properties should validate proposed values as those values are conveyed from the UI to the object. DevForce supports this approach by inscribing calls to the Verification Engine inside the property setters.

Entity Metadata
The developer sometimes needs to know aspects of the conceptual data model itself. For example, she might need to iterate over all the child relationships of an order without knowing what those relationships are in advance. The metadata about such features of the model are often hard or impossible to find in the Entity Framework. DevForce records these features in metadata objects that can be easily reached programmatically through the EntityMetadataStore class. See the Object Persistence section of the Learning Resources for detail.

Eager Entity Loading


By default, a query only returns the entities we ask for. If we query for orders, we get orders and not the other objects related to those orders such as the customers, shipping addresses, line item details, and the product catalog. Thats usually a good thing. Why suffer the performance cost of fetching related objects if we wont need them? With lazy load we can get a related object as we need it, when we need it, if we need it. In many scenarios we know we need the related objects immediately. Suppose our application presents the user with a list of orders. There is a grid beneath the list that displays the order details associated with the currently selected order. Clearly we need both the orders and their details at the same time. But if we stick with lazy loading, well see a flurry of tiny database requests as the grid calls Order.OrderDetails for each order in every displayed row. Performance will stink.
31 | P a g e

Fortunately, in DevForce we can eagerly load the related objects by adding one or more spans to the query. When we add a span that specifies the relationship between Order and OrderDetail, the query engine fetches and caches the order details at the same time that it fetches and returns the selected orders. The grids subsequent calls to Order.OrderDetails are satisfied quickly from the entity cache; there will be no extra trip to the server. The Entity Frameworks LINQ to Entities syntax has a comparable feature called an include. We can add one or more include statements to eagerly load related entities. Unfortunately, there is no way to manage the includes of a LINQ to Entities query; there is no way to discover if it contains an include, no way to remove an include if it is not wanted. Moreover, an include instruction is a string, which means it cannot be type checked. In contrast, the DevForce programmer can inspect a LINQ to DevForce query for spans and add or remove them at will.

Dynamic Data Source Configuration


Data source connection management is unexpected chore. It seems simple at first: record the connection in configuration file and get out of the way. But, for many applications, the connections proliferate and the rules about who gets which connection become complex. The Entity Framework isnt much help in this department in part because it does not contemplate a world of multiple databases. But DevForce can help you tame the complexity.
Two common scenarios illustrate the problem. In typical Enterprise development cycles, an application advances through a sequence of environments that begin with Dev and proceed through QA, Stage, and Production. The executables are the same but the data source connection information changes at each step. It should be easy flip a switch and re-point the application to the database (or set of data sources) that are appropriate for the targeted environment. In some On-Demand applications, each tenant has its own database or data source set. Financial institution A has its database, B has theirs, and so on. Users launch a common application front end. When they enter their credentials, the login module identifies the users company and determines the corporate database that is correct for that users session.

In both illustrations, the data source schemas are the same across all session; what changes from session to session is that actual database used. The data model mapping schema associates each entity type with a home storage schema. That schema has a symbolic name, the DataSourceKey. We know the storage schema at design time. We know the DataSourceKey at design time. But we dont wont know the actual data source to access until runtime. Thats when well use the DataSourceKey to locate the appropriate connection string and hook up to a real data source. By default, DevForce looks for the connection string in an XML configuration file, expecting to find a dataSourceKey node identified by the DataSourceKey name. The connection string should be an

32 | P a g e

element within that node. Continuing our first example, we might locate any one of four connection strings depending upon the environment. We dont want four separate configuration files. So instead, DevForce lets us maintain multiple connection strings for each DataSourceKey. It differentiates among them by means of a DataSourceKeyExtension, an extra bit of string associated with the DataSourceKey name. Now we can record as many connection strings as we need for any conceptual data source by creating distinct nodes uniquely identified by the both key name and extension. Nodes that share the same key name refer to the same conceptual data source; the extension tells us which concrete data source to use at runtime. We control runtime behavior by telling the client-side Entity Manager which extension to use. If were running in the QA environment, well specify a QA extension. If the application entities map to conceptual databases Alpha and Beta, the application will connect to the concrete databases identified by Alpha_QA and Beta_QA. When we run in production we switch to the Prod extension and the application now connects to databases identified by Alpha_Prod and Beta_Prod. Notice that databases travel in sets. There is the QA set and the Prod set. We can use this same technique to support multi-tenant applications that store customer data in separate databases an approach often mandated by financial clients. An Acme client session runs against the Alpha_Acme and Beta_Acme databases. The Baker client runs against the Alpha_Baker and Beta_Baker databases. The DevForce configuration file may not be the best place to store the connection information. In our second On Demand scenario, we could be adding new application tenants frequently. Rather than update the configuration file every time, we write a DataSourceKeyResolver to calculate and locate connection information based on key name and extension.

Custom Key Generation


Every entity must have a unique Entity Key so that the framework (a) can distinguish one entity from another and (b) recognize when two apparently distinct object instances actually represent the same thing. The Entity Key is the conceptual equivalent of a primary key in a database table row. Like a primary key, it can be a single value (e.g., an integer Id) or a composite key (e.g. as when a line items key consists of it parent Order and Product ids). A newly created entity must have a unique key before it can be added to the cache; this is true whether we add the entity to the Entity Framework ObjectContext or to the DevForce Entity Manager. Sometimes we can create the key on the spot. Its easy if the key is a Guid or some other globally unique value that can be determined by the client alone. Its not easy if we must construct the key based on values acquired from a remote source. Thats the more usual case. The key could be mapped to an autoincrementing column in the objects home table. It could be generated by incrementing a counter stored in a separate database table (e.g., a NextId table).
33 | P a g e

Identity Column Keys The Entity Framework supports the attributing of a column described in the storage model (SSDL) section of the Entity Data Model with the StoreGeneratedPattern enumeration. This lets the Entity Framework know that the back-end data store will generate a value for a column upon insert (or upon both insert and update) so that when an entity containing such a column is persisted the Entity Framework knows, post-save, to read the new value from the back-end data store and update the entity in the Entity Framework cache. The Entity Framework supports three states for StoreGeneratedPattern: None (the default), Identity, and Computed. Columns flagged with StoreGeneratedPattern=Identity are those updated only upon insert. Columns flagged with StoreGeneratedPattern=Computed are updated upon both insert and update. DevForce supports the StoreGeneratedPattern=Identity setting, extending its capabilities to encompass entities in the DevForce client-side cache. These entities need primary key values immediately upon creation, though they may not be persisted until much later. DevForce gives such entities a temporary primary key upon creation so they can be referenced client-side without any trip to the data source. Upon saving, their value is updated in the client-side cache to the value generated on the server. The foreign key values in other entities that reference the targeted entity are also updated to reflect the new, server-generated primary key value of the target entity. The Entity Framework can generate the new key for you if the key is a single valued integer key mapped to a SQL Server identity column. The Entity Framework cant set the objects permanent key; that wont happen until the newly created object is saved and even then it will be the database, not the application, that determines the key. So the Entity Framework assigns a temporary key and refers to that key when it adds related entities to the new objects graph. For example, upon creating a new Order, the Entity Framework assigns it a temporary key (e.g., -1). When we add a new OrderDetail to that Order, Entity Framework inserts -1 into the hidden foreign key field of the OrderDetail that links the detail to the parent order. When the application saves these new entities, the Entity Framework acquires the permanent ids from SQL Server and updates the objects accordingly. Continuing our example, the Entity Framework learns that the new Orders primary key is 123 and updates the orders id. It also takes a critical second step: it finds all associated OrderDetails and updates their ParentOrderId column values from -1 to 123. Id Fix-up is our name for this propagation of permanent ids to related objects. Only then does it try to save the fixed-up OrderDetails. Coping with Custom Keys Many applications are tethered to an existing database with its legacy primary key scheme. They cant use Guids. The key may be a simple integer acquired from a counter table named NextId. It might be a semantic key that combines the counter value with meaningful characters; maybe the order key includes the state and fiscal year as in FY07-0270-CA.
34 | P a g e

Well have to write the logic ourselves. When we create the order, we read the current counter from the NextId table, bump it for next time, calculate our key, set the Orders key and then were ready add the entity to the ObjectContext. Its a pain but its manageable for a continuously connected application. Its much harder if we must support an application that can operate offline. We wont always be able to reach the NextId table so we cant always calculate the permanent keys immediately. Well need a custom temporary key and Id Fix-up scheme. DevForce can do all of this for you. You write a custom Id calculation class that conforms to a DevForce interface. DevForce discovers the class and manages key creation, temporary keys, and Id Fix-up during the save. Of course it works even when your application runs offline.

Declarative Concurrency Column Management


Many applications must guard against the possibility that two different users will unknowingly edit and save the same entity simultaneously. Without some kind of checking, the last person to save wins. If I sell a particular item and you sell the same item, we will have sold the same item twice although the database will show only that you sold it. I could have put a database lock on the item record, thus preventing you from reading and editing it. Such pessimistic locking harms performance and leads to troubling lock-out scenarios. Neither DevForce nor the Entity Framework supports such a physical locking scheme. The Entity Framework relies on optimistic concurrency techniques to detect and resolve concurrent access conflicts. Optimistic concurrency assumes that two users rarely wrestle over the same record and therefore allows all users to access records freely. If two users, such as you and I, try to update the same record, it detects the conflict and terminates the second save; it informs the second client be raising a concurrency exception. The Entity Framework implements optimistic concurrency by comparing the value of a concurrency column in the pending record with the value of that column in the stored record. If the values are the same, the pending record can be saved. If the values are different, the pending record is out of sync with the stored record; the framework assumes a concurrency conflict and throws the exception. This technique works so long as the concurrency value is changed after each successful save. Who is responsible for that change? The Entity Framework says that you are. You are fortunate if the database table has an update trigger that can do it. Otherwise, you have to write the code that updates the concurrency column and you have to remember to call it at the right moment. DevForce can handle the concurrency column update for you. In DevForce, you declare the concurrency column (or columns) and pick a method from a list of concurrency column update methods. DevForce will call that method at the appropriate time. Yes, you can extend the list with a custom method.

35 | P a g e

Undo
The Entity Framework lets you accept all entities with pending changes (thus disguising a discrepancy between data in session and data in storage!) but wont let you roll back changes either individually or collectively without many lines of programming. Undo is a one line command in DevForce. Furthermore, with DevForce, you can move a set of entities into another Entity Manager sandbox, which allows you to isolate a set of changes or even an entire workflow from disrupting the state of the rest of the system.

Sandbox Editors
Imagine that customer Jim calls to adjust one of his orders. You find the order in the list and open it in an editor and begin working on it. Youre in the midst of changing deliver addresses, order items, billing information, etc. Suddenly, premium customer Sally calls you with an urgent request for a new order that you must enter right now. Jim kindly agrees to complete his changes later. You begin Sallys order in a second order editor. You are half way through Sallys order when Jim calls you back. He says never mind, that order we were changing is just fine the way it was. You switch briefly over to Jims order and discard all changes simply by shutting down the order editor. You return to Sallys order editor, complete it, and save. There are two distinct orders in flight in this example. Each has its own set of entities some of which may overlap (e.g., the list of shippers) although most do not. With DevForce, you can create separate Entity Managers with separate caches and maintain these editor sets separately, each in their own sandbox. The entities in the Jim Entity Manager are isolated from the entities in the Sally Entity Manager and all of these entities are isolated from the list of orders held in the applications main Entity Manager. Now imagine that this scenario takes place off line. There is no access to the database. That still works in DevForce because you can easily pass copies of entities from one manager to the next without going to the database. You might even prefer this approach when connected if the performance of your application is at a premium and bandwidth is poor.

Managed Lists
Keeping lists of entities up-to-date is a recurring application problem. Its the holiday season as I write this so lets imagine weve written Santas inventory tracker. The tracker displays a list of undelivered packages on Santas dashboard. As each package finds its intended child, the list should grow shorter. An elf in the back of the sleigh is updating package information on a separate screen, marking each one delivered as it drops down the chimney. Santa sees the same dashboard on the console monitor because he and the elf are cabled together.

36 | P a g e

What makes the list shrink when the elf marks the package delivered? Traditionally, wed have written the logic ourselves. But there is a problem: the elfs module doesnt know about the list displayed on the dashboard. So its not as easy as remembering to remove an item from UndeliveredList when the elf clicks the Delivered button. Well probably need some kind of cross module event scheme. DevForce can handle this for us automatically with its managed list feature. Let the two modules share the same Entity Manager, let the list be governed by this manager, give the list the appropriate predicate keep item if not delivered- and the list takes care of itself.

Conclusion
IdeaBlade has been in this arena since.NET 1.0. The DevForce product has long offered most of the capabilities described in this paper including the multi-tier ORM, client-side caching, and code generation. The Microsoft Entity Framework is a solid contribution to the field and its very existence confirms the widespread need for an infrastructure like DevForce. But the Entity Framework by itself cannot fulfill the needs of many enterprise applications. The productivity isnt quite there. The generated code lacks essential support for business object development. Its two-tier architecture limits the applications ability to reach a distributed user community with the required performance and security. With DevForce, developers can quickly realize the potential of an object-oriented, multi-tier, enterprise application connecting hundreds or thousands of users.

37 | P a g e

Hello, DevForce

Hello, DevForce ........................................................................................................................... 38


DevForce Application Architecture - The Big Picture ........................................................................................ 38 DevForce and the ADO.NET EntityModel ......................................................................................................... 40 Your First DevForce Application: a Walk-Through ............................................................................................ 41 Understanding the App.Configs .......................................................................................................................... 71 Monitoring Activity ............................................................................................................................................. 72 Appendix: Listings of Sample App.Config Files ................................................................................................. 74 Appendix: Probing Sequence for the App.Config File ........................................................................................ 75

Creation means finding the new world in that first fierce step with no thought of return. David Whyte, Statue of Buddha

Dont look back. All change, all creation, is attended first by grief for what is lost followed by the clarity in moving on with no thought of return. DevForce is not magic and youre unlikely to build an enterprise application over night. But you can build a good application that youre proud of in reasonable time. Once you lay to rest your old habits and have grieved for them awhile, the new path will embrace you and, in spare moments, you may wonder how you ever did it that old way. But Im so happy in my comfortable way. What if things go wrong? That DevForce thing is just a little intimidating. This chapter should ease you across the threshold.

DevForce Application Architecture - The Big Picture


A DevForce application relies upon a layered architecture for data access. At one end is a data source typically a relational database. At the other end is the user interface which works with business objects in a business object model. There are several components in the middle.

38 | P a g e

Figure 1. Application Components in a DevForce Application

One of them, called an EntityServer, moves data (and data requests) between the ADO.NET Entity Framework and DevForce business objects. The EntityServer leaves the direct communication with the back-end database to the ADO.NET Entity Framework. The EntityServer has a copy of the applications business object model so that it can instantiate DevForce business objects server-side if need be. However, for most operations (such as simple data retrievals), it forwards to the client-side EntityManager the data required for hydrating DevForce business objects there, without ever instantiating DevForce business objects on the server. The data is packaged and passed in a highly efficient format and process. The ADO.NET Entity Data Model includes the mapping information necessary to translate between locations in a relational data source and the corresponding persistent fields in the ADO.NET business entities. The EntityServer mediates between the Entity Framework and the DevForce EntityManager that manages the client-side cache used by your application. The second important DevForce component is the EntityManager. The EntityManager takes instruction from the higher levels of the application such as the UI, and forwards UI requests for entities to the EntityServer. The EntityManager puts the received entities obtained from whatever source by the EntityServer -- into its entity cache and makes them available to the UI. End users review the entities and make changes through the UI. The UI signals the EntityManager to save the changes. It dutifully forwards the changed entities to the EntityServer which communicates with the appropriate component to commit the data into persistent storage.

39 | P a g e

DevForce and the ADO.NET EntityModel


Visual Studios ADO.NET Entity Data Model wizard creates an EDMX file which contains descriptions of a conceptual data schema (the object model), an actual data store schema (the database model), and the mappings between the two. Associated with the EDMX file is a code generator that renders the object model in code in a file named, by default, <ModelName>.Designer.cs (or .vb). With DevForce installed, DevForce takes over the generation of this code, and creates a file named <ModelName>.IB.Designer.cs (or .vb). The developers first step in building the object model for her application will consist in creating an entity model in an EDMX file. Typically she will use the Visual Studio Entity Data Model wizard to create the initial version of the EDMX file. After that, she will work with some combination of the Visual Studio Entity Model Designer and direct XML coding in the EDMX file, depending upon her preferences and whether she needs to use features in her model that are not supported by the Entity Model designer.2 The DevForce Object Mapper adds its own elements and attributes to the .EDMX file, which coexist happily alongside those contributed by the Entity Framework. In addition, DevForce supports the aggregation of multiple EDMX files (and corresponding back-end databases) into a single domain model. This domain model is manifested in the .NET code that DevForce generates to represent the business model to your application. You can extend the generated model using developer partial class files for each entity in the Domain Model. You can create these yourself; or DevForce will generate starter files for you. These developer class files are named <EntityName>.cs (or .vb) and are generated into the same Visual Studio project that contains the Domain Model. Those who have used DevForce 2009 may recall that in that version of DevForce two .NET versions of the business model were employed: one that was DevForce-specific and one that was the standard model generated by the Entity Framework. The Entity Framework version was only used server-side, and behind the scenes; as a developer, you never interacted with it directly. Now, because of enhancements to the Entity Framework, DevForce is now able to use a single model for all operations, server-side and client-side. The resulting efficiencies boost performance and make new capabilities possible. The object model generated by DevForce consists of business classes that inherit from IdeaBlade.EntityModel.Entity. As previously mentioned, we refer to this version of the model as the Domain model. The Domain model is persistence ignorant: it has no knowledge whatsoever of the back-end datastore or the mapping between that and its objects. In an n-tier deployment, it is the only model that is deployed client side. The client needs no connection information for back-end datasources. The Domain Model is a consumer of Entity Data Models, whose .edmx files define its content. Serverside, DevForce delegates to the Entity Framework the jobs of communicating with the database(s) to
2

These are fewer than with the first version of the Entity Framework, but some remain.

40 | P a g e

perform persistence operations including data retrieval and saving. The Entity Framework, in turn, uses the compiled versions of the Entity Data Models, as well as connection information typically stored in an app.config file, to do its work. In DevForce, all direct communications with back-end data sources are considered, logically, as serverside operations, which they will literally be in an application deployed across three or more physical tiers. The application components that facilitate such communications, including the Entity Framework, Entity Data Model, and DevForce EntityServer are considered server-side components, and are kept logically separate from client-side components such as the DevForce EntityManager and the client application. It is perfectly possible to deploy both the logical client-side components and the logical server-side components to the client machine, and this is often the configuration used for much of the development work even on enterprise applications. Youll see how to do this in the walk-through included later in this chapter. When all application components including the database server are deployed on a single physical machine, you have a single-tier deployment. When all application components except the database server are deployed on a single physical machine, and the latter is deployed to a remote machine, you have what is known as a client-server application. When client-side application components are deployed on a separate machine from server-side application components, this is typically referred to as n-tier deployment, even if the database server resides on the same machine as the application server (e.g., the DevForce BOS). However, since the strongest application security, widest availability, greatest scalability, and easiest deployment are all associated with n-tier physical deployment, we figure its much to your benefit to write your application from the beginning to permit that, and we make it as easy for you as we can.

Your First DevForce Application: a Walk-Through


DevForce ships with four Visual Studio project templates, as shown below:

41 | P a g e

Note that these templates are available only when targeting .NET Framework 4. The purposes of these templates are as follows: DevForce BOS Web Application. Creates a web application project for the DevForce Business Object Server. This project contains more complete service configuration information than other DevForce project types. DevForce n-Tier WPF Application. Creates an n-tier WPF application with a DevForce BOS. DevForce Silverlight Application. Creates a DevForce Silverlight application and BOS. DevForce WPF Application. Creates a two-tier DevForce WPF application. A two-tier application does not use a BOS, but you can easily add one later when wanted. In this document well walk through the process of creating a DevForce application, to give you a feel for what its like. Were going to use the DevForce n-Tier WPF Application for the walk-through. If you would like to see a similar walk-through for a DevForce Silverlight application, please find our Four Simple Steps paper and code solution, available from our Getting Started page (see the IdeaBlade DevForce entries on the Windows Start Menu).

42 | P a g e

Create Your Solution Using a DevForce Project Template


In Visual Studio, choose File / New Project from the main menu; under Visual Basic or C#, find the DevForce 2010 templates; select the DevForce n-Tier WPF Application template; name and locate the new solution (all as shown in the previous screen shot), and click OK. You should see something similar to the following:

The template created a WPF project named WpfApplication1, whose resulting assembly is destined for deployment on the client computer. Since we selected the n-Tier WPF application, the template also created a web project (WpfApplication1Web) that will ultimately be deployed on a web server and will host the DevForce Business Object Server. Note, however, that it will be perfectly possible to run, and develop, this application on a single machine: to do so only requires changing the setting of a single property in the app.config file. Well do that later.

Building the Model


Our next job will be to build the business object, or domain, model. In a DevForce app, both the server-side and client-side applications need access to the domain model. We can accomplish that in a couple of different ways:
1. 2. We can create the model in the web project, and then add shared file links in the WPF project; or We can create a separate project for the model, then add references to that project in the web and Wpf Application projects (which already exist now in our solution).

43 | P a g e

Both options are perfectly fine, but we have to choose one, so were going to create a separate project for the model. Create a new Windows class library project in the solution. Name it DomainModel.

Delete the Class1.cs file that Visual Studio creates; we wont need it. To the DomainModel project, add a new item, an ADO.NET Entity Data Model. Name the file NorthwindIBModel.

44 | P a g e

Clicking <Add> will launch a wizard to help you build your Entity Data Model. On the first dialog, accept the default choice of Generate from database:

On the Choose Your Data Connection dialog, select or create a connection to the NorthwindIB database, and tell Visual Studio to save the connections as NorthwindIBEntityManager:

45 | P a g e

The EDM Wizard retrieves schema information from the database and presents you with a tree control of choices for items on which to base entities in your model. Expand the Tables node and select the following tables:
Customer Employee Order OrderDetail Product Supplier

Accept the default settings for the two checkboxes:

Also accept the default model namespace of NorthwindIBModel (the name we specified earlier for the model itself).

46 | P a g e

If you receive the following security warning message

click <OK> so that DevForce can use the Entity Data Model to generate a C# or VB domain model. Until and if you turn this warning off, you will see it whenever DevForce wants to regenerate your domain model code. The EDM designer will create an XML file with extension .edmx to capture the information you (and the database) supplied using the wizard. Then it will render the newly created model visually:
47 | P a g e

If you open the Properties panel you will see, in addition to standard Entity Framework properties, many DevForce-specific ones:

48 | P a g e

If you click in white space in the designer window, you see the properties that apply to the ConceptualEntityModel:

Property DataSource Key

Description A name by which a particular data source is identified within your code by DevForce. With DevForce, you can link your domain model to multiple different databases.

49 | P a g e

EntityManager Name

The name by which you will refer to your specialized IdeaBlade.EntityModel.Entity manager in your code. Specifies whether to generate the binding attributes (including Bindable, Editable, and Display) for entity properties. Specifies whether to generate partial class files that permit you to extend your domain model. Specifies whether to generate .NET validation attributes on entity properties. Specifies whether to generate DevForce verification attributes on entity properties. Name of a class from which you want all of your entities to inherit. Limits the number of entities for which code will be generated in a single file (in order to prevent problems that Visual Studio has with very large code files). DevForce will automatically generate as much files as are needed to fully define your domain model.

Generate Binding Attributes

Generate Developer Classes

Generate Validation Attributes Generate Verification Attributes Injected base type Max.Classes Per File

If you click on a single entity, such as Customer, you see a difference set of properties, including DevForce-specific properties:

50 | P a g e

These properties allow you to specify authorization requirements for querying and saving the entity. This is discussed in more detail in a security-oriented Learning Resources example. Click on a specific property such as the primary key CustomerID

51 | P a g e

or on a navigation property, such as Customer.Orders

52 | P a g e

to see the Entity Framework and DevForce properties specific to that item. As you can see, DevForce is completely integrated with the Entity Model Designer. It also takes over all code generation for the .NET object model. If you select the NorthwindIBModel.edmx file in the Solution Explorer, then right-click it and select Properties, you can see that DevForce has modified the default Custom Tool name so that the default code generator wont be used:

53 | P a g e

If at any time you wish to restore use of the default (Entity Framework) code generator, you can simply delete the <RemoveToRestore> prefix on this entry to do so. Now lets have a look at the generated code.

Three files now accompany the .edmx:

NorthwindIBModel.edmx.tt The template used in the generation of the domain model to NorthwindIBModel.IB.Designer.cs.

NorthwindIBModel.edmx.ReadMe describes how to customize the DevForce code generation templates.


54 | P a g e

NorthwindIBModel.IB.Designer.cs contains the.NET code generated by DevForce:

Note how, at the very top of this file, you are instructed not to modify this code. DevForce owns it, and will regenerate it at will to keep up with changes you make to the Entity Data Model. Youll do your customization in developer partial class files; well discuss those shortly. But for now, have a look at some of the items in the generated code:

55 | P a g e

You see entity classes such as Customer, Employee, Order, and OrderDetail. Associated with each of these entities is an EntityPropertyNames class, and a PropertyMetadata class. The EntityPropertyNames class provides string constants for the names of each of the entitys properties: you can use these throughout your own code, wherever a property name is needed, in preference to hard-coding the string value. The PropertyMetadata class contains static EntityProperty fields corresponding to each of the entitys business properties, which you can use in your code when an EntityProperty is required. The main entity classes, such as Customer, inherit from IdeaBlade.EntityModel.Entity, and contain definitions of all of the entitys business properties. Many other facilities are available via the Entity base class. Before we leave our Entity Data Model, lets fix a couple of property names. The Employee entity, like the Employee table in the NorthwindIB database, has an association with itself, in order to reflect the chain of management. This self-association yields two navigation properties, one to represent the Employee upstream from the current one i.e., his Manager another to represent those downstream his DirectReports. The EDM designer knew from the discovered relationship that two such properties

56 | P a g e

were needed, but it didnt know enough about what they actually represent to name them very well, so it just called them Employee1 and Employee2. By selecting the Employee1 property and viewing its properties, we see that its Return Type is a Collection of Employee:

Employee1 must therefore represent the downstream Employees; well rename it to DirectReports. Double-checking the Employee2 properties verifies what we expect that it returns an Instance of Employee so we know that one represents the Manager and rename it accordingly.

57 | P a g e

We also happen to know that the Employee navigation property on the Order entity, which was automatically added to the model upon discovery of the many-to-one relationship in NorthwindIB between Orders and Employees, represents an employee who acts in the role of a sales representative for that Order. Well rename it to SalesRep.

Add References in the Web and UI Projects to the New DomainModel Project
Theres a great deal more we could, and eventually will, do to refine our model, but its already sufficient fleshed out to support data retrieval and persistence, so lets see how we can use it to do that.
1. 2. Build the DomainModel project. Add references to the DomainModel assembly in the WpfApplication1 and WpfApplication1Web projects.

You may note that both of those projects already have references to several DevForce assemblies. These were put there by the project template that you used to create the solution initially.

Using the Model


The DevForce project template created a WPF Application project, so lets add some code to that. First well flesh out ever so slightly -- the MainWindow window that the template created, by adding a TextBlock within a ScrollViewer inside the windows layout grid. The XAML were starting with is this:

XAML

<Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Grid>

58 | P a g e

</Grid> </Window>

Well enhance that to this:

XAML

<Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Grid> <ScrollViewer> <TextBlock Name="_outputTextBlock" TextWrapping="Wrap" FontFamily="Courier New" /> </ScrollViewer> </Grid> </Window>

For the purpose of this very simple application, were going to use that TextBlock control like the output window of a Console application, and simply write output strings to it. (We can get more sophisticated later, with streaming video, cascading 3D animations, chirping monkeys, and touch-screen facilities!) We could, of course, put code to retrieve and display data in the code behind area of the Window itself, but lets just put ourselves off to a good start and build a very basic Model-View-ViewModel architecture so we can (a) keep our view extremely lightweight and (b) minimize the obstacles to our applications testability. Well create a class well call MainWindowViewModel, give it a public string property named Output, and just bind our TextBlock to the value of that property:

XAML

<Grid > <ScrollViewer> <TextBlock Name="_outputTextBlock" TextWrapping="Wrap" FontFamily="Courier New" Text="{Binding Output}" /> </ScrollViewer> </Grid>

The MainWindows code behind starts out like this:

59 | P a g e

C#

using using using using using using using using using using using using using

System; System.Collections.Generic; System.Linq; System.Text; System.Windows; System.Windows.Controls; System.Windows.Data; System.Windows.Documents; System.Windows.Input; System.Windows.Media; System.Windows.Media.Imaging; System.Windows.Navigation; System.Windows.Shapes;

namespace WpfApplication1 { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } } }

Well add a handler for the windows Loaded event to do all of the following:
C#
using using using using using using using using using using using using using

Instantiate the view model; Set the views DataContext to that view model; and Call a Start() menu on the view model to initiate data retrieval and display.

System; System.Collections.Generic; System.Linq; System.Text; System.Windows; System.Windows.Controls; System.Windows.Data; System.Windows.Documents; System.Windows.Input; System.Windows.Media; System.Windows.Media.Imaging; System.Windows.Navigation; System.Windows.Shapes;

namespace WpfApplication1 { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); this.Loaded += new RoutedEventHandler(MainWindow_Loaded); } void MainWindow_Loaded(object sender, RoutedEventArgs e) { MainWindowViewModel aMainWindowViewModel = new MainWindowViewModel(); this.DataContext = aMainWindowViewModel; aMainWindowViewModel.Start(); } } }

Note that we did not have to know that the views Loaded event handler had to be a RoutedEventHandler, etc.; we simply typed in this.Loaded +=, and then pressed the TAB key a couple
60 | P a g e

of times to let Visual Studio set up an appropriate stub. We then filled in the code representing the actions we wanted to take place. Now lets create that MainWindowViewModel class. Add a new class by that name to the WpfApplication1 project and flesh out its code to look like this:

C#

using using using using using

System; System.Collections.Generic; System.Linq; System.Text; SysComp = System.ComponentModel;

using DomainModel; using IdeaBlade.EntityModel; namespace WpfApplication1 { class MainWindowViewModel : SysComp.INotifyPropertyChanged { #region Constructor public MainWindowViewModel() { } #endregion Constructor #region Methods public void Start() { FirstSample(); } public void FirstSample() { StringBuilder aStringBuilder = new StringBuilder("Started FirstSample()...\n"); var customersQuery = from cust in _mgr.Customers where cust.ContactTitle == "Sales Representative" orderby cust.CompanyName select cust; aStringBuilder.Append(string.Format("Retrieved {0} customers\n", customersQuery.ToList().Count)); foreach (Customer aCustomer in customersQuery) { aStringBuilder.Append(string.Format("Customer: {0}\n", aCustomer.CompanyName)); } Output = aStringBuilder.ToString(); } #endregion Methods #region Data Bound Properties public string Output { get { return _output; } set { _output = value; RaisePropertyChanged("Output"); } } #endregion Data Bound Properties #region INotifyPropertyChanged

61 | P a g e

public event SysComp.PropertyChangedEventHandler PropertyChanged = delegate { }; protected void RaisePropertyChanged(string propertyName) { PropertyChanged(this, new SysComp.PropertyChangedEventArgs(propertyName)); } #endregion #region Private Fields private NorthwindIBEntityManager _mgr = new NorthwindIBEntityManager(); string _output = ""; #endregion Private Fields } }

Note a few things about our ViewModel:


1. We made the class implement the INotifyPropertyChanged from the .NET namespace System.Component model. That interface requires the definition of an event name PropertyChanged, to be raised by an instance of this (MainWindowViewModel) class whenever the value of one of its public properties changes. Weve defined exactly one such public property a string property named Output which when set will cause the PropertyChanged event to be fired. Most WPF and Silverlight user interface controls listen for this event and respond to it by refreshing themselves with current data from the source object to which they are bound. In our app, the TextBlock on MainWindow will get refreshed automatically whenever the value of Output is changed.

2.

Beyond that, our view model, when the Start() method is run, simply submits a LINQ query requesting Customers who meet some condition; then uses that query to retrieve, first a count of such Customers, and then the Customer objects themselves. With each operation it writes text to the Output property.

Change the Client-Side App.Config


Were almost ready to go, but we must make a couple of tweaks to the client-side App.config file to run the application initially in a single tier (or in a client-server configuration if your database is located elsewhere). Once weve tested in a single tier, well switch to n-tier to show how simply that can be accomplished. The client-side App.config for the single-tier or client-server app looks like this:

XML

<?xml version="1.0"?> <configuration> <configSections> <section name="ideablade.configuration" type="IdeaBlade.Core.Configuration.IdeaBladeSection, IdeaBlade.Core"/> </configSections> <ideablade.configuration version="6.00" xmlns="http://schemas.ideablade.com/2010/IdeaBladeConfig"> <logging logFile="DebugLog.xml"/> <objectServer remoteBaseURL="http://localhost" serverPort="9009" serviceName="EntityService.svc" > <clientSettings isDistributed="true" /> </objectServer> <!-- Additional configuration can be added to override defaults. See the sample config files in the Learning Resources for more information. -->

62 | P a g e

</ideablade.configuration> </configuration>

Change the line:


XML
<clientSettings isDistributed="true"

to read:
XML
<clientSettings isDistributed="false"

Then, at the same level as the <ideablade.configuration> section, add a <connectionStrings> section as shown below. You can find the <connectionString> section you need in the copy of App.Config in the DomainModel project, where it was written by the Entity Data Model designer when you built the model. Simply copy it from there and paste it into the App.config for the client project (WpfApplication1).

XML

<connectionStrings> <add name="NorthwindIBEntityManager" connectionString="metadata=res://*/NorthwindIBModel.csdl|res://*/NorthwindIBModel.ssdl|r es://*/NorthwindIBModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=.;Initial Catalog=NorthwindIB;Integrated Security=True;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" /> </connectionStrings>

The completed App.config should look like this:

XML

<?xml version="1.0"?> <configuration> <configSections> <section name="ideablade.configuration" type="IdeaBlade.Core.Configuration.IdeaBladeSection, IdeaBlade.Core"/> </configSections> <ideablade.configuration version="6.00" xmlns="http://schemas.ideablade.com/2010/IdeaBladeConfig"> <logging logFile="DebugLog.xml"/> <objectServer remoteBaseURL="http://localhost" serverPort="9009" serviceName="EntityService.svc" > <clientSettings isDistributed="false" /> </objectServer> <!-- Additional configuration can be added to override defaults. See the sample config files in the Learning Resources for more information. --> </ideablade.configuration> <connectionStrings> <add name="NorthwindIBEntityManager" connectionString="metadata=res://*/NorthwindIBModel.csdl|res://*/NorthwindIBModel.ssdl|r

63 | P a g e

es://*/NorthwindIBModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=.;Initial Catalog=NorthwindIB;Integrated Security=True;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" /> </connectionStrings> </configuration>

Run the App


Now were ready to run our app. A few seconds after launch, you should see the following display:

Add Business Logic to the DomainModel


So far, so good, but now lets see how we add custom logic to our business model. As we previously pointed out, the code that currently comprises the business model all stored in the file NorthwindIBModel.IB.Designer.cs -- is owned entirely by DevForce and will be overwritten by DevForce whenever you change the content of the Entity Data Model (.edmx) file. That makes it a very bad place to put custom code! Where such code should be put is in partial class files that extend the existing model. You can code such files yourself from scratch, but you can also have the code generator write starter versions for you. To do that:
1. 2. 3. Open the Entity Data Model in the designer View the Properties panel in Visual Studio Click on empty white space in the designer window to see the properties of the Conceptual Entity Model.

64 | P a g e

4. 5.

In the DevForce Code Generation section, find the property Generate Developer Classes, and set its value to true. Save the updated model (and respond to the Security Warning about the text template, if it appears, by clicking <OK>).

You will now see one new file in the DomainModel project for each of the entities that you included in your model, as at right. Lets look at the contents of the Customer.cs file:

C#

using using using using

System; System.Linq; IbEm = IdeaBlade.EntityModel; IdeaBlade.Core;

namespace DomainModel { // The IdeaBlade DevForce Object Mapping Tool generates this class once // and will not overwrite any changes you make. You can place your custom // application-specific business logic in this file. Generated: 3/31/2010 6:17:32 PM // // This partial class is the companion to the class regenerated by the // Object Mapping tool whenever the model changes. public partial class Customer : IbEm.Entity { } }

65 | P a g e

Thats pretty unimposing. But since its a partial class it can be enhanced to extend the generated model almost without limit. For example, lets add a static Create() method so we can create and initialize new Customers in a standard way throughout our application. First we add a statement to make items in the DomainModel namespace available within the class:

C#

using DomainModel;

Then we add the Create() method:


C#
static Customer Create(string companyName) { Customer newCustomer = new Customer(); newCustomer.CustomerID = System.Guid.NewGuid(); newCustomer.CompanyName = companyName; newCustomer.EntityAspect.AddToManager(); return newCustomer; }

Our Create() method requires a company name. It instantiates a new Customer, and because the Customer type has a GUID primary key, the code assigns a primary key value. It then assigns the companyName value passed to the method to the corresponding property on the new entity, and then very importantly puts the new entity into the local cache and under an EntityManagers control. It then returns a reference to the new entity to the calling method. You can add other custom methods, and custom properties, as needed and as desired. But suppose what you want to do is to override the behavior of a generated property. You cant override or shadow the property because its defined in the same class youre working in. (Remember, youre in a partial class that is extending the existing, generated partial class.) So how do you accomplish what you need? The answer is to use a property interceptor. This is a bit of code that you provide and decorate with attributes that describe to DevForce your intentions for it. It is then called by DevForce at an appropriate point in a Get or Set process to alter the result of that operation. For example, heres an interceptor that converts the value of Customer.Company to its uppercase equivalent during a Get operation:
C#
[AfterGet(EntityPropertyNames.CompanyName)] public String UppercaseLastName(String companyName) { if (!String.IsNullOrEmpty(companyName)) { return companyName.ToUpper(); } else { return String.Empty; } }

66 | P a g e

If this interceptor is defined within the Customer class, DevForce assumes it is meant to apply to Customers; and because we have said so in the [AfterGet] attribute with which we decorated our interceptor method, DevForce knows the interceptor is meant to be applied only to the CompanyName property. Run the application again and youll see the result of adding the AfterGet interceptor:

You can define interceptors to be as general as you need: for example, you can provide an interceptor method that will apply to every property of Customer; or even to every property of every entity in your model! You can define interceptors that jump into the middle of a Get before a value is obtained from the entity instance (which you could use, for example, to prevent certain data from being made available to unauthorized users); after the value is obtained from the business object but before it is delivered to the requestor (as in the interceptor above); before a proposed new value is pushed into the business object for which it is targetted; and after it is so pushed. You can alter the process of getting and setting business object property values in any way you need to do it. One final note on the developer classes: recall that we got DevForce to generate starter partial classes for extending the entities in our business model by setting the Generate Developer Classes property on the ConceptualEntityModel to true. Even though we havent set that value back to false, were in no danger of losing the customizations we just made to Customer.cs. DevForce will never overwrite an existing developer partial class.

Running the Application in an N-Tier Configuration


Now were ready for the last step, running the application in multiple tiers. We created this application using the DevForce N-Tier WPF Application template, and weve got a web application project in our
67 | P a g e

solution which we havent used yet. The web application project hosts the DevForce Business Object Server, and is almost ready to go, with a few more changes to the config files. Client-side App.Config. We toggled the isDistributed flag off earlier to show the application running in a single tier. Lets turn that flag back on now:

XML

<clientSettings isDistributed="true"

This flag, along with the objectServer settings indicating the URL, port and name of the BOS, will tell the client application where the BOS is located. The BOS in the web application project here is by default also listening at this address, so no additional wiring is required.

XML

<objectServer remoteBaseURL="http://localhost" serverPort="9009" serviceName="EntityService.svc" > <clientSettings isDistributed="true" /> </objectServer>

Server-side Web.Config. We need to ensure the connectionStrings section is available here in the web.config, since data access will now be performed here on the BOS and not in the client application. We previously copied the connectionStrings from the Domain Model app.config into the executable projects app.config; we can now copy (or move if you prefer) this information into the web.config. We dont need any other changes in the web.config, so it should look something like this when done:

XML

<?xml version="1.0"?> <configuration> <configSections> <section name="ideablade.configuration" type="IdeaBlade.Core.Configuration.IdeaBladeSection, IdeaBlade.Core"/> </configSections> <connectionStrings> <add name="NorthwindIBEntityManager" connectionString="metadata=res://*/NorthwindIBModel.csdl|res://*/NorthwindIBModel.ssdl|r es://*/NorthwindIBModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=.;Initial Catalog=NorthwindIB;Integrated Security=True;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" /> </connectionStrings> <ideablade.configuration version="6.00" xmlns="http://schemas.ideablade.com/2010/IdeaBladeConfig" > <logging logFile="log\DebugLog.xml"/> <!-- Additional configuration can be added to override defaults. See the sample config files in the Learning Resources for more information. -->

68 | P a g e

</ideablade.configuration> </configuration>

With these changes were ready to fire up the application in n-tier! One more thing before hitting F5, double-check that the web project will start up automatically. The Always Start When Debugging flag should be True, to ensure the ASP.NET Development Server starts when the client project does.

Now build and run, and youll see the same application window you saw before. But theres a difference this time, the application is now running in multiple tiers the WPF client application is now communicating with the BOS, which is now responsible for communicating with the data tier. The BOS here is running on the same machine (localhost), but you can now deploy the BOS to another machine when youre ready. Deployment is a complex topic, covered separately, but youve now created an application which can run in either a single tier or n-tier at the flip of a switch!

More Capabilities DevForce Can Add to Your Application


That completes our quick tour of the basics. There are, naturally, far more sophisticated things you can do in your application with DevForces help. Just as a few examples, you can:
69 | P a g e

Access multiple separate databases from your Entity Data Model (impossible with the unaided Entity Framework); Easily design your app to run disconnected from the database, saving changes locally until a connection to the database is available and desired; Order complex operations to be performed entirely server-side, with delivery of as little or as much resultant data to the client as desired; Leverage DevForces rich validation subsystem to ensure absolute integrity in the data saved to the database; Intercept data retrievals and saves from and to the database, altering or preventing the operations as desired based upon the users privileges and any other factors you deem relevant.; Instantiate multiple EntityManagers in your application, each managing its own cache, in order to provide work units of information in which local changes can be saved or reversed without impacting other data being used locally; Fully leverage the power of LINQ (Language Integrated Query) for data retrieval and manipulation to free yourself from SQL forever; and, of course, Much more!

You can get detail on these facilities and techniques in other DevForce Learning Resources, including topic documents, code samples, and videos. Enjoy the journey!

70 | P a g e

Understanding the App.Configs


You will soon discover that your Entity Framework / DevForce app includes many app.config files. Each has its necessary and particular role, which well explain below. The sample Visual Studio solution at right includes two app.config files, one in each of the following locations:

1. 2.

In the project for the Domain Model (#1); In the executable project (#2)

The App.Config in the Domain Model project (#1 in the picture) typically gets there by being generated by the Visual Studio Entity Data Model designer. It contains a configuration section with a connectionStrings element. For a sample, see Listing 1 in the Appendix Listings of Sample App.Config Files at the end of this chapter. The App.config in the executable project (#2 in the picture) typically gets there because youve used a DevForce project template to create the project. Alternately you can create a new app.config, or edit an existing one, using the DevForce Configuration Editor.3

The app.config in the executable project (#2) contains, most importantly, an ideaBlade.configuration section which contains settings allowing you to configure your DevForce application. This config may also contain configuration information for other elements of your application not related to DevForce.

Config Editor under IdeaBlade DevForce 2010 / Tools on the Windows Start menu.

71 | P a g e

At run time, DevForce requires connection information for the data sources used by your application. In a two-tier application such as the sample shown, this connection information should be located in the app.config of the executable project. (In an n-tier application, in which a DevForce Business Object Server is used, the connection information is needed only on the server, typically a web.config file when the BOS is hosted by IIS, or otherwise a .config for the server. N-tier applications are covered in later chapters.) In our two-tier sample, since connection string information is required here by DevForce at run time, you need to ensure that youve copied the connectionStrings from the app.config in your Domain Model project (project #1) into the app.config used at run time (project #2). DevForce will not copy this information for you, and you will receive an error at run time if you try to query or save data and you have not supplied connection information. Note that you do not need to use the same database for design-time work as you do at run time therefore the connection information may differ in the actual database/server specified. Note that the App.config in your Domain Model project is used by the Entity Data Model designer at design time when working with the EDMX file. The connectionStrings here tell the designer where your design-time database is located, and allow you to update your Domain Model when needed. The App.config here, since located in a class library, is not used at run time. When Visual Studio builds your projects it automatically copies and renames the App.config files based on the assembly name. For example in the sample shown, a DomainModel.dll.config will be created for the Domain Model project, and a WpfApplication1.exe.config for the executable project. Only the *.exe.config is used by DevForce at run time.

Monitoring Activity
What is actually happening as we run the applications? When is it asking for data? What does the SQL look like?

SQL Profiler
We can always monitor activity on the SQL Server using SQL Profiler. Here we assume SQL Server 2008.
Launch Microsoft SQL Server Management Studio. Select Tools SQL Server Profiler from the menu. Select File New Trace .. from the Profiler menu and connect to your database server Click [Run] on the [Trace Properties] dialog. Return to Visual Studio and re-run the application [F5]

72 | P a g e

The trace window fills, showing us exactly how were hitting the database.

DevForce Tracing
As your application runs DevForce generates trace messages containing information (and warnings) about its actions. These messages will help you diagnose and debug problems should they occur. You can also use the DebugFns.WriteLine and TraceFns.WriteLine methods to send your own debug and trace messages to DevForce. By default DevForce writes these messages to a trace log file4 named DebugLog.xml5 in the executable directory.
Open Windows Explorer. Navigate to the ..\bin\debug directory under the executables directory. Launch DebugLog.xml. The log appears in a browser window.

Each row speaks of some event during the life of the last application run. Youll see database access events among other event occurring from the start of the application until it shuts down. You can launch the DebugLog while the application is running and refresh the browser from time to time to see how the log is progressing as you move through the application.

DevForce TraceViewer

4 5

You can turn it off or filter it, or choose a custom logger. There are companion .css and .xslt files in that directory as well so that the log displays in the browser nicely. You can rename the log in the App.Config file.

73 | P a g e

DevForce also supplies a sample utility, the TraceViewer, which comes in WinForms and WPF flavors. The TraceViewer affords a friendlier and more dynamic look at logged activity as it provides a live view of trace activity for your running application. You can launch the TraceViewer from the IdeaBlade DevForce/Tools menu. It can also be linked directly into your application. See the Object Persistence chapter for details.

Appendix: Listings of Sample App.Config Files


Listing 1. App.Config associated with the Domain Model

XML

<?xml version="1.0" encoding="utf-8"?> <configuration> <connectionStrings> <add name="ServerModelNorthwindIBContext" connectionString="metadata=res://*/ServerModelNorthwindIB.csdl|res://*/ServerModelNorthw indIB.ssdl|res://*/ServerModelNorthwindIB.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=.;Initial Catalog=NorthwindIB;Integrated Security=True;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" /> </connectionStrings> </configuration>

Listing 2. Copy of app.config associated with the executable project

XML

<?xml version="1.0"?> <configuration> <configSections> <section name="ideablade.configuration" type="IdeaBlade.Core.Configuration.IdeaBladeSection, IdeaBlade.Core"/> </configSections>

<ideablade.configuration version="6.00" xmlns="http://schemas.ideablade.com/2010/IdeaBladeConfig">

<logging logFile="DebugLog.xml"/>

<!-- Additional configuration can be added to override defaults. See the sample config files in the Learning Resources for more information. -->

74 | P a g e

</ideablade.configuration>

<connectionStrings> <add name="ServerModelNorthwindIBContext" connectionString="metadata=res://*/ServerModelNorthwindIB.csdl|res://*/ServerModelNorth windIB.ssdl|res://*/ServerModelNorthwindIB.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=.;Initial Catalog=NorthwindIB;Integrated Security=True;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" /> </connectionStrings>

</configuration>

Appendix: Probing Sequence for the App.Config File


Whenever a .NET application attempts to exercise any aspect of the DevForce API that requires DevForce configuration information and such configuration information has not already been found and placed into memory, DevForce will search for a configuration file that contains this information. Its probing path is as follows:

1.

2.

3.

4. 5.

If the ConfigFileLocation property of the IdeaBladeConfig object has been set in the executing code, DevForce will search the indicated location for a file named or matching *.exe.config (or web.config if a web project). If not found, other *.config files in the folder are searched for a valid ideablade.configuration section. If the ConfigFileLocation property was not set, or a suitable config file was not found in the indicated location, then DevForce will look to see if the IdeaBladeConfig.ConfigFileAssembly property has been set. If so, it will look for an embedded resource named app.config. Next, the BaseAppDirectory is searched for a file named or matching *.exe.config (or web.config if a web project). If not found, other *.config files in the folder are searched for a valid ideablade.configuration section. DevForce next searches for an embedded resource named app.config in the entry assembly. If a valid Ideablade.configuration section was not found in any of the above locations then DevForce will create a default IdeaBladeConfig instance. Check your debuglog.xml if you find that your configuration is not being used.

Note that in a test project, such as one created with MSTest, there is no entry assembly so the probing sequence may not correspond to what youll see in your non-test projects. You can work around this by setting IdeaBlade.ConfigFileLocation or IdeaBlade.ConfigFileAssembly, or deploying the config file to the test folder.

75 | P a g e

DevForce Entity Model (EDMX) Designer Enhancements

DevForce Entity Model (EDMX) Designer Enhancements ....................................................... 76


Entity-Model-Level DevForce-Specific Properties ............................................................................................. 77 Type-Level DevForce-Specific Properties ........................................................................................................... 79 Property-Level DevForce-Specific Properties ..................................................................................................... 80

DevForce is integrated seamlessly with the Entity Framework 4.0 EDMX Designer. For practical purposes, this means that DevForce-specific properties now appear directly on their associated EDMX Designer property pages alongside the standard EDMX designer properties. On each property page, you will find these properties grouped under a DevForce Code Generation category heading. Setting or modifying any of these properties will make a modification in the underlying EDMX file. All DevForce-related changes are additive to what is already in the EDMX file and are generated as XML attributes with a DevForce specific namespace qualifier. This insures that DevForce additions cannot conflict with or break an EDMX file. When an EDMX file is saved for the first time, a new project item is added to the project containing the EDMX file. This project item has the same name as the EDMX file but with an added .tt extension. Several additional files are also generated under this project item. One is a ReadMe file that explains in more detail how to customize template generation. The other files have an .IB.Designer extension and contain the DevForce-generated .NET code. Feel free to inspect these files, but do not modify them because they will be regenerated each time the EDMX file changes. In most cases, there will only be a single such file, but there are options (described later) to allow the generated code to span multiple files. One further side effect of this operation is to turn off the standard Entity Framework code generation mechanism. DevForce does this by modifying the Custom Tool property associated with the EDMX file by adding a <RemoveToRestore> prefix6 to the custom tool name specified there. At any time this prefix may be removed to restore the standard Entity Framework code generation. If you do that, it is usually a good idea also to remove the DevForce .tt file, along with its underlying children, to avoid conflicts.

You will receive a compiler warning that the custom tool <RemoveToRestore>EntityModelCodeGenerator was not found. This message is harmless. To eliminate it, you can entirely remove the Custom Tool value for the EDMX file.

76 | P a g e

Every time the EDMX file is saved, the DevForce .tt file along with the associated generated .NET code is recreated. The .tt file is a T47 template, and is used to auto-generate the DevForce domain model files. DevForce code generation will occur under either of two conditions:

1. 2.

When the EDMX file is saved. When the corresponding file with the same name as the EDMX file with a .tt extension has its Run Custom Tool operation performed.

If you wish completely to disable the DevForce behaviors, the Tools -> ExtensionManager menu within Visual Studio 2010 contains an entry for the IdeaBlade OM Designer Extension that can be disabled. The remainder of this document will enumerate each of the DevForce EDMX properties. Each of these properties will be available along with the rest of the Entity Framework properties whenever the corresponding parent object is selected in the EDMX designer. These properties currently exist at three levels within the designer, each of which is accessible by selecting the appropriate object in the designer: 1. 2. 3. At the model level At the type level (Entity or Complex Type) At the property level

Entity-Model-Level DevForce-Specific Properties


To see Entity-Model-Level, select white space on the designer surface.

DataSource Key (string) Each EDMX file represents a single datasource; this field contains a unique name to identifying this datasource. The value specified will be used to create [DataSourceKey] attributes in the generated code. At runtime, the DataSourceKey will be used to find the appropriate connection string, from either the connectionStrings or edmKeys elements in the configuration file.

T4 is shorthand for Text Template Transformation Toolkit. See, for example, the webcast T4 Templates in Visual Studio Code Generation at http://www.pnpguidance.net/Screencast/T4TemplatesVisualStudioCodeGenerationScreencast.aspx .

77 | P a g e

EntityManager Name (string) This will be the name of the generated EntityManager subclass. Its value need not be unique within a project. If a single project has multiple EDMX files, then any models with the same Entity Manager Name will be generated as a series of partial class files together composing a single EntityManager subclass. Otherwise, multiple EntityManager subclasses will be created. C#
[IbEm.DataSourceKeyName(@"NorthwindIBEntities")] public partial class NorthwindIBEntities : IbEm.EntityManager { ..}

Generate Binding Attributes (bool ) This setting controls whether to generate the following attributes onto individual properties of your entities: BindableAttribute, DisplayAttribute, EditableAttribute and ReadOnlyAttribute. In generated, each of these attributes will be generated with the appropriate parameters for each property of the entity. Note that most of these attributes will be generated based in part on other properties within the EDMX file, i.e. property names, whether a setter exists, etc. Generate Developer Classes (bool) This setting allows you to have DevForce auto-generate developer stub files for all entities. Generate Validation Attributes (bool) Whether to generate .NET Validation attributes on applicable properties. Currently the relevant .NET Validation attributes are the StringLengthAttribute and the RequiredAttribute. Generate Verification Attributes (bool) Whether to generate DevForce Validation attributes. If both GenerateValidationAttributes and GenerateVerificationAttributes are true, then only the DevForce attributes will appear in the generated code, but these attributes will be generated in such a way as to act exactly like the .NET Validation attributes when standard .NET Validation calls are used. DevForce Verification will also work as described in the Developer Guide. In general, DevForce Verification provides a richer validation model and is more seamlessly integrated into the DevForce entities. Injected Base Type (string) This allows a developer to inject a new base entity type at the root of the EDMX class hierarchy. All generated classes will either directly or through their base classes inherit from this class. This class will usually be generated just like any other entity class, as a partial class definition within the generated code that can be customized by the addition of other partial class files with additional implementation details. This class will be generated automatically, except in the case where the type name specified represents a qualified name. A qualified name is any name containing a .. In this case no partial class definition will be generated but all other generated code will still derive from a class with the
78 | P a g e

specified name. This feature allows a single base entity class to exist in a separate project with its own namespace and still be shared among multiple EDMX models. Max. Classes per File (number) Models with a large number of entities can sometimes result in generated code files that are too large to be processed by the Visual Studio editor. This problem may be avoided by generating code into more than one file. The Max classes per file setting will limit the number of classes that are generated into a single file and will create as many files as are necessary to meet the specified constraint for each file. The default value for this property is 100 but can be adjusted in either direction. If it is set to 1, then each generated file will contain only a single class. Having many generated files is often a bad idea because of issues involved with synchronizing many changed files within a source control system. We recommend leaving this default as is unless you have a specific issue that requires changing it.

Type-Level DevForce-Specific Properties


To see type-level properties, select any type on the designer surface (e.g., Customer):

Can Query (enum True, False, Default) Determines whether a client application (as opposed to Server side code), can perform queries that return or involve this type. Setting this property to either True or False will cause a ClientCanQuery attribute to be added to the generated code for the specified type. Setting the value to Default will suppress the generation of this attribute. The interpretation of what Default means is then deferred to any implementation of the EntityServerQueryInterceptor (see separate documentation in the Security chapter).
C# [IbEm.ClientCanQuery(true)] public partial class Customer : IbEm.Entity { }

CanSave (enum True, False, Default) Same idea as CanQuery but applied to whether a client application can save entities of this type. The EntityServerSaveInterceptor mediates this process. (See the Security chapter for more information on implementing this class.)

79 | P a g e

C# [IbEm.ClientCanSave(true)] public partial class Customer : IbEm.Entity { }

Property-Level DevForce-Specific Properties


To see property-level properties, select any property within a type (e.g., CompanyName within Customer).

Bindable Mode (enum OneWay, TwoWay, None) If the GenerateBindingAttributes property defined above at the model level is set to true, then this property describes how to construct the BindableAttribute for the property in the generated code.
C# [Bindable(true, BindingDirection.TwoWay)] public string CompanyName { .. }

Concurrency Strategy (enum None, AutoGuid, AutoDateTime, AutoIncrement, ServerCallback, Client) Determines whether this field participates in Optimistic Concurrency conflict resolution and if so how and where this column is updated. This topic is described in more detail in the Developer Guide. The auto-generated DataEntityProperty constructor contains the strategy selected. C#
public static readonly IbEm.DataEntityProperty<Customer, Nullable<int>> RowVersion = new IbEm.DataEntityProperty<Customer, Nullable<int>>("RowVersion", true, false, IbEm.ConcurrencyStrategy.AutoIncrement, false);

Display Name (string) If the GenerateBindingAttributes property defined above at the model level is set to true, then this property is used to construct the DisplayAttribute for the property in the generated code.

80 | P a g e

C#

[Display(Name="CompanyName", AutoGenerateField=true)] public string CompanyName { .. }

81 | P a g e

Customizing the DevForce Code Generation Template


DevForce code generation uses the .NET Text Template Transformation Toolkit (T4) to customize code generation. DevForce ships with a default T4 template that is used for all code generation. Like most T4 templates provided by Microsoft and other vendors, this template can be replaced with a custom version. Custom versions are usually a copy of the default template with some minor modifications. While this mechanism is supported by DevForce, we provide what we believe to be a better approach: we allow the developer to customize our template by subclassing it and overriding only those portions from which he requires custom behaviors. This typically results in substantially less code to maintain. It also means that as we release new and improved versions of the template with new features, customers who have customized their templates are much less likely to be forced into corresponding changes. Also unlike the templates offered by most other vendors, the T4 template with which DevForce ships, and which your customized versions can leverage, has the ability to generate either C# or VB code from a single template file. You will also see later that debugging a DevForce template is substantially easier than debugging a standard T4 template. A DevForce template looks something like the following: T4
<#@ <#@ <#@ <#@ <#@ <#@ <#@ <#@ <#@ <#@ <#@ <# template language="C#" debug="true" hostSpecific="true" #> output extension=".ReadMe" #> Assembly Name="System.Core.dll" #> Assembly Name="IdeaBlade.VisualStudio.DTE.dll" #> Assembly Name="IdeaBlade.VisualStudio.OM.CodeGenerator.dll" #> Assembly Name="IdeaBlade.EntityModel.Edm.Metadata.dll" #> import namespace="System" #> import namespace="System.Reflection" #> import namespace="IdeaBlade.VisualStudio.DTE" #> import namespace="IdeaBlade.VisualStudio.OM.CodeGenerator" #> import namespace="IdeaBlade.EntityModel.Edm.Metadata" #>

// Source for this file located at: C:\Users\Jay\AppData\Local\Microsoft\VisualStudio\10.0\Extensions\IdeaBlade, Inc\IdeaBlade OM Designer Extension\6.0.1.0\DomainModelTemplate.tt // Model1.edmx <--- This is needed so that "Transform Related Text Templates On Save" works correctly. var template = new DomainModelTemplate(this); template.Generate(); #> <#+ #>

There will be one such file added to a project for each EDMX file in the project. This files name will be exactly the same as the EDMX file name but with a further .tt extension to indicate that it is a T4 template.
82 | P a g e

If you have experience with T4 template files, you will notice that our template is much shorter than most. Thats because our T4 template hands off the job of generating code to the DomainModelTemplate class. This class is located in the IdeaBlade.VisualStudio.OM.CodeGenerator assembly, and a copy of the source is provided in the DevForce installation directory. Virtually every major code generation region within your generated code corresponds to a virtual method within this class. To customize your generated code, several options are available:
1. 2. 3. Subclass the DomainModelTemplate class directly in the T4 template file provided Copy the template that you have just customized to the directory named within the first comment within the template. Use approach #2, but separate the DomainModelTemplate subclass into a separate assembly that is then referenced by the template.

We provide detail on these three alternatives below.

Option 1: Subclass the DomainModelTemplate class directly in the provided T4 template file

This might look something like the following: T4


<#@ <#@ <#@ <#@ <#@ <#@ <#@ <#@ <#@ <#@ <#@ <#@ <#@ <# template language="C#" debug="true" hostSpecific="true" #> output extension=".ReadMe" #> Assembly Name="System.Core.dll" #> Assembly Name="Microsoft.VisualStudio.TextTemplating.10.0" #> Assembly Name="IdeaBlade.Core" #> Assembly Name="IdeaBlade.VisualStudio.DTE.dll" #> Assembly Name="IdeaBlade.VisualStudio.OM.CodeGenerator.dll" #> Assembly Name="IdeaBlade.EntityModel.Edm.Metadata.dll" #> import namespace="System" #> import namespace="System.Reflection" #> import namespace="IdeaBlade.VisualStudio.DTE" #> import namespace="IdeaBlade.VisualStudio.OM.CodeGenerator" #> import namespace="IdeaBlade.EntityModel.Edm.Metadata" #>

// Source for this file located at: C:\Users\Jay\AppData\Local\Microsoft\VisualStudio\10.0\Extensions\IdeaBlade, Inc\IdeaBlade OM Designer Extension\6.0.1.0\DomainModelTemplate.tt // Model1.edmx <--- This is needed so that "Transform Related Text Templates On Save" works correctly. var template = new MyTemplate(this); template.Generate(); #> <#+ public class MyTemplate : DomainModelTemplate { public MyTemplate(Microsoft.VisualStudio.TextTemplating.TextTransformation textTransformation) : base(textTransformation) {} protected override void WriteEntityDataPropertyAttributes(EdmPropertyWrapper property) { WriteAttribute("Foo(" + Quote(property.Name) + ")"); base.WriteEntityDataPropertyAttributes(property);

83 | P a g e

} } #>

Modifications to the original template are in italicized bold. This is a very simple customization that adds a Foo attribute to the top of every data entity property that is generated. Note that two additional assembly references were added to the top of the template and that the call to new DomainModelTemplate was replaced by a call to new MyTemplate. The MyTemplate class in this case is provided directly below within the template itself.

While this is certainly simple, it is not the recommended approach for several reasons. Firstly, this customization will only apply to the specific EDMX that it is attached to, and secondly it is difficult to edit a template file with any significant amount of code because of the lack of Intellisense.

Option 2: Copy the customized template to the directory named within the first comment

The second approach to customizing the code generation process is to create a copy of the template that you have just customized and copy it to the directory named within the first comment within the template. This location will vary according to machine and operating system. In the above example we would copy the file to the following directory:
C:\Users\Jay\AppData\Local\Microsoft\VisualStudio\10.0\Extensions\IdeaBlade, Inc\IdeaBlade OM Designer Extension\6.0.1.0\

Within this directory there will be a file named DomainModelTemplate.tt. This is the current template and it is usually a good idea to rename this file for backup purposes. The file that you just copied to this directory should then be renamed to DomainModelTemplate.tt. Two additional edits will be needed to complete the process. The two comment lines within the file should be replaced with the following two comment lines. These can be copied directly from the original DomainModelTemplate.tt file.

T4

// Source for this file located at: $templateFullFileName$ // $edmxname$ <--- Needed so "Transform Related Text Templates On Save" works correctly.

Once this is done, any templates that are automatically added to a project will be based on your new template. If you have older projects that were generated with an earlier version of the template, the

84 | P a g e

.tt files in these directories can be removed and the EDMX files resaved to force a regeneration of both the template file and your new generated code.

Option 3: Use Option2, but separate the DomainModelTemplate subclass into a separate assembly that is then referenced by the template
A third approach to customization is based on the second approach, but separates out the DomainModelTemplate subclass into a separate assembly that is then referenced by the template. The idea is to create your own assembly with a single class that inherits from IdeaBlade.VisualStudio.OM.CodeGenerator.DomainModelTemplate. This would look virtually identical to the MyTemplate class from above:

C#

public namespace MyNamespace { public class MyTemplate : DomainModelTemplate { public MyTemplate(Microsoft.VisualStudio.TextTemplating.TextTransformation textTransformation) : base(textTransformation) {} protected override void WriteEntityDataPropertyAttributes(EdmPropertyWrapper property) { WriteAttribute("Foo(" + Quote(property.Name) + ")"); base.WriteEntityDataPropertyAttributes(property); } }

And your template would then return to something very similar to the original template.

T4

<#@ <#@ <#@ <#@ <#@ <#@ <#@ <#@ <#@ <#@ <#@ <#@ <#

template language="C#" debug="true" hostSpecific="true" #> output extension=".ReadMe" #> Assembly Name="System.Core.dll" #> Assembly Name="IdeaBlade.VisualStudio.DTE.dll" #> Assembly Name="IdeaBlade.VisualStudio.OM.CodeGenerator.dll" #> Assembly Name="IdeaBlade.EntityModel.Edm.Metadata.dll" #> Assembly Name=MyAssembly #> or <#@ Assembly Name={path to MyAssembly} import namespace="System" #> import namespace="System.Reflection" #> import namespace="IdeaBlade.VisualStudio.DTE" #> import namespace="IdeaBlade.VisualStudio.OM.CodeGenerator" #> import namespace="IdeaBlade.EntityModel.Edm.Metadata" #>

#>

// Source for this file located at: $templateFullFileName$ // $edmxname$ <--- This is needed so that "Transform Related Text Templates On Save" works correctly var template = new MyNamespace.MyTemplate(this); template.Generate(); #> <#+ #>

The two references to MyAssembly in the template above have to do with whether MyAssembly has been placed in the GAC or not. If it has, then the first syntax is appropriate, otherwise, use the fully qualified path to

85 | P a g e

your assembly. The advantage of this approach is that both debugging and construction, because of intellisense, becomes much simpler.

86 | P a g e

IdeaBlade DevForce

Property Interceptors

Property Interceptors

Property Interceptors ................................................................................................................... 87


State of the Release Candidate Documentation ................................................................................................... 88 Code Snippets in This Document ........................................................................................................................ 88 Attribute Interception........................................................................................................................................... 88 Named vs. Unnamed Interceptor Actions ............................................................................................................ 91 Interceptor Chaining and Ordering ...................................................................................................................... 92 Multiple Attributes on a Single Interceptor Action ............................................................................................. 94 Defining an Interceptor to Act Against Multiple Types ...................................................................................... 95 The EntityPropertyNames class ........................................................................................................................... 96 PropertyInterceptorArgs and IPropertyInterceptorArgs ...................................................................................... 96 PropertyInterceptor Attribute Discovery ........................................................................................................... 104 Alternative PropertyInterceptor Attribute Method Signatures ........................................................................... 106 Dynamic Property Interception and the PropertyInterceptorManager. .............................................................. 107 EntityProperties and Property Interceptors ........................................................................................................ 109 PropertyInterceptor Keys ................................................................................................................................... 109 Mechanics of Property Interception ................................................................................................................... 109

DevForce provides a mechanism to intercept and either modify or extend the behavior of any .NET property. This interception is intended to replace, and expand upon, the technique of marking properties as virtual and overriding them in a subclass. This facility is a lightweight form of what is termed Aspect-Oriented Programming. Interception can be accomplished either statically, via attributes on developer-defined interception methods, or dynamically, via runtime calls to the current instance of the PropertyInterceptorManager (described later). Attribute interception is substantially easier to write and should be the default choice in most cases.

87 | P a g e

IdeaBlade DevForce

Property Interceptors

State of the Release Candidate Documentation


We are working hard to update all of our documentation from DevForce 2009 to DevForce 2010, .NET 4, and Silverlight 4. During this conversion, you may find some sections that are out of date, but you should be able to get many of the examples to work, with small modifications, by checking against the API Documentation for the current method signatures.

The C# code snippets in this document (and the corresponding Visual Studio solution) are current for DevForce 2010, but the VB snippets have not yet been updated. If you have questions about the currency of particular material, please contact support.

Code Snippets in This Document


The code snippets in this document are duplicated in accompanying C# and VB8 code solutions. After installing DevForce, you will find these solutions in the _TopicDocumentSnippets folder under the Business Object Persistence topic in the Learning Resources. The captions associated herein with the snippets reflect the corresponding method names in the code solutions. In this chapter two methods are often referenced:
1. 2. The Entity class (e.g., Customer, Employee, etc.) where the interceptor is defined A test method in MainDemo.cs that exercises the interceptor and demonstrates its effect.

Attribute Interception
DevForce supplies four attributes that are used to specify where and when property interception should occur. These attributes are
IdeaBlade.Core.BeforeGetAttribute IdeaBlade.Core.AfterGetAttribute IdeaBlade.Core.BeforeSetAttribute IdeaBlade.Core.AfterSetAttribute

Under most conditions these attributes will be placed on methods defined in the custom partial class associated with a particular DevForce entity. For example, the code immediately below represents a snippet from the autogenerated Employee class. (Generated code)

forthcoming

88 | P a g e

IdeaBlade DevForce
C#

Property Interceptors

public partial class Employee : IdeaBlade.EntityModel.Entity {

public string LastName { get { return PropertyMetadata.LastName.GetValue(this); } set { PropertyMetadata.LastName.SetValue(this, value); } }

VB
Partial Public Class Employee Inherits IdeaBlade.EntityModel.Entity Public Property LastName() As String Get Return LastNameEntityProperty.GetValue(Me) End Get Set(ByVal value As String) LastNameEntityProperty.SetValue(Me, value) End Set End Property

Property interception of the get portion of this property would be accomplished by adding the following code fragment to a custom Employee partial class definition:
Code Listing 1. Customer.UppercaseCompanyNameAfterGet(). Tested by MainDemo.TriggerBasicAfterGet().

C#
[AfterGet(EntityPropertyNames.CompanyName)] public void UppercaseCompanyNameAfterGet( PropertyInterceptorArgs<Customer, String> args) { var companyName = args.Value; if (!String.IsNullOrEmpty(companyName)) { args.Value = args.Value.ToUpper(); } }

VB

<AfterGet(EntityPropertyNames.LastName)> _ Public Sub UppercaseLastName(ByVal args As PropertyInterceptorArgs(Of Employee, String))

89 | P a g e

IdeaBlade DevForce
Dim lastName = args.Value If Not String.IsNullOrEmpty(lastName) Then args.Value = args.Value.ToUpper() End If End Sub

Property Interceptors

DevForce will insure that this method is automatically called as part of any call to the Employee.LastName get property. The AfterGet attribute specifies that this method will be called internally as part of the get process after any internal get operations involved in the get are performed. The effect is that the LastName property will always return an uppercased result. For the remainder of this document, methods such as this will be termed interceptor actions. The corresponding set property can be intercepted in a similar manner.
Code Listing 2. Customer.UppercaseCityBeforeSet(). Demoed by MainDemo. TriggerBasicBeforeSet().

C#

[BeforeSet(EntityPropertyNames.City)] public void UppercaseCityBeforeSet(IbCore.PropertyInterceptorArgs<Customer, String> args) { var city = args.Value; if (!String.IsNullOrEmpty(city)) { args.Value = args.Value.ToUpper(); } }

VB

<BeforeSet(EntityPropertyNames.LastName)> _ Public Sub UppercaseLastName(ByVal args As PropertyInterceptorArgs(Of Employee, String)) Dim lastName = args.Value If Not String.IsNullOrEmpty(lastName) Then args.Value = args.Value.ToUpper() End If End Sub

In this case we are ensuring that any strings passed into the LastName property will be uppercased before being stored in the Employee instance ( and later persisted to the backend datastore). Note that, in this case, the interception occurs before any internal operation is performed. In these two cases we have implemented an AfterGet and a BeforeSet interceptor. BeforeGet and AfterSet attributes are also provided and operate in a similar manner.

90 | P a g e

IdeaBlade DevForce

Property Interceptors

Named vs. Unnamed Interceptor Actions


The property interception code snippets shown above were all examples of what are termed Named interceptor actions, in that they each specified a single specific named property to be intercepted. It is also possible to create Unnamed interceptor actions that apply to all of the properties for a specific target type. For example, suppose that the following code were implemented in the Employee partial class:
Code Listing 3. Product.BeforeSetAny(). Demoed by MainDemo.ExerciseUnrestrictedBeforeSet().

C#

[BeforeSet] public void BeforeSetAny(IbCore.IPropertyInterceptorArgs args) { if (!Thread.CurrentPrincipal.IsInRole("Administrator")) { throw new InvalidOperationException("Only admistrators can change Product data!"); } }

VB
' Note that no parameter follows the BeforeSet attribute <BeforeSet> _ Public Sub BeforeSetAny(ByVal args As IPropertyInterceptorArgs) If Not Thread.CurrentPrincipal.IsInRole(Administrator) Then Throw New InvalidOperationException(Only admistrators can change data) End If End Sub

The result of this code would be that only those users logged in as administrators would be allowed to call any property setters within the Employee class. A similar set action might look like the following:
Code Listing 4. Customer.AfterSetAny(). Demoed by MainDemo.ExerciseUnrestrictedAfterSet().

91 | P a g e

IdeaBlade DevForce
C#

Property Interceptors

[AfterSet] public void AfterSetAny(IbCore.IPropertyInterceptorArgs args) { LogChangeToCustomer(args.Instance); }

VB
<AfterSet> _ Public Sub AfterSetAny(ByVal args As IPropertyInterceptorArgs) LogChangeToEmployee(args.Instance) End Sub

This would log any changes to the employee class. Later in this document we will also describe how to define interceptors that apply across multiple types as well as multiple properties within a single type.

Interceptor Chaining and Ordering


Any given property may have more than one interceptor action applied to it. For example:
Code Listing 5. See #region in Customer class. Demoed in MainDemo.ExerciseChainedInterceptors().

C#

#region Chained Interceptors

[AfterGet(EntityPropertyNames.LastName)] public void UppercaseLastName(PropertyInterceptorArgs<Employee, String> args) { /// do something interesting } [AfterGet(EntityPropertyNames.LastName)] // same mode (afterGet) and property name as above public void InsureNonEmptyLastName(PropertyInterceptorArgs<Employee, String> args) { // do something else interesting } [AfterGet] // same mode as above and applying to all properties on employee. public void AfterAnyEmployeeGet(PropertyInterceptorArgs<Employee, Object> args) { // global employee action here }

#endregion Chained Interceptors

VB
<AfterGet(EntityPropertyNames.LastName)> _ Public Sub UppercaseLastName(ByVal args As PropertyInterceptorArgs(Of Employee,

92 | P a g e

IdeaBlade DevForce
String)) ''' do something interesting End Sub

Property Interceptors

<AfterGet(EntityPropertyNames.LastName)> _ Public Sub InsureNonEmptyLastName(ByVal args As PropertyInterceptorArgs(Of Employee, String)) ' do something else interesting End Sub

<AfterGet> _ Public Sub AfterAnyEmployeeGet(ByVal args As PropertyInterceptorArgs(Of Employee, Object)) ' global employee action here End Sub

In this case, three different interceptor actions are all registered to occur whenever the Employee.LastName property is called. To execute these actions, the DevForce engine forms a chain where each of the registered interceptor actions is called with the same arguments that were passed to the previous action. Any interceptor can thus change the interceptor arguments in order to change the input to the next interceptor action in the chain. The default order in which interceptor actions are called is defined according to the following rules.
1) 2) 3) 4) 5) Base class interceptor actions before subclass interceptor actions. Named interceptor actions before unnamed interceptor actions. Attribute interceptor actions before dynamic interceptor actions. For attribute interceptor actions, in order of their occurrence in the code. For dynamic interceptor actions, in the order that they were added to the PropertyInterceptorManager.

Because of the rigidity of these rules, there is also a provision to override the default order that any interceptor action is called by explicitly setting its Order property. For attribute interceptors this is accomplished as follows:

93 | P a g e

IdeaBlade DevForce
Code Listing 6. See #region in Customer class. Demoed in MainDemo.ExerciseChainedInterceptors().

Property Interceptors

C#

[AfterGet(EntityPropertyNames.ContactName, Order=1)] public void ContactNameAfterGet03(IbCore.PropertyInterceptorArgs<Customer, String> args) {


...

VB
<BeforeSet(EntityPropertyNames.LastName, Order:=-1.0)> _ Public Sub UppercaseLastName(ByVal args As PropertyInterceptorArgs(Of Employee, String)) End Sub

The Order property is defined as being of type double and is automatically defaulted to a value of 0.0. Any interceptor action with a property of less that 0.0 will thus occur earlier than any interceptors without a specified order and any value greater that 0.0 will correspondingly be called later, and in order of increasing values of the Order parameter. Exact ordering of interceptor actions can thus be accomplished.

Multiple Attributes on a Single Interceptor Action


There will be cases where you want a single interceptor action to handle more than one property but less than an entire class. In this case, it may be useful to write an interceptor action similar to the following:
Code Listing 7. Customer.UppercaseName().

C#

[BeforeSet(EntityPropertyNames.CompanyName)] [BeforeSet(EntityPropertyNames.ContactName)] [BeforeSet(EntityPropertyNames.ContactTitle)] public void UppercaseName(IbCore.PropertyInterceptorArgs<Employee, String> args) { var name = args.Value; if (!String.IsNullOrEmpty(name)) { args.Value = args.Value.ToUpper(); } }

VB
<BeforeSet(EntityPropertyNames.FirstName), BeforeSet(EntityPropertyNames.LastName), BeforeSet(EntityPropertyNames.MiddleName)> _ Public Sub UppercaseName(ByVal args As PropertyInterceptorArgs(Of Employee, String)) Dim name = args.Value

94 | P a g e

IdeaBlade DevForce
If Not String.IsNullOrEmpty(name) Then args.Value = args.Value.ToUpper() End If End Sub

Property Interceptors

Defining an Interceptor to Act Against Multiple Types


You can also define an interceptor to work across more than one type; e.g., against multiple entity types. In this event, you will want to locate the interceptor in a separate (non-entity) class; and you will need to tell DevForce explicitly to discover the interceptors therein in order to activate them. Heres an AfterGet interceptor that intercepts every Get against every property of the Employee and Product entities. (Dont overuse this technique, or you may slow down your app unacceptably!) The interceptor checks to see if the property is a DateTime, or something derived from a DateTime, and that its value is not null. If both things are true, it converts the DateTime to local time. (The interceptor assumes that the DateTime has been stored in the database as a universal time.)

C#

[IbCore.AfterGet(TargetType = typeof(Employee))] [IbCore.AfterGet(TargetType = typeof(Product))] public static void UniversalGetInterceptor(IbEm.IEntityPropertyInterceptorArgs args) { // breaking these out to demonstrate some of the info you have available... string propertyName = args.EntityProperty.Name; string propertyDataType = args.EntityProperty.DataType.ToString(); bool isDateTime = args.EntityProperty.DataType.IsAssignableFrom(typeof(DateTime)); bool valueIsNull = args.Value == null; if (isDateTime && !valueIsNull) { DateTime aDateTime = ((DateTime)args.Value); aDateTime = aDateTime.ToLocalTime(); args.Value = aDateTime; } }

VB Note that, since we defined the above interceptor in a non-entity class, we had to specify in the AfterGet attributes the types against which it is to operate: here, Employee and Product. DevForce wont find interceptors defined outside entity classes automatically. Heres how you let DevForce know youve defined some interceptors in a class named AuxiliaryPropertyInterceptors:

95 | P a g e

IdeaBlade DevForce
C#

Property Interceptors

IbCore.PropertyInterceptorManager.CurrentInstance.DiscoverInterceptorsFromAttributes(typeo f(AuxiliaryPropertyInterceptors));

VB

The EntityPropertyNames class


In all of the previous examples we have shown Named attributes specified with the form EntityPropertyNames.,PropertyName-. This is a recommended pattern that ensures type safety. However, the following two attribute specifications have exactly the same effect:

C#

[BeforeSet(EntityPropertyNames.FirstName)] // or [BeforeSet(FirstName)]

VB The EntityPropertyNames reference is to a class that is generated (as multiple partial classes) inside the designer code file associated with the EntityModel. Its primary purpose is to allow specification of property names as constants. Note that because EntityPropertyNames is generated as a set of partial classes, you can add your own property names to the class for any custom properties that you create.

PropertyInterceptorArgs and IPropertyInterceptorArgs


Interceptor actions get all of the information about the context of what they are intercepting from the single interceptor argument passed into them. This argument will obviously be different for different contexts; i.e. a set versus a get action, a change to an employee versus a company, a change to the FirstName property instead of the LastName property. Because of this there are many possible implementations of what the single argument passed into any interceptor action might contain. However, all of these implementations implement a single primary interface: IPropertyInterceptorArgs. Every interceptor action shown previously provides an example of this. In each case, a single argument of type PropertyInterceptorArgs<Employee, String> or of type IPropertyInterceptorArgs was passed into each of the interceptor methods. In fact, the type of the args instance that is actually be passed into each of these methods at runtime is an instance of a subtype of the argument type declared in the methods signature. For any interceptor action defined on a DevForce entity, the actual args passed into the action will be a concrete implementation of one of the following classes.
96 | P a g e

IdeaBlade DevForce

Property Interceptors

DataEntityPropertyGetInterceptorArgs<TInstance, TValue> DataEntityPropertySetInterceptorArgs<TInstance, TValue> NavigationEntityPropertyGetInterceptorArgs<TInstance, NavigationEntityPropertySetInterceptorArgs<TInstance,

TValue> TValue>

The boldfaced characters above indicate whether we are providing interception to a get or a set property, as well as whether we are intercepting a data or a navigation property. In general, you can write an interception method with an argument type that is any base class of the actual argument type defined for that interceptor. If you do use a base class, then you may need to perform runtime casts in order to access some of the additional properties provided by the specific subclass passed in at runtime. These subclassed properties will be discussed later. The entire inheritance hierarchy for property interceptor arguments is shown below:

Assembly Where Defined IdeaBlade.Core

Property Interceptor Arguments


IPropertyInterceptorArgs IPropertyInterceptorArgs<TInstance, TValue> PropertyInterceptorArgs<TInstance, TValue>

IdeaBlade.EntityModel

DataEntityPropertyInterceptorArgs<TInstance, TValue> DataEntityPropertyGetInterceptorArgs<TInstance, TValue> DataEntityPropertySetInterceptorArgs<TInstance, TValue> NavigationEntityPropertyInterceptorArgs<TInstance, TValue> NavigationEntityPropertyGetInterceptorArgs<TInstance, TValue> NavigationEntityPropertySetInterceptorArgs<TInstance, TValue>

The generic <TInstance> argument will always be the type that the intercepted method will operate on, known elsewhere in this document and the interceptor API as the TargetType. The <TValue> argument will be the type of the property being intercepted. i.e. String for the LastName property. Note that the interceptor arguments defined to operate on DevForce entities break into multiple subclasses with additional associated interfaces based on two primary criteria.
1) Is it a get or a set interceptor? a. get interceptor args implement IEntityPropertyGetInterceptorArgs b. set interceptor args implement IEntityPropertySetInterceptorArgs

97 | P a g e

IdeaBlade DevForce

Property Interceptors

2) Does it involve a DataEntityProperty or a NavigationEntityProperty?. a. DataEntityProperty args implement IDataEntityPropertyInterceptorArgs b. NavigationEntityProperty args implement INavigationEntityPropertyInterceptorArgs

The API for each of the interfaces above is discussed below. IPropertyInterceptorArgs The root of all property interceptor arguments is the IPropertyInterceptorArgs interface. Its properties will be available to all interceptors.

C#

public interface IPropertyInterceptorArgs { Object Instance { get; } Object Value { get; set; } bool Cancel { get; set; } Action<Exception> ExceptionAction { get; set; } object Tag { get; set; } object Context { get; } } Public Interface IPropertyInterceptorArgs ReadOnly Property Instance() As Object Property Value() As Object Property Cancel() As Boolean Property ExceptionAction() As Action(Of Exception) Property Tag() As Object ReadOnly Property Context() As Object End Interface

VB

In general the most useful of these properties will be the Instance and Value properties. The Instance property will always contain the parent object whose property is being intercepted. The Value will always be the value that is being either retrieved or set. The Cancel property allows you to stop the execution of the property interceptor chain at any point by setting the Cancel property to true. The ExceptionAction property allows you to set up an action that will be performed whenever an exception occurs anywhere after this point in the chain of interceptors. The Tag property is intended as a general purpose grab bag for the developer to use for his/her own purposes. The Context property is used for internal purposes and should be ignored.

An example of using the ExceptionAction and Cancel is shown below:


98 | P a g e

IdeaBlade DevForce

Property Interceptors

Code Listing 8. Order.LogExceptionsAndCancelSets(). Demoed in MainDemo.TestLoggingOfExceptionsAndAutoCancellationOfSets().

C# /// Do not let any setters throw an exception. Instead, eat them, /// log them, and cancel the remainder of the Set operation.
[IbCore.BeforeSet(Order = -1)] public void LogExceptionsAndCancelSets(IbCore.IPropertyInterceptorArgs args) { args.ExceptionAction = (e) => { Common.LogManager.LogAnAction(string.Format("Here in {0}", e.Message)); args.Cancel = true; }; } public static void LogAnAction(string msg) { //... some Logging action }

VB
<BeforeSet> _ Public Sub BeforeSetAny(ByVal args As IPropertyInterceptorArgs) ' Do not let any setters throw an exception ' Eat them and log them, and cancel the remainder of the set operation. args.ExceptionAction = Function(e) SetterExceptionHandler (e, args) End Sub

Private Function SetterExceptionHandler(ByVal e As Object, _ ByVal args As IPropertyInterceptorArgs) As Object LogException(e) args.Cancel = True Return Nothing End Function

Note that we applied an explicit Order value less than 0 for this interceptor. Assuming that none of the property-specific interceptors have an explicit Order defined, their Order value defaults to zero, this interceptor will run first for all properties of the type on which its defined.

99 | P a g e

IdeaBlade DevForce

Property Interceptors

In our samples, this interceptor happens to be defined on the Order type. Please note that there is no relationship between that fact and the use of the Order parameter in the BeforeSet attribute. Two different things!

Generic IPropertyInterceptorArgs The following is a generic version of IPropertyInterceptorArgs where both the Instance and Value properties are now strongly typed; otherwise it is identical to IPropertyInterceptorArgs.

C#

public interface IPropertyInterceptorArgs<TInstance, TValue> : IPropertyInterceptorArgs { TInstance Instance { get; } TValue Value { get; set; } bool Cancel { get; set; } Action<Exception> ExceptionAction { get; set; } object Tag { get; set; } object Context { get; } } Public Interface IPropertyInterceptorArgs(Of TInstance, TValue) Inherits IPropertyInterceptorArgs ReadOnly Property Instance() As TInstance Property Value() As TValue Property Cancel() As Boolean Property ExceptionAction() As Action(Of Exception) Property Tag() As Object ReadOnly Property Context() As Object End Interface

VB

IEntity PropertyInterceptorArgs and subclasses Whereas the interfaces above can be used to intercept any property on any object, the argument interfaces below are for use only with DevForce specific entities and complex objects. Each interface below provides additional contextual data to any interceptor actions defined to operate on DevForce entities. The most basic of these is simply the idea that each property on a DevForce entity has a corresponding EntityProperty ( discussed elsewhere in this guide).

C#

public interface IEntityPropertyInterceptorArgs : IPropertyInterceptorArgs { EntityProperty EntityProperty { get; } } Public Interface IEntityPropertyInterceptorArgs Inherits IPropertyInterceptorArgs ReadOnly Property EntityProperty() As EntityProperty End Interface

VB

100 | P a g e

IdeaBlade DevForce
An example is shown below:
Code Listing 9. Customer.AfterSetAnyUsingIEntityPropertyInterceptorArgs().

Property Interceptors

C#

[IbCore.AfterSet] public void AfterSetAnyUsingIEntityPropertyInterceptorArgs(IbCore.IPropertyInterceptorArgs args) { // Cast the IPropertyInterceptorArgs to the entity-specific version, then // values information available on the EntityProperty contained thereby. var entityPropertyArgs = args as IbEm.IEntityPropertyInterceptorArgs; if (entityPropertyArgs != null) { Common.LogManager.LogAnAction(string.Format("Property [Customer.{0}] was set to the value: [{1}]", entityPropertyArgs.EntityProperty.Name, args.Value.ToString())); } }

VB
<AfterSet> _ Public Sub AfterSetAny(ByVal args As IPropertyInterceptorArgs) Dim entityPropertyArgs = TryCast(args, IEntityPropertyInterceptorArgs) If entityPropertyArgs IsNot Nothing Then Log(The + entityPropertyArgs.EntityProperty.Name + was set to the value:= + args.Value.ToString()) End If End Sub

The next two interfaces provide additional context based on whether the interceptor action being performed is a get operation or a set operation. For a get operation, IdeaBlade entities have a concept of possibly multiple versions, i.e. an original, current, or proposed version, of an entity at any single point in time. It may be useful to know which version is being retrieved during the current action. Note that the version cannot be changed.
C#

public interface IEntityPropertyGetInterceptorArgs : IEntityPropertyInterceptorArgs { EntityVersion EntityVersion { get; } } Public Interface IEntityPropertyGetInterceptorArgs Inherits IEntityPropertyInterceptorArgs ReadOnly Property EntityVersion() As EntityVersion End Interface

VB

101 | P a g e

IdeaBlade DevForce

Property Interceptors

The DevForce EntityProperty is an abstract class with two concrete subclasses; a DataEntityProperty and a NavigationEntityProperty ( discussed elsewhere in this guide). The next two IEntityPropertyInterceptorArgs subinterfaces allow access to instances of one or the other of these depending on whether the property being intercepted is a data or a navigation property.
C#

public interface IDataEntityPropertyInterceptorArgs : IEntityPropertyInterceptorArgs { DataEntityProperty DataEntityProperty { get; } } Public Interface IDataEntityPropertyInterceptorArgs Inherits IEntityPropertyInterceptorArgs ReadOnly Property DataEntityProperty() As DataEntityProperty End Interface

VB

C#

public interface INavigationEntityPropertyInterceptorArgs : IEntityPropertyInterceptorArgs { NavigationEntityProperty NavigationEntityProperty { get; } } Public Interface INavigationEntityPropertyInterceptorArgs Inherits IEntityPropertyInterceptorArgs ReadOnly Property NavigationEntityProperty() As NavigationEntityProperty End Interface

VB

IPropertyInterceptorArgs Type Coercion One of the first issues that a developer will encounter with writing interceptor actions that handle more than one property is that it becomes difficult or impossible to use a concrete subtype as the argument to the interceptor. For example, imagine that we wanted to write a single action that handled two or more very different properties each of a different type: This could be written as follows:
Code Listing 10. Employee.ActionToBePerformedAgainstDifferentTypesV1().

C#

[BeforeSet(EntityPropertyNames.HireDate)] // hire date is of type datetime [BeforeSet(EntityPropertyNames.FirstName)] // firstname is of type string public void ActionToBePerformedAgainstDifferentTypesV1(IbCore.IPropertyInterceptorArgs args) { var emp = (Employee)args.Instance; var entityProperty = ((IbEm.IDataEntityPropertyInterceptorArgs)args).EntityProperty; //.. do some action with emp and entityProperty }

VB

<BeforeSet(EntityPropertyNames.HireDate), BeforeSet(EntityPropertyNames.FirstName)> _ Public Sub ActionToBePerformedAgainstDifferentTypes (ByVal args As IPropertyInterceptorArgs) Dim emp = CType(args.Instance, Employee) Dim entityProperty = (CType(args, IDataEntityPropertyInterceptorArgs)).EntityProperty .. do some operation with emp and entityProperty End Sub

102 | P a g e

IdeaBlade DevForce

Property Interceptors

But ideally we would prefer to write it like this, in order to avoid performing a lot of superfluous casts:
Code Listing 11. Employee.ActionToBePerformedAgainstDifferentTypesV2().

C#

[BeforeSet(EntityPropertyNames.HireDate)] // hire date is of type datetime [BeforeSet(EntityPropertyNames.FirstName)] // firstname is of type string public void StrangeAction(DataEntityPropertySetInterceptorArgs<Employee, Object> args) { // no casting var emp = args.Instance; var entityProperty = args.DataEntityProperty; .. some very baroque operation }

VB
<BeforeSet(EntityPropertyNames.HireDate), BeforeSet(EntityPropertyNames.FirstName)> _ Public Sub StrangeAction(ByVal args As DataEntityPropertySetInterceptorArgs(Of Employee, Object)) ' no casting Dim emp = args.Instance Dim entityProperty = args.DataEntityProperty .. some very baroque operation End Sub

The problem is that, according to the rules of inheritance, the two concrete classes that this method will be called with:
Type 1: DataEntityPropertySetInterceptorArgs<Employee, String> Type 2: DataEntityPropertySetInterceptorArgs<Employee, DateTime>

do not inherit from:


Type 3: DataEntityPropertySetInterceptorArgs<Employee, Object>

In fact, the only class or interface that they do share is:


IPropertyInterceptorArgs

So in order to allow this construction, DevForce needs to coerce each of Type1 and Type2 into Type3 for the duration of the method call. Because DevForce does do this, any of the following arguments are also valid:

Type 4: DataEntityPropertySetInterceptorArgs<Entity, Object> Type 5: DataEntityPropertySetInterceptorArgs<Object, Object> Type 5: PropertyInterceptorArgs<Employee, Object> etc.

The basic rule for the type coercion facility is that any concrete type can be specified if its generic version is a subtype of the generic version of the actual argument type that will be passed in.
103 | P a g e

IdeaBlade DevForce

Property Interceptors

PropertyInterceptor Attribute Discovery


In general, any interceptor method declared within a DevForce entity and marked with a property interceptor attribute will be automatically discovered before the first property access. PropertyInterceptors will most commonly be defined within the developer-controlled partial class associated with each entity. Property interceptors can also be defined on any base class and these will also be discovered automatically. In order to reduce the surface area of any entity class, a developer may not want to expose the property interceptor methods directly on the surface of his or her class. To facilitate this, DevForce will also probe any public inner classes of any entity class and will locate any property interceptors defined there as well. Example:
Code Listing 12. Employee.LastNameInterceptor().

C#

public partial class Employee : IdeaBlade.EntityModel.Entity { // internal class just for property interceptors public class PropertyInterceptorsDefinitions { [BeforeGet(Employee.EntityPropertyNames.LastName)] public static void LastNameInterceptor(IEntityPropertyInterceptorArgs args) { } [AfterSet] public static void LoggingInterceptor(IEntityPropertyInterceptorArgs args) { } }

VB
Partial Public Class Employee Inherits IdeaBlade.EntityModel.Entity

' internal class just for property interceptors Public Class PropertyInterceptorsDefinitions <BeforeGet(Employee.EntityPropertyNames.LastName)> _ Public Shared Sub LastNameInterceptor(ByVal args As IEntityPropertyInterceptorArgs) End Sub

104 | P a g e

IdeaBlade DevForce

Property Interceptors

<AfterSet> _ Public Shared Sub LoggingInterceptor(ByVal args As IEntityPropertyInterceptorArgs) End Sub End Class

Important note: Property interceptor methods defined on a class directly may be either instance or static methods; whereas property interceptors defined on an inner class (or anywhere other than directly on the entity class) must be static methods.

In the event that a developer wants to completely isolate his interception methods in another nonentity-based class, then discovery will not occur automatically. In this case, the DiscoverInterceptorsFromAttributes(Type targetType) method on the PropertyInterceptorManager class may be used to force discovery of any specified type and all of its base types. Attribute interceptors that are declared outside of the classes to which they apply must be further qualified via the TargetType property as shown below:
Code Listing 13. AuxiliaryPropertyInterceptors.LoggingInterceptor().

C#

[AfterSet(Employee.EntityPropertyNames.LastName, TargetType = typeof(Employee))] public static void LoggingInterceptor(IEntityPropertyInterceptorArgs args) { // ... }

VB
Public Class UnattachedInterceptor

<AfterSet(User.EntityPropertyNames.Name, TargetType:=GetType(User)> _ Public Shared Sub LoggingInterceptor(ByVal args As IEntityPropertyInterceptorArgs) End Sub End Class

105 | P a g e

IdeaBlade DevForce

Property Interceptors

Alternative PropertyInterceptor Attribute Method Signatures


While the property interceptor methods described previously allow a great deal of control over the entire interception process, there are times when this is overkill. Sometimes all you really want is to do is modify or inspect the incoming or outgoing values. In these cases, a simplified signature for an interception method is also provided. For example the following standard interceptor action:
Code Listing 14. Customer.UppercaseLastName().

C#

[AfterGet(EntityPropertyNames.LastName)] public void UppercaseLastName(PropertyInterceptorArgs<Employee, String> args) { var lastName = args.Value; if ( !String.IsNullOrEmpty(lastName)) { args.Value = args.Value.ToUpper(); } }

VB
<AfterGet(EntityPropertyNames.LastName)> _ Public Sub UppercaseLastName(ByVal args As PropertyInterceptorArgs(Of Employee, String)) Dim lastName = args.Value If Not String.IsNullOrEmpty(lastName) Then args.Value = args.Value.ToUpper() End If End Sub

can also be written as


Code Listing 15. Customer.UppercaseLastNameV2().

C#

[IbCore.AfterGet(EntityPropertyNames.CompanyName)] public String UppercaseLastNameV2(String companyName) { if (!String.IsNullOrEmpty(companyName)) { return companyName.ToUpper(); } else { return String.Empty; } }

VB
<AfterGet(EntityPropertyNames.LastName)> _ Public Function UppercaseLastName(ByVal lastName As String) As String If Not String.IsNullOrEmpty(lastName) Then

106 | P a g e

IdeaBlade DevForce
Return lastName.ToUpper() Else Return String.Empty End If End Function

Property Interceptors

In general, any property interceptor action that only inspects or modifies the incoming value without the need for any other context can be written in this form. In fact, if the action does not actually modify the incoming value, the return type of the interceptor action can be declared as void.

Dynamic Property Interception and the PropertyInterceptorManager.


Property interceptors can be added and removed dynamically by making use of the PropertyInterceptorManager and the PropertyInterceptor classes. Their APIs are shown below:

C#

public sealed class PropertyInterceptorManager { public static PropertyInterceptorManager CurrentInstance { get; set; } public void DiscoverInterceptorsFromAttributes(Type targetType) public void AddAction(PropertyInterceptorAction interceptorAction) public bool RemoveAction(PropertyInterceptorAction interceptorAction) public IList<PropertyInterceptorAction<TArgs>> GetActions<TArgs>(Type targetType, String targetName, PropertyInterceptorMode mode) where TArgs : class, IPropertyInterceptorArgs } Public NotInheritable Class PropertyInterceptorManager Private privateCurrentInstance As PropertyInterceptorManager Public Shared Property CurrentInstance() As PropertyInterceptorManager Get Return privateCurrentInstance End Get Set(ByVal value As PropertyInterceptorManager) privateCurrentInstance = value End Set End Property public void DiscoverInterceptorsFromAttributes(Type targetType) public void AddAction(PropertyInterceptorAction interceptorAction) public Boolean RemoveAction(PropertyInterceptorAction interceptorAction) public IList(Of PropertyInterceptorAction(Of TArgs)) GetActions(Of TArgs)(Type targetType, String targetName, PropertyInterceptorMode mode) where TArgs : class, IPropertyInterceptorArgs End Class

VB

107 | P a g e

IdeaBlade DevForce
C#

Property Interceptors

public class PropertyInterceptorAction<TArgs> : PropertyInterceptorAction where TArgs : class, IPropertyInterceptorArgs { public PropertyInterceptorAction(Type targetType, String targetName, PropertyInterceptorMode mode, Action<TArgs> action); public PropertyInterceptorAction(Type targetType, String targetName, PropertyInterceptorMode mode, Action<TArgs> action, Double order, String key); public Type TargetType { get; } public String TargetName { get; } } public PropertyInterceptorMode Mode { get; } public String Key { get; } public Double Order { get; } public Type ArgsType { get; } public Type InstanceType { get; } public Type ValueType { get; } public PropertyInterceptorAction<TArgs> ConvertTo<TArgs>() where TArgs : class, IPropertyInterceptorArgs; }

VB

Public Class PropertyInterceptorAction(Of TArgs As {Class, IPropertyInterceptorArgs}) Inherits PropertyInterceptorAction public PropertyInterceptorAction(Type targetType, String targetName, PropertyInterceptorMode mode, Action(Of TArgs) action) public PropertyInterceptorAction(Type targetType, String targetName, PropertyInterceptorMode mode, Action(Of TArgs) action, Double order, String key) public Type TargetType {get;} public String TargetName {get;} End Class public PropertyInterceptorMode Mode {get;} public String Key {get;} public Double Order {get;} public Type ArgsType {get;} public Type InstanceType {get;} public Type ValueType {get;} public PropertyInterceptorAction(Of TArgs) ConvertTo(Of TArgs)() where TArgs : class, IPropertyInterceptorArgs

Since there is no public constructor for the PropertyInterceptorManager class, the only instance available to the developer is via the CurrentInstance property. This property will always have a value. The current instance is the container for all currently registered interceptor actions. PropertyInterceptorActions can be added to an entity type as follows:

C#

Employee.PropertyMetadata.Title.GetterInterceptor.AddAction( IbCore.PropertyInterceptorTiming.Before, FormatTitleProperty);

VB

108 | P a g e

IdeaBlade DevForce

Property Interceptors

EntityProperties and Property Interceptors


Within a DevForce application, every property interceptor has a GetterInterceptor and a SetterInterceptor property. These properties can also be used to modify the property interceptor actions associated with that property. Under the covers this is going through the PropertyInterceptorManager mechanism described above, but the syntax is often simpler. For example:
Code Listing 16. ApplyInterceptorToEmployeeTitleDynamically().

C#

Employee.PropertyMetadata.Title.GetterInterceptor.AddAction( PropertyInterceptorTiming.Before, args => args.Value = args.Value.ToUpper());

VB

Employee.AddressEntityProperty.SetterInterceptor.AddAction(PropertyInterceptorTiming.Before, args => args.Value = AddZipCode(args.Value)); Employee.AddressEntityProperty.SetterInterceptor.AddAction(PropertyInterceptorTiming.Before, Function(args) args.Value = AddZipCode(args.Value))

PropertyInterceptor Keys
Every property interceptor action has a key that can either be specified via an optional attribute property or dynamically when the action is first created. If no key is defined, the system will automatically create one. This key will be used to identify an action for removal. The PropertyInterceptorManager.RemoveAction(interceptorAction) attempts to find an interceptor that matches the one passed in. This match requires that the TargetType, TargetName, Mode, and Key be the same between the two interceptor actions.

Mechanics of Property Interception


Property interception within DevForce is accomplished by dynamically generating compiled lamda expressions for each interceptor action. DevForce interceptors are discovered (but not compiled) as each entity class is first referenced. Runtime compilation of each property interceptor occurs lazily the first time each property is accessed. After this first access, the entire code path for each property access is fully compiled. Properties that are never accessed do not require compilation. The addition or removal of interceptor actions after they have been compiled does require a new compilation the next time the property is executed. This happens automatically. Errors encountered during the compilation process will thus appear when a property is accessed for the first time. These exceptions will be of type PropertyInterceptorException and will contain information on the specific method that could not be compiled into a property interceptor action. These are usually a function of a PropertyInterceptorArgs parameter type that is not compatible with the property or properties being accessed.

109 | P a g e

IdeaBlade DevForce

Business Object Persistence

Business Object Persistence

110 | P a g e

IdeaBlade DevForce

Business Object Persistence

Business Object Persistence....................................................................................................... 110


State of the Release Candidate Documentation ................................................................................................. 114 Note: Code Snippets in This Document............................................................................................................. 115 Object Persistence Overview .............................................................................................................................. 115 The Big Picture .................................................................................................................................................. 115 DevForce and the ADO.NET EntityModel ....................................................................................................... 116 Locating the Physical Data Source with a Key .............................................................................................. 118 Support for POCOs (Plain Old CLR Objects) ................................................................................................... 118 Persistence Management Capabilities ............................................................................................................... 119 Retrieving business objects ............................................................................................................................ 119 The Entity Cache ........................................................................................................................................... 120 Business objects in motion ............................................................................................................................ 121 Creating new business objects ....................................................................................................................... 121 Saving and undoing business object changes ................................................................................................ 122 Offline Support .............................................................................................................................................. 122 Application Security ...................................................................................................................................... 123 Business Object Security ............................................................................................................................... 123 N-Tier Architecture ....................................................................................................................................... 124 Three-Tier Deployment ............................................................................................................................. 125 Model Choice by Configuration ................................................................................................................ 125 Conclusion ..................................................................................................................................................... 126 Entity Queries and Entity Navigation ................................................................................................................ 126 Entity Queries .................................................................................................................................................... 127 Query v. Method Syntax ................................................................................................................................ 136 LINQ.............................................................................................................................................................. 137 The DevForce Predicate Builder ................................................................................................................ 138 Example: Simulate an In() Clause Condition on a Distantly Related Entity ............................................. 144 The PredicateDescription Class ................................................................................................................ 145 Example: Given a Collection of Parent Entities, Retrieve the Related Children ....................................... 147 PassthruESQL Queries .............................................................................................................................. 148 Remote Service Method Call (RSMC) ...................................................................................................... 149 111 | P a g e

IdeaBlade DevForce

Business Object Persistence

Entity Navigation ............................................................................................................................................... 150 Parent-Child Navigation properties ........................................................................................................... 150 Navigation Properties in Silverlight........................................................................................................... 151 Deferred Retrieval ..................................................................................................................................... 156 Proactive Data Loads ................................................................................................................................. 157 Missing objects .......................................................................................................................................... 159 The Null Entity .................................................................................................................................................. 160 Asynchronous Communication with the Business Object Server ................................................................... 160 Asynchronous Queries ....................................................................................................................................... 161 IAsyncResult Asynchronous Pattern ................................................................................................................. 163 Asynchronous Fulfillment of Navigation Property Queries .............................................................................. 163 Canceling Pending Operations........................................................................................................................... 164 The EntityListManager ....................................................................................................................................... 164 Entity Caching ..................................................................................................................................................... 168 All Business Objects are Cached ....................................................................................................................... 168 Entity Ancestry and Organization of the Cache ............................................................................................. 168 Business objects are unique in each cache ..................................................................................................... 170 Entities in Lists .............................................................................................................................................. 170 Business object proper, not the business object graph ................................................................................... 171 Queries, Navigation, and the Cache ................................................................................................................... 171 Query Cache .............................................................................................................................................. 171 Primary key queries ................................................................................................................................... 172 Object Not Found and the Null Entity .................................................................................................... 172 Cache use when disconnected .................................................................................................................... 172 Modifications ............................................................................................................................................. 173 Stale Entity Data ........................................................................................................................................ 173 Fetch Life Cycle Events ............................................................................................................................. 174 Query Workflow ................................................................................................................................................ 174 Query Strategy ................................................................................................................................................... 176 Fetch Strategies .......................................................................................................................................... 177 MergeStrategies ......................................................................................................................................... 178 InversionMode ........................................................................................................................................... 179 Pre-Defined QueryStrategies ..................................................................................................................... 181 112 | P a g e

IdeaBlade DevForce

Business Object Persistence

Custom QueryStrategies ............................................................................................................................ 182 DefaultQueryStrategy ................................................................................................................................ 183 When to Use The Different QueryStrategies ............................................................................................. 183 Making a One-Time Change to the QueryStrategy With Which a Given Query Is Run ........................... 184 Span Queries ...................................................................................................................................................... 185 Performance Details................................................................................................................................... 187 Cached Entity Lifespan...................................................................................................................................... 188 Saving the Cache Locally .................................................................................................................................. 188 The TraceViewer: Watch What Data Is Being Loaded, and How ..................................................................... 189 Using the Trace Viewer Stand-Alone ............................................................................................................ 190 Embedding the Trace Viewer in Your Application ....................................................................................... 193 Embedding the WPFTraceViewer in Your WPF App ................................................................................... 193 Embedding the WinTraceViewer in Your WinForms App ........................................................................... 195 Getting Generated SQL to Display in the TraceViewer ................................................................................ 199 Using the Trace Viewer With a Silverlight App ............................................................................................ 200 Creating Business Objects .................................................................................................................................. 202 When Not to Create ........................................................................................................................................... 202 The Business Object Create Method ................................................................................................................. 203 Generating unique identifiers......................................................................................................................... 203 GUIDs ........................................................................................................................................................ 204 Store-Generated Ids ................................................................................................................................... 204 Custom id generation ................................................................................................................................. 204 Ids in mapping objects ................................................................................................................................... 206 Creating a valid business object ..................................................................................................................... 207 Auxiliary Business Object Class Methods ......................................................................................................... 208 CompareTo() ............................................................................................................................................. 208 ToString() .................................................................................................................................................. 208 Adding and Removing Related Objects using Add() and Remove() ................................................................. 208 Business Object Creation Review...................................................................................................................... 211 Saving Business Objects ...................................................................................................................................... 211 EntityState of an Object ..................................................................................................................................... 211 Undo .................................................................................................................................................................. 212 Validation .......................................................................................................................................................... 212 113 | P a g e

IdeaBlade DevForce

Business Object Persistence

Temporary Id Fix-up ......................................................................................................................................... 212 Life Cycle Events .............................................................................................................................................. 213 Client-Side Life Cycle Events ....................................................................................................................... 213 Saves and Transaction Management .................................................................................................................. 215 Distributed Transactions ............................................................................................................................ 216 Re-query After Save .......................................................................................................................................... 216 When Save Fails ................................................................................................................................................ 217 SaveChanges() Exceptions ............................................................................................................................ 217 EntityManagerSaveException ................................................................................................................... 218 SaveResult ................................................................................................................................................. 218 Alternatives to Default SaveChanges Exceptions .......................................................................................... 219 Data Source Concurrency .................................................................................................................................. 219 Saving the Dependency Graph ....................................................................................................................... 226 Association Types ...................................................................................................................................... 227 Compositions ............................................................................................................................................. 227 Save the Root Entity .................................................................................................................................. 228 Saving Event Handler ................................................................................................................................ 228 Composition Business Rules ..................................................................................................................... 229 Concurrency Violations ............................................................................................................................. 229 Dependency Graph Retrieval ............................................................................................................................. 229 Workflow For a Save ......................................................................................................................................... 232 Saving the Cache to a Local Disk File ............................................................................................................... 233 XML Serialization of Business Objects ............................................................................................................. 234

State of the Release Candidate Documentation


We are working hard to update all of our documentation from DevForce 2009 to DevForce 2010, .NET 4, and Silverlight 4. During this conversion, you may find some sections that are out of date, but you should be able to get many of the examples to work, with small modifications, by checking against the API Documentation for the current method signatures.

The C# code snippets in this document (and the corresponding Visual Studio solution) are current for DevForce 2010, but the VB snippets have not yet been updated. If you have questions about the currency of particular material, please contact support.
114 | P a g e

IdeaBlade DevForce

Business Object Persistence

Note: Code Snippets in This Document


The code snippets in this document are duplicated in accompanying C# and VB code solutions. After installing DevForce, you will find these solutions in the _TopicDocumentSnippets folder under the Business Object Persistence topic in the Learning Resources. The captions associated herein with the snippets reflect the corresponding method names in the code solutions. You will find these methods in the Program.cs or Main.vb files, respectively, for the C# and VB solutions.

Object Persistence Overview


In previous chapters youve seen how object mapping declares relationships between business objects and remote data sources. You learned that it generated classes for each business object as well as some helper classes such as EntityRelations. The collection of these classes constitutes the applications business object model. In this chapter we describe how the DevForce persistence scheme works with the business object model. You will learn that instances of the business object class (AKA the entity class) are held in a container called the entity cache. This cache belongs to and is managed by an instance of the DevForce EntityManager class. You will discover that an EntityManager instance is rich in capabilities that go beyond retrieving and saving business objects. Well introduce them here and elaborate on a few of them in subsequent sections. By the end of the chapter, you will appreciate that the EntityManager class is one of the most important and useful classes in the DevForce framework.

The Big Picture


A DevForce application relies upon a layered architecture for data access. At one end is a data source typically a relational database. At the other end is the user interface which works with business objects in a business object model. There are several components in the middle.

115 | P a g e

IdeaBlade DevForce

Business Object Persistence

Figure 2. Cross-tier flow of data and business objects.

One of them, called an EntityServer, moves data (and data requests) between the ADO.NET Entity Framework and the DevForce EntityManager. The EntityServer is responsible for issuing query and save requests to the ADO.NET Entity Framework . It also manages application security. You can learn more about the additional features of the EntityServer in the Business Object Server and Security documents.
The EntityServer is an important component and you should understand its role in the object persistence process. That said, you will seldom see or deal with it directly.

The second important DevForce component is the EntityManager. The EntityManager takes instruction from the higher levels of the application such as the UI, and forwards UI requests for entities to the EntityServer. The EntityManager puts the received entities obtained from whatever source by the EntityServer -- into its entity cache and makes them available to the UI. End users review the entities and make changes through the UI. The UI signals the EntityManager to save the changes. The EntityManager dutifully forwards the changed entities to the EntityServer which communicates with the appropriate component to commit the data into persistent storage.

DevForce and the ADO.NET EntityModel


Visual Studios ADO.NET Entity Data Model wizard creates an EDMX file which contains descriptions of a conceptual data schema (the object model), an actual data store schema (the database model), and the mappings between the two. The DevForce OM Designer Extension to the EDM Designer provides for customization of the model with additional DevForce attributes, and also generates the object model in
116 | P a g e

IdeaBlade DevForce

Business Object Persistence

.NET code in a file named <ModelName>.IBDesigner.cs (or .vb). We generally refer to the generated model as the Domain model, since DevForce allows for multiple data sources to be combined into a single logical model. You can learn more about building the Domain Model in the Business Object Mapping documents. The Domain model is persistence ignorant: unlike the Entity Framework model, it has no knowledge whatsoever of the back-end data store or the mapping between that and its objects. In an n-tier deployment, it is the only model that is deployed client side. The client needs no connection information for back-end data sources. A copy of the assembly containing the Domain model is also deployed server-side in an n-tier deployment. Architecture of the DevForce Business Object Class The (partial) inheritance hierarchy for a DevForce business class is as follows:
Figure 3. Inheritance Hierarchy for a DevForce Business Class

The class for a business type is generated as one or two partial classes. In the partial classes labeled in the picture as DevForce-controlled, the essential data structure of the type is defined. This partial class is driven by settings in the domain model and gets regenerated whenever the EDMX is saved. Thus it should never be modified by the developer.
All DevForce-controlled partial classes for types originating from a given Entity Data Model are generated into a single file, named <EntityModelName>.IBDesigner.cs (or .vb, if generated in Visual Basic rather than C#). If the domain model includes multiple Entity Models, one such code file will be generated for each model.

The partial class described in Figure 3 as Developer-controlled is optional, and can be generated by the DevForce OM Extension in a one-time operation, or hand-coded by the developer. In either case, once it exists, DevForce will not overwrite or modify it. The developer-controlled partial class is the developers workshop, where he can add custom properties, methods, and events, as well as create property interceptors9 to change the getter/setter behavior of properties defined in the DevForce-controlled partial class.

See the Developers Guide chapter on Property Interceptors

117 | P a g e

IdeaBlade DevForce

Business Object Persistence

Modifying the behavior of a generated property DevForce provides a mechanism to intercept and either modify or extend the behavior of any .NET property, including, of course, those generated into the DevForce-controlled partial business classes. You can accomplish virtually any desired behavior modification of property getters or setters via this interception mechanism. The mechanism replaces, and expands upon, the technique of marking properties as virtual and overriding them in a subclass. This facility is a lightweight form of what is termed Aspect-Oriented Programming. You can find detail about this in the chapter, Property Interceptors.

Locating the Physical Data Source with a Key


How does the EntityServer locate the physical storage to use? You learned earlier that every business object every concrete entity is mapped to a particular data source. That data source is identified symbolically by a data source key. That key is compiled into the entity and cannot be changed at run-time. The EntityServer has a copy of the business object model so it knows the data source key for each kind of business object. But the key is purely symbolic. It does not contain the location of a physical data source nor can it determine how to connect to such a data source. It does not contain a database connection string, for example. Fortunately, the EntityServer also has a private copy of the application configuration file. It can use the data source key to find in that file the physical data source configuration information it needs such as the connection string for the physical data source it should use. This is all we need to know for the moment to assure ourselves that a DevForce application actually can move data between a physical data source and business objects in the client application. We turn next to the EntityManager which is the keeper of business objects on the client side.

Support for POCOs (Plain Old CLR Objects)


DevForce supports POCOs: instances of such objects can be queried, cached, updated, and saved just like DevForce entities. Consider Figure 4. DevForce EntityManager Support for POCOs. The business entity you saw diagrammed earlier in Figure 3. Inheritance Hierarchy for a DevForce Business Class is now shown wrapped by a DevForce EntityWrapper. Alternatively, a POCO is wrapped. The abstraction of the EntityWrapper permits the DevForce EntityManager to work with either type of object.

118 | P a g e

IdeaBlade DevForce
Figure 4. DevForce EntityManager Support for POCOs

Business Object Persistence

POCOs are discussed in detail later in this chapter (POCO Support in DevForce).

Persistence Management Capabilities


In this section we introduce the most important capabilities of the EntityManager. Some topics deserve extended attention and are discussed more fully in later chapters but youll get a preview here of how DevForce persistence management can
retrieve business objects from data sources manage them in its cache move business objects across the Internet create new business objects save additions, changes, and deletions to a data source restore pending changed and deleted objects to their retrieved state continue to function when disconnected (even in Silverlight!) preserve cache contents temporarily in local storage log in and log out of the central server ensure business object security, and exploit an n-tier architecture.

Retrieving business objects


DevForce applications deal in business objects. Accordingly, the DevForce retrieval mechanisms return business objects. There are two such mechanisms: entity queries and entity navigation.

119 | P a g e

IdeaBlade DevForce

Business Object Persistence

An entity query hunts for objects with attributes that match specified search criteria. Suppose you need a list of employees over 40. In DevForce you could express this criterion in a LINQ-To-DevForce query which could be enumerated over to return a collection of Employee entities that happens to include sales representative Nancy Davolio. Entity navigation involves traversing from one kind of business object to another along a relation between them. You can navigate from a sales order to Nancy with an expression such as anOrder.SalesRep. This returns an Employee entity. Entity navigation can return a collection of entities as well. The expression aSalesRep.Orders returns the orders assigned to this employee sales rep. The orders are returned in special kind of generic list whose contents are managed by the EntityManager, a feature youll find especially useful in your UI. The section Entity Queries and Entity Navigation offers greater detail.

The Entity Cache


A EntityManager caches business objects both for performance and to enable offline operation of the application. Each instance of EntityManager has its own cache of entities. Entities enter the cache in one of three ways:
from a data source as a result of entity query or entity navigation by creation as new entities by import from another EntityManager or outside source

Most entities enter the cache from a data source. Standard entity queries and entity navigations check the cache first to see if the desired objects are present; they resort to the data source only if the objects are not found10. This behavior is usually desirable as it improves performance. The risk is that the entities in the cache become stale. The programmer can, at his election, by-pass the cache and query the database directly (the query results still end up in the cache). There are a host of other options which are addressed in the section Entity Queries and Entity Navigation. After a successful query, the cache holds the root business objects of the result. If you searched for employees, the cache will hold employee entities. The cache may hold other related entities as well. But it may not, and you shouldnt assume that it holds the entire business object graph of an employee after retrieving that employee. For example, after querying for Nancy Davolio, she is in the cache, but the Orders for which she is responsible as Sales Rep probably are not.

10

DevForce keeps a cache of query objects for use in determining whether requested objects are already in the cache; well cover this in more detail later.

120 | P a g e

IdeaBlade DevForce

Business Object Persistence

A cache holds at most one copy of a business object. Recall that a business object has a unique identity implemented as a unique primary key. There is only one Employee in the application universe with an Id = 42. If follows that there can only be one Employee in the cache with Id = 42.11 Finally, entities stay in the cache until the application terminates or they are removed explicitly. If your application retrieves a great deal of data, you may have to take steps to prevent cache overflow, and a variety of facilities are provided to assist with this. However, for most applications this never even becomes an issue. Well have more to say about caching in the coming pages.

Business objects in motion


The EntityServer and EntityManager exchange data in a highly optimized manner. Because of our efficient, automatic, and as-needed dehyration and rehydration of objects, as well as our seamless interaction with the Microsoft Entity Framework and its objects, your experience of the exchange of data between logical tiers is that it is simply DevForce business objects that are moving back and forth. A DevForce business object sent from the EntityManager to the EntityServer, or vice versa, is, in all important respects, exactly the same object when it arrives as when it left. It is of the same type, with the same persistable field values, properties, methods, and events. In practical effect, the entire object has traveled over the network; it is a mobile business object. There are two important implications.
Developers write one business object class with the full capacity to execute on either the client or server as required. They dont write one kind of object for the server and a different one for the client. They write one class, period. The application can be deployed on one physical tier, two tiers, three, or n-tiers without recoding.

We guarantee complete object fidelity for cross-process or cross-machine communication, achieving this through a combination of storage format, serialization methods, transport mechanisms, and data merge facilities.

Creating new business objects


The developer can make a new entity by invoking either a constructor or a factory method that returns an instance of the business object. For most circumstances we recommend the latter technique, since it permits you fully to control the details of the instantiation (such as initialization of required properties).
11

An application can actually have more than one EntityManager instance, though this is a needed only in sophisticated
applications and for special purposes. Each EntityManager instance will have its own cache, and each cache can contain an instance of any given business object. But every entity instance knows its own EntityManager. If we ever encounter two Employee entities with Id = 42, we can ask them who is your EntityManager? For more information on the use of multiple EntityManagers, see the section Multiple Entity Manager Instances under Advanced Business Object Concepts. For the balance of the current discussion, we will assume the application uses just one EntityManager instance.

121 | P a g e

IdeaBlade DevForce

Business Object Persistence

You write the factory method, and typically call it Create, making it a public static (Shared in VB) method of the business objects developer-controlled partial class; e.g., Employee.Create(). There are four steps to the typical Create method:
Get a prototype instance of the new entity from the EntityManager Give the prototype a unique identity Initialize some of its values Add the completed prototype to the EntityManagers cache

We explore these steps in the section Creating Business Objects.

Saving and undoing business object changes


Adding, changing, and deleting are operations affecting business objects in a EntityManager cache only. They are purely local modifications. They have no effect on the database and are invisible to other application users. The developer updates the database by telling the EntityManager to save changes. The developer can save an individual entity, an arbitrary list of entities, or all entities with pending changes. The wise developer will validate the business objects before saving them. The developer can also undo the changes in which case the affected business objects are restored to their state when last retrieved. We explore these summary remarks with greater depth in the section Saving Business Objects.

Offline Support
A client application can lose its connection to the central servers. The interruption may be brief, sudden, and unexpected, as when a mobile device loses its signal; or it may be voluntary and last for hours, as when the user runs the application offline on an airplane.
An application which is susceptible to connection failures is called a partially connected or intermittently connected application.

A DevForce smart client application can operate when disconnected -- whether suddenly and unexpectedly or on purpose -- for any length of time. It can be shut down and re-started without skipping a beat. While disconnected, the application can still create new objects and modify or delete cached entities. Such changes accumulate in the cache until the application reconnects and performs a save. All it takes is a little programming using some simple DevForce EntityManager features.

122 | P a g e

IdeaBlade DevForce

Business Object Persistence

Step #1: Manage the connection. The developer can control voluntary connection to the host and respond to unexpected disconnects with the help of a small number of EntityManager properties, methods, and events. Step #2: Save a copy of the cache locally. The typical sequence is:
1. 2. 3. 4. 5. Fill the cache with entities that will be needed while running disconnected. Disconnect and continue running. Save the cache to the clients local storage (e.g., a file) just before exit. Shut down. On re-launch, restore the cache from the client copy.

All pending changes are preserved across the two sessions. See the Saving the Cache Locally section of the Business Object Caching chapter to learn more.

Application Security
Well devote a later chapter to securing your application, so well just mention the topic briefly in this overview. Application security has three aspects:
Confidentiality Authentication Authorization

Confidentiality A secure application guards against eavesdroppers intercepting and reading traffic flowing between client and host. DevForce supports a variety of encryption measures including standard SSL. They are discussed in the Security chapter of this Developers Guide. Authentication A secure application employs an authentication scheme to ensure that both parties to a connection are who they claim to be. In a smart-client context there are two authentication burdens: (1) the server must confirm and remain confident it is talking to a real, authorized client and (2) each client must be confident it is conversing with an authentic server. DevForce has mechanisms to support both kinds of checks. Authorization The EntityManagers Login method stamps the client-side application thread with a Principal object representing the authenticated user. This Principal has an IsInRole method that returns true if the user participates in a named role passed to it. The developer has total flexibility in determining the implementation of the login method, the IPrincipal object returned from it, and the definition and usage of the role scheme. For its own part, DevForce maintains a tamper-proof SessionBundle object that is used to authenticate every transaction between the EntityManager and EntityServer.

Business Object Security


123 | P a g e

IdeaBlade DevForce

Business Object Persistence

A secure application prevents improper access to data in the data source. The first step is to remove connection strings from the client. Connection strings have database addresses and passwords. There is no disguising or hiding them on the client. They belong in a safe place on the server. The EntityManager doesnt connect to the data source so it doesnt need connection strings. It tells the EntityServer which data source to use by sending a symbolic data source key. The key is just a name. The EntityServer knows how to use the key to find and connect to the data source. No process on the client side can use it. A secure application provides more fine-grained security than just whether or not the client can access the data source. It should prevent certain users from retrieving certain business objects. It should discriminate among users in determining which kinds of data source update are permitted. The screening could be at any level of detail from, say, the tables, down to a single column of a particular record. Spoofing In n-tier applications, whether browser-based or smart client, there is always a risk that some process pretending to be a valid client will attempt access the database in an unauthorized way. A good security design assumes that the client process -- because it cannot be physically secured -- will be compromised. While it may not be possible to fully protect the client, you can secure the host by deploying the DevForce Business Object Server (BOS) which includes the full-scale version of the EntityServer. The BOS will run special security methods whenever the client attempts to reach the server. As discussed above, the EntityServer includes interception points for both queries and saves in the EntityServerQueryInterceptor and EntityServerSaveInterceptor. Through these, you can authorize and modify requests and results. Finally, DevForce business objects can be digitally signed before transmission to the client. A rogue client cannot order the server to update the data source with an imposter entity.

N-Tier Architecture
We discussed n-tier architecture at the beginning of this chapter. The Big Picture topic described three data management tiers:
1. 2. 3. Data source(s) on the data tier
EntityServer(s) as the data access tier EntityManager within a client tier

You can run all three logical tiers on the client machine if you have a totally stand-alone application. This is the preferred choice for most development work because it eliminates the complexities of coordinating with other people, software, and hardware.

124 | P a g e

IdeaBlade DevForce

Business Object Persistence

When a data-driven application is deployed for production use, the database must reside on a central tier so that many users can share the data efficiently. If, with the database so deployed, you put both the EntityManager and an EntityServer in the same process running on a client PC, you have the ever popular two-tier, client/server model. This simplifies the exchanges between an EntityServer and the EntityManager. The two components dont have to communicate over a process boundary, so in a DevForce application deployed thusly, a light-weight version of the EntityServer reads and writes directly to the EntityManager cache. An EntityServer running under such circumstances cannot provide any meaningful security or monitoring services. It serves simply a data access abstraction a job it does very well.

Three-Tier Deployment
Enterprise-grade applications will deploy the logical tiers on three separate physical tiers: a database server, an application server, and PC client machines. The application server hosts the Business Object Server (BOS) which runs multiple instances of a more muscular version of the EntityServer. This three-physical-tier deployment provides some remarkable advantages over the two-tier model. You get: Improved performance over connections slower than a local area network (e.g., the internet). The slow, heavy work takes place between the BOS and the database over a fat, fast pipe. Communications and data passing between the client and the middle tier are concise, compact, and highly optimized. Application Reach -- Because the application can be on-line wherever there is an Internet connection and without resort to VPN, it can be deployed and used by larger numbers and with reduced system requirements. Whereas SQL commands and result sets the raw data exchanged between a database and a client-side access layer cannot flow over web protocols, DevForces business objects can. Security is much tighter. We covered earlier the many layers of security available with the BOS in place. Scalability. It is impractical to maintain live connections for each client when the number of simultaneous users becomes large. The tipping point appears to be around one hundred. An EntityServer running on a central server can pool connections to the data sources and serve many clients simultaneously. The server is stateless there is no need for session awareness so fail-over and load balancing are easy options.

Model Choice by Configuration


One-tier? Two-tier? Three-tier? You dont have to make the choice right away. You write our code pretty much the same way no matter what the model. In general, you dont have to think about which code is executing where, or by what route our business objects arrived in cache. For the most part, you write code as if every aspect of the application takes place inside your development PC.

125 | P a g e

IdeaBlade DevForce

Business Object Persistence

When you are ready to deploy to a multi-tiered environment, you set a few values in the XML application configuration file (App.Config) and build some set-up projects.

Conclusion
We just took a high-level view of the persistence management landscape. Some of the key points were:
The EntityManager is perhaps the most important component in the DevForce framework. It is the client applications gateway to the remote data. The EntityManager holds and manages an entity cache of business object instances and makes them available to the application UI. All entities within a cache are unique; no two entities can have the same primary key. You can fetch entities into the cache from remote data sources using entity queries and entity navigation. Entity navigation returns a collection whose contents are managed dynamically by a EntityManager. You can create, modify, delete, remove, and save cached entities. These actions raise Life Cycle events to which you may subscribe. Entities in a cache can come from many different data sources. Each data source is identified by its data source key. Each entity belongs to just one data source. A smart-client application can run off-line. An EntityServer handles the data access and object map translation chores for each of the application data sources. It exchanges business objects with one or more EntityManager instances on individual client machines. A Business Object Server (BOS) running on a central host provides enterprise-grade security, scalability, data integrity, performance, and application monitoring.

The following sections and chapters delve deeper into the features introduced here.

Entity Queries and Entity Navigation


Entity queries and entity navigation are the two mechanisms for retrieving business objects from a data source. Both deposit business objects into the EntityManagers cache. You use entity queries to get started in a work flow. In response to a question like What orders were placed last month?, they return Order entities. If your query asks Which employees were hired last year?, you get Employee entities. The results of entity queries are root objects. Once you have a root object, your subsequent queries are often about entities related to the root object. Given employee Sally, you start exploring her object graph by looking for her address, her manager, her orders, etc. You traverse Sallys object graph using entity navigation and it has its own simple and intuitive syntax. Most applications require surprisingly few entity queries. Once you have a list of orders or employees that interest you, youre likely to settle in and poke around using entity navigation. It is common for applications to show 10 or 20 times as many entity navigations as entity queries. Since we cant navigate anywhere until we have some root entities in hand, lets start with entity queries.
126 | P a g e

IdeaBlade DevForce

Business Object Persistence

Entity Queries
Use an EntityQuery when you want to retrieve a set of business objects that satisfy selection criteria the set of employees who were hired last year, for example. Entity queries come in many flavors. Some of them are linguistically independent of any particular data source; some are specialized to a particular data source. Some can query the data source and the entity cache at the same time; some can only query the data source12. EntityQueries, like .NET ObjectQueries, are enumerable, and so can be executed in a variety of stepwise ways. Consider, for example, the following query:
Code Snippet 1. BasicQueriesDemo.BasicQuerySyntaxQuery()

C#

var customersQuery = from cust in _em1.Customers where cust.ContactTitle == "Sales Representative" orderby cust.CompanyName select cust; Dim customersQuery = From cust In _em1.Customers _ Where cust.ContactTitle = "Sales Representative" _ Order By cust.CompanyName _ Select cust

VB

This can also be written in method-based syntax13 as


Code Snippet 2. BasicQueriesDemo.BasicMethodSyntaxQuery()

C#

var customersQuery = _em1.Customers .Where(c => c.ContactTitle == "Sales Representative") .OrderBy(c => c.CompanyName) .Select(c => c); Dim customersQuery = _em1.Customers _ .Where(Function(c) c.ContactTitle = "Sales Representative") _ .OrderBy(Function(c) c.CompanyName) _ .Select(Function(c) c)

VB

12

This means this kind of query can be used only when the application is connected to the data source; such queries cant run when the application is off-line. Query-based syntax looks a great deal like SQL and is, for that reason, attractive to many developers, especially those new to LINQ. At IdeaBlade we tend to prefer the more regularly structured and comprehensive method-based syntax for most queries, so you will see most of our sample queries in that format. Be assured, however, that you may write your LINQ queries in the syntax you prefer! We discuss the two syntaxes more in the section Query v. Method Syntax, in this document.

13

127 | P a g e

IdeaBlade DevForce

Business Object Persistence

Or just,
Code Snippet 3. BasicQueriesDemo.MethodSyntaxShortForm()

C#

var customersQuery = _Em1.Customers .Where(c => c.ContactTitle == "Sales Representative") .OrderBy(c => c.CompanyName); Dim customersQuery = _em1.Customers _ .Where(Function(c) c.ContactTitle = "Sales Representative") _ .OrderBy(Function(c) c.CompanyName)

VB

Each of these returns an IdeaBlade.EntityQuery.EntityQuery<Customer>. If you choose to type your variable to hold the querys return value explicitly as a DevForce EntityQuery<T>, the statement becomes the following:
Code Snippet 4. BasicQueriesDemo.QueryWithExplicitlyTypedReturnValue()

C#

IEntityQuery<Customer> customersQuery = _em1.Customers .Where(c => c.ContactTitle == "Sales Representative") .OrderBy(c => c.CompanyName); Dim customersQuery As IEntityQuery(Of Customer) = _ _em1.Customers _ .Where(Function(c) c.ContactTitle = "Sales Representative") _ .OrderBy(Function(c) c.CompanyName)

VB

The following query retrieves only a single Customer entity is retrieved from the data source into the local cache. If no Customer matches the stated criterion, DevForce returns the Null Entity Customer:
Code Snippet 5. BasicQueriesDemo.RetrieveFirstCustomer()

C#

Customer firstCustomer = _em1.Customers .Where(c => c.ContactTitle == "Sales Representative") .OrderBy(c => c.CompanyName) .FirstOrNullEntity(); Dim firstCustomer As Customer = _em1.Customers _ .Where(Function(c) c.ContactTitle = "Sales Representative") _ .OrderBy(Function(c) c.CompanyName) _ .FirstOrNullEntity()

VB

The addition of a call to extension method ToList() forces DevForce to execute the query immediately:
Code Snippet 6. ForcingExecutionDemo.ForceImmediateExecution()

C#

ICollection<Customer> customersQuery = _em1.Customers .Where(c => c.ContactTitle == "Sales Representative") .OrderBy(c => c.CompanyName) .ToList();

128 | P a g e

IdeaBlade DevForce
VB

Business Object Persistence

Private Sub ForceImmediateExecution() Dim customersQuery As ICollection(Of Customer) = _ _em1.Customers _ .Where(Function(c) c.ContactTitle = "Sales Representative") _ .OrderBy(Function(c) c.CompanyName) _ .ToList()

The call to ToList(), because it demands a complete set of pointers to the retrieved matching customers, forces the complete query to be executed. Below is a DevForce DebugLog listing for a test that first issued a First() call like the one we just considered, then a call to ToList(). Weve removed some of the columns included in the log so the table wont be quite so wide, but note the highlighted Fetch value messages. The first one, when delivered to the EntityFramework, will be translated into a SQL query that returns a single record; the second will be translated into a SQL query that returns all of the matching customers.

If you want to see the SQL generated by the EntityFramework to process your query, find the appropriate edmKey in your App.Config file and add a logTraceString attribute set to true:

129 | P a g e

IdeaBlade DevForce

Business Object Persistence

This will result in output like the following. (Again, some columns were omitted to reduce the table width for inclusion here.) Note the generated SQL statements:

In between the two extremes of asking a query object for its first element and asking it to dump its contents ToList() are many possibilities, such as using it in a foreach loop:

130 | P a g e

IdeaBlade DevForce
C#

Business Object Persistence

IEntityQuery<Customer> customersQuery = _em1.Customers .Where(c => c.ContactTitle == "Sales Representative") .OrderBy(c => c.CompanyName); foreach (Customer aCustomer in customersQuery) { // All customers are retrieved at the start of the loop }

VB

Dim customersQuery As IEntityQuery(Of Customer) = _em1.Customers.Where(Function(c) c.ContactTitle = "Sales Representative").OrderBy(Function(c) c.CompanyName) For Each aCustomer As Customer In customersQuery ' All customers are retrieved at the start of the loop Next aCustomer

Code Snippet 7. ForcingImmediateExecution.ForceRetrievalUsingForEach()

The foreach loop returns references to the retrieved Customers one at a time, but it does so from a collection of those references which must be obtained up front. Thus, as soon as the first iteration of the loop is executed, the entire set of Customers is retrieved to the local cache, and a collection of references to them is assembled. The debug log will show only a single query:

On the other hand, the following query results in exactly five (5) entities being retrieved from the data source:
Code Snippet 8. SkipAndTakeDemo.QueryWithSkipAndTake()

C#

IEntityQuery<Customer> customersQuery = _em1.Customers .Where(c => c.ContactTitle == "Sales Representative") .OrderBy(c => c.CompanyName) .With(QueryStrategy.DataSourceOnly); ICollection<Customer> customers = customersQuery.Skip(5).Take(5).ToList(); Dim customersQuery As IEntityQuery(Of Customer) = _em1.Customers _ .Where(Function(c) c.ContactTitle = "Sales Representative") _ .OrderBy(Function(c) c.CompanyName) _ .With(QueryStrategy.DataSourceOnly) Dim customers As ICollection(Of Customer) = customersQuery.Skip(5).Take(5).ToList()

VB

Note the use of the DataSourceOnly QueryStrategy. Thats often important when using Skip(). You can learn why in the section of this chapter on FetchStrategies. The With() Extension Method DevForce provides an extension method, With(), that permits you to substitute a different QueryStrategy, a different target EntityManager, or both, on an existing query. The original query will be left unaltered. When a call to With() is chained to a query, the result may be either a new query or a reference to the original query. Normally it will be a new query, but if the content of the With() call is such that the
131 | P a g e

IdeaBlade DevForce

Business Object Persistence

resultant query would be the same as the original one, a reference to the original query is returned instead of a new query. If you ever want to be sure that you get a new query, use the Clone() extension method instead of With(). With() avoids the overhead of a Clone() when a copy is unnecessary. You can pass null arguments to With(). When a query has a null EntityManager assigned, it uses the DefaultManager. When a query has a null QueryStrategy, it uses the DefaultQueryStrategy of the assigned (or default) EntityManager. See the code below for more detail on the possibilities.
Code Snippet 9. QueriesWithWITH Demo.QueriesWithWITH()

C#

IEntityQuery<Customer> query0 = _em1.Customers .Where(c => c.CompanyName.ToLower().StartsWith("a")); query0.QueryStrategy = QueryStrategy.DataSourceOnly; // Use With() to run the existing query against a different EntityManager: ServerModelNorthwindIBContext em2 = new ServerModelNorthwindIBContext(); List<Customer> customers = new List<Customer>(query0.With(em2)); // The next two examples use With() to run the query with a different QueryStrategy. // The With() call in the right-hand side of the following statement // specifies a query that is materially different from query0, in // that it has a different QueryStrategy associated with it. // Accordingly, the right-hand side of the statement will return // a new query: IEntityQuery<Customer> query1 = query0.With(QueryStrategy.CacheOnly); // Because the content of the With() call in the right-hand side // of the following statement doesn't result in a modification // of query0, the right-hand side will return a reference to // query0 rather than a new query. IEntityQuery<Customer> query2 = query0.With(QueryStrategy.DataSourceOnly); // If you want to be certain you get a new query, use Clone() // rather than With(): EntityQuery<Customer> query3 = (EntityQuery<Customer>)query0.Clone(); query3.QueryStrategy = QueryStrategy.DataSourceOnly; // Change both the QueryStrategy and the EntityManager IEntityQuery<Customer> query4 = query0.With(em2, QueryStrategy.CacheOnly); // You can pass null arguments to With(). When a query has a null EntityManager // assigned, it uses the DefaultManager. When a query has a null QueryStrategy, // it uses the DefaultQueryStrategy of the assigned (or default) EntityManager. // Run the query against the default EntityManager, using its default QueryStrategy: IEntityQuery<Customer> query5 = query0.With(null, null); // When you pass a single null to With, you must cast it to the appropriate // type so the compiler know's which single-parameter overload you mean to use: // Run the query against the default EntityManager, using the base query's // assigned QueryStrategy: IEntityQuery<Customer> query6 = query0.With((ServerModelNorthwindIBContext)null); // Run the query against the assigned EntityManager, using that EntityManager's // default QueryStrategy: IEntityQuery<Customer> query7 = query0.With((QueryStrategy)null);

132 | P a g e

IdeaBlade DevForce
VB

Business Object Persistence

Dim query0 As IEntityQuery(Of Customer) = _em1.Customers.Where(Function(c) c.CompanyName.ToLower().StartsWith("a")) query0.QueryStrategy = QueryStrategy.DataSourceOnly ' Use With() to run the existing query against a different EntityManager: Dim em2 As New DomainModelEntityManager() Dim customers As New List(Of Customer)(query0.With(em2)) ' The next two examples use With() to run the query with a different QueryStrategy. ' The With() call in the right-hand side of the following statement ' specifies a query that is materially different from query0, in ' that it has a different QueryStrategy associated with it. ' Accordingly, the right-hand side of the statement will return ' a new query: Dim query1 As IEntityQuery(Of Customer) = query0.With(QueryStrategy.CacheOnly) ' Because the content of the With() call in the right-hand side ' of the following statement doesn't result in a modification ' of query0, the right-hand side will return a reference to ' query0 rather than a new query. Dim query2 As IEntityQuery(Of Customer) = query0.With(QueryStrategy.DataSourceOnly) ' If you want to be certain you get a new query, use Clone() ' rather than With(): Dim query3 As EntityQuery(Of Customer) = CType(query0.Clone(), EntityQuery(Of Customer)) query3.QueryStrategy = QueryStrategy.DataSourceOnly ' Change both the QueryStrategy and the EntityManager Dim query4 As IEntityQuery(Of Customer) = query0.With(em2, QueryStrategy.CacheOnly) ' You can pass null arguments to With(). When a query has a null EntityManager, ' assigned, it uses the DefaultManager. When a query has a null QueryStrategy, ' it uses the DefaultQueryStrategy of the assigned (or default) EntityManager. ' Run the query against the default EntityManager, using its default QueryStrategy: Dim query5 As IEntityQuery(Of Customer) = query0.With(Nothing, Nothing) ' When you pass a single null to With, you must cast it to the appropriate ' type so the compiler know's which single-parameter overload you mean to use: ' Run the query against the default EntityManager, using the base query's ' assigned QueryStrategy: Dim query6 As IEntityQuery(Of Customer) = query0.With(CType(Nothing, DomainModelEntityManager)) ' Run the query against the assigned EntityManager, using that EntityManager's ' default QueryStrategy: Dim query7 As IEntityQuery(Of Customer) = query0.With(CType(Nothing, QueryStrategy))

The FirstOrNullEntity() ExtensionMethod LINQ to Entities provides First() and FirstOrDefault() extension methods on queries. First() returns the first item in a collection meeting the query criteria; FirstOrDefault() returns that, or if no items meet the criteria, the default value for the target type. For integer target types, FirstOrDefault() returns a zero; for string types, it returns an empty string. For complex types or other types that have no default, it returns a null. DevForce adds a FirstOrNullEntity() extension method that can be used when you are querying for target types that inherit from IdeaBlade.EntityModel.Entity. If no entity meets the specified criteria, FirstOrNullEntity() returns the DevForce NullEntity for the target type. The NullEntity is a non-saveable,

133 | P a g e

IdeaBlade DevForce

Business Object Persistence

immutable, syntactically correct instance of an entity represents nothing there but will not trigger an exception.

The ToQuery () ExtensionMethod Every IdeaBlade.EntityModel.Entity has a ToQuery() extension method that returns an IEntityQuery<T> where T is an Entity type. This IEntityQuery<T> specifies the Entity on which it was based using its EntityAspect.EntityKey, and can be extended to perform various useful operations. Consider, for example, the following statements:
Code Snippet 10. UsingToQuery.UsingToQueryPt01

C#

Customer aCustomer = _em1.Customers.FirstOrNullEntity(); var query = aCustomer.ToQuery<Customer>() .Include(Customer.PathFor(c => c.Orders)); query.With(QueryStrategy.DataSourceOnly).ToList();

VB

Dim aCustomer As Customer = _em1.Customers.FirstOrNullEntity() Dim query = aCustomer.ToQuery().Include(Customer.PathFor(Function(c) c.Orders)) query.With(QueryStrategy.DataSourceOnly).ToList()

134 | P a g e

IdeaBlade DevForce

Business Object Persistence

Here, from a Customer entity, we have created a query that will retrieve that same Customer. We have then extended with a call to Include() it to create a span query that will also retrieve all of that Customers associated Orders. We do not otherwise have so convenient a way to accomplish this goal. The ToQuery() extension method is also provided on any IEnumerable<T> collection, when T is an Entity. Thus you can turn an arbitrary list of Customers into a query that will return the same set of Customers. The Where() clause on the resultant query will specify a series of ORd key values. For example, consider the following statements:
Code Snippet 11. UsingToQuery.UsingToQueryPt02

C#

List<Customer> customers = _em1.Customers .Where(c => c.CompanyName.ToLower().StartsWith("a")).ToList(); var query2 = customers.ToQuery<Customer>();

VB

Dim customers As List(Of Customer) = _em1.Customers _ .Where(Function(c) c.CompanyName.ToLower().StartsWith("a")).ToList() Dim query2 = customers.ToQuery()

Placing query2 in a watch window reports its value as the following: {value(IdeaBlade.EntityModel.EntityQueryProxy`1[DomainModel.Customer]).W here(t => ((((t.CustomerID = 785efa04-cbf2-4dd7-a7de-083ee17b6ad2) || (t.CustomerID = b61cf396-206f-41a6-9766-168b5cbb8edd)) || (t.CustomerID = f214f516-d55d-4f98-a56d-7ed65fd79520)) || (t.CustomerID = 256d4372-baa74937-9d87-d9a4e06146f8)))} The first query evidently placed four Customers in the customers list; the query returned by ToQuery() specifies those four by their (GUID) key values. Other Query Types In addition to the EntityQuery, DevForce provides the PassthruESQLQuery and StoredProcQuery types for querying using Entity SQL and stored procedures, respectively. Like the EntityQuery, these types implement DevForces IEntityQuery interface.

Code Snippet 12. EsqlAndStoredProcQueriesDemo.PassThruEsqlQuery

C#

PassthruEsqlQuery query = new PassthruEsqlQuery(typeof(Employee), "SELECT VALUE e FROM Employees AS e Where e.EmployeeID < 10"); IEnumerable results = _em1.ExecuteQuery(query); Dim query As New PassthruEsqlQuery(GetType(Employee), _ "SELECT VALUE e FROM Employees AS e Where e.EmployeeID < 10") Dim results As IEnumerable = _em1.ExecuteQuery(query)

VB

135 | P a g e

IdeaBlade DevForce
Code Snippet 13. EsqlAndStoredProcQueriesDemo.StoredProcQuery

Business Object Persistence

C#

QueryParameter param01 = new QueryParameter("EmployeeID",1); QueryParameter param02 = new QueryParameter("Year",1996); StoredProcQuery query = new StoredProcQuery(typeof(Order)); query.Parameters.Add(param01); query.Parameters.Add(param02); // Note that a FunctionImport must be defined in the Entity Model query.ProcedureName = "OrdersGetForEmployeeAndYear"; _em1.ExecuteQuery(query);

VB

Dim param01 As New QueryParameter("EmployeeID", 1) Dim param02 As New QueryParameter("Year", 1996) Dim query As New StoredProcQuery(GetType(Order)) query.Parameters.Add(param01) query.Parameters.Add(param02) ' Note that a FunctionImport must be defined in the Entity Model query.ProcedureName = "OrdersGetForEmployeeAndYear" _em1.ExecuteQuery(query)

The Query Object return type An entity query returns one and only one kind of thing. That kind of thing is always an entity type declared in the business object model. The query developer must identify that entity type and ensure that the substance of the query actually will return such entities.
Although the query returns only one kind of entity, it may populate the entity cache with other kinds of entities. Youll see just how useful this can be when we discuss span queries and query inversion.

The Fetch and Merge The EntityManager evaluates the query and searches for suitable entities either in the cache, in the data source, or in both. Where it looks for entities and what it does with the ones it finds are determined by a QueryStrategy object which we will cover in the Caching topic below.

Query v. Method Syntax


The following LINQ query is written in the syntax known as query syntax, query comprehension syntax, or just comprehension syntax:
Code Snippet 14. BasicQueriesDemo.BasicQuerySyntaxQuery (Repeated)

C#

var customersQuery = from cust in _Em1.Customers where cust.ContactTitle == "Sales Representative" orderby cust.CompanyName select cust; Dim customersQuery = From cust In _em1.Customers _ Where cust.ContactTitle = "Sales Representative" _ Order By cust.CompanyName _ Select cust

VB

136 | P a g e

IdeaBlade DevForce
This can also be written in method-based syntax as
Code Snippet 15. BasicQueriesDemo.BasicMethodSyntaxQuery (Repeated)

Business Object Persistence

C#

var customersQuery = _Em1.Customers .Where(c => c.ContactTitle == "Sales Representative") .OrderBy(c => c.CompanyName) .Select(c => c); Dim customersQuery = _em1.Customers _ .Where(Function(c) c.ContactTitle = "Sales Representative") _ .OrderBy(Function(c) c.CompanyName) _ .Select(Function(c) c)

VB

At IdeaBlade we mostly prefer the method-based syntax as a general rule. The capabilities available in method-based syntax are substantially a superset of those available in query syntax, so when using query syntax you may be forced into concatenating method-based clauses anyway to get what you want, as in the following:
Code Snippet 16. BasicQueriesDemo.MixedQueryAndMethodSyntax

C#

ICollection<Customer> customers = (from cust in _em1.Customers orderby cust.CompanyName select cust) .ToList(); Dim customers As ICollection(Of Customer) = _ (From cust In _em1.Customers _ Order By cust.CompanyName _ Select cust) .ToList()

VB

Having said that, there are a few things that are arguably a bit easier or more natural to do in query syntax14, and of course there are simply personal preferences. So use what you like!

LINQ
The typical data-oriented approach to retrieving objects relies upon a specialized query language such as SQL. SQL is a powerful query language requiring considerable sophistication and experience to use properly. But there are pitfalls to using SQL and several good reasons to prefer LINQ to SQL queries, including:
Object orientation Compile time checking

14

Joseph and Ben Albahari, in a fine discussion of LINQ, opine that query comprehension syntax is much simpler for queries that involve any of the following: A let clause for introducing a new variable alongside the iteration variable SelectMany, Join, or GroupJoin, followed by an outer iteration variable reference

See their excellent book C#3.0 In a Nutshell, OReilly Media Inc., 2007, p.285

137 | P a g e

IdeaBlade DevForce
Query portability Query manipulation

Business Object Persistence

LINQ is a vast subject and is, for the most part, beyond the scope of this document. A web search on LINQ will provide you with an abundance of excellent resources for learning about LINQ. It suffices to say here that our implementation of LINQ -- LINQ to DevForce -- permits the same query to be used against a local cache or a back-end datasource supported by Microsofts LINQ to Entities. You can specify, by means of a QueryStrategy property on the query object, just what you want its target data store or data stores to be; or you can let DevForce apply sensible defaults which work well for the majority of cases.

The DevForce Predicate Builder


The time comes when you want to construct a LINQ Where clause programmatically. It should be easy. It turns out to be more challenging until you use the DevForce PredicateBuilder. (You will find this class in the IdeaBlade.Linq namespace, in either the IdeaBlade.Linq or IdeaBlade.Linq.SL [for Silverlight] assembly.) Imagine a product search interface. The user can enter words in a Name Search text box. Your program should find and display every product that contains any of the words entered by the user. You dont know how many words the user might enter. What do you do? The solution would be easy if you knew the user would enter exactly one word.

Code Snippet 17. UsingThePredicateBuilderDemo.ProductsWithNamesThatContainSpecifiedString

C#

var word = "Sir"; var q = _em1.Products .Where(p => p.ProductName.Contains(word)); var results = q.ToList();// returns 3 Northwind products Dim word = "Sir" Dim q = _em1.Products _ .Where(Function(p) p.ProductName.Contains(word)) Dim results = q.ToList() ' returns 3 Northwind products

VB

Of course you dont know how many words the user will enter. You want to be prepared for more than one so you write this too-simple helper method that returns an array of words from the text entered in the text box:

Code Snippet 18. UsingThePredicateBuilderDemo.GetWords

C#

private IEnumerable<String> GetWords(string phrase) { return phrase.Split(new[] {' '}, StringSplitOptions.RemoveEmptyEntries); } Private Function GetWords(ByVal phrase As String) As IEnumerable(Of String) Return phrase.Split(New Char() {" "c}, StringSplitOptions.RemoveEmptyEntries) End Function

VB

138 | P a g e

IdeaBlade DevForce

Business Object Persistence

Now all you have to do is replace the Where clause with a sequence of OR clauses. Youll want to construct it by iterating over the words. Go ahead and write it. Well wait... Having trouble? Ill give you the users input: Sir Cajun Louisiana. Did that help? You will probably come up with something like the following:

C#

var q = _em1.Products .Where(p => p.ProductName.Contains("Sir") || p.ProductName.Contains("Cajun") || p.ProductName.Contains("Louisiana") ); var results = q.ToList(); // returns 6 Northwind products Dim q = _em1.Products.Where(Function(p) _ p.ProductName.Contains("Sir") _ OrElse p.ProductName.Contains("Cajun") _ OrElse p.ProductName.Contains("Louisiana")) Dim results = q.ToList() ' returns 6 Northwind products

VB

Code Snippet 19. UsingThePredicateBuilderDemo.ProductsWithNamesThatContainSpecifiedStrings

This is ultimately what the lambda expression must look like. Of course you cannot demand that the user enter exactly three words any more than you can insist she enter exactly one. You want to construct the lambda dynamically based on the actual number of words entered. Sadly, there is no obvious way of constructing a lambda expression dynamically. But the DevForce PredicateBuilder can help you build predicates dynamically. Whats a predicate? A predicate is a function that evaluates an expression and returns true or false. The code fragment...

C# p.ProductName.Contains(Sir) /VB ...is a predicate that examines a product and returns true if the products ProductName contains the Sir string. The CLR type of the predicate in our example is:

139 | P a g e

IdeaBlade DevForce
C# VB

Business Object Persistence

Func<Product, bool> Func(Of Product, Boolean)

Which we can generalize to:

C# VB

Func<T, bool> Func(Of T, Boolean)

We almost have what we want. When the compiler sees an example of this kind of thing, it immediately resolves it into an anonymous delegate. But we dont want the delegate. We need a representation that retains our intent and postpones the resolution into a delegate until the last possible moment; because before we get that delegate, we may want to build a more complex expression. What we need is an expression made up of Func<T, bool>s:

C# VB

Expression<Func<T, bool>> Expression(Of Func(Of T, Boolean))

As it so happens, this is exactly what the DevForce Where extension method demands:

C#

public static IEntityQuery<T> Where<TSource>( this IEntityQuery<T> source1, Expression<Func<T,bool>> predicate) public static IEntityQuery(Of T) Where(Of TSource) _ (Me IEntityQuery(Of T) source1, Expression(Of Func(Of T,Boolean)) predicate)

VB

The methods of the static IdeaBlade.Linq.PredicateBuilder class take things even a step farther: they permit us to combine two or more Predicate Expressions into a single Predicate Expression that we can pass to that Where() method. Lets stick with the example and see one of those PredicateBuilder methods in action. Lets first write a little method to produce an IEnumerable of Predicate Expressions, one expression for each string in a collection of strings:
140 | P a g e

IdeaBlade DevForce
Code Snippet 20. UsingThePredicateBuilderDemo.ProductNameTests

Business Object Persistence

C#

private IEnumerable<Expression<Func<Product, bool>>> ProductNameTests(IEnumerable<String> words) { foreach (var each in words) { var word = each; yield return p => p.ProductName.Contains(word); } }

VB

Private Function ProductNameTests(ByVal words As IEnumerable(Of String)) _ As IEnumerable(Of Expression(Of Func(Of Product, Boolean))) Dim expressions As New List(Of Expression(Of Func(Of Product, Boolean)))() For Each [each] In words Dim word = [each] ' include this statement so *each* is evaluated at each iteration expressions.Add(Function(p) p.ProductName.Contains(word)) Next [each] Return expressions End Function

The result is an IEnumerable of Predicate Expressions about the Product entity. The body is an iterator that returns a Predicate Expression for each word. That expression is exactly the same as the first predicate we wrote when we knew only one word. If we give it the three-word input in our example, well get an IEnumerable of three Predicate Expressions, each looking for one of the words in the products ProductName. Were want to OR these Predicate Expressions together so we will use a static method of PredicateBuilder named, well, Or():

C#

public static Expression<Func<T, bool>> Or<T>( params Expression<Func<T, bool>>[] expressions) public static Expression(Of Func(Of T, Boolean)) Or(Of T) _ (params Expression(Of Func(Of T, Boolean))() expressions)

VB

You see it takes an array (a params array to be precise) of Predicate Expressions. We will convert the output of our ProductNameTests into an array before giving it to this PredicateBuilder method. The final code looks like this:

Code Snippet 21. UsingThePredicateBuilderDemo.PredicateBuilder01

C#

var words = GetWords("Sir Cajun Louisiana"); var tests = ProductNameTests(words).ToArray(); if (0 == tests.Length) return; var productNamePredicate = PredicateBuilder.Or(tests); var q = _em1.Products.Where(productNamePredicate); var results = q.ToList(); // returns 6 Northwind products

141 | P a g e

IdeaBlade DevForce

Business Object Persistence

VB

Dim words = GetWords("Sir Cajun Louisiana") Dim tests = ProductNameTests(words).ToArray() If 0 = tests.Length Then Return End If Dim productNamePredicate = PredicateBuilder.Or(tests) Dim q = _em1.Products.Where(productNamePredicate) Dim results = q.ToList() ' returns 6 Northwind products

To summarize the steps were taking:


1. 2. 3. 4. 5. Split the users search text into separate words Generate an array of Predicate Expressions that look for each word in the ProductName Skip the query if there are no clauses because there are no words Ask PredicateBuilder.Or to combine the tests into a single Predicate Expression Run it to get results.

PredicateBuilder Methods There are seven methods of interest:

Method Or Or And And True False Not

Syntax by example p1.Or(p2) PredicateBuilder.Or(p1, p2, p3 .. pn) p1.And(p2) PredicateBuilder.And(p1, p2, p3 .. pn) PredicateBuilder.True() PredicateBuilder.False() PredicateBuilder.Not(p1)

p = Predicate Expression, Expression<Func<T, bool>>. All expressions must be of the same type (e.g., Product).

Examples Here are some examples using the PredicateBuilder methods:


Code Snippet 22. UsingThePredicateBuilderDemo.PredicateBuilderMiscExamples

C#

Expression<Func<Product, bool>> p1, p2, p3, p4, bigP; // Sample predicate expressions p1 = p => p.ProductName.Contains("Sir"); p2 = p => p.ProductName.Contains("Cajun");

142 | P a g e

IdeaBlade DevForce
p3 = p => p.ProductName.Contains("Louisiana"); p4 = p => p.UnitPrice > 20; bigP = p1.Or(p2); // Name contains "Sir" or "Cajun" bigP = p1.Or(p2).Or(p3); // Name contains any of the three

Business Object Persistence

bigP = PredicateBuilder.Or(p1, p2, p3); // Name contains any of the 3 bigP = PredicateBuilder.Or(tests); // OR together some tests bigP = p1.And(p4); // "Sir" and price is greater than 20 // Name contains "Cajun" and "Lousiana" and the price is greater than 20 bigP = PredicateBuilder.And(p2, p3, p4); bigP = PredicateBuilder.And(tests); // AND together some tests // Name contains either Sir or Louisiana AND price is greater than 20 bigP = p1.Or(p3).And(p4); // bigP = PredicateBuilder.Not(p1); // Name does not contain "Sir" bigP = PredicateBuilder.True<Product>().And(p1);// same as p1 bigP = PredicateBuilder.False<Product>().Or(p1);// same as p1 // Not useful bigP = PredicateBuilder.True<Product>().Or(p1);// always true bigP = PredicateBuilder.False<Product>().And(p1);// always false

VB

Dim p1 As Expression(Of Func(Of Product, Boolean)), p2 As Expression(Of Func(Of Product, Boolean)), p3 As Expression(Of Func(Of Product, Boolean)), p4 As Expression(Of Func(Of Product, Boolean)), bigP As Expression(Of Func(Of Product, Boolean)) ' Sample predicate expressions p1 = Function(p) p.ProductName.Contains("Sir") p2 = Function(p) p.ProductName.Contains("Cajun") p3 = Function(p) p.ProductName.Contains("Louisiana") p4 = Function(p) p.UnitPrice > 20 bigP = p1.Or(p2) ' Name contains "Sir" or "Cajun" bigP = p1.Or(p2).Or(p3) ' Name contains any of the three bigP = PredicateBuilder.Or(p1, p2, p3) ' Name contains any of the 3 bigP = PredicateBuilder.Or(tests) ' OR together some tests bigP = p1.And(p4) ' "Sir" and price is greater than 20 ' Name contains "Cajun" and "Lousiana" and the price is greater than 20 bigP = PredicateBuilder.And(p2, p3, p4) bigP = PredicateBuilder.And(tests) ' AND together some tests ' Name contains either "Sir" or "Louisiana" AND price is greater than 20 bigP = p1.Or(p3).And(p4) bigP = PredicateBuilder.Not(p1) ' Name does not contain "Sir" bigP = PredicateBuilder.True(Of Product)().And(p1) ' same as p1 bigP = PredicateBuilder.False(Of Product)().Or(p1) ' same as p1 ' Not useful bigP = PredicateBuilder.True(Of Product)().Or(p1) ' always true bigP = PredicateBuilder.False(Of Product)().And(p1) ' always false

143 | P a g e

IdeaBlade DevForce

Business Object Persistence

Observations Regarding the PredicateBuilder Methods Notice that one each of the Or(), And(), and Not() methods are Predicate Expression extension methods; they make it easier to compose predicates from a number of Predicate Expressions known at design time. Put a breakpoint on any of the bigP lines and ask the debugger to show you the result as a string. Here is the Immediate Window output for bigP = p1.Or(p3).And(p4);:

{p => ((p.ProductName.Contains("Sir") || p.ProductName.Contains("Louisiana")) && (p.UnitPrice > Convert(20)))}

The True() and False() methods return Predicate Expression constants that simply help you jumpstart your chaining of PredicateBuilder expressions. Two of the combinations True()Or() and False()And() -- are not useful.

Example: Simulate an In() Clause Condition on a Distantly Related Entity


Consider the following query:
Code Snippet 23. UsingThePredicateBuilderDemo.PredicateBuilderForInClause()

C#

var employeeTerritoriesQuery = _em1.EmployeeTerritories .Where(et => et.Employee.Orders.Any(o => o.Customer.City == "Albuquerque" || o.Customer.City == "Frankfurt" || o.Customer.City == "London" || o.Customer.City == "Rio de Janeiro" || o.Customer.City == "Sao Paulo")); Dim employeeTerritoriesQuery = _em1.EmployeeTerritories _ .Where(Function(et) et.Employee.Orders _ .Any(Function(o) o.Customer.City = "Albuquerque" OrElse _ o.Customer.City = "Frankfurt" OrElse _ o.Customer.City = "London" OrElse _ o.Customer.City = "Rio de Janeiro" OrElse _ o.Customer.City = "Sao Paulo"))

VB

We have, in essence, placed an In() condition on the Customer for any Order associated with the Employee that is associated with the EmployeeTerritory entities we want to retrieve. Of course, In() isnt support by the version of LINQ in .NET 3.5, so we had to code it the hard way. Still, it works, so were happy until we realize that we need to use such a query in a situation where we dont know until runtime what cities or how many cities our end user will want to match. We need to let that user pick the cities from a list, or even type their names in freeform. For this, well need the PredicateBuilder, as shown in the version of the query below. This version uses a string array of city names as input to the query. We stuff that array in a code statement here, but it could, of course, be populated by user input in the user interface.

144 | P a g e

IdeaBlade DevForce
Code Snippet 24. UsingThePredicateBuilderDemo.PredicateBuilderForInClause()

Business Object Persistence

C#

string[] targetCities = _ { "Albuquerque", "Frankfurt", "London", "Rio de Janeiro", "Sao Paulo" }; IEnumerable<Expression<Func<EmployeeTerritory, bool>>> tests = CustomerCityNameTests(targetCities.ToArray()); var cityNamePredicate = PredicateBuilder.Or(tests.ToArray()); IEntityQuery<EmployeeTerritory> search3 = _em1.EmployeeTerritories.Where(cityNamePredicate); search3.ToList(); private IEnumerable<Expression<Func<EmployeeTerritory, bool>>> CustomerCityNameTests(IEnumerable<String> words) { foreach (var each in words) { var word = each; // must include this statement so *each* is evaluated at each iteration yield return et => et.Employee.Orders.Any(o => o.Customer.City == word); } }

VB

Dim targetCities() As String = _ {"Albuquerque", "Frankfurt", "London", "Rio de Janeiro", "Sao Paulo"} Dim tests As IEnumerable(Of Expression(Of Func(Of EmployeeTerritory, Boolean))) = _ CustomerCityNameTests(targetCities.ToArray()) Dim cityNamePredicate = PredicateBuilder.Or(tests.ToArray()) Dim search3 As IEntityQuery(Of EmployeeTerritory) = _ _em1.EmployeeTerritories.Where(cityNamePredicate) search3.ToList() Private Function CustomerCityNameTests(ByVal words As IEnumerable(Of String)) _ As IEnumerable(Of Expression(Of Func(Of EmployeeTerritory, Boolean))) Dim predicateExpressions = _ New List(Of Expression(Of Func(Of EmployeeTerritory, Boolean)))() For Each [each] In words Dim word = [each] ' must include this statement so *each* is evaluated at each iteration predicateExpressions.Add( _ Function(et) et.Employee.Orders.Any(Function(o) o.Customer.City Is word)) Next [each] Return predicateExpressions End Function

The PredicateBuilder versions retrieves exactly the same set of entities into the cache as the hard-coded version.

The PredicateDescription Class


So far, so good: but what about when you need to build a filter for a query dynamically? For example, suppose the filter criteria, including the search field and operator, are user-controlled (e.g., obtained from UI controls). With the facilities youve seen so far, you dont have a good tool. Enter the PredicateDescription. Here are some examples:
Create two filters. The snippet below comprises two statements, each of which uses PredicateBuilder.Make(Type type, string propertyName, FilterOperator filterOp, object value) to create a PredicateDescription representing a single predicate (filter criteria):

Code Snippet 25. UsingThePredicateBuilderDemo.PredicateDescriptions01

C# 145 | P a g e

IdeaBlade DevForce

Business Object Persistence

PredicateDescription p1 = PredicateBuilder.Make(typeof(Product), "UnitPrice", FilterOperator.IsGreaterThanOrEqualTo, 24); PredicateDescription p2 = PredicateBuilder.Make(typeof(Product), "Discontinued", FilterOperator.IsEqualTo, true);

VB

Dim p1 As PredicateDescription = PredicateBuilder.Make(GetType(Product), "UnitPrice", _ FilterOperator.IsGreaterThanOrEqualTo, 24) Dim p2 As PredicateDescription = PredicateBuilder.Make(GetType(Product), "Discontinued", _ FilterOperator.IsEqualTo, True)

Create a filter query, ANDing the two filters. This snippet uses PredicateBuilder.FilterQuery(IQueryable baseQuery, IPredicateDescription predicateDescription) to create a new, filtered query from a base query. The new query can then be executed by an EntityManager as usual:

Code Snippet 26. UsingThePredicateBuilderDemo.PredicateDescriptions01

C#

var query = PredicateBuilder.FilterQuery(_em1.Products, p1.And(p2)); var results = _em1.ExecuteQuery<Product>((IEntityQuery<Product>)query); // The above query is the same as: //var queryb = _em1.Products.Where(p => p.UnitPrice > 24 && p.Discontinued); Dim query = Dim results ' The above 'var queryb PredicateBuilder.FilterQuery(_em1.Products, p1.And(p2)) = _em1.ExecuteQuery(Of Product)(CType(query, IEntityQuery(Of Product))) query is the same as: = _em1.Products.Where(p => p.UnitPrice > 24 && p.Discontinued);

VB

Now lets accomplish the same thing in a slightly different manner. Multiple predicates can be Anded and Ored together to form a CompositePredicateDescription.
Create a composite filter from two individual filters.

C#

PredicateDescription p1 = new PredicateDescription(typeof(Product), "UnitPrice", FilterOperator.IsGreaterThanOrEqualTo, 24); PredicateDescription p2 = new PredicateDescription(typeof(Product), "Discontinued", FilterOperator.IsEqualTo, true); // And the two filters. CompositePredicateDescription p3 = p1.And(p2);

VB

Dim p1 As New PredicateDescription(GetType(Product), _ "UnitPrice", FilterOperator.IsGreaterThanOrEqualTo, 24) Dim p2 As New PredicateDescription(GetType(Product), _ "Discontinued", FilterOperator.IsEqualTo, True) ' And the two filters. Dim p3 As CompositePredicateDescription = p1.And(p2)

Code Snippet 27. UsingThePredicateBuilderDemo.CompositePredicateDescription

146 | P a g e

IdeaBlade DevForce

Business Object Persistence

We cant use the CompositePredicateDescription directly in or on a query. Instead we, must first convert it into a Lambda expression. We can then use that in the query: Create a lambda expression, and use that in a Where clause.

Code Snippet 28. UsingThePredicateBuilderDemo.CompositePredicateDescriptionToLambda

C#

using System.Linq.Expressions; ... var exprFunc = (Expression<Func<Product, bool>>)p3.ToLambdaExpression(); var filterQuery = _em1.Products.Where(exprFunc); var results = _em1.ExecuteQuery(filterQuery);

VB

Imports System.Linq.Expressions ... Private exprFunc = CType(p3.ToLambdaExpression(), Expression(Of Func(Of Product, Boolean))) Private filterQuery = _em1.Products.Where(exprFunc) Private results = _em1.ExecuteQuery(filterQuery)

A PredicateDescription can always be instantiated from its constructor, and ANDd or ORd with another PredicateDescription to form a CompositePredicateDescription. The method ToLambdaExpression() can be used to turn any predicate description into an expression which can be used in a standard LINQ Where clause.

Example: Given a Collection of Parent Entities, Retrieve the Related Children


As a further example of the use of the PredicateBuilder and PredicateDescription types, lets consider the following scenario: you want to retrieve a set of Orders related to an arbitrary collection of Customers. In fact, youre going to let your end user select the Customers whose Orders she wants to see. You wont know until runtime. Heres the code:
Code Snippet 29. UsingThePredicateBuilderDemo.GetRelatedChildrenOfParentCollection

C#

// Start with a list of customers that youve populated however you see fit, // perhaps from end-user input. Here, well arbitrarily populate one as follows: List<Customer> customers = _em1.Customers.Where(c => c.Country == "USA").ToList(); customers.AddRange(_em1.Customers.Where(c => c.Country == "Brazil").ToList()); // From the list of customers, create an IEnumerable<PredicateDescription> var predicates = customers.Select(c => new PredicateDescription(typeof(Customer), "CustomerId", FilterOperator.IsEqualTo, c.CustomerID)); // Convert that IEnumerable<PredicateDescription> to an array, and feed the array // to the PredicateBuilders Or() method to get a CompositePredicateDescription var customerPredicate = PredicateBuilder.Or(predicates.ToArray()); // Convert the CompositePredicateDescription to a LambdaExpression; pass the // LambdaExpression to the Where clause of a Customer query; and project out // the related Orders using a SelectMany() call. Execute the query to retrieve

147 | P a g e

IdeaBlade DevForce

Business Object Persistence

// the desired Orders! var exprFunc = (Expression<Func<Customer, bool>>)customerPredicate.ToLambdaExpression(); var ordersQuery = _em1.Customers.Where(exprFunc).SelectMany(c => c.Orders); var orders = _em1.ExecuteQuery(ordersQuery);

VB

' Start with a list of customers that youve populated however you see fit, ' perhaps from end-user input. Here, well arbitrarily populate one as follows: Dim customers As List(Of Customer) = _ _em1.Customers.Where(Function(c) c.Country = "USA").ToList() customers.AddRange(_em1.Customers.Where(Function(c) c.Country = "Brazil").ToList()) ' From the list of customers, create an IEnumerable(Of PredicateDescription) Dim predicates = customers.Select(Function(c) New PredicateDescription(GetType(Customer), "CustomerId", FilterOperator.IsEqualTo, c.CustomerID)) ' Convert that Ienumerable(Of PredicateDescription) to an array, and feed the array ' to the PredicateBuilders Or() method to get a CompositePredicateDescription Dim customerPredicate = PredicateBuilder.Or(predicates.ToArray()) ' Convert the CompositePredicateDescription to a LambdaExpression; pass the ' LambdaExpression to the Where clause of a Customer query; and project out ' the related Orders using a SelectMany() call. Execute the query to retrieve ' the desired Orders! Dim exprFunc = CType(customerPredicate.ToLambdaExpression(), _ Expression(Of Func(Of Customer, Boolean))) Dim ordersQuery = _em1.Customers.Where(exprFunc).SelectMany(Function(c) c.Orders) Dim orders = _em1.ExecuteQuery(ordersQuery)

PassthruESQL Queries
DevForce supports queries in Entity SQL (ESQL) with its PassThruEsqlQuery() method.
Code Snippet 30. EsqlAndStoredProcQueriesDemo.EsqlBasic

C#

var query = new PassthruEsqlQuery(typeof(Customer), "SELECT VALUE c FROM Customers AS c Where c.Country == 'Brazil'"); var result = query.With(_em1).Execute().Cast<Customer>(); Dim query = New PassthruEsqlQuery(GetType(Customer), _ "SELECT VALUE c FROM Customers AS c Where c.Country == 'Brazil'") Dim result = query.With(_em1).Execute().Cast(Of Customer)()

VB

As you can see, PassThruEsqlQuery() requires the Entity type to which you want references returned and the ESQL query string. Heres an ESQL query that takes a parameter, bonus, which well give a value of 2000:
Code Snippet 31. EsqlWithParameter

C#

var param = new QueryParameter("country", "Brazil"); var paramEsql = new ParameterizedEsql( "SELECT VALUE c FROM Customers AS c Where c.Country > @country", param); var query = new PassthruEsqlQuery(typeof(Customer), paramEsql); var result1 = query.With(_em1).Execute().Cast<Customer>(); Console.WriteLine("Retrieved {0} Customers from {1}", result1.Count(), param.Value.ToString()); // Retrieved 75 Customers from Brazil param.Value = "Germany"; var result2 = query.With(_em1).Execute().Cast<Customer>(); Console.WriteLine("Retrieved {0} Customers from {1}", result2.Count(), param.Value.ToString()); // Retrieved 46 Customers from Germany

148 | P a g e

IdeaBlade DevForce
VB

Business Object Persistence

Dim param = New QueryParameter("country", "Brazil") Dim paramEsql = New ParameterizedEsql( _ "SELECT VALUE c FROM Customers AS c Where c.Country > @country", param) Dim query = New PassthruEsqlQuery(GetType(Customer), paramEsql) Dim result1 = query.With(_em1).Execute().Cast(Of Customer)() Console.WriteLine("Retrieved {0} Customers from {1}", result1.Count(), _ param.Value.ToString()) Retrieved 75 Customers from Brazil param.Value = "Germany" Dim result2 = query.With(_em1).Execute().Cast(Of Customer)() Console.WriteLine("Retrieved {0} Customers from {1}", result2.Count(), _ param.Value.ToString()) Retrieved 46 Customers from Germany

Note that the value of the parameter can be changed and the same query re-executed, returning different results. When you use Entity SQL, youre responsible for formulating a query string that constitutes a valid query. If you goof, you wont know until you run it. A PassthruEsqlQuery will not interrogate the local cache15. It goes directly to the Entity Data Model to which the application must be connected when the query is issued. The EntityServer will throw an exception if it cannot convert the result set into objects of the target entitys type. We highly recommend a try/catch around your passthru query call.

Remote Service Method Call (RSMC)


DevForce offers a Remote Service Method Call (RSMC) facility that enables a client-side caller to invoke an arbitrary static method of a class accessible to the DevForce Business Object Server (BOS). The method can return any kind of serializable object16: a list, a custom object, a list of custom objects, etc. The client calls EntityManager.InvokeServerMethod() with the appropriate arguments: typically a class name, method name, and arguments for the method. An EntityServer instance in the BOS runs a security check and (if passed) invokes the requested method. The BOS serializes the result and transmits it back to the requesting EntityManager which presents the object to the caller after deserialization. It is up to the caller to make sense of this object. There is no restriction on what the remote method does or how it does it. The object returned must be serializable and like business objects must be of the same type on both client and server. The RSMC mechanism ensures that remote method callers go through the same security checks as the other EntityManager query methods.

15 16

We can extend some Passthru queries to search the cache. See Advanced Business Object Concepts. RPC is not an entity query facility because it is not required to return entities.

149 | P a g e

IdeaBlade DevForce

Business Object Persistence

An asynchronous version of the Remote Service Method Call is also provided. Its perfect for any timeconsuming, server-based operation whose results are not needed immediately for continued work in the client application. The asynchronously RSMC can, for example, be used to load huge and even unrelated collections of data from the backend data store to the local cache without freezing the UI. The end user continues productive work while the data is being loaded; and then subsequently enjoys extremely crisp response in all aspects of the client application that depend upon the data that was loaded, which is now available directly from the local cache.

Entity Navigation
Entity navigation is a convenient syntax for accessing data from related business objects. Consider these familiar scenarios:
Get all of a particular sales reps orders. Find the employees home address Calculate the sales tax for an order

In each instance, we want information (orders, address, sales tax table for the ship-to-address) related to a single entity (salesrep, employee, order). The desired information exists somewhere in the entitys business object graph the network of other entities that are related to our primary entity. In DevForce, you can begin with an entity arbitrarily designated the root entity and traverse its relations to reach other entities, both near and far. We call this navigating the graph. All you do is write a simple navigation property expression such as myOrder.Customer. Observe that the navigation property syntax, myOrder.Customer, looks just like one of the entitys simple properties, myOrder.ShippedDate. The key difference is that it returns an entity (Customer) rather than a value (DateTime). Entities have properties so you can write myOrder.Customer.Name. They have navigation properties so you can walk further along the graph to the HeadquartersAddress entity where youll find the headquarters city:
myOrder.Customer.HeadquartersAddress.City

Parent-Child Navigation properties


So far weve considered only navigation properties that return a single entity. Navigation properties can return many entities. The myOrder.OrderDetails navigation property, for example, returns the many line items of a single order. Navigation properties that return multiple entities are invariable parent-child properties. The property belongs to the parent entity such as Order and it returns child entities such as OrderDetail entities. The navigation property returns child entities in a RelatedEntityList<T> collection. The Order.OrderDetails property returns its OrderDetail children in a concrete collection, RelatedEntityList<OrderDetail>.
150 | P a g e

IdeaBlade DevForce
A brief example I am writing a program in C#.

Business Object Persistence

I write and run the following statements and learn that there are three line items in the collection owned by anOrder:
Code Snippet 32. NavigationSynchronousDemo.NavigationSynchronousBasic

C#

Order anOrder = _em1.Orders.FirstOrNullEntity(); List<OrderDetail> lineItems = new List<OrderDetail>(anOrder.OrderDetails); Dim anOrder As Order = _em1.Orders.FirstOrNullEntity() Dim lineItems As New List(Of OrderDetail)(anOrder.OrderDetails)

VB

We decide to increase the quantity ordered for the first OrderDetail as follows.

C#

OrderDetail firstItem = lineItems[0]; firstItem.Quantity = 10; Dim firstItem As OrderDetail = lineItems(0) firstItem.Quantity = 10

VB

Navigation Properties in Silverlight


Because all data retrieval and save operations in Silverlight are required to be asynchronous, navigation properties return their results to callback methods. Consider the following code:

151 | P a g e

IdeaBlade DevForce
Code Snippet 33. NavigationBasicAsynchronous

Business Object Persistence

C#

public void NavigationBasicAsynchronous() { _em1.UseAsyncNavigation = true; IEntityQuery<Order> query = _em1.Orders.Where(o => o.OrderID == 10248); _em1.ExecuteQueryAsync<Order>(query, GotOrder, null); PromptToContinue(); } private void GotOrder(EntityQueryOperation<Order> args) { if (args.Error != null) { Console.WriteLine(args.Error.Message); } else { // Retrieve a single related entity using a scalar navigation property Order targetOrder = (Order)args.Results.ToList()[0]; Console.WriteLine("Order: {0}", targetOrder.OrderID.ToString()); targetOrder.Customer.PendingEntityResolved += new EventHandler<PendingEntityResolvedEventArgs>( Customer_PendingEntityResolved); Customer aCustomer = targetOrder.Customer; Console.WriteLine("Customer (from GotOrders): {0}", aCustomer.CompanyName); // Retrieve a collection of related entities using a collection navigation property targetOrder.OrderDetails.PendingEntityListResolved += new EventHandler<PendingEntityListResolvedEventArgs<OrderDetail>>( OrderDetails_PendingEntityListResolved); } } void Customer_PendingEntityResolved(object sender, PendingEntityResolvedEventArgs e) { Customer customer = (Customer)e.ResolvedEntity; Console.WriteLine("Customer (from Customer_PendingEntityResolved): {0}", customer.CompanyName); } void OrderDetails_PendingEntityListResolved(object sender, PendingEntityListResolvedEventArgs<OrderDetail> e) { Console.WriteLine("OrderDetails retrieved: {0}", e.ResolvedEntities.Count); } private void PromptToContinue() { Console.WriteLine(); Console.WriteLine("Press ENTER to continue..."); Console.ReadLine(); }

152 | P a g e

IdeaBlade DevForce
VB

Business Object Persistence

Public Sub NavigationBasicAsynchronous() ResetEntityManager(_em1) _em1.UseAsyncNavigation = True Dim query As IEntityQuery(Of Order) = _em1.Orders.Where(Function(o) o.OrderID = 10248) _em1.ExecuteQueryAsync(Of Order)(query, AddressOf GotOrder, Nothing) PromptToContinue() End Sub Private Sub GotOrder(ByVal args As EntityFetchedEventArgs(Of Order)) If args.Error IsNot Nothing Then Console.WriteLine(args.Error.Message) Else ' Retrieve a single related entity using a scalar navigation property Dim targetOrder As Order = CType(args.Result.ToList()(0), Order) Console.WriteLine("Order: {0}", targetOrder.OrderID.ToString()) AddHandler targetOrder.Customer.PendingEntityResolved, _ AddressOf Customer_PendingEntityResolved Dim aCustomer As Customer = targetOrder.Customer Console.WriteLine("Customer (from GotOrders): {0}", aCustomer.CompanyName) ' Retrieve a collection of related entities using a collection navigation property AddHandler targetOrder.OrderDetails.PendingEntityListResolved, _ AddressOf OrderDetails_PendingEntityListResolved 'Console.WriteLine("OrderDetails retrieved: {0}", targetOrder.OrderDetails.ToList().Count) End If End Sub Private Sub Customer_PendingEntityResolved(ByVal sender As Object, ByVal e As PendingEntityResolvedEventArgs) Dim customer As Customer = CType(e.ResolvedEntity, Customer) Console.WriteLine("Customer (from Customer_PendingEntityResolved): {0}", customer.CompanyName) End Sub Private Sub OrderDetails_PendingEntityListResolved(ByVal sender As Object, ByVal e As PendingEntityListResolvedEventArgs(Of OrderDetail)) Console.WriteLine("OrderDetails retrieved: {0}", e.ResolvedEntities.Count) End Sub Private Sub ResetEntityManager(ByVal em As EntityManager) em.Clear() em.UseAsyncNavigation = False End Sub

In the methods first statement we set the UseAsyncNavigation property of the EntityManager to true. This step would be unnecessary in a Silverlight application, as true is the default setting for that property in that environment. But the above code could run in both Silverlight and non-Silverlight environments. Now consider the statements that retrieve the Order. For a couple of reasons, we cant simply say this

C# VB

Order anOrder = _em1.Orders.FirstOrNullEntity(); Dim anOrder As Order = _em1.Orders.FirstOrNullEntity()

firstly, because the attempt to execute the above statement would fail in a Silverlight app with a message to the effect that Queries in Silverlight must be executed asynchronously. But in fact it also is
153 | P a g e

IdeaBlade DevForce

Business Object Persistence

not possible at present to execute asynchronously immediate execution queries (of which any query ending with a call to FirstOrNullEntity() is an example). So to get our single Order, we need to submit a query with a condition that retrieves the desired Order, as you saw in the main snippet. That query must, of course, also be submitted asynchronously, and a callback method provided to process the results.

C#

... IEntityQuery<Order> query = _em1.Orders.Where(o => o.OrderID == 10248); _em1.ExecuteQueryAsync<Order>(query, GotOrders, null); ... } private void GotOrder(EntityQueryOperation<Order> args) { if (args.Error != null) { Console.WriteLine(args.Error.Message); } else { // Retrieve a single related entity using a scalar navigation property Order targetOrder = (Order)args.Results.ToList()[0]; Console.WriteLine("Order: {0}", targetOrder.OrderID.ToString()); } }

VB

... Private IEntityQuery(Of Order) query = _em1.Orders.Where(Function(o) o.OrderID = 10248) _em1.ExecuteQueryAsync(Of Order)(query, GotOrders, Nothing) ... private void GotOrder(EntityFetchedEventArgs(Of Order) args) If args.Error IsNot Nothing Then Console.WriteLine(args.Error.Message) Else ' Retrieve a single related entity using a scalar navigation property Dim targetOrder As Order = CType(args.Result.ToList()(0), Order) Console.WriteLine("Order: {0}", targetOrder.OrderID.ToString()) ... End If End Sub

In this case, since were using the primary key to fetch our Order, we know that args.Result will contain at most one entity; so we simply cast it into an Order and proceed. To get the Customer related to that Order (refer back to the full snippet), we set up a handler for the PendingEntityResolved event of the Customer navigation property, targetOrder.Customer. Then to initiate the asynchronous retrieval of that customer, we reference it in a code statement:

C# VB

Customer aCustomer = targetOrder.Customer; Dim aCustomer As Customer = targetOrder.Customer

154 | P a g e

IdeaBlade DevForce

Business Object Persistence

We included a call to Console.WriteLine() immediately following the above statement just to show that the desired Customer simply isnt going to be available at that point. The statement will write out a blank for the Customers CompanyName. Where we will get results is in the Customer_PendingEntityResolved handler:

C#

void Customer_PendingEntityResolved(object sender, PendingEntityResolvedEventArgs e) { Customer customer = (Customer)e.ResolvedEntity; Console.WriteLine("Customer (from Customer_PendingEntityResolved): {0}", customer.CompanyName); } Private Sub Customer_PendingEntityResolved(ByVal sender As Object, _ ByVal e As PendingEntityResolvedEventArgs) Dim customer As Customer = CType(e.ResolvedEntity, Customer) Console.WriteLine("Customer (from Customer_PendingEntityResolved): {0}", _ customer.CompanyName) End Sub

VB

Collection Navigation Properties in Silverlight For navigation properties that return a collection, DevForce provides a PendingEntityListResolved event, similar to the PendingEntityResolved event weve just discussed:

C#
...

private void GotOrder(EntityQueryOperation<Order> args) { // Retrieve a collection of related entities using a collection navigation property targetOrder.OrderDetails.PendingEntityListResolved += new EventHandler<PendingEntityListResolvedEventArgs<OrderDetail>>( OrderDetails_PendingEntityListResolved); } } void OrderDetails_PendingEntityListResolved(object sender, PendingEntityListResolvedEventArgs<OrderDetail> e) { Console.WriteLine("OrderDetails retrieved: {0}", e.ResolvedEntities.Count); }

VB

Private Sub GotOrder(ByVal args As EntityQueryOperation(Of Order)) ... ' Retrieve a collection of related entities using a collection navigation property AddHandler targetOrder.OrderDetails.PendingEntityListResolved, _ AddressOf OrderDetails_PendingEntityListResolved End If End Sub

When we run the full snippet, the code displays the following results in the Console window:

155 | P a g e

IdeaBlade DevForce

Business Object Persistence

The output line Press ENTER to continue.. comes from the utility method PromptToContinue(), which executes synchronously and immedately. Then we see reflected back the OrderID of the retrieved Order; the non-existent CompanyName of the not-yet-retrieved, related Customer; the CompanyName of the Customer written after its retrieval by the Customer_PendingEntityResolved callback method; and the display of OrderDetails retrieved, written by the OrderDetails_PendingEntityListResolved method. Using An Anonymous Method for Navigation Property Callback If youre working in C#, you can also use inline, anonymous methods for your ExecuteQueryAsync() callbacks:

Code Snippet 34. NavigationBasicAsynchronousAnonymousCallback (C# only)

C#

public void NavigationBasicAsynchronousAnonymousCallback() { _em1.UseAsyncNavigation = true; IEntityQuery<Order> query = _em1.Orders.Where(o => o.OrderID == 10248); _em1.ExecuteQueryAsync<Order>( query, // IEntityQuery<Order> (args) => { // AsyncCompletedCallback Console.WriteLine("Order: {0}", // " ((Order)args.Result.ToList()[0]).OrderID); // " }, // " null // UserState object ); PromptToContinue(); }

These are handy when the logic to be included in the callback isnt too involved. VB.NET doesn't support multi-statement lambda expressions or anonymous methods.

Deferred Retrieval
When does the EntityManager fetch myOrders line items from the data source? We might have written DevForce to fetch them automatically when it fetched myOrder. But if DevForce were to get the line items automatically, why stop there? It could get the customer for the order, the sales rep for the order, and the products for each line item. Those are just the immediate neighbors. It could get the customers headquarter address, the sales reps address and manager, and each products manufacturer. If it continued like this, it might fetch most of the database.

156 | P a g e

IdeaBlade DevForce

Business Object Persistence

Retrieving the entire graph is obviously wasteful and infeasible. How often do we want to know the manager of the sales rep who booked the order? Clearly we have to prune the object graph. But where do we prune? How can we know in advance which entities we will need and which we can safely exclude? We cannot know. Fortunately, we dont have to know17. We keep it simple. We use an entity query to get the root entities (such as myOrder). Then we use entity navigation to retrieve neighboring related entities as we need them. This just-in-time approach is called deferred retrieval (also known as lazy instantiation, lazy loading, Just-In-Time *JIT+ data retrieval, and so on).

Proactive Data Loads


Having established that the DevForce default is deferred retrieval, we hasten to add that there are many circumstances when it absolutely makes sense to load data before it is specifically needed to satisfy some demand of the application. Filling a large data grid is an excellent example of such a situation. Suppose youre filling a grid with Orders lots of them and that for each Order you also wish to display the name of the Customer who placed it, the Sales Representative who wrote it, and the Shipping Company that will deliver it. With deferred retrieval, filling a single row of the grid would require three extra trips to the data source one each for a Customer, Employee, and Shipper entity -- above and beyond the one that got all of the Orders to begin with. If the grid were populated with a thousand Orders, there would be three thousand separate (and unnecessary) trips to the data source to retrieve the related entities. You can well imagine that this might negatively impact your applications performance. For circumstances like these where there is an obvious impending need for a great deal of related data, you can add Include() clauses to your data retrieval query to bring back the related data at the same time your retrieve the root data. The following example retrieves selected Customers and a graph of related data: the Customers Orders, the OrderDetails for those Orders, the Products referenced in the OrderDetails, the Suppliers of those Products, and the SalesRep who wrote the Orders:
Code Snippet 35. NavigationSynchronousPreload

C#

IEntityQuery<Customer> query = _em1.Customers.Where(c => c.Country == "France") .Include("Orders") .Include("Orders.OrderDetails") .Include("Orders.OrderDetails.Product") .Include("Orders.OrderDetails.Product.Supplier") .Include("Orders.SalesRep"); _em1.ExecuteQuery<Customer>(query); // _em1.ExecuteQuery(query); // accomplishes the same thing // query.ToList(); // accomplishes the same thing

17

We dont have to know if we can be certain of continuous connection to the data source. If we expect the application to run offline, well have to anticipate the related entities well need and pre-fetch them. Well get to this issue later.

157 | P a g e

IdeaBlade DevForce
VB

Business Object Persistence

Dim query As IEntityQuery(Of Customer) = _ _em1.Customers.Where(Function(c) c.Country = "France") _ .Include("Orders") _ .Include("Orders.OrderDetails") _ .Include("Orders.OrderDetails.Product") _ .Include("Orders.OrderDetails.Product.Supplier") _ .Include("Orders.SalesRep") _em1.ExecuteQuery(Of Customer)(query) ' _em1.ExecuteQuery(query) // accomplishes the same thing ' query.ToList() // accomplishes the same thing

Proactive Data Loads in Silverlight In Silverlight apps, where all data retrieval must be asynchronous, the benefits of preloading data are even more general. In the following snippet, we preload, using a span query, a large object graph for each of a group of Customers who meet a specified condition. Having done so, all of our subsequent queries for entities can be cache-only and synchronous:
Code Snippet 36. NavigationAsynchronousPreload

C#

public void NavigationAsynchronousPreload() { ResetEntityManager(_em1); _em1.UseAsyncNavigation = true; IEntityQuery<Customer> query = _em1.Customers.Where(c => c.Country == "France") .Include("Orders") .Include("Orders.OrderDetails") .Include("Orders.OrderDetails.Product") .Include("Orders.OrderDetails.Product.Supplier") .Include("Orders.SalesRep"); _em1.ExecuteQueryAsync<Customer>(query, GotCustomers, null); PromptToContinue(); } private void GotCustomers(EntityFetchedEventArgs<Customer> args) { if (args.Error != null) { Console.WriteLine(args.Error.Message); } else { DisplayCacheContents(); } } private void DisplayCacheContents() { Console.WriteLine("Contents of Cache"); Console.WriteLine("-----------------"); Console.WriteLine("Customers: {0}", _em1.Customers.With(QueryStrategy.CacheOnly).Count()); Console.WriteLine("Employees: {0}", _em1.Employees.With(QueryStrategy.CacheOnly).Count()); Console.WriteLine("Orders: {0}", _em1.Orders.With(QueryStrategy.CacheOnly).Count()); Console.WriteLine("OrderDetails: {0}", _em1.OrderDetails.With(QueryStrategy.CacheOnly).Count()); Console.WriteLine("Products: {0}", _em1.Products.With(QueryStrategy.CacheOnly).Count()); Console.WriteLine("Suppliers: {0}", _em1.Suppliers.With(QueryStrategy.CacheOnly).Count()); }

VB

Public Sub NavigationAsynchronousPreload() ResetEntityManager(_em1)

158 | P a g e

IdeaBlade DevForce

Business Object Persistence

_em1.UseAsyncNavigation = True Dim query As IEntityQuery(Of Customer) = _em1.Customers _ .Where(Function(c) c.Country = "France") _ .Include("Orders") _ .Include("Orders.OrderDetails") _ .Include("Orders.OrderDetails.Product") _ .Include("Orders.OrderDetails.Product.Supplier") _ .Include("Orders.SalesRep") _em1.ExecuteQueryAsync(Of Customer)(query, AddressOf GotCustomers, Nothing) PromptToContinue() End Sub Private Sub GotCustomers(ByVal args As EntityFetchedEventArgs(Of Customer)) If args.Error IsNot Nothing Then Console.WriteLine(args.Error.Message) Else DisplayCacheContents() End If End Sub Private Sub DisplayCacheContents() Console.WriteLine("Contents of Cache") Console.WriteLine("-----------------") Console.WriteLine("Customers: {0}", _ _em1.Customers.With(QueryStrategy.CacheOnly).Count()) Console.WriteLine("Employees: {0}", _ _em1.Employees.With(QueryStrategy.CacheOnly).Count()) Console.WriteLine("Orders: {0}", _ _em1.Orders.With(QueryStrategy.CacheOnly).Count()) Console.WriteLine("OrderDetails: {0}", _ _em1.OrderDetails.With(QueryStrategy.CacheOnly).Count()) Console.WriteLine("Products: {0}", _ _em1.Products.With(QueryStrategy.CacheOnly).Count()) Console.WriteLine("Suppliers: {0}", _ _em1.Suppliers.With(QueryStrategy.CacheOnly).Count()) End Sub

Here is the output of the above method:

Missing objects
Every order should have a shipping address. What if it doesnt? Will myOrder.ShippingAddress.City throw an exception? Will we have to wrap every entity navigation in a giant try/catch block? Will it return null? Will we have to follow every entity navigation with a test for null? That might be worse than catching an exception.
159 | P a g e

IdeaBlade DevForce

Business Object Persistence

Fortunately entity navigation neither returns a null nor throws an exception. Instead, when the EntityManager discovers there is no shipping address, it returns the Address Null Entity.

The Null Entity


The null entity is a sentinel object that looks and behaves, for the most part, like a real entity instance. Every entity class defines its own null entity instance. When a query such as anEntityManager.DiscontinuedProducts must return an entity and it has no valid entity instance to return, it returns a null entity of the requested type instead. When a navigation property should return a related entity instance and there is no such instance, it will return a null entity instead. This is far better than returning a null (Nothing in VB). The caller cant do a thing with null and may even crash. The null entity, on the other hand, has the properties of a real entity instance. For example, it can report its type and the EntityManager that owns it18. All cached entities answer to IsNullEntity; only a null entity replies true. Most of its properties return runtime safe but semantically empty values that can be displayed in a UI. If anEmployee is a null entity, for example, the expression anEmployee.FirstName returns an empty string. The navigation property anEmployee.Orders returns an empty IList<Order>. The navigation property anEmployee.HomeAddress returns the Address null entity. This means we can write a long expression such as anEmployee.HomeAddress.State.Name without throwing an exception. In this case the Address null entitys State navigation property returns a State null entity whose Name property returns an empty string. The null entity cannot be changed, deleted, or saved. But the savvy developer can redefine a null entitys default property responses by overriding the UpdateNullEntity() method in the entitys Developer class19. She could change the Address.City property, for example, so that it returns the string <unknown>.

Asynchronous Communication with the Business Object Server


The EntityManager supports asynchronous versions of methods which communicate with the BOS. These methods include:

18 19

ConnectAsync LoginAsync LogoutAsync ExecuteQueryAsync

Like real cached entities, null entities must belong to a EntityManager and, in fact, are created by a EntityManager This method is inherited from the root business object class, Entity.

160 | P a g e

IdeaBlade DevForce
ExecuteQueryAsync<T> SaveChangesAsync ForceIdFixupAsync RefetchEntitiesAsync InvokeServerMethodAsync

Business Object Persistence

Asynchronous communication with the BOS is considerably more complicated than synchronous; but alas, it is the law of the land in Silverlight applications. So, for those many of you who are working in that environment, we are addressing the topic here, rather than in the Business Object Persistence Advanced document. The EntityManager supports a hybrid of the .NET event-based asynchronous pattern20 for these asynchronous methods. We refer to it as a hybrid because corresponding events have not been defined for these methods. So instead of subscribing to an event to receive notification about the completion status, you can instead pass a method-specific callback as part of the call. You can identify this hybrid pattern by the OperationNameAsync naming convention. See, for example, the code below to submit a query asynchonously.

Asynchronous Queries
Youve seen asynchronous queries earlier in this document, but here we revisit them with a slightly more formal treatment, and in the context of other asynchronous communications with the Business Object Server. The following code defines an EntityQuery and launches it asynchronously, assigning the result set to a list in the operations callback method:
Code Snippet 37. BOSCom_AsyncQuery

C#

private void BOSCom_AsyncQuery() { ResetEntityManager(_em1); var query = new EntityQuery<Customer>() .Where(c => c.Country == "Denmark"); int token = 1; _em1.ExecuteQueryAsync<Customer>(query, QueryCompletedCallback, token); } private void QueryCompletedCallback(EntityQueryOperation<Customer> args) { var resultList = args.Results; Console.WriteLine("Query returned {0} entities", resultList.Count()); }

20

The standard .NET Event-based Asynchronous Pattern is described in described in an article at this URL:
http://msdn2.microsoft.com/en-us/library/wewwczdw(en-US,VS.80).aspx

161 | P a g e

IdeaBlade DevForce
VB

Business Object Persistence

Private Sub BOSCom_AsyncQuery() ResetEntityManager(_em1) Dim query = New EntityQuery(Of Customer)().Where(Function(c) c.Country = "Denmark") Dim token As Integer = 1 _em1.ExecuteQueryAsync(Of Customer)(query, AddressOf QueryCompletedCallback, token) End Sub Private Sub QueryCompletedCallback(ByVal args As EntityQueryOperation(Of Customer)) Dim resultList = args.Results Console.WriteLine("Query returned {0} entities", resultList.Count()) PromptToContinue() End Sub

As youve seen previously, in C#, you have the additional option of passing a lambda expression for the callback instead of defining a separate method:
Code Snippet 38. BOSCom_AsyncQueryLambda

C#

private void BOSCom_AsyncQueryLambda() { var query = new EntityQuery<Customer>().Where(c => c.Country == "Denmark"); int token = 2; _em1.ExecuteQueryAsync<Customer>( query, (args) => { var resultList = args.Results; Console.WriteLine("Query returned {0} entities", resultList.Count()); }, token); PromptToContinue(); }

The signature for the above queries is as follows:

C#

public EntityQueryOperation<T> ExecuteQueryAsync<T>(IEntityQuery<T> query, Action<EntityQueryOperation<T>> userCallback, object userState = null);

VB

You can run multiple ExecuteQueryAsync operations simultaneously. The final parameter, userState, is a unique object created by the developer to identify the async query. When a query completes, the UserState is returned to the caller as part of the EntityQueryOperation argument so she can distinguish one query from another. The UserState object can be as simple as an integer, or it can be an arbitrarily complex custom type. Completed Queries The EntityQueryOperation parameter passed into an async querys callback method contains the following members:
162 | P a g e

IdeaBlade DevForce

Business Object Persistence

Property
Cancelled

Description True if the query was canceled. Cancellation of an async operation can be ordered by a call to EntityQueryOperation.Cancel (). Such cancellation only succeeds if the order is received in time. An IList containing every entity added to or modified in the EntityManager cache. An Exception object, of an exception was thrown during the async operation. True if the operation completed successfully. True if the operation was executed synchronously and completed successfully. A query, for example, will execute synchronously if DevForce determines that it can be satisfied entirely from the EntityManager cache. The IEntityQuery<T> object used in the asynchronous operation. An IEnumerable<T> of returned objects. The object passed in the async call uniquely to identify the operation.

ChangedEntities Error IsCompleted IsCompletedSynchronously

EntityQuery Results UserState

IAsyncResult Asynchronous Pattern


For those needing additional control over their asynchronous operations, the EntityManager also supports the IAsyncResult asynchronous pattern through an explicit implementation of the IEntityManagerAsync interface. You will need to cast an EntityManager to this interface in order to use methods following this pattern. In the IAsyncResult pattern an asynchronous operation is implemented as two methods named BeginOperationName and EndOperationName to begin and end the asynchronous operation "OperationName". More information on using this interface is available in the IdeaBlade DevForce Reference Help, available from the IdeaBlade DevForce Windows Start menu.

Asynchronous Fulfillment of Navigation Property Queries


DevForce returns data for navigation properties (such as Order.Customer or Order.OrderDetails) by issuing queries. Explicit queries in your DevForce app can be written using the asynchronous method calls detailed above, but control over the fulfillment of navigation properties must be exercised in a different manner. The EntityManager now has a boolean UseAsyncNavigation property that can be set to specify that navigation properties should be fulfilled using asynchronous queries. When reference is made to a navigation property, DevForce returns either an entity (if the property is scalar) or a RelatedEntitiesList<T> (for collection properties). Entities now have an IsPendingEntity property;
163 | P a g e

IdeaBlade DevForce

Business Object Persistence

When EntityManager.UseAsyncNavigation is set to true, the entities initially returned for scalar properties, and the RelatedEntityLists returned for collection properties, will have a pending state until the asynchronous query issued for their fulfillment actually returns data. This state can be diagnosed with one of the following properties:

Entity.IsPendingEntity RelatedEntityList<T>.IsPendingEntityList

Entities and RelatedEntityLists also now have events that fire when the data for pending entities is returned. These are:

Entity.PendingEntityResolved RelatedEntityList<T>.PendingEntityListResolved Handlers can be attached to these event to perform actions when the data for pending entities becomes available to your app.

Canceling Pending Operations


We may attempt to cancel an asynchronous operation by calling the Cancel() method on the operation object returned by the asynchronous call. The caller can confirm that the query was successfully canceled by checking the Cancelled parameter of the EventArgs object; it should read true.

The EntityListManager
Instances of IdeaBlade.EntityModel.EntityListManager<T> watch the DevForce cache for changes and add entity references to designated lists if such changes meet developer-defined rules. Consider the following code:
Code Snippet 39. SetUpEntityListManager

C#

var filter = new Predicate<Employee>( delegate(Employee anEmployee) { return anEmployee.City == "London"; }); _employeeEntityListManager = new EntityListManager<Employee>(_em1, filter, null); bool refreshListWhenPlacedUnderManagement = true; _employeeEntityListManager.ManageList(_salesReps, refreshListWhenPlacedUnderManagement);

VB

Dim filter = New Predicate(Of Employee)( _ Function(anEmployee As Employee) anEmployee.City = "London") _employeeEntityListManager = New EntityListManager(Of Employee)(_em1, filter, Nothing) Dim refreshListWhenPlacedUnderManagement As Boolean = True _employeeEntityListManager.ManageList(_salesReps, refreshListWhenPlacedUnderManagement)

This code sets up an EntityListManager to watch the cache for changes to Employees, or the insertion of new Employees. If any changed or new Employee is found to be based in London, a reference to that
164 | P a g e

IdeaBlade DevForce

Business Object Persistence

Employee will be added to the _salesReps list. At the same time, _employeeEntityListManager will inspect all items in the _salesReps list to see that they meet the specified rule about London. The only requirements for _salesReps are that it
implement System.Collections.IList; and contain instances of IdeaBlade.EntityModel.Entity.

A single EntityListManager can manage as many different lists as you wish. To put _employeeEntityListManager in charge of additional lists, you would simply invoke its ManageList() method again for each desired list:

C#

_employeeEntityListManager.ManageList(_telecommuters, false); _employeeEntityListManager.ManageList(_fieldAgents, false);

VB

_employeeEntityListManager.ManageList(_telecommuters, False) _employeeEntityListManager.ManageList(_fieldAgents, False)

Of course, it only makes sense to do this when the same inclusion criteria apply to each targetted list. In additions to changes to the cache, changes to a managed list trigger action by the managing EntityListManager. Thus, any of the follows statements will cause _employeeEntityListManager to examine the current contents of the cache and add references to all London employees to the _salesReps list:

C#

_salesReps.Add(anEmployee); _salesReps.Remove(anEmployee); _salesReps.Clear();

VB

_salesReps.Add(anEmployee) _salesReps.Remove(anEmployee) _salesReps.Clear()

In the case of the statement _salesReps.Clear(), you will not end up with an empty list unless you first remove _salesReps from the list of lists being managed by employeeEntityListManager. Removing an entity that the rule says should be included also will not result in the entity disappearing from the list. The EntityListManager will just put it right back! In general, beware of making manual changes (adds or removals) to the set of items contained in a managed list. EntityListManagers and The NullEntity One exception occurs when you want the NullEntity for the type contained in a list to be included. NullEntities are singletons and do not reside in the cache, so there is no way that an EntityListManager

165 | P a g e

IdeaBlade DevForce

Business Object Persistence

will ever find one there to add a reference to! If you want the NullEntity in a managed list, you should manually add it. The ListManager will not remove it. EntityListManagers and Duplicates The EntityListManager will not eliminate duplicates from a list. For example, suppose you direct the following statement against a list, _salesReps, that is already being managed to include Employees based in London:

C# VB

_salesReps.ReplaceRange(_entityManager.Employees.Where(e=>e.City == "London"));

_salesReps.ReplaceRange(_entityManager.Employees.Where(e=>e.City == "London"))

You will end up with duplicate references to each of the London employees! EntityListManagers and Performance EntityListManagers do create a certain amount of overhead, so be judicious in their use. It is also possible to narrow their scope of what they must monitor more than we did in our examples above. We instantiated our EntityListManager as follows:

C#

var filter = new Predicate<Employee>( delegate(Employee anEmployee) { return anEmployee.City == "London"; }); _employeeEntityListManager = new EntityListManager<Employee>(_entityManager, filter, null);

VB The third argument, which we left null, is an array of EntityProperty objects. By leaving it null, we told the manager to submit any added or modified Employee to the test encoded in the filter Predicate. Suppose that, instead, we pass a list of properties of the Employee to this argument:

C#

new EntityListManager<Employee>(_entityManager, filter, new EntityProperty[]{Employee.CityEntityProperty});

VB

_employeeEntityListManager = New EntityListManager(Of Employee)(_em1, filter, _ New EntityProperty() {Employee.CityEntityProperty})

166 | P a g e

IdeaBlade DevForce

Business Object Persistence

Now the EntityListManager will apply its test (about City being equal to London) only to an Employee whose City property, specifically, was modified. If you simply change only the Birthdate of an Employee already in the cache, the rule will not be evaluated. It can, after all, be safely assumed that said Employee would already be in the lists being managed if the value in its City property were London. Coding More Involved Rules In the examples above we passed an anonymous delegate to the constructor of the Predicate filter. Thats great for simple rules, but you can declare the predicate separately if you need to do something more involved. This also gives you a chance to name the rule, which can make your code more readable. Heres a simple example:
Code Snippet 40. SetUpEntityListManagerWithNamedDelegate

C#

private void SetUpEntityListManagerWithNamedDelegate() { // Identify Customer currently being edited by some process; // this is a stand-in. _currentCustomer = _em1.Customers.FirstOrNullEntity(); EntityListManager<Order> orderEntityListManager = new EntityListManager<Order>(_em1, FilterOrdersByDate, new EntityProperty[] { Order.OrderDateEntityProperty, Order.CustomerEntityProperty } ); } /// <summary> /// This rule gets the 1996 Orders for the current Customer /// </summary> /// <param name="pOrder"></param> /// <returns></returns> Boolean FilterOrdersByDate(Order pOrder) { return (pOrder.OrderDate.Value.Year == 1996 && pOrder.Customer == _currentCustomer); }

VB

Private Sub SetUpEntityListManagerWithNamedDelegate() ' Identify Customer currently being edited by some process; ' this is a stand-in. _currentCustomer = _em1.Customers.FirstOrNullEntity() Dim orderEntityListManager As New EntityListManager(Of Order)(_em1, _ AddressOf FilterOrdersByDate, New EntityProperty() { _ Order.OrderDateEntityProperty, Order.CustomerEntityProperty}) End Sub ''' <summary> ''' This rule gets the 1996 Orders for the current Customer ''' </summary> ''' <param name="pOrder"></param> ''' <returns></returns> Private Function FilterOrdersByDate(ByVal pOrder As Order) As Boolean Return (pOrder.OrderDate.Value.Year = 1996 AndAlso pOrder.Customer.Equals(_currentCustomer)) End Function

167 | P a g e

IdeaBlade DevForce

Business Object Persistence

Entity Caching
There are at least three good reasons to cache business objects:
1. 2. 3. The connection to the server may break during a session Writing business object changes directly to the data source is impractical and often unwise. In real life applications, the same entities are retrieved repeatedly; it wastes time and resources to bother the server with redundant requests for the same entities.

Each DevForce EntityManager has its own, private entity cache that:
holds all retrieved and newly created entities; is searchable by query and object navigation; tracks cached entity changes, deletions and additions; insulates the developer from cache mechanics; enables the developer to control how entities are fetched and merged into the cache; raises events when entities are fetched, changed, deleted, or added; permits the developer to manipulate the cache when necessary; can be persisted to and retrieved from client storage.

All Business Objects are Cached


We always create or retrieve a business object into the cache of a particular Entity Manager instance. Every business object can report to which Entity Manager instance it belongs. The developer can rummage around in its cache discovering and manipulating the business objects therein.

Entity Ancestry and Organization of the Cache


The business object developer class inherits from IdeaBlade.EntityModel.Entity. Entity itself inherits from IdeaBlade.EntityModel.EntityWrapper, which implements several interfaces. These include: IEditableObject IRevertibleChangeTracking IChangeTracking INotifyPropertyChanged INotifyDataErrorInfo IComparable ICloneable
168 | P a g e

IdeaBlade DevForce

Business Object Persistence

Within the EntityManager cache, an Entity instance lives in an IdeaBlade.EntityModel.EntityGroup, which lives within an EntityCache. You may find that you rarely need to interact directly with an EntityGroup; and virtually all of the metadata you will ever need about an entity can be accessed through the Entitys EntityAspect.EntityMetaData property. Public properties and methods of that include the following:

Member Type Property Property

Name

Function

EntityType IsComplexType

Gets the Type of the entity Returns whether this metadata describes a "ComplexObject"

Property Property

DataSourceKeyName DefaultEntitySetName

Gets the data source key name. The default EntitySetName for entities of this type.

Property

EntityProperties

Returns a collection of EntityProperties that belong to entities of this type. Returns a collection of DataEntityProperties for entities of this type. Returns a collection of DataEntityProperties for entities of this type. Returns a collection of EntityProperties that are keys for entities of this type. Returns a collection of EntityProperties that are concurrency properties for entities of this type. Returns a collection of EntityProperties that describe complex object properties for entities of this type.

Property

DataProperties

Property

NavigationProperties

Property

KeyProperties

Property

ConcurrencyProperties

Property

ComplexTypeProperties

169 | P a g e

IdeaBlade DevForce
Property CanQueryByEntityKey

Business Object Persistence


Gets whether primary key queries are allowed.

Method

CreateEntity()

Creates a new entity of the type describe by this metadata item. Returns the default value of a type: usually '0' or null for any data type. Note that this is subtly different from the TypeFns.GetDefaultValue method in that it returns Today for a default date time.

Method

GetDefaultValue(Type pType)

Business objects are unique in each cache


DevForce persistence management ensures that each business object appears at most once in a particular EntityManager cache. No matter how many times the employee Nancy Davolio is read into the cache, she appears at most once. Within the application, a reference to any Nancy Davolio employee object is a reference to the same one employee object. If we change her first name to Sue, she becomes Sue everywhere in the session unless unless there is more than one EntityManager instance21. Each EntityManager instance maintains its own independent cache. The Nancy Davolio retrieved into EM1 is not the same object as the Nancy Davolio retrieved into EM2, even though they are both mapped to the same row in the Employee table of the database. Changes to a copy of a business object in one cache are invisible to other copies in other caches both in this client and in all other clients. Changes become visible to other caches only after the object is saved to the data source and re-fetched to those caches.

Entities in Lists
Entities in lists are always references to entities in the EntityManagers cache. This is true whether the EM maintains the list or you maintain the list. In general we prefer to work with only one list of entities of a particular type. But it may be useful to have two such lists that are a little different. For example, one list could hold all employees of the company while the second list holds the subset of those employees who are managers. Both lists contain references to the same employee instances in cache but they are very different lists.
21

Multiple EntityManagers have their place but most applications will need only one. Multiple EMs are covered in Advanced Business Object Concepts.

170 | P a g e

IdeaBlade DevForce

Business Object Persistence

If we change the Employee A who happens to be a manager, we are also changing the Employee A in the general employee list. They are the same Employee A. If follows that if the PM re-fetches a clean copy of Employee 'A' from the data source, the pending changes will disappear for all viewers of Employee A whether they are looking at A in the first list or in the second list.

Business object proper, not the business object graph


When speaking of a business object held in cache, we may easily lose sight of what we mean by a business object. We distinguished earlier between the business object proper, which encapsulates the simple, scalar values stored in the objects base table, and the business object graph which embraces the entire network of other business objects to which it is related. For example, the simple Employee properties such as FirstName and LastName access data values that are stored in the Employee table; these are properties of the employee business object proper. The HomeAddress navigation property, on the other hand, delivers a related business object, the employees home address. The data values of the address come from a different table (Address) and belong to the address business object proper, not the employee per se. An EntityManager instance retrieves and holds business objects proper, not their graphs. Objects in the graph of a particular business object may be in the cache. Or they may not. They dont enter the cache simply by virtue of being in another objects graph. The employees home address object will not enter the cache just because we retrieved the employee object. It will enter the cache after we execute an expression such as anEmployee.HomeAddress.

Queries, Navigation, and the Cache


Weve covered entity queries and entity navigation. Although entity queries make explicit reference to the EntityManager, we learned that entity navigation is also performed by the EntityManager. Here we explain how the EntityManager processes both explicit entity queries and the implicit queries inside entity navigation syntax. We will see that EM query processing is guided by a query strategy. When following the default, normal strategy, the EM tries first to satisfy a query from data in its cache; it reaches out to the data source only if it must.

Query Cache
When a EntityManager begins to process a normal query, it checks its query cache to see if it has processed this exact query before.

171 | P a g e

IdeaBlade DevForce

Business Object Persistence

The query cache holds queries and is not the same as the entity cache which holds objects and is what we usually mean when we refer to the cache.

If the EntityManager finds the query in the query cache, it assumes that the objects which satisfy the query are in the entity cache; accordingly, it satisfies the query entirely from the cache without consulting the data source. A one-to-many entity navigation, such as from employee to the employees orders, is translated implicitly to an entity query language (OQL) query that also enters the query cache. The next time the application navigates from that same employee to its orders, the EntityManager will recognize that it has performed the query before and look only in the cache for those orders. The query cache grows during the course of a session. Certain operations clear it as one of their sideeffects; removing an entity from the cache is one such operation. The developer can also clear the query cache explicitly. We just said that the EntityManager searches the query cache for an exact match of the current query, but that was really a little white first approximation. Actually, the EntityManager does better than that: it searches either for an exact match, or for an unrestricted query returning the same type. If, for example, you have previously retrieved all Customers and now ask for Customers from Canada, your new query will be satisfied from the cache.

Primary key queries


A query for business objects by primary key may be resolved entirely in the cache. If we search22 for the employee with Id = 1 the EntityManager will try to find it in the cache and, if not found there, will only then look for it in the data source. The EntityManager treats navigation along a one-to-one relationship, such as from Employee to HomeAddress, as a primary key query. Navigation in the parent direction along a one-to-many relationship, such as from an OrderDetail to its parent Order, is also a primary key query.

Object Not Found and the Null Entity


When we search for an entity and do not find it, the EntityManager, rather than returning a null that may cause an exception in your application, returns a sentinel object called the Null Entity. Such a sentinel behaves much like a real entity of the sought-for type except that it cant be changed, deleted, or saved. Every business object class defines its own null entity. See The Null Entity elsewhere in the section on queries and navigation.

Cache use when disconnected


When the EntityManager knows it is disconnected from the server, it will satisfy a navigation, or a query submitted with the Normal QueryStrategy, from the cache alone; it will not attempt to search the

22

If we use the default QueryStrategy; we are just about to discuss QueryStrategy so bear with me.

172 | P a g e

IdeaBlade DevForce

Business Object Persistence

data source. If a sought-for object is not in the cache, the EntityManager will return the Null Entity for objects of that type. The EntityManager raises an exception if it discovers during query processing that it cant reach the data source; see the Lost Connections topic in the Advanced Business Object Concepts section below.

Modifications
Each business object carries a read-only EntityState property that indicates if the object is new, modified, marked for deletion, or unchanged since it was last retrieved. It bears repeating that our local modifications affect only the cached copy of a business object, not its version in the data source. The data source version wont be updated until the application tells the EntityManager to save the changed object. It follows that the data source version can differ from our cached copy either because we modified the cached copy or because another user saved a different version to the data source after we retrieved our copy. It would be annoying at best if the EntityManager overwrote our local changes each time it queried the data source. Fortunately, in a normal query, the EntityManager will only replace an unmodified version of an object already in the cache; our modified objects are preserved until we save or undo them.

Stale Entity Data


All of this is convenient. But what if another user has made changes to a cached entity? The local application is referencing the cached version and is unaware of the revisions. For the remainder of the user session, the application will be using out-of-date data. The developer must choose how to cope with this possibility. Delayed recognition of non-local changes is often acceptable. A list of U.S. States or zip codes is unlikely to change during a user session. Employee name changes may be too infrequent and generally harmless to worry about. In such circumstances the default caching and query behavior is fine.
If concurrency checking is enabled and the user tries to save a changed object to the data source, DevForce will detect the collision with the previously modified version in the data source. The update will fail and DevForce will report this failure to the application which can take steps to resolve it.

Some objects are so volatile and critical that the application must be alert to external changes. The developer can implement alternative approaches to maintaining entity currency by invoking optional DevForce facilities for managing cached objects and forcing queries that go to the data source and merge the results back into the cache. The facilities for this are detailed in the section Query Strategy further on in this chapter.

173 | P a g e

IdeaBlade DevForce Fetch Life Cycle Events

Business Object Persistence

DevForce raises the client-side Querying event as query execution begins. The Fetching event is raised before sending the query to the EntityServer; note that if a query can be satisfied from cache then Fetching is not raised. The Queried event is raised just before returning query results. We can listen to these events by attaching a custom handler. The Fetching event provides the query object. Our handler can examine the object (it implements IEntityQuery) and choose to let the query through, modify it first, or cancel it. If we cancel the query, the Entity Manager method returns as if it found nothing23. The Queried event fires just before the query method returns. Entities have been fetched and merged into the cache. The event arguments include the list of entities that came from the data source. There might be none if the query found nothing or was satisfied entirely from the cache. It could include entities of the target entity type the kind we expected returned from the query. It could include entities of other types as is likely if this is a span query or if the query provoked query inversion24.

Query Workflow
Putting these points together, we can construct a schematic workflow for normal25 DevForce entity queries and entity navigation when the application is connected to the Business Object Server (BOS) running on its own physical tier.

Table 1. Entity Query and Navigation Workflow When QueryStrategy = Normal

Component Client Tier Application Code Client Tier EntityManager

Action The client application requests a particular set of entities (the desired entities) either by entity query or by entity navigation Raises Fetching event. Listeners can see the query and, optionally, cancel the query. Checks if it can satisfy the query with the entities in the client-side cache. If so, it returns them immediately; end of workflow. If not, the EntityManager sends the query along with authentication information to the Business Object Server (BOS) on the middle tier. It may modify the request before sending to the BOS if it can determine that some of desired entities

23

If the method returns a scalar entity, it yields the return entity types Null Entity; otherwise, it returns a null entity list. Beware of canceling an entity navigation list query method Span queries are later in this section. We cover Query Inversion in the Advanced Business Object Concepts. The workflow is different in a few places when we use a different QueryStrategy. See the QueryStrategy topic under Advanced Business Object Concepts.

24 25

174 | P a g e

IdeaBlade DevForce
are already in the client side cache. The BOS authenticates the client (the currently logged in user) and runs any developer-specified security checks in the AuthorizeQuery handler. If security checks fail, it raises a security exception and sends this back to the client tier. Having passed security checks, the BOS converts the query into one or more LINQto-Entities queries in the form expected by the ADO.NET Entity Framework. If a relational database is the data source, the Entity Framework converts the LINQ to Entities query into one or more SQL queries and submits them to the data source query mechanism. The data source performs the query or queries and returns one or more result sets back to the Business Object Server. The Entity Framework converts the result sets returned from the data source into ADO.NET entities and delivers them to the EntityServer. The EntityServer repackages the entities obtained from the data source into a format that can be transmitted efficiently. It then ships the entity data to the client side application. After transmission, the BOS allows the servers local copy of the entities to go out of scope and the garbage collector reclaims them. This enables the BOS to stay stateless. Client Tier EntityManager: Compares fetched entities to entities already in the cache. Adds new entities to the cache. Replaces matching cached entities that are unmodified (in essence refreshing them). Preserves cached entities with pending modifications because the query strategy is normal. Client Tier EntityManager: Reapplies the original query to the cache to locate all desired entities. Client Tier EntityManager: Raises the Fetched event. Listeners can examine the list of entities actually retrieved from the data source. Client Tier EntityManager: Returns the desired entities to the application. Client Tier Application Code: The entities are available for processing.

Business Object Persistence

Middle Tier - Business Object Server

Middle Tier - Business Object Server

Data source Data Source Middle Tier - Business Object Server

Middle Tier Business Object Server

Middle Tier Business Object Server

Client Tier EntityManager

Client Tier Application Code

The application developer may proceed blissfully unaware of all this effort.

175 | P a g e

IdeaBlade DevForce

Business Object Persistence

Query Strategy
When the EntityManager performs a query, it follows a query strategy. That strategy determines several things, chief among them these: the source of the data returned in a query; how data obtained from a source external to the EntityManager cache is merged with existing data in the cache; and how issues related to satisfaction of the query from the cache are handled.

The QueryStrategy is a settable property of the query itself:


Code Snippet 41. QueryStrategyAssortedSyntaxExamples

C#

EntityQuery<Order> query01 = _em1.Orders; query01.QueryStrategy = QueryStrategy.DataSourceThenCache; Dim query01 As EntityQuery(Of Order) = _em1.Orders query01.QueryStrategy = QueryStrategy.DataSourceThenCache

VB

In addition, every EntityManager has a DefaultQueryStrategy that is used whenever you do not explicitly specify the query strategy you want to use with a particular query. You can also change this default:

C# VB

_em1.DefaultQueryStrategy = QueryStrategy.Normal; _em1.DefaultQueryStrategy = QueryStrategy.Normal

Entity navigation (e.g., myEmployee.Orders) is implemented with relation queries governed by the DefaultQueryStrategy. In addition, any query whose QueryStrategy property has a value of null will be executed with the DefaultQueryStrategy for the EntityManager underwhich it is run.

176 | P a g e

IdeaBlade DevForce

Business Object Persistence

The QueryStrategy object has four properties: FetchStrategy, MergeStrategy, InversionMode, and TransactionSettings. The FetchStrategy controls where DevForce looks for the requested data: in the cache, in the datasource, or in some combination of the two. The MergeStrategy controls how DevForce resolves conflicts between the states of objects which, although already in the cache, are also retrieved from an external source. The InversionMode controls whether DevForce attempts to retrieve objects that are referenced in the query but are not the target type (e.g., the query give me all Customers with Orders in the current year will return references to Customer objects, but must process Order objects along the way). The TransactionSettings object permits you to control the TimeOut and IsolationLevel associated with a query, and also whether and how to use the Microsoft Distributed Transaction Coordinator. There are five static (Shared in VB) properties in the IdeaBlade.EntityModel.QueryStrategy class that return the five most common combinations of a FetchStrategy, a MergeStrategy, and an InversionMode. These will be named and discussed momentarily, but are much easier to understand after examining the available FetchStrategy, MergeStrategy, and InversionMode options.

Fetch Strategies
Five FetchStrategies are available in DevForce:

Table 2. FetchStrategies

Strategy
CacheOnly

Action Apply this query against the cache only, returning references only to entities already there. Do not consult the data source. (Note that this query leaves the cache unchanged.) Retrieve matching entries from the datasource into the entity cache. Return references only to those entities retrieved from the the data source. A result set returned from a query using this FetchStrategy would not include locally added entities that had not yet been persisted to the data source. First retrieve matching entries from the datasource into the entity cache. Discard all references to entities retrieved in this step. Resubmit the same query against the updated cache. Return references only to entities matched by this second, CacheOnly query. First retrieve matching entries from the datasource into the entity cache. Retain references to entities retrieved in this step. Resubmit the same query as CacheOnly. Combine (union) the references obtained in this second, CacheOnly query with those obtained in the data source retrieval step. Check the query cache to see if the current query has previously been submitted (and, if necessary, inverted) successfully. If so, satisfy the query from the entity cache, and skip the trip to the datasource. 177 | P a g e

DataSourceOnly

DataSourceThenCache

DataSourceAndCache

Optimized

IdeaBlade DevForce

Business Object Persistence


If the query cache contains no query matching or encompassing the current query, then determine if all entities needed to satisfy the query correctly from the cache can be retrieved into the cache.26 If so, apply the DataSourceThenCache FetchStrategy. Otherwise, apply the DataSourceOnly FetchStrategy.

Operation of the FetchStrategies When the Client is Disconnected from the Data Source If the client is disconnected from the data source, the DataSourceOnly, DataSourceThenCache, and DataSourceAndCache strategies will throw an InvalidOperationException. The Optimized strategy will behave as a CacheOnly query. It will not throw an exception, even if no matching query exists in the query cache.

MergeStrategies
A MergeStrategy comes into play whenever DevForce discovers that an entity retrieved from an external source already exists in the entity cache. (The two versions are recognized as the same entity because of matching type and primary key value.) The MergeStrategy determines how DevForce will resolve any conflict found in the two instances of the entity.27 DevForce supports five different MergeStrategies: PreserveChanges, OverwriteChanges, PreserveChangesUnlessOriginalObsolete, PreserveChangesUpdateOriginal, and NotApplicable. Their meanings are shown in Table 3. When reviewing the table, remember that, for every cached DevForce entity, two states are maintained: Original and Current. The Original state comprises the set of values for all properties as they existed at the time of the last retrieval from, or save to, the datasource. The Current state comprises the set of values for the objects properties as the end user sees them. That is, the Current state values reflect any local changes that have been made since the entity was retrieved, or last saved. When an entity is persisted, it is the values in its Current state that are saved.
Table 3. MergeStrategies

Strategy
PreserveChanges OverwriteChanges PreserveChangesUnless OriginalObsolete

Action when cached entity has pending changes


Preserves the state of the cached entity. Overwrites the cached entity with data from the data source. Sets the EntityState of the cached entity to Unchanged. Preserves the values in the Current state of the cached entity, if its Original state matches the state retrieved from the datasource. If the state as retrieved from the datasource differs from that found locally in the Original set of property values, this indicates that the entity has been changed externally by another user or process. In this case (with this MergeStrategy), DevForce overwrites the local entity,

26 27

See the discussion on query inversion for more detail. Conflicts are diagnosed by comparing the values in the entitys designated Concurrency column.

178 | P a g e

IdeaBlade DevForce

Business Object Persistence


setting the values in both its Current and Original states to match that found in the datasource. DevForce also then sets the EntityState of the cached instance to Unchanged. Unconditionally preserves the values in the Current version for the cached entity; and also updates the values in its Original version to match the values in the instance retrieved from the datasource. This has the effect of rendering the local entity savable (upon the next attempt), when it might otherwise trigger a concurrency exception. This merge strategy must be used and may only be used with the CacheOnly fetch strategy. No merge action applies because no data is retrieved from any source outside the cache.

PreserveChangesUpdateOriginal

NotApplicable

We drill deeper into the topic of merge strategies in the section MergeStrategy In More Detail much later in this chapter. We suggest you defer reading that at least until youve completed this section on Query Strategy so you dont miss the big picture.

InversionMode
Query inversion applies to queries which: a) are directed against a data source, and b) though returning references to instances a single business object type, or a scalar simple type, must process other types in order to acquire the result. For example, the query get me all Customers with Orders in the current year will return references to Customer objects, but must first examine many Order objects in order to return the correct set of Customers. The query give me the count of Customers located in Idaho will return an integer, but must examine the Customer collection in the data source. Query inversion is the process of retrieving those non-targeted objects that are nonetheless necessary for correct completion of a query. The most fundamental reason for doing query inversion is so that the query can be applied against a pool of data that combines unpersisted local data with data that exists in the datasource. This is, after all, what your end user normally wants: query results based on the state of the data as she has modified it. The only place that combined pool of data can exist, prior to persisting changes, is the local cache. Therefore the query must ultimately be applied against the cache; and that operation, if it is to return correct results, requires the cache to contain all entities that must be examined in the course of satisfying the query. So to satisfy the query get me all Customers with Orders in the current year, the cache must contain not only the Customers to which references will be returned, but also all extant current-year Orders, so we can know which Customers those are. A handy side-effect of inverting queries is that the same query, if resubmitted during the same application session, can be satisfied entirely from the cache, without requiring another trip to the datasource. Another results from the fact that there is a reasonably good statistical chance that the
179 | P a g e

IdeaBlade DevForce

Business Object Persistence

related objects needed for satisfaction of the query will also be referenced in other ways by the application. In this very common scenario, the effect of the extra data retrieved is to improve client-side performance by eliminating the need for separate retrieval of the related objects. Note that the end result of a query inversion process is very similar to that which occurs when the .Include() method is used in a query. Both processes result in the retrieval and local storage of objects that are related to a set of root objects that are the primary target of a particular query. Four InversionModes are available in DevForce for a query:
Table 4. InversionModes

Strategy
On

Implicit Instuctions to DevForce Attempt to retrieve, from the datasource and into the cache, entities other than the targetted type which are needed for correct processing of the query. If this attempt fails, throw an exception. Do not attempt to retrieve entities other than the targetted type into the cache. Attempt to retrieve, from the datasource and into the cache, all entities other than the targetted type which are needed for correct processing of the query. However, if this attempt fails, just retrieve the entities of the directly targetted type, and do not throw an exception. Dont attempt to invert the current query; but act as if it were successfully inverted (if it needed to be). You (the developer) should only use this InversionMode when you are prepared to guarantee, on your own, that the entity cache contains (or will contain, after the DataSource portion of the query operation) all the necessary related objects to return a correct result if submitted against the cache. Normally you would make good on this guarantee by performing other data retrieval operations (prior to the one in question) to retrieve the necessary related data; or by including calls to the Include() extension method in the current query, sufficient to retrieve the necessary related data.

Off Try

Manual

The default InversionMode is Try, and this will likely be your choice for most queries. You should use On only if your application absolutely depends upon the related entities being brought into the cache by your query, and you should include exception handling in case the strategy fails. Choose the Off setting if you only want the targeted entries retrieved into the cache. Be sure you choose a compatible FetchStrategy. For queries that DevForce can successfully invert, the InversionModes of Try and On will yield the same end state: the query will be cached, and all related objects necessary to permit future satisfaction of the query entirely from the cache will be assumed to be present in the cache. If you use the InversionMode of Manual properly that is, you take care to see that the necessary related objects get retrieved into the cache by some means or another before the query is submitted then it, too, will produce the same ending state as the Try and On settings.
180 | P a g e

IdeaBlade DevForce
Queries That Cannot Be Inverted The following types of queries cannot be inverted:

Business Object Persistence

A query that returns a scalar result. This includes all aggregate queries (Count, Sum, Avg, etc.).28

C# VB

var query02 = _em1.Orders.Select(o => o.FreightCost).Sum(); Dim query02 = _em1.Orders.Select(Function(o) o.FreightCost).Sum()

A query whose return type is a single element. These include queries that call .First(), .Last(), and .Single()

C# VB

var query03 = _em1.Products.OrderByDescending(c => c.ProductName).FirstOrNullEntity();

Dim query03 = _em1.Products.OrderByDescending(Function(c) c.ProductName) _ .FirstOrNullEntity()

A query whose return type is different from the type contained in the collection first referenced.

C#

var query04 = _em1.Customers .Where(c => c.Country == "Argentina") .SelectMany(c => c.Orders); Dim query04 = _em1.Customers _ .Where(Function(c) c.Country = "Argentina") _ .SelectMany(Function(c) c.Orders)

VB

much later in this chapter. Again, we suggest you defer reading that at least until youve completed this section on Query Strategy.

Pre-Defined QueryStrategies
As mentioned previously, every QueryStrategy combines a FetchStrategy, a MergeStrategy, and a InversionMode. Since there are five FetchStrategies, five MergeStrategies, and four InversionModes,

28

Note that this group includes the example mentioned earlier in this discussion: Give me the count of Customers located in Idaho.

181 | P a g e

IdeaBlade DevForce

Business Object Persistence

there are potentially 100 versions of QueryStrategy, even keeping the TransactionSettings constant. However, in practice, a much smaller set of QueryStrategies suffices for the great majority of purposes. DevForce has identified five of them as being of particular significance, enshrining them as static (Shared in VB) properties of the QueryStrategy class. These pre-defined QueryStrategies combine FetchStrategy, MergeStrategy, and InversionMode strategies as shown in Table 5.
Table 5. Fetch and merge strategies of the common query strategies

Query Strategy
Normal CacheOnly DataSourceOnly DataSourceThenCache DataSourceOnlyWithQueryInversion

Fetch Strategy
Optimized CacheOnly DataSourceOnly DataSourceThenCache DataSourceAndCache

Merge Strategy
PreserveChanges (Not Applicable) OverwriteChanges OverwriteChanges OverwriteChanges

InversionMode
Try (Not Applicable) Off Try On

Heres how you assign a pre-defined QueryStrategy:

C# VB

query04.QueryStrategy = QueryStrategy.DataSourceThenCache; query04.QueryStrategy = QueryStrategy.DataSourceThenCache

Custom QueryStrategies
As just noted, only five of the possible combinations of a FetchStrategy and a MergeStrategy are covered by the named QueryStrategies. What if you want one of the other combinations? You can create your own QueryStrategy by supplying the fetch and merge strategy enumerations to its constructor. The result is a new immutable QueryStrategy instance29.

29

Immutable meaning that we can get the component fetch and merge strategies but we cannot reset them.

182 | P a g e

IdeaBlade DevForce

Business Object Persistence

Heres an example of the creation and assignment of a custom QueryStrategy:

C#

QueryStrategy aQueryStrategy = new QueryStrategy(FetchStrategy.DataSourceThenCache, MergeStrategy.PreserveChanges, QueryInversionMode.On);

VB

' Creating a custom QueryStrategy Dim aQueryStrategy As New QueryStrategy(FetchStrategy.DataSourceThenCache, _ MergeStrategy.PreserveChanges, _ QueryInversionMode.On)

DefaultQueryStrategy
We mentioned earlier that the DevForce EntityManager has a DefaultQueryStrategy property that can be used to shape the fetch and merge behavior of queries where the QueryStrategy is not explicitly specified. The default setting for the EntityManagers DefaultQueryStrategy is QueryStrategy.Normal. If you leave this setting at its default value, and in an individual query do nothing to countermand the default settings, then the FetchStrategy of Optimized will be used in combination with the MergeStrategy of PreserveChanges. If for some reason you wanted a EntityManager where the default QueryStrategy would always involve a trip to the data source, you could assign a different QueryStrategy, such as DataSourceOnly, to the PMs DefaultQueryStrategy property. For a given query, you could still use any desired QueryStrategy by explicitly specifying a different one.

When to Use The Different QueryStrategies


For most users, most of the time, the DevForce defaults are perfect: Satisfy a query from the entity cache whenever possible; When a trip to the data source is found necessary, resolve any conflicts that occur between incoming data and data already cache by giving the local version priority; and Perform query inversion as needed; if needed and undoable, revert to a DataSourceOnly FetchStrategy.

Your choice of a non-default strategy can be driven by a variety of things. For example, suppose your application supports online concert ticket sales. Your sales clerks need absolutely up-to-date information about what seats are available at the time they make a sale. In that use case, it will be essential to direct your query for available seats against the data source, so a FetchStrategy of DataSourceOnly might be in order.

183 | P a g e

IdeaBlade DevForce

Business Object Persistence

In code to handle concurrency conflicts, one might need a QueryStrategy with a MergeStrategy of PreserveChangesUpdateOriginal to make an entity in conflict savable. (The data source version of the conflicted entity would only be retrieved and used to partially overwrite the cache version after the concurrency conflict had been resolved by some predetermined strategy.) You can and will think of your own reasons to use different combinations of FetchStrategy, MergeStrategy, and InversionMode. Just ask yourself, for a given data retrieval operation, whether the data in the cache is good enough, or you need absolutely current data from the data source. Then ask yourself how you want to resolve conflicts between data already cached and duplicate incoming data. Then consider the process DevForce will use to satisfy the query and make sure it will have the data it needs to give you a correct result. DevForce gives you the flexibility to set the behavior exactly as need it.

Making a One-Time Change to the QueryStrategy With Which a Given Query Is Run
You may find yourself with an existing IEntityQuery object that you dont want to disturb in any way, but which you would like to run with a different QueryStrategy for a specific, one-time purpose. DevForce provides an extension method, With(), that permits you to do this.30 When a call to With() is chained to a query, the result may be either a new query or a reference to the original query. Normally it will be a new query, but if the content of the With() call is such that the resultant query would be the same as the original one, a reference to the original query is returned instead of a new query. If you ever want to be sure that you get a new query, use the Clone() extension method instead of With(). With() avoids the overhead of a Clone() when a copy is unnecessary.
Code Snippet 42. QueryStrategyWithAndCloning

C#

IEntityQuery<Customer> query00 = _em1.Customers .Where(c => c.CompanyName.ToLower().StartsWith("a")); query00.QueryStrategy = QueryStrategy.DataSourceOnly; // The With() call in the right-hand side of the following statement // specifies a query that is materially different from query0, in // that it has a different QueryStrategy associated with it. // Accordingly, the right-hand side of the statement will return // a new query: IEntityQuery<Customer> query01 = query00.With(QueryStrategy.CacheOnly); // Because the content of the With() call in the right-hand side // of the following statement doesn't result in a modification // of query0, the right-hand side will return a reference to // query0 rather than a new query. IEntityQuery<Customer> query02 = query00.With(QueryStrategy.DataSourceOnly); // If you want to be certain you get a new query, use Clone() // rather than With():

30

Our topic here is QueryStrategy, but in fact some overloads of the With() method also (or alternatively) permit you to make a one-time change to the EntityManager against which the query will be run.

184 | P a g e

IdeaBlade DevForce

Business Object Persistence

EntityQuery<Customer> query03 = (EntityQuery<Customer>)query00.Clone(); query03.QueryStrategy = QueryStrategy.DataSourceOnly;

VB

Dim query00 As IEntityQuery(Of Customer) = _em1.Customers.Where(Function(c) c.CompanyName.ToLower().StartsWith("a")) query00.QueryStrategy = QueryStrategy.DataSourceOnly ' The With() call in the right-hand side of the following statement ' specifies a query that is materially different from query0, in ' that it has a different QueryStrategy associated with it. ' Accordingly, the right-hand side of the statement will return ' a new query: Dim query01 As IEntityQuery(Of Customer) = query00.With(QueryStrategy.CacheOnly) ' Because the content of the With() call in the right-hand side ' of the following statement doesn't result in a modification ' of query0, the right-hand side will return a reference to ' query0 rather than a new query. Dim query02 As IEntityQuery(Of Customer) = query00.With(QueryStrategy.DataSourceOnly) ' If you want to be certain you get a new query, use Clone() ' rather than With(): Dim query03 As EntityQuery(Of Customer) = CType(query00.Clone(), EntityQuery(Of Customer)) query03.QueryStrategy = QueryStrategy.DataSourceOnly

Span Queries
A EntityManager query method always returns entities of a single type, the return type identified in the query object. But what about entities related to the returned entities? When do we get those? Consider a query for second quarter orders. We display them in a grid with their customer names and order totals.

The Order entities entered the cache when we processed the query. Not so the Customer and the OrderDetail entities that we need to calculate the order total. The EntityManager gets these entities only when we ask for them explicitly. Such delayed fetching we called deferred retrieval. The grid control binding calls an Order property each time it fills a cell. The Customer and Order Total columns are bound to two properties that resolve to two relation queries, one for Customer entities and one for OrderDetail entities. This means the grid control invokes two relation queries for each and every row. There are three rows showing in the screen shot so there will be six queries, each one requiring a round trip to the data source. In other words, filling this grid requires six trips to the data source.

185 | P a g e

IdeaBlade DevForce

Business Object Persistence

Now suppose that we had an excellent quarter and placed a thousand orders. The user clicks the Customer column caption, causing the grid to sort by customer. The sort requires examination of every one of those thousand orders. Most grids will fire every visible property on every examined row. That could mean two thousand separate trips to the server: one thousand fetches of customers and one thousand fetches of order details. The UI will stall for ten uncomfortable seconds and then return to its familiar crisp responsiveness. Subsequent sorts and scrolling are fast; all of the entities are now in cache so there are no trips to the data source31. But those ten seconds felt like an eternity. The problem wasnt the ten seconds; its that they occurred when the user thought they should not. She expected the search for orders take some time; maybe not ten seconds but she expected a pause of some length. On the other hand, she expected the sort to happen immediately. When it didnt, she thought there was something wrong with the application. Is the sort delay necessary? Of course not! The program cannot anticipate needing the related data and so it fetches entities inefficiently. We know better. When we grab the thousand orders, we can fetch their customers and order details at the same time. Not every Customer in the data source. Not every OrderDetail entity either. We only need the customer and order details that are related to those thousand second quarter orders. We should get them all at once, not piecemeal as we scroll or sort the grid. Span queries to the rescue. We can add span instructions to our query so that the EntityManager gets the related entities when it gets the orders. A span query instruction describes a path along the root entity graph to a particular entity type know as the span target. Of course, a EntityManager returns references to the root objects when it executes a span query. At the same time it fetches every span target entity related to any of the returned root entities and puts them in the cache. Well need two of spans for our example. There is a simple syntax for spanning to the immediate neighbors of the querys result entity type:

C#

var query = _em1.Customers .Include("Orders") Dim query = _em1.Customers _ .Include("Orders")

VB

31

The volume of data is not the issue. We might think that wed improve performance if we used a view that summed the OrderDetails on the server. Wed get one value per row instead of having to bring down the details and sum them locally. When we try this, we observe no improvement whatsoever. The delays were due entirely to the round-tripping, not the data volume nor the summations.

186 | P a g e

IdeaBlade DevForce

Business Object Persistence

We can span to entities farther away on the Order business object graph also as we might do if we were displaying product name in the Orders OrderDetails grid.
Code Snippet 43. NavigationSynchronousPreload (repeated)

C#

var query = _em1.Customers .Include("Orders") .Include("Orders.OrderDetails"); .Include("Orders.OrderDetails.Product"); .Include("Orders.OrderDetails.Product.Supplier"); .Include("Orders.SalesRep") Dim query = _em1.Customers _ .Include("Orders") _ .Include("Orders.OrderDetails") _ .Include("Orders.OrderDetails.Product") _ .Include("Orders.OrderDetails.Product.Supplier") _ .Include("Orders.SalesRep")

VB

Again, span queries dont change the list of entities to which references are returned from the query. The caller still receives the same thousand orders. But before returning the orders, the span query processing fetches the related entities and merges them into the cache. When the grid cells call upon Order properties to return customers or calculated order totals, those properties will find the pertinent entities waiting in cache. The main order query is a little slower because there are more entities retrieved. The user wont notice; she expected the search to take a beat or two. The first sort is instantaneous; she is thrilled.

Performance Details
While spans greatly reduce the number of queries submitted to the database, they do not, of course, eliminate them altogether. Each span resolves to a separate query and each of these span queries necessitates a separate trip to the database. Thus, if our we added three spans to an Order query, there would be four queries (one for the Orders, one for the related type referenced in each of the spans) and four trips to the database. But these four trips -- as our previous discussion has illustrated might well replace thousands of trips required in the absence of spans. In an n-tier deployment using the Business Object Server (BOS), the picture is even rosier. In that configuration, the client submits the entire request, including spans, in a single transmission to the BOS. It is the BOS that makes the four trips to the database. When the BOS has a fast, fat pipe to the database - as it should those four trips are very quick indeed. The BOS then combines the results from its queries against the database into a single package that it ships back to the client. There has been only one trip across the slow connection between client and server! Note also that the total loads on the EntityServer and database are reduced when each client is making efficient data requests using spans. Thus, every individual client benefits from the improved efficiency of the other clients.
187 | P a g e

IdeaBlade DevForce

Business Object Persistence

Performance matters ... but not all time and effort spent optimizing performance returns equal results. We strongly advise instrumenting your queries during development and testing to identify performance hotspots. Then optimize where it really matters.

Cached Entity Lifespan


Entities stay in the cache until the application terminates or they are removed. There is no garbage collection. We may need to purge the cache of unwanted entities if
we accumulate a large volume of entities during a user session a session might last a long time days, weeks, etc. the cache contents will be saved and later restored from local storage.

The programmer has many remove options including the ability to remove a single entity, a list of entities, entities of a particular type, and all entities with a specified EntityState. Removal and deletion are not the same thing. Remove means remove the entity from the cache. There are no data source implications. The entity is simply no longer in the cache; it is as if we had never fetched it. Delete means schedule the entity for deletion from the data source. The entity remains hidden in the cache, waiting for the moment when we send a delete request to the data source. That moment arrives when we save the deleted entity. Once saved (that is, deleted from the data source), the object is removed from the cache. New entities are removed from the cache immediately when deleted; they were never in the data source so there is nothing there to delete, nothing to schedule.

Saving the Cache Locally


An EntityManager can save its cache locally. This feature is useful in many scenarios including these two:
The application must be able to run offline for extended periods. It must be possible to exit the application and launch it again later while still disconnected. The developer is worried that the user may accumulate many changes for a long time without saving to the data source. The application would snapshot the changes periodically in case the application goes down. But many of the modified business objects wont pass data source validity checks or wont satisfy business rules for permanent business objects. They cant be saved to the data source.

In the first case the application cant reach the data source and in the second its access is blocked. The application needs a local option. The application can tell the EntityManager to serialize its object cache as an XML stream and save the stream to a file on the clients file system. Variations on the theme enable encryption of the stream and filing to isolated storage or other arbitrary destinations.

188 | P a g e

IdeaBlade DevForce

Business Object Persistence

On command or when the application is re-launched, the application can locate the file and restore its contents to the EntityManagers cache. The developer can choose to completely replace the target cache or merge the saved cache objects into it; in a merge, objects from the saved cache replace corresponding objects in the target cache.
The pool of temporary ids maintained by the developers custom implementation of IIdGenerator is also saved and restored.

The process preserves pending business object changes additions, modifications, deletes. When the application next obtains a server connection, it can synchronize local objects with the central data source. It can refresh local unmodified copies of business objects that have been changed by other users. It can save local pending changes, relying upon DevForce optimistic concurrency checking to prevent overwriting other users changes. If the developer expects the application to operate offline, she should prep the cache by retrieving the business objects the user is likely to need before disconnecting and saving the cache locally. While disconnected, queries and object navigation can only access objects already in cache.

The TraceViewer: Watch What Data Is Being Loaded, and How


Sometimes you may not be aware of what data is being loaded during particular processes. In this, the DevForce TraceViewer can be extremely helpful. It monitors all communications with the business object server, providing a real-time log of same. There are two different ways to use the Trace Viewer:
Stand-alone, and Embedded in your application.

To use the Trace Viewer in stand-alone mode, you will typically launch it from the Windows Start Menu for DevForce:

You can use the Trace Viewer in this mode with no change to your application code, but only if run your application in n-tier mode, with the Business Object Server running in a separate process from the client application. You can also use the stand-alone Trace Viewer without running n-tier if you are willing to add a single line of code to your application.
189 | P a g e

IdeaBlade DevForce

Business Object Persistence

Embedding the Trace Viewer in your application requires a couple of minor (and isolated) changes to your application code, but offers greater convenience you can set it to begin working automatically whenever you start the app and it does not require that the Business Object Server be launched in a separate process. For our own development work, in non-release versions of our applications, we often use the Trace Viewer this way. Well detail both approaches in the following material.

Using the Trace Viewer Stand-Alone


To use the Trace Viewer stand-alone, launch it from the Windows Start Menu entry shown in the screen shot above. It will display a dialog window like the following:

Once launched, the Trace Viewer makes periodic attempts to connect with a TracePublisher. It will find a Business Object Server instance once one is running.

Using the Stand-Alone Trace Viewer While Running N-Tier To see the server activity instigated by your application without making any code changes, youll need to launch the app in n-tier mode. You can do that easily, on a single development machine, using the NTier Configuration Starter utility which you will also find on the IdeaBlade DevForce / Tools menu. You can find step-by-step instructions for working with the N-Tier Configuration Starter in the Deployment topic document, in the section N-Tier Configuration Starter.

Once your application in running n-tier, youll see communications with the BOS logged as follows:
190 | P a g e

IdeaBlade DevForce

Business Object Persistence

The activity logged just above resulted from execution of the following method in an app:
C#
private void DoIt() { _mgr.Customers.ToList(); _mgr.Orders.ToList(); _mgr.Products.ToList(); _mgr.Suppliers.ToList(); _mgr.Employees.ToList(); } Private Sub DoIt() _mgr.Customers.ToList() _mgr.Orders.ToList() _mgr.Products.ToList() _mgr.Suppliers.ToList() _mgr.Employees.ToList() End Sub

VB

The method simply fires off five queries that must hit the server to get their data. Using the Stand-Alone Trace Viewer While Running Single -Tier (Client and Server in the Same Process) To see activity in the stand-alone Trace Viewer when running in single-tier, development mode, you must add one line of code to your application:

C#

TracePublisher.LocalInstance.MakeRemotable();

191 | P a g e

IdeaBlade DevForce

Business Object Persistence

VB

TracePublisher.LocalInstance.MakeRemotable()

Once your app has made the above call to MakeRemotable(), it begins functioning as a TracePublisher, doing so on a default port and default service name that matches the defaults on the Trace Viewer. You can also make it a publisher on a different port and with a different service name, but then you will need to change the settings on the Trace Viewer to listen on the specified channel.

C# VB

TracePublisher.LocalInstance.MakeRemotable(9010, "MyClientService"); TracePublisher.LocalInstance.MakeRemotable(9010, " MyClientService")

For most uses, you probably wont find it necessary to change the port or service name. Note that when a DevForce app is deployed n-tier, separate sets of messages are published server-side and client-side. (These messages end up in the server- and client-side debug logs, as well as in any Trace Viewers that are listening for them.) When running single-tier, messages written by the (logically serverside) EntityService (which in single-tier mode runs inside the same process as the client application) will be published along with messages from the logical client-side. Youll see everything. Here are the messages captured by the stand-alone Trace Viewer after adding the MakeRemotable() call and running single-tier:

Note that including the call to MakeRemotable() and running the stand-alone Trace Viewer is the only way to use the Trace Viewer with a (single-tier) console app. The options for embedding the TraceViewer (described below) require WPF or WinForm applications.
192 | P a g e

IdeaBlade DevForce Embedding the Trace Viewer in Your Application

Business Object Persistence

For convenience during development, you may prefer to embed the Trace Viewer in your application. This requires adding a reference to the TraceViewer executable and adding a line or two of code. There are actually two different implementations of the TraceViewer within DevForce: one for WinForm apps and one for WPF apps. The names of their executables are as shown below:

Target Application Type WinForm App WPF App

Executable WinTraceViewer.exe WPFTraceViewer.exe

To use either TraceViewer in your app, you must first set a reference (in your apps UI project) to the executable file where it lives. Both versions of the TraceViewer are deployed to the DevForce installation folder, usually C:\Program Files\IdeaBlade DevForce.

Embedding the WPFTraceViewer in Your WPF App


To add the reference to the WPF TraceViewer, for example, right-click the references node in your desired UI project, and select Add Reference. On the Add Reference dialog, select the Browse tab, then browse to the file and click OK:

Heres some code for the startup window of a simple WPF app. The code launches the WPF TraceViewer during initialization, and includes a button click handler that launches a query for some data:
193 | P a g e

IdeaBlade DevForce
C#

Business Object Persistence

namespace Wpf01 { /// <summary> /// Interaction logic for Window1.xaml /// </summary> public partial class Window1 : Window { public Window1() { InitializeComponent(); SetUpTraceViewer(); } private void SetUpTraceViewer() { WPFTraceViewer tv = new WPFTraceViewer(); tv.Show(); } private void _loadDataButton_Click(object sender, RoutedEventArgs e) { List<Customer> customers = _em1.Customers.Where(c => c.Country == "Brazil").ToList(); _outputTextBlock.Text += String.Format("Customer retrieved: {0}\n", customers.Count); } #region Private Fields DomainModelEntityManager _em1 = new DomainModelEntityManager(); #endregion Private Fields } }

VB

Public Class Program Public Shared Sub main() #If DEBUG Then Dim tv As New IdeaBlade.DevTools.TraceViewer.TraceViewerForm() tv.Show() #end If Application.Run(MainForm) End Sub End Class

Here is the display that results:

194 | P a g e

IdeaBlade DevForce

Business Object Persistence

The TraceViewer logs all operations against the Entity Server, so you can use it to see exactly what data loading operations result from actions performed in the user interface. In this app, additional clicks of the <Load Data> button result in no further activity against the Entity Server, since the desired Customers, once retrieved into the cache, can be accessed there thenceforward.

Embedding the WinTraceViewer in Your WinForms App


To add the reference to the WinTraceViewer, right-click the references node in your desired UI project, and select Add Reference. On the Add Reference dialog, select the Browse tab, then browse to the file and click OK:

195 | P a g e

IdeaBlade DevForce

Business Object Persistence

Heres some code for the startup program of a simple WinForm app that launches the WinForms TraceViewer during initialization:

196 | P a g e

IdeaBlade DevForce
C#

Business Object Persistence

static class Program { [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false);

IdeaBlade.DevTools.TraceViewer.TraceViewerForm tv = new IdeaBlade.DevTools.TraceViewer.TraceViewerForm(); tv.Show();


Application.Run(new _customerForm()); } }

VB

The main method, after launching the TraceViewer, launches _customerForm as the startup form:

197 | P a g e

IdeaBlade DevForce

Business Object Persistence

The handler for the buttons click event launches a query for some Customers:

C#

private void _loadDataButton_Click(object sender, EventArgs e) { _customers.ReplaceRange(_em1.Customers .Where(c => c.Country == "Brazil")); }

VB

When you click the button, you see activity logged in the TraceViewer:

The TraceViewer logs all operations against the Entity Server, so you can use it to see exactly what data loading operations result from actions performed in the user interface. In this app, additional clicks of the <Load Data> button result in no further activity against the Entity Server, since the desired Customers, once retrieved into the cache, can be accessed there thenceforward.
198 | P a g e

IdeaBlade DevForce

Business Object Persistence

Getting Generated SQL to Display in the TraceViewer


By default, both TraceViewers (WPF and WinForms) show queries in a LINQ-like representation:

The above, for example, is an unrestricted query for entities of type Employee. You can, however, elect to see the SQL generated server-side by the Entity Framework. To do that, you must change the logTraceString setting in the applicable app.config file32 to true. Note that logTraceString is an attribute of a particular edmKey (which represents a single data source).

This results in a display like the following:

The TraceViewer can be invaluable in troubleshooting performance problems. These are often caused by inefficient data retrieval (such as loading a data grid where each rows triggers several trips to the server to pick up related objects that were not pre-loaded).

32

In a development app with all parts running on a single machine, choose the App.Config file in the AppHelper project.

199 | P a g e

IdeaBlade DevForce Using the Trace Viewer With a Silverlight App

Business Object Persistence

For Silverlight applications, the EntityService automatically runs in a separate process from the client, so a stand-alone TraceViewer will automatically pick up the server messages associated with your app (assuming you havent changed the default service name and port). If you wish to see client-side Trace messages, you will need to embed a UserControl into your Silverlight front end. Here is the XAML for the control...

XAML

<UserControl x:Class="DevForceSilverlightApp.TraceWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:data="clrnamespace:System.Windows.Controls;assembly=System.Windows.Controls.Data" Width="Auto" Height="Auto"> <Grid x:Name="LayoutRoot" Margin="20,20,20,20" > <data:DataGrid x:Name="_dataGrid" HorizontalAlignment="Left" VerticalAlignment="Top" AutoGenerateColumns="True" MinWidth="250" MinHeight="100" Background="#FFB5BAB5" Margin="0,0,20,0" IsReadOnly="True" /> </Grid> </UserControl>

...and here is the code behind:

C#

using using using using

System; System.Collections.ObjectModel; System.Windows.Controls; IdeaBlade.Core;

namespace DevForceSilverlightApp { /// <summary> /// Sample trace subscriber. You can drop the TraceViewer UserControl onto a page /// to display tracing information from the Silverlight application in a grid. /// </summary> /// <remarks> /// To use the TraceSubscriber: 1) listen for its Publish event, and 2) call StartSubscription() /// to have tracing messages sent to you. You can also call StopSubscription() /// to temporarily or permanently stop receiving messages. /// </remarks> public partial class TraceWindow : UserControl {

200 | P a g e

IdeaBlade DevForce
public TraceWindow() { InitializeComponent();

Business Object Persistence

_messages = new ObservableCollection<TraceMessage>(); _subscriber = new TraceSubscriber(); _subscriber.Publish += new EventHandler<PublishEventArgs>(_subscriber_Publish); _subscriber.StartSubscription(); _dataGrid.ItemsSource = _messages; } private void _subscriber_Publish(object sender, PublishEventArgs e) { _messages.Add(e.TraceMessage); if (_dataGrid.Columns.Count > 0) { _dataGrid.ScrollIntoView(e.TraceMessage, _dataGrid.Columns[0]); } } TraceSubscriber _subscriber; ObservableCollection<TraceMessage> _messages; } }

VB

Be sure to change the namespace in both the XAML and the code to match your app! In the following, we have embedded the above TraceWindow UserControl in another UserControl:

XAML

<UserControl x:Class="DevForceSilverlightApp.ConsoleUserControl" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:DevForceSilverlightApp" > <Grid x:Name="LayoutRoot" Background="White"> [... snip] <ScrollViewer x:Name="_traceWindowScrollViewer" Grid.Row="1" Margin="0,0,20,0"> <local:TraceWindow /> </ScrollViewer> [... snip] </Grid> </UserControl>

Thats enough to get it to display client-side trace messages written by DevForce. We can add our own trace messages as follows...

201 | P a g e

IdeaBlade DevForce
C# VB

Business Object Persistence

IdeaBlade.Core.TraceFns.WriteLine("Hello world!");

IdeaBlade.Core.TraceFns.WriteLine("Hello world!")

...resulting in the following output (the TraceWindow control contains the DataGrid):

Creating Business Objects


In this short chapter we discuss business object creation in a bit more detail. Well explain why and when the developer must write her own creation method and what minimal steps are essential to its implementation. We delve into the special challenge of creating unique business object identifiers and how DevForce supports this process. We mention also two other custom class methods, CompareTo() and ToString(). We may want to add them while writing the creation method.

When Not to Create


A business object class needs a create method only if the application can add new business objects of its type. This is not so in a surprising number of cases. For example, we dont add states to the USA very
202 | P a g e

IdeaBlade DevForce

Business Object Persistence

often. Our application may want access to these states as business objects but it is unlikely to need to add new ones (or change existing states).

The Business Object Create Method


Most applications will add new instances to many of its business object classes. The developer must write a Create method for each of these classes and call it whenever she wants a new object.
For technical reasons, we must acquire new instances via a class method rather than by means of a constructor. The expression emp = new Employee() is always invalid; instead it must look something like emp = Employee.Create().

Most Create method implementations return a single business object after following these four steps:
1. 2. 3. 4. Ask the EntityManager for a prototype of the new business object Give the prototype a unique identity Fill in some of its initial values (optional) Add the completed prototype to the EntityManagers cache

Why cant DevForce take care of this for us? Because steps 2 and 3 require application-specific knowhow that DevForce can neither discover nor supply. Step #2 concerns the identity of the object. DevForce requires that every business object have a unique identity. Identity is captured in the objects primary key which is composed of one or more identifiers. There is no way for DevForce to know how identifiers are determined. While it can discover that a particular database tables key is a single integer field, this fact is insufficient to generate an identifier. The integer could come from anywhere. Step #3 concerns the validity of the object. It is generally a good idea to maintain an object in a valid state. This isnt always possible but it is a useful goal and the Create method is a place to start. Of course DevForce is ignorant of application business rules so if there is to be any object initialization it is up to the developer to code it here.

Generating unique identifiers


Unless the primary key is an Identity column, DevForce doesnt know how to generate an objects primary key identifier(s) so it cannot deliver new business objects on its own. That is why the EntityManager provides a prototype in Step #1 that is not yet in the cache. Once the developer sets the primary keys identifier(s) in the prototype, the prototype may be added to the EntityManagers cache and become a business object accessible to the application. It may still be invalid from a business perspective but it is programmatically acceptable to DevForce. A business objects key must be unique not only within the context of the current user session but across the application domain. We have to make sure the key we assign to a new employee object cannot also be assigned to a different employee object by someone else.

203 | P a g e

IdeaBlade DevForce GUIDs

Business Object Persistence

GUIDs (globally unique identifiers) make great identifiers (aka ids) because they are easy to mint, are nearly certain to be unique, and can be generated locally, independent of any external resource. If we are in complete control of the database schema design, GUIDs are the way to go.
The MS SQL uniqueidentifier data type is the database analog for a GUID.

When we need a new GUID, we ask .NET to compute one for us, assign it to the prototypes identifier data member, and move on to the next step in object creation. GUIDs have two disadvantages:
GUID values are long and obscure. Users find them difficult to type correctly and difficult to remember. At 16 bytes, the GUID is large compared to other data types such as 4-byte integers. Database indexes built using GUID keys may be relatively slower than indexes using an integer key.

In our experience, striving for meaningful identifiers leads to disappointment and failure; we strongly council against using identifiers with semantic content. If you disagree, you may regard these additional GUID properties as disadvantageous:
GUID values are random and cannot accept any patterns that may make them more meaningful to users. There is no way to determine the sequence in which GUID values are generated. They are not suited for applications that depend on incrementing key values.

If GUIDs work for you, you may skip the next section on custom id generation. Unfortunately, few of us have this option. We are usually given a database that we cannot change. Were not allowed to replace all table ids and all foreign key columns with 16 byte integer GUIDs. We have to conform to the existing key schemes which impose both the identifier data types and the manner of their generation.

Store-Generated Ids
Store-generated Ids are those Id fields marked as store-generated in the EDMX. These Ids will typically be generated as SQL Server identity columns or Oracle sequences. DevForce detects these fields and generates temporary ids automatically. DevForce also automatically performs fixup on these temporary Ids after a save has occurred. When using store-generated Ids you do not need to implement a custom IIdGenerator of your own, or otherwise worry about setting these Ids in your code.

Custom id generation
Custom id generation almost always requires access to some external resource, some applicationspecific logic for deriving new ids, and additional logic to increment the resource. Suppose our application uses integer keys for all of its tables. The database has a special NextId table that holds the next integer id. To get a new id, a server-side process could quickly lock that table, grab the id, update the table to hold a new next id, and free the table.

204 | P a g e

IdeaBlade DevForce

Business Object Persistence

This is just one among thousands of ways applications generate ids. The commonality is the external resource, the functional equivalent of the NextId table, without which we could not be sure of generating identifiers that are unique within the application domain. The developer must write the code that reads the resource, calculates ids, and updates the resource. If only it were this simple. Remember that we are describing a smart client application in which new object creation begins on the client machine. The client machine could be disconnected and thus unable to reach a NextId table or some other external source of permanent ids. We still want to be able to create new objects while disconnected. We know that we will have to connect to that external resource to get permanent ids and store the new objects in the database. In the interim, we must finesse the situation and use locally generated, temporary ids until we can reconnect and replace them with permanent ids. For example, since our permanent ids are always positive integers, we could use negative integers for temporary ids, acquiring them by decrementing a client-side counter. We assign temporary ids (however generated) to the new objects and to the foreign keys of the objects that reference them. At some point when were sure were connected, we run around to all the locations with temporary ids and replace them with permanent ids. Id Fix-up Just before we save objects back to the database is a good time to attempt this fix-up because (a) we must be connected to save and (b) we must fix all locally modified objects before saving any of them in case one such object has a reference to a temporary id. The DevForce id generation facility can help. In essence:
The developer writes an id generation class that conforms to the DevForce IIdGenerator interface. This class will handle id generation for every class of business object in the data source. The developer implements the prescribed methods in the id generation class that provide temporary and permanent ids. Back in the business object creation method, the developer invokes the EntityManager.GenerateId() method which assigns a temporary id to the new object prototype. A typical call looks like: pm.GenerateId(protoEmp, Employee.IdEntityColumn). The EntityManager attempts a save The DevForce framework tells the developers id generation class to give it the map of temporary ids to permanent ids. The framework runs around the cache, replacing temporary ids with permanent ids.

Foreign Key Fix-Up The framework replaces temporary ids in entity properties that are connected to the generated id column by a relation. The generated Id column in this example is the Employee.IdEntityColumn.

205 | P a g e

IdeaBlade DevForce

Business Object Persistence

Suppose there is a relation defined in the model between Order.SalesRepId and EmployeeId, but that no relation is defined between Customer.SalesRepId and EmployeeId. This is a critical omission, as you will see. We create a new employee, myEmployee. The Id generator gives him a temporary id value of 1. During the application session myEmployee is assigned to myOrder:
myOrder.SalesRep = myEmployee

But no relation was defined between Employee and Customer, so there is no Customer.SalesRep property33 to which myEmployee can be assigned directly. Nevertheless, the determined developer stuffs the EmployeeId value directly into the Customer.SalesRepId property.
myCustomer.SalesRepId = myEmployee.Id

This is a bad practice and should be avoided. The absence of the myCustomer.SalesRep property should have been a warning that a critical relation was missing. See what happens: The user saves and the fix-up begins.
The value of myEmployee.Id is updated to its permanent value, 301.
myOrder.SalesRepId is fixed up to 301 (since there is a relation back to Employee.Id) . myCustomer.SalesRepId stays stuck with id = 1. (There was no relation from myCustomer.SalesRepId back to Employee.Id so the PM didnt know to replace the SalesRepId.)

Not good! In most cases the end result of all this would be an errant foreign key value persisted to the data source. If, however, the data source did have the necessary foreign key constraint (but the related relation had been deleted from the model), the result of attempting to persist the errant foreign key value would be a foreign key constraint exception. That might appear to reflect a PersistenceOrder problem (e.g., saving a child before saving its new parent) when in fact it is not.
Important: Map all of the relations.

Sample Id Generator DevForce ships with source for example id generator classes that you can either use directly or adapt for your application.
Its now easy to see why we prefer GUIDs. We can use .NETs free GUID generator while disconnected because it works locally without resort to an external resource. GUIDs are globally unique so the ids we create are fine as permanent ids. All of the complexity disappears. The 16-byte cost of GUIDs is usually worth it. Use GUIDs if you can.

Ids in mapping objects


33

At least, there would be no such property generated by the DevForce Object Mapper

206 | P a g e

IdeaBlade DevForce

Business Object Persistence

We cannot leave this subject without observing that some business objects do not use generated ids. Mapping objects relate one kind of business object to another in a many-to-many relationship. OrderDetails in the NorthwindIB database is one such business object. In addition to carrying information about a particular purchase item such as quantity and price, it relates Orders to Products in a many-to-many relationship. Orders have many items each associated with a particular product being purchased. A given product will appear as a purchased item on many different orders. A mapping objects primary key is typically a combination of the ids from the two objects it relates. The key of an OrderDetail object is comprised of its parent orders id and the id of the product being sold. It is an {OrderId, ProductId} tuple. In such cases, the ids that form the primary key are not generated within the create method but rather passed into the method in its parameter list. The create method for OrderDetail would include an order object and a product object among its parameters. Inside the Create() method, we would extract their ids and set the OrderDetail prototypes primary key accordingly. OrderDetail happens also to be the detail object in a master/detail relationship. The id of the master object is often one of the identifiers in the detail objects key and it will usually be passed into the method in one of its parameters.

Creating a valid business object


EntityManager delivers a prototype in step #1 of the create method. DevForce ensures that the prototype has a non-null value for every object member that is mapped to a non-nullable field in the database. This assistance is often helpful but it may be wrong. Suppose business rules demand that every employee have a hire date and that hire dates must be later than the companys incorporation. The HireDate field in the database is mandatory so the prototype carries a default value for the corresponding object data member. The developer should make no assumption about this value other than that it is a valid date from the perspective of the database. It could be anything and might well be a date prior to the founding of the company. The hire date is probably unknown when the object is created. The developer may choose to wait until it becomes known in which case the prototype default value will suffice for awhile. The object cant be saved but we will have time to get a valid hire date from the user before we save it. Alternatively, the developer may decide that a particular date, such as todays date, makes a good initial hire date. She will initialize the prototypes hire date accordingly, here in the Create method. The lesson: strive to make the new object as valid as possible by setting appropriate initial values in this step of the creation method. It is often helpful to add parameters to the Create method so the caller can pass in appropriate initial values. None of this is required but it is good practice.

207 | P a g e

IdeaBlade DevForce

Business Object Persistence

Auxiliary Business Object Class Methods


While were adding a new object creation method inside the business object class, its a good time to mention two other useful methods: CompareTo() and ToString().

CompareTo()
When DevForce sorts a collection of business objects it often looks to the class CompareTo() method to determine which of two objects sorts before the other. Business objects inherit a CompareTo() method from the root class of all business objects, Entity. Its rarely what we want; the results are arbitrary and unpredictable. We should override it with a comparison that is useful. A CompareTo()for the Employee class might compare employee first and last names.

ToString()
It is common for both DevForce and .NET to invoke an objects ToString() method. An objects default ToString() returns the objects class name. This is rarely useful. For example, anEmployee.ToString() might return Tutorial.Entities.Employee. We should override the Employee ToString() method so that it returns something useful like Nancy Davolio. Many classes, not just business object classes, should have their own ToString() methods.

Adding and Removing Related Objects using Add() and Remove()


Navigation properties that return a collection (e.g., anEmployee.Orders) have Add() and Remove() methods. Add() The Add() method takes a parameter of the type contained by the collection (e.g., an Order).
Code Snippet 44. AddUsingAdd()

C#

Order anOrder = new Order(); anOrder.OrderDate = DateTime.Today; anOrder.FreightCost = Convert.ToDecimal(999.99); anEmployee.Orders.Add(anOrder); Dim anOrder As New Order() anOrder.OrderDate = Date.Today anOrder.FreightCost = Convert.ToDecimal(999.99) anEmployee.Orders.Add(anOrder)

208 | P a g e

IdeaBlade DevForce

Business Object Persistence

Invoking Add() adds the supplied item to the collection. If the relation between the parent and child types is 1-to-many and the supplied item is currently associated with a different parent, then Add() simultaneously removes it from the corresponding collection of the other parent.34 Note that, in the above snippet, we did not need to set the SalesRep property of the new Order to the Employee whom we wanted to become its parent:

C# VB

// anOrder.SalesRep = anEmployee; ' anOrder.SalesRep = anEmployee

//don't need this; Add() will handle it '' don't need this; Add() will handle it

Invocation of the Add() method on anEmployee.Orders produced the equivalent result. Remove () Remove() also takes a parameter of the type contained by the collection. It dissociates the indicated instance from the collections parent35.

C# VB

anEmployee.Orders.Remove(anOrder); anEmployee.Orders.Remove(anOrder)

Note that while Remove unassigns the Order from the target Employee, removing it from the collection returned by the navigation property, it does not remove it from the cache or mark it for deletion. If you want the Order removed from the cache or deleted from the back-end datastore, you must order those actions separately by calling the Orders EntityAspect.Remove() or EntityAspect.Delete() methods, as appropriate. Add() and Remove () on Many-to-Many Navigation Properties You can also use Add() and Remove () on many-to-many navigation collections generated by the Entity Data Model. You get these in your Entity Data Model when two entities are linked by a many-to-many linking table that has no payload; that is, no columns other than the two foreign keys (which also form

34

The equivalent result on table rows in a relational database is that the child entitys foreign key value is changed.
35

Speaking again of the equivalent result on table rows in a relational database, the child entitys foreign key value is set to null.
209 | P a g e

IdeaBlade DevForce

Business Object Persistence

a composite primary key).36 An example would be an Employee linked to a Territory by means of an EmployeeTerritory table whose composite primary key consists of the two foreign keys EmployeeId and TerritoryId, and which has no other columns. When you have such an association, invoking Add() on the many-to-many navigation property creates (in the EntityManager cache) the necessary linking object in the EntitySet for the linking objects37. Remove() marks as deleted the linking object that formerly connected the two entities in the many-tomany relationship. Both changes the insertion of a new linking object or the deletion of an existing one are propagated to the back-end data store upon the execution of SaveChanges() on the governing EntityManager. Adding and Removing Items in Custom-Coded Many-to-Many Navigation Properties You can (and probably will) also have in your model many-to-many associations involving linking entities that do have payload. (For example, in the NorthwindIB database, Order links Employees (who act as sales reps) to Customers in a many-to-many relationship.) For these cases, you should add and remove elements to the m-to-m collection (e.g., anEmployee.Customers) by inserting or deleting instances of the linking entity. Since that linking entity is probably significant in its own right (again consider an Order), it likely has properties that need their values set at creation time in any case. For example, the following code will have the indirect effect of adding a new Customer to the Customers collection of anEmployee, but only if the Order being added is for a Customer with which anEmployee is not already linked through some other Order. Otherwise, aCustomer is already in anEmployees Customers collection.

C#

// May add a Customer to anEmployees Customers collection anOrder = Order.Create(_entityManager, aCustomer, anOrderDate); anEmployee.Orders.Add(anOrder); ' May add a Customer to anEmployees Customers collection anOrder = Order.Create(_entityManager, aCustomer, anOrderDate) anEmployee.Orders.Add(anOrder)

VB

Similarly, the following code will have the indirect effect of removing aCustomer from the Customers collection of anEmployee, but only if anEmployee has no other Orders for aCustomer. If she does, then aCustomer will remain in her Customers collection.

36 37

See the appendix Many-to-Many Associations in the Entity Framework in the Object Mapping chapter for more information. Note that those objects are not exposed in the conceptual model, and are never manipulated directly by you.

210 | P a g e

IdeaBlade DevForce
C#

Business Object Persistence

// May remove a Customer from anEmployees Customers collection anOrder.EntityAspect.Delete(); ' May remove a Customer from anEmployees Customers collection anOrder.EntityAspect.Delete()

VB

Business Object Creation Review


Developers will write a creation method for each business object class that can add new objects. That method will return a new business object after it
gets a prototype from the EntityManager assigns an id to the prototype (optionally) sets certain prototype values to satisfy minimum standards of validity adds the prototype to the EntityManager

If we have to generate custom ids for our business objects, we probably will write and register an id generation class that conforms to the DevForce IIdGenerator interface.

Saving Business Objects


Add, change, and delete operations only affect entities in a EntityManager cache. They are not written to the data source nor are they visible to other application users until the application tells the EntityManager to save them. Alternatively, the application can undo the changes rather than save them. If the application decides to save, it issues one of the overloads of EntityManager.SaveChanges() that can save an individual business object, an arbitrary list of objects, or all entities with pending changes. Saves are always transactional in DevForce. If concurrency checking is enabled, DevForce will confirm that entities being saved have not been modified or deleted by another process since they were last retrieved. This chapter elaborates on each of these points.

EntityState of an Object
Unmodified entities are never saved. Attempts to save them are ignored. The application can determine if a particular object is new, modified, marked for deletion, or unmodified by examining its EntityState property which returns one of the corresponding EntityRowState enumerations.

211 | P a g e

IdeaBlade DevForce

Business Object Persistence

The application can also query the cache for all entities that are in one particular EntityState or specific combination of EntityStates and submit them together for save.

Undo
Modified business objects dont have to be saved. The application can undo changes made to a single object or a list of objects in the cache. This is a single level undo. Undoing a pre-existing object, whether changed or marked for deletion, restores it to its state when last retrieved from the data source38; its EntityState becomes unmodified. Undoing a newly created object deletes it immediately and removes it from the cache. There is no undo of an undo.

Validation
The wise developer will validate business objects before saving them. Many developers perform validity checks in the presentation layer. Some checks in the UI make sense especially when they provide crisp and immediate user feedback. But good design keeps most validation logic out of the presentation layer and delegates it to the business object. Here are four good reasons:
As the application evolves there are likely to be multiple screens even multiple UIs updating the same business object. There is high risk that they will perform validation differently and omit essential checks if each handles its own validation. The object may be changed by a batch program or by a web service. We need to perform the same validations in these modes as we do in a graphical interface. Cross-field and cross-record checks in the UI can create deadlocks and recursion problems. Its easier to apply rules such as the birth date comes before the hire date and orders weighing more than 100 pounds must be shipped by ground after the user presses a button rather than try to enforce them while the user is typing. Its easier to break up or combine forms in an interface if you dont also have to juggle the validation code to match.

DevForce offers extensive facilities for defining and executing validation logic. See the chapter, Validation Through Verification.

Temporary Id Fix-up
Initiation of any save operation causes the EntityManager to attempt to replace temporary ids with permanent ids. Subsequent success, failure, or cancellation is immaterial. The act of saving launches the fix-up process. The fix-up process was covered above, in the section Id Fix-up. Be sure you understand the fix-up process as detailed in that section.

38

Technically, undoing a modified entity sets the current version of the entity to its original version. Entity versions are covered in Advanced Business Object Concepts.

212 | P a g e

IdeaBlade DevForce

Business Object Persistence

Life Cycle Events


Creation, retrieval, modification, removal, deletion, and save are key moments in a cached entitys life cycle. DevForce raises events on these occasions. The developer can subscribe and react accordingly.

Client-Side Life Cycle Events


Client-side life cycle events on the EntityManager include Fetching, Fetch, Saving, and Saved. These are summarized in Table 6:
Table 6. EntityManager Life-Cycle Events

Event Fetching Queried Saving Saved

Typical Uses of the Corresponding Event Handler Modify the query being submitted, or refuse the request for data. Modify the objects that were returned by the query Modify the object submitted for saving, or refuse the request to perform inserts and/or updates. Modify the saved object (which might be different from the object submitted for saving by virtue of triggers that were fired on the back end to modify the latter after it was saved).

The EntityManager raises a Fetching event shortly after the application initiates a data retrieval operation. It raises a Fetched event if any entities are retrieved successfully. We can add our own event handlers to these events. The Fetching event provides the handler with a copy of the query object that the caller proposes to submit. The event handler can scrutinize the query object, modifying it or rejecting the query entirely if security or other considerations make that the appropriate response. The EntityManager raises the Queried event if any entity is retrieved. The handler receives a list of the entities that were retrieved. The EntityManager raises a Saving event shortly after the application initiates a save. It raises a Saved event if any entities are saved successfully. We can add our own event handlers to these events. The Saving event provides the handler with a list of entities that the caller proposes to save. It will calculate that list if the method parameters do not prescribe the list39. The event handler can scrutinize the list, invoke validation methods on selected entities, clean up others (e.g., clear meaningless error conditions), add additional entities to the list, and even exclude entities from the list. Lastly, it can cancel the save. The EntityManager raises the saved event if any entity is saved. The handler receives a list of the entities that were saved successfully. In transactional saves, either every entity in the save list is saved or none of them are. In DevForce, saves are always transactional, even across disparate back-end data sources.

39

SaveChanges() with no arguments, for example, is a blanket request to save every changed entity in cache.

213 | P a g e

IdeaBlade DevForce

Business Object Persistence

Server-Side Life Cycle Events Server-side life cycle events on the EntityServer are handled by two interceptors the EntityServerQueryInterceptor and the EntityServerSaveInterceptor. These are summarized in Table 7.
Table 7. PersistenceServer Life-Cycle Events

Event AuthorizeQuery FilterQuery ExecuteQuery AuthorizeQueryResults AuthorizeSave ValidateSave ExecuteSave

Typical Uses of the Corresponding Event Handler Determine if the user is authorized to make the request. Modify the query as needed. Can intercept immediately before and after the query is executed. Always call the base method to allow DevForce to execute the query. Allows for authorization of types in the result set. Determine if the user is authorized to perform the save. Perform server-side validation of the data to be saved. Can intercept immediately before and after the save request is executed. Always call the base method to allow DevForce to perform the save.

These events provide the developer with the opportunity to do perform server-side, before-the-fact and after-the-fact operations on both queries and saves. The EntityManager, which resides client side, provides corresponding client side events: Fetching, Fetched, Saving, and Saved. The developer thus has complete flexibility to perform centralized processing on data retrievals and updates, client-side or server-side, as her use case dictates. Implementing Server-Side Life-Cycle Event Handlers

Unlike the client-side life-cycle events, the server-side events are handled by sub-typing the DevForce base classes, EntityServerQueryInterceptor and EntityServerSaveInterceptor, as needed. These classes reside in the IdeaBlade.EntityModel.Server assembly (which must, of course, be referenced by the project that contains the life-cycle handlers).

Once you have provided an implementation of the desired class, make sure that the assembly containing the implementation is deployed to the server. Heres a post-build event that ensures that the Server assembly will be deployed to the executables folder in a single-machine development environment:

214 | P a g e

IdeaBlade DevForce

Business Object Persistence

Saves and Transaction Management


EntityManager save methods can save a single business object, a list of objects, all objects in a

particular modified state (e.g., new), or all entities with pending changes.
Recall that modified objects include additions, updates, and deletes. Deleted records are actually marked for delete and must be saved to be deleted from the data source.
EntityManager saves are transactional by default. When the developer saves more than one entity at a

time, DevForce processes them together as a single unit of work. Either every save succeeds, or they are all rolled back. Behind the scenes, DevForce causes the necessary INSERT, UPDATE, and DELETE statements to be wrapped within Begin Transaction and Commit Transaction or Rollback Transaction statements. If all succeed the transaction is committed. If any fail, the data source is restored to its pre-transaction condition40. The application relies upon the data source manager to provide two key benefits throughout the transaction:

40

We cover save failures in topic coming up soon.

215 | P a g e

IdeaBlade DevForce

Business Object Persistence

Consistency - simultaneous queries and change requests cannot collide with each other, and users must never see or operate on data that is in mid-change. In a multi-user environment the data source manager must prevent simultaneous queries and data modification requests from interfering with each other. This is important because if the data being processed by a query can be changed by another user's update, the results of the query may be ambiguous. Recovery - in case of system failure, data source recovery is complete and automatic.
SQL defines different degrees of consistency enforcement called isolation levels. Each database vendor has a different default isolation level and a proprietary syntax to change it. The developer is responsible for setting the database isolation level and all other global database behavior options. Such settings may be made in the database itself or with proprietary information embedded in the connection string. Consult the database vendors documentation.

Distributed Transactions
DevForce can provide transactional integrity when saving entities to two or more data sources. These data sources can be of different types from different vendors. Their data source managers must support the X/Open XA specification for Distributed Transactions41. The developer instructs DevForce to use the .NET Enterprise Services (AKA, COM+) Distributed Transaction Coordinator (DTC) to handle transaction management. DTC performs a two phase commit. In the first prepare phase all parties to the transaction signal their readiness to commit their parts of the transaction. In so agreeing they must guarantee that they can complete their tasks even after a crash. If any participant does not agree, all parties roll back the transactions they have underway. If all participants agree, the transaction moves into the second, commit phase in which the parties actually commit their changes. If the transaction is successful, the entities are re-queried.

Re-query After Save


DevForce immediately re-queries the entity after inserting or updating it successfully. Re-query is essential because the insert or update may provoke a data source trigger that modifies the data source object. We often use a trigger to update an optimistic concurrency column. A database-maintained timestamp is another common example. In such cases, the row in the database is no longer exactly the same as the row we wrote. The EntityServer must update the entity and then send it back to the clients EntityManager. The revised entity re-enters the cache, replacing its original; its EntityState becomes Unchanged.

41

At this writing, databases are the only DevForce supported data sources that support the X/Open XA protocol.

216 | P a g e

IdeaBlade DevForce

Business Object Persistence

When Save Fails


The EntityManager.SaveChanges() method overrides all return a SaveResult object.
SaveResult.Ok returns true if the save was entirely successful. If the save was cancelled in a

ServerSaving handler, SaveResult.WasCancelled will return true and SaveResult.Ok will return false. If the save failed for any reason, the PM throws an EntityManagerSaveException. This is the default behavior. You can change that behavior indeed, SaveChanges() used to behave differently in versions prior to 3.1.3 but we recommend that you stay with this default. It follows that you prepare your code to catch and analyze an exception. You will find the information you need in the exception, including the SaveResult object that SaveChanges() would have returned.
Always handle SaveChanges exceptions. Do wrap every call to EntityManager.SaveChanges() in your own custom Save method. Do wrap every SaveChanges in a Try/Catch and analyze the exception when thrown.

Heres a code fragment showing a Save method that matches our recommendation:
Code Snippet 45. WhenSaveFails()

C#

internal void SaveAll() { try { using ( new WaitCursor(Page.ParentForm) ) { MainEm.Manager.SaveChanges();// Save everything } DisplaySaveOk(); } catch ( EntityManagerSaveException saveException ) { ProcessSaveFailure(saveException); } catch { throw; // re-throw unexpected exception } } Friend Sub SaveAll() Try _em1.SaveChanges() ' Save everything DisplaySaveOk() Catch saveException As EntityManagerSaveException ProcessSaveFailure(saveException) Catch Console.WriteLine("While saving, an exception not of type " + _ "EntityManagerSaveException was thrown.") End Try End Sub

VB

The serious failure interpretation and recovery work is in the ProcessSaveFailure method which is custom code that we write. The information we need is in the EntityManagerSaveException instance passed as a parameter to the method.

SaveChanges() Exceptions
217 | P a g e

IdeaBlade DevForce

Business Object Persistence

The EntityManager raises a EntityManagerSaveException if the save is canceled (e.g., you cancel it in your Saving event handler) or if there is any kind of exception. The EntityServerError handler gets the first crack at the exception. If there is no handler or it doesnt handle the exception, the PM throws it again, now in the context of the SaveChanges() call.
We recommend that you do not handle save exceptions in the EntityServerError; leave that to the code near your SaveChanges() call that catches and interprets save failures. Youll find examples of this recommendation in the Funhouse in the ApplicationController and EmployeePageController classes.

EntityManagerSaveException
The EntityManagerSaveException inherits from EntityServerException, supplementing that base class with information pertaining to the save. That information includes an instance of SaveResult such as would have been returned from SaveChanges(). Well discuss that in a moment. First well get a rough idea of what went wrong by looking at the exceptions Failure Type.

FailureType Connection Data Concurrency Other

Description The Entity Manager could not reach the data source. There might be a network connection problem or the data source itself could be down. The data source threw an exception such as a referential integrity violation. There was a concurrency conflict. Could be anything but usually the cause is that the save was canceled by the Saving event handler. Check the SaveResult.Canceled.

Once weve learned the category of failure we can decide how to handle it. We can look to the precipitating exception itself to further refine our response. When the failure type is anything but Connection, well likely want to examine the SaveResult to learn about which entities were affected and how.

SaveResult
Among its contents are:
SaveResult.Canceled which is true if the save was canceled while handling the Saving event. The precipitating exception, whether from an attempt to connect to the data source or an exception from the data source itself such as a concurrency conflict or referential integrity violation. A list of the entities that were not saved called EntitiesWithErrors. In practice, this will always be a list of one -- the first entity to fail -- since saves are transactional.

218 | P a g e

IdeaBlade DevForce

Business Object Persistence

These entities remain in the cache and retain exactly the values and setting they had before the save attempt.

Alternatives to Default SaveChanges Exceptions


In prior versions, the PM threw an exception only if there was a problem connecting to or exchanging data with the database. Database exceptions, such as concurrency violations or referential integrity violations, were returned to the user in an instance of SaveResult. There are two problems with that approach:
4. 5. Many developers neglected to check the SaveResult and did not realize that the save had failed. It was difficult to anticipate which kinds of problems would appear in the SaveResult and which would cause an exception.

Data Source Concurrency


A multi-user application must decide how to resolve the conflict when two users try to update the same data source entity42. Consider the following:
1. 2. 3. 4. I fetch the Employee with Id = 42 You fetch the Employee with Id = 42 You change and save your copy of the Employee with Id = 42 I try to save my copy of the Employee with Id = 42

Is this really going to happen? There is always a risk that another client or component will change the data source entity while we are holding our cached copy of it. The risk grows the longer we wait between the time we fetch the entity and the time we save or refresh it. In offline scenarios, the time between fetch and update can be hours or days. There could be a great many concurrency conflicts waiting to happen. If I save my copy now, should it overwrite the one you saved? If so, weve chosen last-in-wins concurrency checking. My copy replaces your copy; your changes are lost. This is the default in DevForce but we strongly recommend that you adopt another type of concurrency control. Permitting one user to blithely overwrite changes that another user made can be dangerous or even fatal. There is an enormous literature on this subject of concurrency checking. The coping strategies are many and complex.

42

The data source entity is the term of convenience we use to describe the data in the data source that map to a corresponding entity in cache. The data source entity may be a single row in a database table as when an Employee cached entity maps to a row in an Employee table. Alternatively, the data may be scattered in many places in some other kind of data source. We have no clue as to the actual location of data behind a Web service entity.

219 | P a g e

IdeaBlade DevForce

Business Object Persistence

Basic Mechanics of Concurrency Detection DevForce defers to the ADO.NET Entity Frameworks mechanism for detecting concurrency conflicts at the time an update is submitted to the back-end data source. The Entity Framework (EF) permits the developer to designate, in the Entity Model, one or more columns of a types data source table as concurrency columns. When a client application submits an update order against such a model to the EF, the EF prepares a SQL Update statement. To that statement it adds a WHERE clause that ensures that all columns designated as a concurrency columns have the same value they did when the record was last retrieved by the submitting application. (In other words, they have not been changed in the meantime by another user.) If that proves not to be the case, the exception thrown by the back-end data source will be propagated back down the applications calling chain. It is the developers responsibility to ensure that concurrency columns that should change upon an update do change. DevForce makes that considerably easier by providing, in the Object Mapper, a mechanism for automatically updating the value of a given column-based property upon any other change to the record. The Object Mapper offers six Concurrency Strategies that can be applied to a given property:

220 | P a g e

IdeaBlade DevForce
Concurrency Strategy

Business Object Persistence


Instruction to DevForce (Action to Perform Whenever the Entity Is Updated) Replace existing value of the property with a new GUID value. Replace existing value of the property with the current Date/Time. Increment the existing value of the property by 1. Find, in one of the data sources probe assemblies, a class that implements the IConcurrencyStrategy interface, and call its
SetNewConcurrencyValue()

AutoGuid AutoDateTime AutoIncrement Server Callback

Client

None

method, passing that method the Entity and the ConcurrencyProperty as parameters. Said method must be written to update the ConcurrencyProperty as appropriate. Just include this column in the concurrency test. The client application will take responsibility for seeing that it is properly updated whenever the entity is modified. Do not use this property to test concurrency.

Note that some of the strategies only apply to properties of specific types: clearly we cannot force a GUID value into an integer property, or a DateTime value into a boolean property, and so forth. It remains the developers responsibility to handle any concurrency exception thrown by the back end. One Concurrency Column, or Many? Since the Entity Framework permits you to designate any number of columns as concurrency columns, it may be tempting simply to designate them all.43 Thats one way of making sure that, if anything in the record has been changed by another user since you got your copy, a concurrency conflict will be diagnosed. This may be your only alternative if you have no design access to the database, but be aware that there will be a performance impact. Every update will be accompanied by a flurry of activity comparing values. As with other performance issues, you should do some upfront testing to determine whether the performance impact is unacceptable, or even significant. If you do have design access to the database, or youre fortunate enough to inherit a database already designed the way you want it, its generally a better alternative to provide a single column that is guaranteed to be updated whenever anything else is, and to use that as your sole determinant of a concurrency conflict. A simple integer column that is incremented each time the record is updated will
43

You can, of course, safely omit the primary key.

221 | P a g e

IdeaBlade DevForce

Business Object Persistence

do quite nicely; you can also use a GUID, timestamp, or any other type and methodology that guarantees that the value will change in a non-cyclical way. As you have seen, DevForce makes it easy for you to make a column auto-updating. Concurrency and the Object Graph A large part of the complexity revolves around the scope of concurrency checking. Have I changed an order if I add, change or delete one of its OrderDetail items? If I change the name of a customer, have I changed its orders? These considerations have to do with concurrency control of the business object graph. DevForce does not support graph concurrency directly. DevForce supports single-table, business object proper concurrency control. The developer can achieve the desired degree of graph concurrency control by employing single-table checking within a properly conceived, transactional concurrency plan. It doesnt have to be wildly difficult. In brief, the developer adds custom business model code such that
Added, changed, or deleted children entites always modify their parents. An application save attempt always includes the root entity of the dependency graph. During a save attempt, the root entity ensures that its children are included in the entity-save list. These children include their children.

Handling concurrency conflicts in these situations is discussed further in the section Concurrency and Dependent Entries. For now we return to concurrency checking of single business objects. Pessimistic versus Optimistic Concurrency Checking There are two popular approaches to concurrency checking: pessimistic and optimistic. In pessimistic concurrency, we ask the data source to lock the data source entity while we examine it. If we change it, we write it back to the data source. When we are done looking at it or updating it, we free the lock. While we have it locked, no one else can see or use it. This approach holds up other users trying to reach the object we hold. It gets worse if we need many objects at once. There are potential deadlocks (I grab A, you grab B, I want B too, but cant get it, so I wait. You want A also, but cant get it, so you wait. We both wait forever). There are more complicated, less draconian implementations to this approach but they amount to the same punishing performance. Under optimistic concurrency, we dont lock the table row. We bet that no one will change the source data while were working with it and confirm our bet when (and if) we try to update the data. The mechanism works as follows. We fetch a copy of the table row and turn it into a business object. We work with this copy of the data source entity. We may decide to update the entity or mark it for deletion. When we save an altered entity, the business object server converts our intention into a data source management command. That
222 | P a g e

IdeaBlade DevForce

Business Object Persistence

command, in the process of updating or deleting the supporting table row, confirms that the row still exists and has not changed since we fetched it. If the row is missing or has changed, the command fails and its up to the application to figure out what to do about it. Changes are comparatively rare so we have reason to be optimistic that the row will be exactly as we last found it. Resolving Concurrency Collisions Our optimism is usually rewarded. Occasional disappointment is inevitable. Eventually, we will encounter a conflict between our cached entity, with its pending changes, and the newly-updated data source entity. We will want to resolve that conflict one way or the other. The possible resolutions include:
Preserve the pending changes and ask the user what to do. Abandon the pending changes and re-fetch the entity. Arrange for the cached entity to become the current entity while preserving the pending changes Compare the cached entity with the current data source entity and merge the difference per some business rules or as guided by the user.

The first choice is the easiest place to start. We do nothing with the entity and report the problem to the user. The cached entity cannot be saved. We leave it up to the user to decide either to abandon the changes (option #2) or push them forward (options #2 and #3). The remaining options involve re-fetching the entity from the data source. They differ in what they do with the entity retrieved a difference determined by the MergeStrategy44 and how we use it.

C# VB

aManager.RefetchEntity(anEntity, aMergeStrategy); aManager.RefetchEntity(anEntity, aMergeStrategy)

OverwriteChanges

The second choice uses the OverwriteChanges strategy to simply discard the users changes and update the entity to reflect the one current in the datasource. While unmatched in simplicity, it is almost the choice least likely to satisfy the end user. If this is the only option, we should have the courtesy to explain this to the user before erasing her efforts.

44

We discuss merge strategies in Advanced Business Object Concepts.

223 | P a g e

IdeaBlade DevForce
PreserveChangesUpdateOriginal

Business Object Persistence

The third choice makes the cached entity current by re-fetching with the PreserveChangesUpdateOriginal strategy. This strategy causes the cached entity to trump the current datasource entity with a little trickery. The refetch replaces the cached entitys original version45 with the values from the current data source entity but it preserves the cached entitys current version values, thus retaining its pending changes. The cached entitys original concurrency column value now matches the concurrency column value in the datasource record.
Code Snippet 46. CurrentAndOriginal()

C#

// the current value of the property in the cached entity Employee.FirstNameEntityProperty.GetValue(anEmployee, EntityVersion.Current); // the value from the datasource when most recently retrieved Employee.FirstNameEntityProperty.GetValue(anEmployee, EntityVersion.Original);

VB

' the current value of the property in the cached entity Employee.FirstNameEntityProperty.GetValue(anEmployee, EntityVersion.Current) ' the value from the datasource when most recently retrieved Employee.FirstNameEntityProperty.GetValue(anEmployee, EntityVersion.Original)

The effect is as if we had just read the entity from the datasource and applied the users changes to it. If we ask the persistence layer to save it now, the datasource will think that we modified the most recently saved copy of the entity and welcome the changed record. This option is much like last one wins concurrency with a crucial difference: it was no accident. We detected the concurrency collision and forced the issue in accordance with approved business rules.
The PreserveChangesUpdateOriginal strategy works only if the entity is governed by optimistic concurrency. If the entity lacks a concurrency column, the refetch uses the OverwriteChanges strategy instead. Of course we wouldnt be talking about concurrency resolution if there were no concurrency columns.
PreserveChangesUpdateOriginal with Merge

The fourth possibility begins, like the third, with a re-fetch governed by the PreserveChangesUpdateOriginal strategy. This time we dont forcibly save the cached entity. We execute business logic instead which compares the current and original versions, column by column, deciding whether to keep the locally changed value (the current value) or the datasource value (now tucked inside the original value).
45

We cover entity versions in Advanced Business Object Concepts.

224 | P a g e

IdeaBlade DevForce

Business Object Persistence

Such logic can determine if and when the cached entitys values should prevail. The logic may be entirely automatic. Alternative, the program could present both versions to the user and let her decide each difference. Concurrency and Dependent Entities What if a bunch of entities are mutually dependent? Suppose we have an order and its details. User A adds two more details and changes the quantity on a third. She deletes the fourth detail and then saves. In many applications, an order is never less than the sum of its parts. The order and every one of its details must be treated as a unit at least for transactional save purposes. We will describe this network of dependency as a Dependency Graph.
DevForce does not offer native support for dependency graphs and its concurrency conflict detection and resolution features target single entity, business object proper concurrency only. We are about to consider how you can extend DevForce concurrency checking for dependency graphs. Well talk more about dependency graphs in general later in this section.

Detection Continuing our story and standing at an Olympian distance with an all knowing eye, we see that User B changed the fifth order detail and saved before User A tried to save her changes. User A didnt touch the fifth order detail. She wont know about the change because there will be no concurrency conflict to detect; she cant detect a concurrency conflict unless she save the fifth order detail and she has no reason to do so. If this worries you (it worries me), you may want to establish business rules that detect concurrency violations for any of entity in a dependency graph. A good approach is to
Identify the root entity of the graph (Order) and Ensure that a change to any node in the graph (OrderDetail) causes the root entity to change.

User Bs change to the fifth detail would have meant a change to the order. User As changes also modified the order. User As save attempt will provoke a concurrency violation on the root entity, the order. Resolution Now that User A has learned about the violation, what can she do? There is no obvious problem. Neither A nor B changed the order entity itself so there are not differences to reconcile. There is only the tell-tale fact that their concurrency column values are different. It doesnt seem proper to proceed blithely, ignoring the violation and proceeding as if nothing happened. User A should suspect something is amiss in the details. The application should re-read all details, even those the user didnt change. It should look for diffences at any point in the graph and only

225 | P a g e

IdeaBlade DevForce

Business Object Persistence

after applying the application-specific resolution rules should it permit the entire order to be saved again. What are those resolution rules? We suggest taking the easiest way out if possible: the application should tell the User A about the problem and then throw away her changes. There must be something fundamentally wrong if two people are changing an order at the same time. In any case, the complexity of sorting out the conflict and the risk of making a total mess during reconciliation argue for a re-start. If you cant take the easy way out if you have to reconcile here are a few pointers. It is probably easiest to use a temporary second EntityManager for the analysis. A single EntityManager can only hold one instance of an entity at a time and we need to compare two instances of the same entity. This is manageable if there is only one entity to deal with weve seen how to use the current and original versions within each entity to carry the difference information. This trick falls apart when we are reconciling a dependency graph. Instead well put User As cached order and its details in one manager and their dopplegangers from User B in another. The author thinks it is best to import User As order and details into the second manager and put User Bs version into the main manager by getting them with the OverwriteChanges strategy. This seems counter-intuitive but there are a couple of good reasons.
We can ImportEntities into the second manager without logging it in. Wed have to log in the second manager before we could use it to get GetEntities. This is not hard, but avoiding it is even easier! The application probably should favor User Bs order; if so that order will be in the main manager where it belongs.

Show some restraint The orders entire object graph is not its dependency graph. The order may consist of details but that may be as far as it goes. For example, every detail is associated with a product. If User B changed the fifth details product name or its color, should this provoke a concurrency conflict? It User C updated the orders customer name, does that mean all orders sold to that customer must be scrutinized for concurrency collisions. Most businesses will say no.

Saving the Dependency Graph


The DevForce relations between entity classes are indicative of associations among those classes. These associations define an object graph which may cast a wide net over the data source data. In this section we consider a portion of that object graph in which a change to one node requires a change to another node. Such nodes form a sub-graph of mutual dependency we could call a dependency graph. Lets look a little closer.
226 | P a g e

IdeaBlade DevForce Association Types


Associations come in a variety of strengths:

Business Object Persistence

Type Association

Description A simple association is typically read as a Has a relationship. An Address has a State or a Part has a Color. The two ends have independent lifetimes. A change to the city or the name of the part does not alter the state or the color.46 An aggregation implies a stronger, Owns a relationship. A Company owns its employees. The two ends still have independent lifetimes. There is still a law against slavery and the employee may transfer to another company. Yet the bond between Company and Employee is stronger than between Part and Color. There are ramifications to the making and breaking of ties. A composition is a whole / part relationship in which the whole is said to consist of or be made up of the parts. An Order is substantially made up of its detailed items. This is the strongest bond. The lifetimes of the two ends are closely tied. If the order disappears, its details disappear with it. Adding, changing, or deleting details alters the parent order.

Aggregation

Composition

DevForce itself has no mechanism for distinguishing among these association types. In fact, DevForce treats its relations as the simplest association. It makes no assumption about the consequences for related entities of any alterations to either parent or child. It is not clear that there is a meaningful programmatic distinction between Association and Aggregation. There will be more business rules surrounding an Aggregation but business rules always require custom coding so the difference is one of degree rather than of kind. The relevant fact in this context is that parent and child may be modified independently. Yes, we must adjust the child if we delete the parent. There may be constraints and consequences to joining and separating parent and child. There can be side-effects of altering parent or child data unrelated to their bond. But, in general, we dont require a modification in one to effect a modification of the other.

Compositions
There are systems that explicitly support the Composition distinction. If you mark a relationship as a composition, the system will implement it differently. The parts (children) in a composition will be contained by the whole (parent) and they may only be accessed through the parent. If you marked an Orders OrderDetails property as a composition, the only way to obtain details would be through this property. OrderDetails fetched through any other mechanism would be different objects than the conceptually same entities fetched with the OrderDetails property.

46

They may become incompatible as when the change to city moves the address to a different state or the part turns out to be colorless but compatibility is a matter for business rules unrelated to the fundamental nature of the association.

227 | P a g e

IdeaBlade DevForce

Business Object Persistence

We think that is a rare and extreme position which is more trouble than it is worth. The developer can program to it when it occurs but DevForce does not encourage the practice with any means of its own. No mechanism is provided to mark the OrderDetails navigation property as a Composition. But this is not to diminish the importance of the Composition bond. In many applications, we should consider the Order modified if we add, change, or delete one of its OrderDetail entities. If we delete the Order, we almost certainly intend to delete its details as well. This is precisely the behavior sought by systems with native support for composition. But we can achieve the same effect in DevForce. It is not hard work, although it requires some care. The reward is flexibility. Each application has its own requirements.We can offer only a brief outline of the main points here.
Our application save operation concentrates on the root entity (or enties) of the dependency graph. We implement a Saving handler to invoke composition business rules of the entities. We add the composition business rules to the business object, wrapped in a method the Saving handler can call. We provide for intelligent concurrency resolution to detect and manage the collision of our changes with changes by other users.

Save the Root Entity


This step is irrelevant if our save operation calls one of the Save all methods of the EntityManager. The Save all methods saves every changed entity in the cache. Our Saving handler must be clever because it might encounter a child entity before its parent. It may not learn of the parent at all; the child entity will be responsible for modifying its parent and including that parent in the list of entities to save. On the other hand, if we choose to save a particular set of entities the current order and its graph for example it may be convenient to compose the save list entirely of root entities orders in this case. We will see in a moment that compositional business rules ensure that (a) the root entity is in an altered state and (b) its modified dependent objects are also in the save list.

Saving Event Handler


Remember, the EntityManager raises the Saving event whenever it is ready to write entities to the host data sources. We were going to write a Saving handler anyway. We should validate every business object just before saving it to make sure it is safe to persist. The best approach is to write a Saving event handler that iterates over the list of entities-to-save (the save list), calling a validate method on each. We might as well extend this approach to call a PrepareSave method instead that both validates and enforces composition business rules.

228 | P a g e

IdeaBlade DevForce Composition Business Rules

Business Object Persistence

We may have any number of composition business rules. One of them must ensure that, if a child is on the save list, its parent is also on the list. Thats easy because we can always add (and remove) items from the save list within the Saving event handler. Another composition rule must ensure that any change to a child entity modifies its parent. Thats necessary because DevForce will only save an entity that has been changed. It is not sufficient merely to add the parent to the save list; we must make sure it is in an altered state.
Code Snippet 47. EnforcingConcurrencyCheckingOnAConceptualEntity()

C#

anOrderDetail.UnitPrice *= 1.1M; Order parentOrder = anOrderDetail.Order; if (parentOrder.EntityAspect.EntityState == EntityState.Unchanged) { parentOrder.EntityAspect.SetModified(); } anOrderDetail.UnitPrice *= 1.1D Dim parentOrder As Order = anOrderDetail.Order If parentOrder.EntityAspect.EntityState = EntityState.Unchanged Then parentOrder.EntityAspect.SetModified() End If

VB

Concurrency Violations
We always use transactional saves. Weve taken steps to ensure that all members of the dependency graph the order and all of its details, for example, - are part of the same save list and are slated for persistence as a single transaction. When DevForce persistence layer detects a concurrency violation, it terminates the transaction and returns the offending entity as we learned earlier. Chances are there will be more than one entity in the transaction that is in potential concurrency conflict with its corresponding object in the data source. The end user will be most unhappy if we walk her through each entity one by one. We should resolve the concurrency conflicts of all entities in the dependency graph in a single shot. While the exact details will be application specific, they will be some variation on the techniques you learned for resolving conflicts of individual entities.

Dependency Graph Retrieval


Many large DevForce applications use multiple EntityManagers (PM) to maintain separate editing contexts. They often need to transfer entire entity graphs between PMs. For example, an application might have a main PM to hold lists of entities. One list might hold SalesReps and the application could display that list in a grid. Double clicking on one SalesRep row launches a popup editor for the selected sales rep. Double clicking a different SalesRep row launches a second popup editor for the rep in that row.
229 | P a g e

IdeaBlade DevForce

Business Object Persistence

The user can make changes to the first rep, switch to the second editor and make changes to that rep, go back to the first editor, make more changes, and save them. The second editor (and the rep it edits) remains open, and its pending changes are not saved. The user may decide to cancel the second editor, discarding the changes; of course the changes to the first rep have been saved independently. To implement this scenario, we recommend that each editor have its own PM which constitutes an editing context that is independent both of the main PM and of other editors. When the application launches an editor, it populates the editors PM with the selected SalesRep from the grid. Because that SalesRep is in the main PM, the application will likely transfer (import) the selected SalesRep into the editors PM. At this moment, the rep in the editor PM is a clone of the rep in the main PM. After save, the application might export the saved rep back into the main PM where it now displays in the grid in its post-save glory[1]. Notice that we mentioned only the transfer of a single entity the root entity between the PMs. In practice, we often want to transfer both the root entity and many of its related entities. We might transfer the sales rep and his order information (Orders, OrderDetails) as well so that the entire entity graph can be edited in a single context. Heretofore, the developer would have to implement the logic to gather up the entities in the graph before transferring them, a task that could require a sophisticated knowledge of the DevForce Object Query Language. Now she can use the DevForce span technology to compose a single query-like statement to do the job. The following example implements the scenario described above:

C#

// Copy selected Employees and their Orders, OrderDetails, and Products // from one PM to another. private void GetGraph_OneRootOneSpan() { DomainModelEntityManager em1 = new DomainModelEntityManager(); DomainModelEntityManager em2 = new DomainModelEntityManager(); int employeeID = 1; var targetedEmployeesQuery = em1.Employees .Where(e => e.EmployeeID == employeeID); int targetedEmployeesCount = targetedEmployeesQuery.Count(); if (targetedEmployeesCount != 1) { Console.WriteLine("Unable to retrieve Employee with EmployeeID == {0}", employeeID); PromptToContinue(); return; } // FindEntityGraph() operates against the cache only: it does not retrieve // entities into the cache. So let's retrieve the desired entities... Console.WriteLine("Retrieving Emp-Orders-OrderDetails-Products..."); List<Employee> employees = targetedEmployeesQuery .Include("Orders.OrderDetails.Product").ToList(); Employee anEmployee = employees[0];

230 | P a g e

IdeaBlade DevForce
// Create roots list and add the employee. List<Entity> roots = new List<Entity>(); roots.Add(anEmployee); // Add span(s). List<EntitySpan> spans = new List<EntitySpan>(); EntitySpan aSpan = new EntitySpan(typeof(Employee), EntityRelations.FK_Order_Employee, EntityRelations.FK_OrderDetail_Order, EntityRelations.FK_OrderDetail_Product); spans.Add(aSpan); // Get entity graph for entities in roots EntityState entityState = EntityState.Unchanged;

Business Object Persistence

IList<Object> entityGraph = em1.FindEntityGraph(roots, spans, entityState); Console.WriteLine("{0} entities collected by FindEntityGraph.", entityGraph.Count ); // Import graph into a second EntityManager em2.ImportEntities(entityGraph, MergeStrategy.OverwriteChanges); }

VB

Private Sub GetGraph_OneRootOneSpan() Dim em1 As New DomainModelEntityManager() Dim em2 As New DomainModelEntityManager() Dim employeeID As Integer = 1 Dim targetedEmployeesQuery = em1.Employees.Where(Function(e) e.EmployeeID = employeeID) Dim targetedEmployeesCount As Integer = targetedEmployeesQuery.Count() If targetedEmployeesCount <> 1 Then Console.WriteLine("Unable to retrieve Employee with EmployeeID == {0}", employeeID) PromptToContinue() Return End If ' FindEntityGraph() operates against the cache only: it does not retrieve ' entities into the cache. So let's retrieve the desired entities... Console.WriteLine("Retrieving Emp-Orders-OrderDetails-Products...") Dim employees As List(Of Employee) = targetedEmployeesQuery.Include("Orders.OrderDetails.Product").ToList() Dim anEmployee As Employee = employees(0) ' Create roots list and add the employee. Dim roots As New List(Of Entity)() roots.Add(anEmployee) ' Add span(s). Dim spans As New List(Of EntitySpan)() Dim aSpan As New EntitySpan(GetType(Employee), EntityRelations.FK_Order_Employee, EntityRelations.FK_OrderDetail_Order, EntityRelations.FK_OrderDetail_Product) spans.Add(aSpan) ' Get entity graph for entities in roots Dim entityState As EntityState = entityState.Unchanged Dim entityGraph As IList(Of Object) = em1.FindEntityGraph(roots, spans, entityState) Console.WriteLine("{0} entities collected by FindEntityGraph.", entityGraph.Count)

231 | P a g e

IdeaBlade DevForce

Business Object Persistence

' Import graph into a second EntityManager Console.WriteLine() Console.WriteLine("Entities imported to second EntityManager:") Console.WriteLine("------------------------------------------") em2.ImportEntities(entityGraph, MergeStrategy.OverwriteChanges) DisplayCacheContents(em2) PromptToContinue() End Sub

Workflow For a Save


Lets put most of these ideas together along with our other knowledge of DevForce business objects and look at a schematic workflow for saving all pending changes to a single database.
Table 8. Transactional Save Workflow in an N-Tier Deployment

Component Client Tier Application Code Client EntityManager

Middle Tier Business Object Server

Client EntityManager Middle Tier Business Object Server

Middle Tier Business Object Server Data Tier - Data Source Middle Tier Business Object Server

Action The client application adds, modifies and deletes any number of business objects on the client. The client application asks a EntityManager to save all pending changes. Makes a save list of the new, modified, and deleted entities in cache. Fires the Saving event. Assume that application listener okays the save. Connects to the data source and authenticates the user. Assume success. If there are any temporary ids, the PM sends them to the BOS for fix-up. Builds map of data source-generated ids (e.g., for Identity columns). Calls method on instance of developers id generation class with remaining temporary. This method returns a map of temporary-to-permanent ids which the BOS returns to the client tier. Uses the temp-to-perm id map to replace all temporary ids. Transmits the save list to the BOS. First the Saving event. This can be used to perform security checks on each entity in the save list. If a security check fails, an exception can be thrown back to the EntityManager (or any other desired action taken.) Workflow ends. Otherwise Constructs a batch of insert, update, and delete operations, adjusted for optimistic concurrency checking as required. Arranges them by type per the prescribed PersistenceOrder. Forwards them to the Entity Framework for execution. Performs the persistence operations. If there are no failures, it commits them; if there is a single failure, it rolls them all back. If the transaction failed, returns to the EntityManager the identity of the culprit entity and the exception raised by the data source. The EntityManager stores this information in the SaveResult and returns to the client application. Workflow ends. Otherwise The transaction succeeded. The BOS re-queries the database(s) for all of the inserted and modified entities that are sourced in databases, thus capturing the effects of triggers that fired during save. Converts the (potentially) revised data into entities and sends them to the client side EntityManager. The servers local copy of the entities go out of scope and the garbage collector 232 | P a g e

IdeaBlade DevForce

Business Object Persistence


reclaims them. This enables the object server to stay stateless. Replaces cashed entities with updates from BOS. They are marked unchanged because they are now current. Raises the Saved event with list of saved inserted and modified entities. The application resumes.

Client Tier EntityManager Client Tier Application Code

Saving the Cache to a Local Disk File


EntityManager has a property, CacheStateManager, that can be used to capture, save, and restore the contents of the EntityManagers cache to a local disk file. The following statements save the contents of the cache managed by EnttyManager _mgr:
Code Snippet 48. SaveRestoreCacheToDisk()

C#

string cacheFilePath = System.IO.Directory.GetCurrentDirectory() + "EntityCacheState.bin"; _em1.CacheStateManager.SaveCacheState(cacheFilePath); Dim cacheFilePath As String = System.IO.Directory.GetCurrentDirectory() & "EntityCacheState.bin" _em1.CacheStateManager.SaveCacheState(cacheFilePath)

VB

These statements restore the contents of the EntityCacheState file "C:\_DevForceCache.dat" to the EntityManagers current cache:

C# VB

_em1.CacheStateManager.RestoreCacheState(cacheFilePath); _em1.CacheStateManager.RestoreCacheState(cacheFilePath)

When called using the overload above, the restore operation using RestoreStrategy.Normal. That RestoreStrategy restores the data in the cache state file using a MergeStrategy of PreserveChanges (see the discussion of this elsewhere in this chapter); it also restores the DefaultSaveOptions and DefaultQueryStrategy saved in that file, overwriting the current values for those properties. When RestoreStrategy.Normal doesnt meet your needs, you can restore using a custom RestoreStrategy:

C#

bool restoreSaveOptions = true; bool restoreQueryStrategy = false; RestoreStrategy aRestoreStrategy = new RestoreStrategy( restoreSaveOptions, restoreQueryStrategy, MergeStrategy.OverwriteChanges); _em1.CacheStateManager.RestoreCacheState(cacheFilePath, aRestoreStrategy); Dim restoreSaveOptions As Boolean = True Dim restoreQueryStrategy As Boolean = False

VB

233 | P a g e

IdeaBlade DevForce

Business Object Persistence

Dim aRestoreStrategy As New RestoreStrategy(restoreSaveOptions, restoreQueryStrategy, MergeStrategy.OverwriteChanges) _em1.CacheStateManager.RestoreCacheState(cacheFilePath, aRestoreStrategy)

CacheStateManager also includes a method, GetCacheState(), which returns the state of the cache as a serializable in-memory object:

C# VB

EntityCacheState cacheState = _em1.CacheStateManager.GetCacheState(); Dim cacheState As EntityCacheState = _em1.CacheStateManager.GetCacheState()

This can be used in a variety of ways; for example, in a server-side method called from the client using EntityManager.InvokeServerMethod() or InvokeServerMethodAsync(), you could fill an EntityManagers cache with any arbitrary collection of data, capture that in an EntityCacheState, and return that EntityCacheState to the client where it could be restored using another overload of RestoreCacheState:

C# VB

_em1.CacheStateManager.RestoreCacheState(cacheState); _em1.CacheStateManager.RestoreCacheState(cacheState)

You can also, of course, encrypt the cache state before saving it to local storage.

XML Serialization of Business Objects


IdeaBlade entities can be serialized as XML for any number of purposes, including exposing these entities to a Web Service, or as the first step in some XSLT transform for reporting or further processing. Please see Microsofts WCF documentation for detailed examples of exposing objects to Web Services via data contract serialization. All entities generated by DevForce are marked with WCF DataContract attributes; and all public properties of these entities are marked with a DataMember attribute. Serialization using standard WCF via either the
System.Runtime.Serialization.DataContractSerializer, or the System.Runtime.Serialization.NetDataContractSerializer

is supported. One of the big issues with XML Serialization when serializing object graphs (objects that are connected to other objects, ad-infinitum) has to do with the depth of the object graph that should be serialized.

234 | P a g e

IdeaBlade DevForce

Business Object Persistence

Without some mechanism to control the depth of the graph, the serialization of a single entity might result in hundreds or even thousands of related entities being serialized. DevForce controls this by only serializing entities that are present within an EntityManagers cache at the inception of serialization and are navigable from the directly serialized entities. (Think of this as all entities that are available via a CacheOnly query) This allows fine-grained control over what will be serialized. Any relation properties that would return an entity or entities that are not in the cache will instead serialize the property value either as a null entity (for scalar properties), or as an empty collection (for collection properties). The examples serializes two employees along with all related Orders and their line items (OrderlDetails):
Code Snippet 49. SerializeBusObjects()

C#

private void SerializeBusObjects(string serializerName) { var employees = _em1.Employees .OrderBy(e => e.LastName).Take(2).Include("Orders.OrderDetails").ToList(); var aMemoryStream = new MemoryStream(); var aXmlDictionaryWriter = System.Xml.XmlDictionaryWriter.CreateTextWriter(aMemoryStream); if (serializerName == "NetDataContactSerializer") { SerializeWithNetDataContractSerializer(employees, aXmlDictionaryWriter); } else { SerializeWithDataContractSerializer(employees, aXmlDictionaryWriter); } aXmlDictionaryWriter.Flush(); aMemoryStream.Position = 0; string result = StreamFns.ToString(aMemoryStream); } private static void SerializeWithNetDataContractSerializer(List<Employee> employees, System.Xml.XmlDictionaryWriter aXmlDictionaryWriter) { NetDataContractSerializer aNetDataContractSerializer = new NetDataContractSerializer(); aNetDataContractSerializer.WriteObject(aXmlDictionaryWriter, employees); } private static void SerializeWithDataContractSerializer(List<Employee> employees, System.Xml.XmlDictionaryWriter aXmlDictionaryWriter) { DataContractSerializer aDataContractSerializer = new DataContractSerializer( typeof(Employee), new Type[] { typeof(List<Employee>) }, int.MaxValue, false, true, null); aDataContractSerializer.WriteObject(aXmlDictionaryWriter, employees); }

VB

''' <summary> ''' DataContract serialization ''' </summary> Private Sub SerializeBusObjects(ByVal serializerName As String) Dim employees = _em1.Employees.OrderBy(Function(e) e.LastName).Take(2).Include("Orders.OrderDetails").ToList() Dim aMemoryStream = New MemoryStream() Dim aXmlDictionaryWriter = System.Xml.XmlDictionaryWriter.CreateTextWriter(aMemoryStream) If serializerName = "NetDataContactSerializer" Then SerializeWithNetDataContractSerializer(employees, aXmlDictionaryWriter) Else SerializeWithDataContractSerializer(employees, aXmlDictionaryWriter) End If

235 | P a g e

IdeaBlade DevForce

Business Object Persistence

aXmlDictionaryWriter.Flush() aMemoryStream.Position = 0 Dim result As String = StreamFns.ToString(aMemoryStream) Console.WriteLine("Two employees, serialized using the {0}..." & vbLf, serializerName) Dim abbreviatedResult As String = result.Substring(0, 500) & vbLf & vbLf & "...[snip]..." & vbLf & vbLf & result.Substring(result.Length - 201) Console.WriteLine(abbreviatedResult) PromptToContinue() End Sub Private Sub SerializeWithNetDataContractSerializer(ByVal employees As List(Of Employee), ByVal aXmlDictionaryWriter As System.Xml.XmlDictionaryWriter) Dim aNetDataContractSerializer As New NetDataContractSerializer() aNetDataContractSerializer.WriteObject(aXmlDictionaryWriter, employees) End Sub Private Sub SerializeWithDataContractSerializer(ByVal employees As List(Of Employee), ByVal aXmlDictionaryWriter As System.Xml.XmlDictionaryWriter) Dim aDataContractSerializer As New DataContractSerializer(GetType(Employee), New Type() {GetType(List(Of Employee))}, Integer.MaxValue, False, True, Nothing) aDataContractSerializer.WriteObject(aXmlDictionaryWriter, employees) End Sub

236 | P a g e

Business Object Persistence Advanced

237 | P a g e

Business Object Persistence Advanced .................................................................................. 237


State of the Release Candidate Documentation ................................................................................................ 239 Getting Information About an Entity Type with GetEntityMeta() ................................................................. 239 Access Both Local and Remote Data Sources In the Same N-tier Application ............................................. 240 Stored Procedure Queries ................................................................................................................................... 241 SQL Server Stored Procedure Queries .............................................................................................................. 243 Stored Procedure Entity Navigation .................................................................................................................. 246 Forced Re-fetch .................................................................................................................................................... 246 Lost Connection During Query .......................................................................................................................... 248 Query Cache ......................................................................................................................................................... 249 EntityManager.RemoveEntities Overload Preserves Query Cache ................................................................... 249 MergeStrategy In More Detail............................................................................................................................ 251 The EntityManager.AttachEntity Method ........................................................................................................ 255 Filtering Queries .................................................................................................................................................. 258 Query Inversion in More Detail ......................................................................................................................... 260 Transactional Queries ......................................................................................................................................... 265 DevForce and Data Sources Deep Dive ........................................................................................................... 266 DataSourceKeys, DataSourceKeyResolvers, and DataSourceExtensions ......................................................... 267 EntityManagers and DataSourceExtensions ...................................................................................................... 268 Tenant Extensions.............................................................................................................................................. 271 Multi-Part Extensions ........................................................................................................................................ 271 Extensions and EntityServers ............................................................................................................................ 272 Dynamic DataSourceKeys and the DataSourceKeyResolver ............................................................................ 273 Multiple Application Environments ................................................................................................................... 275 Multiple EntityManager Instances..................................................................................................................... 276 Multi-Threading in a DevForce App ................................................................................................................. 277 Batching Asynchronous Tasks ............................................................................................................................ 279 Service Oriented Architecture ............................................................................................................................ 281 POCO Support in DevForce ............................................................................................................................... 283 Examples of POCO Classes ............................................................................................................................... 284 Examples of a POCO Service Provider Class .................................................................................................... 285 Example of a Client-Side Class Containing Extension Methods for the EntityManager ................................... 288 Obtaining an EntityAspect Property on Your POCO Object ............................................................................. 289 238 | P a g e

Data Contract Serializer (DCS) versus .NET Data Contract Serializer (NDCS) .............................................. 290 POCO Save mechanisms ................................................................................................................................... 293 Summary Things to Remember When Using POCOs in Your DevForce App .............................................. 298

State of the Release Candidate Documentation


We are working hard to update all of our documentation from DevForce 2009 to DevForce 2010, .NET 4, and Silverlight 4. During this conversion, you may find some sections that are out of date, but you should be able to get many of the examples to work, with small modifications, by checking against the API Documentation for the current method signatures.

Getting Information About an Entity Type with GetEntityMeta()


The instance method GetEntityMetadata() on the EntityMetadataStore type returns an EntityMetadata object that is rich with information about a specified entity type:

C#

EntityMetadata employeeEntityMetaData = EntityMetadataStore.Instance.GetEntityMetadata(typeof(DomainModel.Employee));

VB The EntityMetadata objects provides the following members:

239 | P a g e

The table below provides an explanation for key members:

Property Property Property Method Property Property Property Property Property Property Property

CanQueryByEntityKey ComplexTypeProperties ConcurrencyProperties CreateEntity() DataEntityProperties DataSourceKeyName DefaultEntitySetName EntityProperties EntityType KeyProperties NavigationProperties

Gets whether primary key queries are allowed. Returns a collection of EntityProperties that describe complex object properties for entities of this type. Returns a collection of EntityProperties that are concurrency properties for entities of this type. Creates a new entity of the type described by this metadata item. Returns a collection of DataEntityProperties for entities of this type. Gets the name of the Data Source Key associated with this type. Gets the default EntitySetName for entities of this type. Returns a collection of EntityProperties that belong to entities of this type. Gets the type of the entity. Returns a collection of EntityProperties that are keys for entities of this type. Returns a collection of NavigationEntityProperties for entities of this type.

Such metadata can be useful in many situations. For example, suppose you wish to dynamically populate a form with bound controls for the properties of a type. You could easily get the list you need from the EntityMetadataStore.

Access Both Local and Remote Data Sources In the Same N-tier Application
An application may need to persist volatile data to a centrally hosted database and have the ability to simultaneously access comparatively static data on a local database. Field technicians who service complex machine parts may need ready access to voluminous parts catalogs and repair manuals. The catalog and repair data dont change often . They may be stored in a database on the techs laptop. On the other hand, the central office needs to monitor the technicians rounds and dispatch him to new client sites. There could be significant exchange of information between the dispatch center and the remote technician. A DevForce program should be able to provide access to both the remote and local database in an n-tier deployed application. One of the EntityManager constructors facilitates construction of such an application.
240 | P a g e

C#

public EntityManager( bool pShouldConnect, String pDataSourceExtension, PersistenceServiceOption pPersistenceServiceOption)

VB

The caller sets the third parameter to the value of a PersistenceServiceOption enumeration that indicates how the how the EntityManagers PersistenceService should be configured.
C#
public enum PersistenceServiceOption { /// <summary> /// Use the Ibconfig file [remoting][remotePersistenceEnabled] node. /// </summary> UseDefaultService = 0, /// <summary> /// Use a local service - Service will run in process with the client /// The Ibconfig file [remoting][remotePersistenceEnabled] node is ignored. /// </summary> UseLocalService = 1, /// <summary> /// Use a remote service as defined in the Ibconfig file [remoting] node. /// The Ibconfig file [remoting][remotePersistenceEnabled] node is ignored. /// </summary> UseRemoteService = 2 }

VB

After configuration, the EntityManager will connect either to the remote database or to the local database. A specific PM cannot switch between the two modes. But the application can have more than one EntityManager and bridge the two at convenient moments which is exactly how wed approach the scenario described above.

Stored Procedure Queries


Stored procedure queries are not available in DevForce 2010 RC1.

We broached the subject earlier of the occasional need to use a stored procedure to query for business objects. The need arises most frequently when we require the entities resulting from an extraordinarily complex query involving large volumes of intermediate data that are not themselves required on the client. One might imagine a multi-step query that touched several tables, performed multi-way joins, ordered and aggregated the intermediate results, and compared values with many thousands of records, all so as to return a handful of qualifying results. All of the other data were needed only to satisfy the query; the user wont see any of them and there is no point to transmitting them to the client.
241 | P a g e

This is a clear case for a stored procedure because we can and should maximize performance by performing all operations as close to the data source as possible. Chances are that the entities returned by the stored procedure are entities we already know. That procedure could be just an especially resource-consuming query for Order entities that we retrieve and save in the usual way under normal circumstances. The Stored Procedure Query is perfect for this situation. We define such a query, identify Order as the query return type, and turn it loose on the database. We accept the sproc-selected Order objects and work with them in our typical merry way. Note that a stored procedure query, by its nature, must be executed by the database: we cant run it against the entity cache47. So we may not invoke it while the application is running offline.

Accessing Related Entities Via Navigation Properties On Entities Retrieved Using Stored Procedure Queries
When using a stored procedure query, the Entity Framework handles the retrieval of information about related entities differently than it does for normal queries. In the normal case, foreign key values are retrieved and retained with the returned entities. These foreign key values are not exposed as public properties on the returned entities, but theyre present under the covers. For entities retrieved via stored procedures, the EF does not have sufficient information reliably to identify foreign keys, and so does not retrieve values for any. Recall that in the Entity Framework in contrast to the behavior DevForce -- all related entities must be retrieved by explicit command. When such command is given, EF always returns the related entities. But for parent entities that were retrieved using stored procedures, it necessarily uses a different (and less performant) process to get the related entities than for entities retrieved using ordinary queries. That is made necessary by the lack of foreign key values on the parent entities.

DevForce, in contrast to the EF, retrieves related entities automatically; all you need do is to make reference to them. However, in the case of entities retrieved via stored procedure queries, we had to make a tough call. One choice was to retrieve foreign key values automatically during any stored procedure query. That would produce the simplest and most intuitive behavior on the client: for all entities, retrieval of related entities referenced through navigation properties would be automatic.
But of course there was a problem: each foreign key requires an additional round trip to the database from the object server; and there is, of course, a performance price for this. We elected to make the default the more performant choice: unless you ask for them explicitly, we do not retrieve the foreign key values during stored procedure queries. In consequence, by default, references to navigation properties on such entities will return Null Entities. If you know you will need the related entities for entities retrieved using a stored procedure proc, you can get them via the ShouldLoadEntityRefs property on the StoredProcQuery. If you set this property to true the default is false -- all foreign key properties on the entity are looked up during the initial query, and references to related entities will return the proper entities.

47

There is an advanced technique for applying a stored procedure query to the cache that we cover briefly in Advanced Business Object Concepts.

242 | P a g e

SQL Server Stored Procedure Queries


Suppose your data source table includes a stored procedure named SalesByYear. It is defined as follows:

TSQL

ALTER procedure "SalesbyYear" @Beginning_Date DateTime, @Ending_Date DateTime AS SELECT OrderSummary.ShippedDate, OrderSummary.id, "Order Subtotals".Subtotal, DATENAME(yy,ShippedDate) AS Year FROM OrderSummary INNER JOIN "Order Subtotals" ON OrderSummary.Id = "Order Subtotals".OrderSummaryId WHERE OrderSummary.ShippedDate Between @Beginning_Date And @Ending_Date

When included among the items imported into an Entity Data Model, this results in the following Function element in the schema (SSDL) section of the Entity Model file:

XML

<Function Name="SalesbyYear" Schema="dbo" Aggregate="false" BuiltIn="false" NiladicFunction="false" IsComposable="false" ParameterTypeSemantics="AllowImplicitConversion"> <Parameter Name="Beginning_Date" Type="datetime" Mode="In" /> <Parameter Name="Ending_Date" Type="datetime" Mode="In" /> </Function>

To make this convenient available for calling directly off of our EntityManager (as you would equally have to do to make it available on the ADO.NET ObjectContext), you must add a FunctionImport element in the conceptual model (CSDL) section of the Entity Model:

XML

<FunctionImport Name="GetSalesByYear" EntitySet="SalesByYearResults" ReturnType="Collection(IdeaBladeTest1Model.EF.SalesbyYear)"> <Parameter Name="Beginning_Date" Type="DateTime" Mode="In" /> <Parameter Name="Ending_Date" Type="DateTime" Mode="In" /> </FunctionImport>

This will cause a C# or VB method to be generated in your EntityManager class by the name you specified, GetSalesByYear. Note that the FunctionImport element also specifies the EntitySet into which results returned by the stored proc will be housed: SalesByYearResults; and the return type of the method, which will be a collection of SalesByYear entities.

The SalesByYear Entity type must be defined in your conceptual model:

243 | P a g e

XML

<EntityType Name="SalesbyYear" Abstract="false" ib:PrevName="SalesbyYear"> <Key> <PropertyRef Name="ShippedDate" /> </Key> <Property Name="ShippedDate" Type="DateTime" Nullable="false" /> <Property Name="id" Type="Int64" Nullable="false" /> <Property Name="Subtotal" Type="Decimal" Nullable="false" Precision="19" Scale="4" /> <Property Name="Year" Type="String" Nullable="false" MaxLength="4" /> </EntityType>

The method specified in the conceptual model in the FunctionImport element must be mapped to the Function element in the SSDL that represents the stored procedure. That mapping must, of course, be specified in the mapping (MSL) section of the Entity Model:

XML

<FunctionImportMapping FunctionImportName="GetSalesByYear" FunctionName="IdeaBladeTest1Model.EF.Store.SalesbyYear" />

Having done all of that in your Entity Model, you can now use the resultant method as shown following two examples:

C#

_em1 = new IdeaBladeTest1Entities(); [TestMethod] public void StoredProcQuery() { DateTime dt1 = DateTime.Parse("1/1/1990"); DateTime dt2 = DateTime.Parse("1/1/2000"); var results = _em1.GetSalesByYear(dt1, dt2); } [TestMethod] public void StoredProcQuery2() { DateTime dt1 = DateTime.Parse("1/1/1995"); DateTime dt2 = DateTime.Parse("12/31/1996"); var results = _em1.GetSalesByYear(dt1, dt2).Where(s => s.Subtotal > 2500); }

VB

The method is simply called on the EntityManager with appropriate parameters. It returns an IEnumerable<SalesByYear>, which can be subjected to qualifying filters as you see in the second example above. Below is the Generated code in the domain model designer code file for the GetSalesByYear() method:

244 | P a g e

C#

#region GetSalesByYear StoredProcQuery /// <summary> /// Constructs and executes the <see cref="T:IdeaBlade.EntityModel.StoredProcQuery"/> /// associated with the given stored procedure. /// </summary> public IEnumerable<IdeaBladeTest1Model.SalesbyYear> GetSalesByYear( Nullable<DateTime> Beginning_Date, Nullable<DateTime> Ending_Date) { StoredProcQuery query = GetSalesByYearQuery(Beginning_Date, Ending_Date); return this.ExecuteQuery<IdeaBladeTest1Model.SalesbyYear>(query); } /// <summary> /// Constructs and returns the <see cref="T:IdeaBlade.EntityModel.StoredProcQuery"/> /// associated with the given stored procedure. /// </summary> public StoredProcQuery GetSalesByYearQuery( Nullable<DateTime> Beginning_Date, Nullable<DateTime> Ending_Date) { QueryParameter Beginning_DateParameter; if (Beginning_Date.HasValue) { Beginning_DateParameter = new QueryParameter("Beginning_Date", Beginning_Date); } else { Beginning_DateParameter = new QueryParameter("Beginning_Date", typeof(DateTime)); } QueryParameter Ending_DateParameter; if (Ending_Date.HasValue) { Ending_DateParameter = new QueryParameter("Ending_Date", Ending_Date); } else { Ending_DateParameter = new QueryParameter("Ending_Date", typeof(DateTime)); } StoredProcQuery query = new StoredProcQuery(typeof(IdeaBladeTest1Model.SalesbyYear), "GetSalesByYear", Beginning_DateParameter, Ending_DateParameter); return query; } #endregion GetSalesByYear StoredProcQuery

VB

For the record, heres an alternative way to invoke your stored procedure:

C#

[TestMethod] public void StoredProcQuery3() { DateTime dt1 = DateTime.Parse("1/1/1996"); DateTime dt2 = DateTime.Parse("12/31/1998"); StoredProcQuery query = new StoredProcQuery(typeof(SalesbyYear)); // Note that a FunctionImport must be defined in the Entity Model query.ProcedureName = "GetSalesByYear"; query.Parameters.Add(new QueryParameter("Beginning_Date", dt1)); query.Parameters.Add(new QueryParameter("Ending_Date", dt2)); var results = _em1.ExecuteQuery<SalesbyYear>(query); }

VB 245 | P a g e

Stored Procedure Entity Navigation


Dot Navigation is a bit tricky for business objects that are defined by a stored procedure (sproc entities). If the source class is a sproc entity, the tool can implement the Source.Target navigation property if the target class is a table or view entity. Unfortunately, there is no obvious way to automatically generate the implementation if the target is also a sproc entity. Consider an example. Suppose the source is Customer and the target is Order and both are mapped to stored procedures. In principle we could map the Customer to Order by creating a relation that joins Order.CustomerId to 48 Customer.Id . We tell the tool implement this! Unfortunately, the Object Mapper must give up immediately. The tool knows the signature of the base stored procedure but has no idea how the sproc actually responds to different parameter values. Therefore, it can not invoke the Orders underlying stored procedure such that the sproc returns all orders for a given customer. That operation may not even be possible. A developer can interpret the stored procedure well enough to know what call (if any) would do the job. Accordingly, the developer may choose to implement a Customer.Orders property within the custom logic of the Customer class, using a stored procedure query.
The same conundrum confronts us when we devise a relation heading the other direction, from any business object entity to a stored procedure entity. Once again, the Object Mapper does not know how to call the stored procedure so that it returns the objects expected by the source entity type.

Table 9 summarizes the situation.

Table 9. Who writes the navigation property involving a sproc entity.

Navigation property Source Entity Type Sproc Sproc Any type Target Entity Type Table or View Sproc or Web Service Sproc

Relation Written By Tool Developer

Forced Re-fetch
There are a number of methods that help us re-fetch specific entities from their data sources. Among them are EntityList.ForceRefetch and EntityManger.RefetchEntities<T>. They assume the OverwriteChanges merge strategy but we can give them any of the other merge strategies.

48

In fact you cant do this within the Object Mapper for reasons we are now discussing.

246 | P a g e

OverwriteChanges replaces the cached entities, overwriting our pending changes. We often want to (a)

keep pending changes but (b) refresh copies of unmodified entities. The PreserveChanges strategies can help us achieve our purpose.

Table 10. PreserveChanges strategies in a forced re-fetch

Strategy
PreserveChanges PreserveChangesUnless OriginalObsolete PreserveChangesUpdateOriginal

Description
Replace unchanged entities but keep changed entities as they are. Replace unchanged entities and changed entities that are obsolete (i.e., that would fail an optimistic concurrency check if saved now). Replace unchanged entities. Keep changed entities and make them current if they are obsolete by updating their original versions.

Custom Navigation property with Forced Re-fetch


Navigation properties execute according to strategy prescribed by the EntityManager. DefaultQueryStrategy. The default is Normal. We can change it dynamically but the Normal strategy is the best default choice for most applications so lets assume we leave it that way. The first time we call the navigation property the PM will get the entities from the data source and put them in the cache. The next time, and every subsequent time, the navigation property will look in the cache first and find the entities there. So during the entire user session these entities may never be refreshed. This is great for a list of states but not so great for more volatile entities such as theater seats. Some developers will be tempted to override the navigation property to get fresh data from the data source every time. The following is a typical example that strives to keep the Customer.Orders ultracurrent:

C#

public override ReadOnlyEntityList<Order> Orders { get {return base.Orders. ForceRefetch(MergeStrategy.Overwrite);} } Public Overrides ReadOnly Property Orders() As _ IdeaBlade.Persistence.ReadOnlyEntityList(Of Order) Get Return MyBase.Orders.ForceRefetch(MergeStrategy.Overwrite) End Get End Property

VB

247 | P a g e

Performance is likely to be terrible. Entity properties fire frequently and sometimes unexpectedly. Properties should return quickly. This one goes to the data source every time. Not good. The intention is laudable and we can make this work. One approach is to remember the last time we invoked this method. If we just did it, return with the most recently fetched list. If we did it too long ago, force the re-fetch.

Lost Connection During Query


What if the EntityManager cant reach the data source when processing a query49 either because of a network connection problem or because the data source is unavailable? This is a non-issue for the CacheOnly query but applies to all other fetch strategies. The PersisenceManager responds differently depending upon whether or not it knows that the connection is broken before attempting the query. If it knows it is disconnected, its behavior is simple: treat every query as a CacheOnly query. This is consistent with the general principle that writing code for a disconnected application should be as easy as possible. We shouldnt have to write a lot of special case logic once we have acknowledged that the application is off-line.

Unexpected loss of connection


When the EntityManager believes it is connected, it will attempt to search the database once the cache proves inadequate. If in fact it is not connected or the connection is broken during the search, the EntityManager will and then raise an event. If the application doesnt handle the event, it throws an exception. If the EntityManager believes it is connected and discovers that it cant reach the data source while processing the query, it will take the following steps in sequence.
6. 7. 8. change its internal state to disconnected raise An EntityServerError event throw An EntityServerException unless the event says it handled the problem.

We can and should supply the EntityManager with An EntityServerError event handler. Our handler can quickly tell that the cause is a connection problem. It can distinguish between network connection failure and data source unavailability. If we know what to do, we can do it and signal that weve handled it; the EntityManager wont throw an exception. If we dont handle the event or dont signal that weve handled it, the EntityManager will throw An EntityServerErrorException.

49

This analysis applies to both entity query and entity navigation.

248 | P a g e

Query Cache
DevForce caches queries to improve performance50. Consider a query for employees with FirstName = Nancy. The QueryStrategy is Normal which means the fetch strategy is CacheThenDataSource. When we execute this query in an empty EntityManager, there will be a trip across the network to fetch the entities from the data source. We get back Nancy Davolio and Nancy Sinatra. If we execute the query again, the EntityManager satisfies the query from the entity cache and returns the same result; it does not seek data from the data source. During the first run the EntityManager stored the query in its Query Cache51. The second time it found the query in the Query Cache and thus knew it could use apply the cache to the query instead. If we change Nancy to Sue and run the query again, we get back just Nancy Sinatra. If we change Sally Wilson to Nancy Wilson and run it again, well get the principals of a strange duet. So far, everything is working fine. Meanwhile, another user saves Nancy Ajram to the data source. We run our query again and we still have just a duet. The EntityManager didnt go to the data source so it doesnt find the Lebanese pop star. Such behavior may be just fine for this application. If it is not, the developer has choices. She can:
use a QueryStrategy with a different fetch strategy that looks at the database first. clear the query cache explicitly by calling EntityManager.ClearQueryCache clear the query cache implicitly by removing any entity from the entity cache

EntityManager.RemoveEntities Overload Preserves Query Cache


When we remove an entity from a EntityManagers entity cache, DevForce automatically clears the PMs entire query cache. Thats right it erases the EntityManagers memory of all the queries it has performed. Suppose we frequently query for employees hired this year. If we issue this query twice. The first query fetches the employees from the database; the second retrieves them from the cache. The second query is almost instantaneous. Then we remove an unrelated entity such as a Customer or an Address. We query again. Instead of reading from the cache as it did before, the PM goes back to the database for these employees. Seems unfair, doesnt it? But its the safe thing to do.

50

This analysis applies to both entity queries and entity navigation. Both use CacheFirstThenDataSource fetch strategy by default. The PersistenceManager stores the query in the query cache when (a) the query is successful and (b) it searched the data source (not just the cache).

51

249 | P a g e

If we issue the same query multiple times, we expect the same results every time. We expect a different result only if data relevant to our query have changed. The EntityManager will search the local cache instead of the database only if it believes that all essential information necessary to perform the query are resident in the cache. If it thinks that the cache has been compromised, it should go back to the data source to satisfy the query. Removing an entity compromises the cache. For sure it invalidates at least one query the query that fetched it in the first place. But is that the only invalidated query? The EntityManager does not know. So it does the safe thing and forgets all queries. You and I know (or we think we know) that removing a Customer or Address has no bearing on employees hired this year. The EntityManager is not so sure. There are circumstances when (a) we have to remove an entity and (b) we are certain that no queries will be adversely affected. For example, our query may return entities which weve marked as inactive. We never want inactive entities in our cache but, for reasons we need not explain here, we have inactive entities in the cache. We want to remove those entities. Being inactive they cannot possibly contribute to a correct query result. Unfortunately, removing those entities clears the entire query cache. The EntityManager will satisfy future queries from the database until it has rebuild its query cache. This is not a problem if we rarely have to purge inactive entities. But what if we have to purge them after almost every query52? We will never have a query cache and we will always search the database. The performance of our application will degrade Fortunately, there is now a RemoveEntities signature that can remove entities without clearing the query cache. In the full knowledge of the risk involved, we can call
EntityManager.RemoveEntities(entitieToRemove, false)

The false parameter tells the PM that is should not clear the query cache.
Remember: removing an entity and deleting it are different operations. Removing it from the cache erases it from client memory; it says nothing about whether or not the entity should be deleted from its permanent home in remote storage. Delete, on the other hand, is a command to expunge the entity from permanent storage. The deleted entity stays in cache until the program can erase it from permanent storage.

52

This is not a rare scenario.

250 | P a g e

MergeStrategy In More Detail


The discussion here expands upon that in the section Inversion Mode earlier in the basic topic document for Business Object Persistence. It is provided as a supplement for a deeper understanding of the topic. What happens during the merge of a data source entity and a cached entity depends upon the answers to three crucial questions:
1. 2. 3. Is the entity current or obsolete? How has it changed? Is the entity represented in the data source?

Is the entity current or obsolete relative to the data source?


We compare the cached entitys concurrency column property value to that of its data source entity. If the two are the same, the cached entity is current; if they differ, the cached entity is obsolete. As it happens, the cached entity has two concurrency column property values, a current one and an original one. The value of the concurrency column in the current version is meaningless. Its the value of the concurrency column in the original version that counts. Every DevForce entity has an original version and a current version of its persistent state. We can get to one or the other by means of a static GetValue() method defined on the EntityProperty class. For example, the following code gets the original value (as retrieved from the database) for the RequiredDate property of a particular Order instance:

C#

DomainModelEntityManager mgr = DomainModelEntityManager.DefaultManager; anOrder = mgr.Orders.Where(o => o.OrderID == 10248); Datetime reqdDate = Order.RequiredDateEntityProperty.GetValue(anOrder, EntityVersion.Original);

VB Both of the following statements get the current value for the same property: C#
reqdDate = Order.RequiredDateEntityProperty.GetValue(anOrder, EntityVersion.Current); reqdDate = anOrder.RequiredDate; // same as above (but simpler!)

VB

Again, DevForce and the Entity Framework determine if our cached entity is current or obsolete based on the original version of the property value.
251 | P a g e

How has it changed?


The merge action depends upon whether the entity was added, deleted, or changed since we set its original version. The entitys EntityState property53 tells us if and how it has changed.

Is the entity represented in the data source?


If there is a data source entity that corresponds to the cached entity, we may use the data from data source entity to change the cached entity in some way. If we dont find a matching data source entity, we have to decide what to do with the cached entity. Maybe someone deleted the data source entity in which case we might want to discard the cached entity. If we, on the other hand, we want to save the cached entity, well have to insert it into the data source rather than update the data source.

Merging when the entity is in the data source


Well look at each strategy and describe the outcome based on (a) whether or not the cached entity is current and (b) the entitys EntityState. If the entity is Unchanged, we always replace both its original and current versions with data from the data source entity. Our remaining choices are evident in the following table.
Table 11. Merge strategy consequences for a changed cached entity that exists in the data source.

Merge Strategy
PreserveChanges

Current Y N Y or N Y N

Added NC NC OW ---OW NC

Deleted NC NC OW NC OW NC

Detached NC NC OW NC OW NC

Modified NC NC OW NC OW NC

Post Current Y N Y Y Y Y

OverwriteChanges PreserveChangesUnless OriginalObsolete

PreserveChangesUpdateOriginal

Y or N

NC = No change; preserve the current version values of the cached entity OW = Overwrite the cached entitys current version values with data from the data source entity Post Current = Y means the cached entity is current relative to the data source after the merge.

There are important artifacts not immediately observable from this table.

53

The possible values are Added, Deleted, Detached, Modified, and Unchanged. See Data Row State in the glossary.

252 | P a g e

The entitys EntityState may change after the merge. It will be marked Unmodified after merge with OverwriteChanges. It will be marked Unmodified after merge with PreserveChangesUnlessOriginalObsolete if the entity is obsolete. Note that deleted and detached entities are resurrected in both cases. An added cached entity must be deemed obsolete if it already exists in the data source54. We will not be able to insert that entity into the data source; well have to update the data source instead. The PreserveChangesUpdateOriginal strategy enables us to force our changes into the data source even if the entity is obsolete. An added entity merged with PreserveChangesUpdateOriginal will be marked Modified so that DevForce knows to update the data source when saving it. These effects are summarized in the following table:
Table 12. EntityState after merge.

Merge Strategy
PreserveChanges OverwriteChanges PreserveChangesUnless OriginalObsolete PreserveChangesUpdateOriginal A = Added, D = Deleted,

Current Y or N Y or N Y N Y or N

Added A U --U M

Deleted D U D U D

Detached Dt U Dt U Dt

Modified M U M U M

Dt = Detached,

M = Modified,

U = Unchanged

The merge may change the original version of a changed cached entity to match the data source values.

PreserveChanges never touches the original version.

The original version is always changed with the OverwriteChanges strategy. It is reset with the PreserveChangesUnlessOriginalObsolete strategy if (and only if) the entity is obsolete..
PreserveChangesUpdateOriginal updates the original version (but not the current version!) if the

entity is obsolete. This step ensures that the cached entity appears current while preserving the pending changes.

These effects are summarized in the following table:

54

The entity exists in the data source if the query returns an object with a matching primary key. If we think we created Employee with Id=3 and we fetch one with Id=3, someone beat us to it and used up that Id value. Our entity is obsolete.

253 | P a g e

Table 13. Merge strategy effect on the original version of the cashed entity.

Merge Strategy
PreserveChanges OverwriteChanges PreserveChangesUnless OriginalObsolete PreserveChangesUpdateOriginal

Current Y or N Y or N Y N Y or N

Added NC OW ---OW OW

Deleted NC OW NC OW OW

Detached NC OW NC OW OW

Modified NC OW NC OW OW

Merging when the cached entity is not in the data source


We begin by considering cached entities that are unchanged. If the query applied to the cache returns an unchanged entity, X, and the query applied to the data source did not return its mate, we can safely assume that X was deleted after we fetched it. We can remove X from the cache. We turn next to changed cached entities where we must distinguish between a query that tests only for the primary key and one that tests for something other than the primary key. If the query tests for anything other than the primary key, we can draw no conclusions from the fact that a cached entity was not found in the database. For what does it mean if we have an employee named Sue in cache and we dont find her in the data source? Perhaps someone deleted her from the data source. Maybe someone merely renamed her. Maybe we renamed her. The combinations are too many to ponder. On the other hand, if we query for Employee with Id = 3 and we dont find that employee in the data source, we can be confident of a simple interpretation55. A business object must have unique identity so if it isnt there, either it was never there or it has been deleted. What happens next depends upon the EntityState of the cached entity and the merge strategy.
DevForce recovers gracefully when it attempts to save an entity marked for deletion and it cant find the data source entity to delete so the merge can leave this cached entity alone. It can also skip over the detached entities.
PreserveChanges forbids merge effects on changed entities. The entity stays put in the cache. OverwriteChanges takes the data source as gospel. If the cached entitys EntityState is Modified,

there should be an existing data source entity. There is not, so DevForce assumes the data source entity has been deleted and the cache should catch up with this reality. It removes 56 the entity from the cache. On the other hand, if the cached entity is new (Added), we dont expect it to be in the data source. The entity remains as is in the cache, a candidate for insertion into the data source.
PreserveChangesUnlessOriginalObsolete behaves just like OverwriteChanges.

55

DevForce confirms that the primary key has not changed. While it is good practice to use immutable keys, it is not always so. If the primary key has been changed, DevForce leaves the cached entity alone. Removal from the cache is just that. The entity disappears from cache and will not factor in a save. It does not mean delete which requires DevForce to try to delete the entity from the data source. It is an action neutral to the data source..

56

254 | P a g e

PreserveChangesUpdateOriginal strives to position the entity for a successful save. It must intervene to enable data source insertion of a modified entity by changing its EntityState to Added57.

In sum:
Table 14. Merge strategy consequences for a changed cached entity that does not exist in the data source.

Merge Strategy
PreserveChanges OverwriteChanges PreserveChangesUnlessOriginalObsolete PreserveChangesUpdateOriginal A = Added, M = Modified, R = Removed

Added A A A A

Modified M R R A

DataSourceOnly Subtleties
We may get a nasty surprise if we use a DataSourceOnly or DataSourceThenCache query with other than the OverwriteChanges merge strategy. Consider the following queries using the PreserveChanges merge strategy. Suppose we hold the Nancy employee in cache. We change her name to Sue and then search the database for all Employees with first names beginning with S. We will not get Sue because she is still Nancy in the database. Suppose we search again but this time we search for first names beginning with N. This time we get Sue. That will confuse the end user but it is technically correct because the Sue in cache is still Nancy in the database58.

The EntityManager.AttachEntity Method


Those of you who write tests and don't want those tests to touch the database will appreciate this method. Here is its signature:

C#

AttachEntity(object entity)

As you know, you sometimes need to write tests which rely upon interaction with the EntityManager. You want to populate a disconnected EntityManager with a small collection of hand-rolled stub entities. While such tests are integration tests because they rely on a dependency, we still want to make them easy to write and we want them to be fast. That means we don't want a trip to a database when we run them; we shouldn't need to have a database to run them.

57 58

An update would fail because there is no data source entity to update. DataSourceThenCache will produce the same anomaly for the same reason: the database query picks up the object in the database as Nancy but preserves the modification in cache which shows her as Sue.

255 | P a g e

I usually start by creating a test-oriented, disconnected EntityManager ... which can be as simple as the following:

C#

var testManager = new EntityManager(false /* disconnected */ );

The easiest way to get a stub entity is to "new" it up, set some of its properties, give it an EntityKey, and dump it in our testManager. When we're done it should appear there as an unchanged entity ... as if you had read it from the datastore. The catch lies in the answer to this question: "How do I add the entity to the manager?" In the absence of AttachEntity() method, you would have to use EntityManager.AddEntity(). But after AddEntity, the EntityState of the entity is always "Added". You want a state of "Unchanged" so you have to remember to call AcceptChanges (which changes the state to "Unchanged"). That's not too hard. Unfortunately, it gets messy if the key of the entity is auto-generated (e.g., mapped to a table whose id field is auto-increment) because DevForce automatically replaces your key with a temporary one as part of its auto-id-generation behavior. We could explain how to work around this, but what was really needed was a simple way to simulate the result of retrieving an entity. That's why we created the AttachEntity() method. Here's the XML documentation for AttachEntity: Adds a detached entity to this EntityManager in an Unmodified state. Throws an exception if an entity with the same key already exists in the manager of if the specified entity is not in a detached state.

Let us elaborate here and compare it to some similar methods by calling out some facts about the following code fragment:

C#

theEntityManager.AttachEntity(object theEntity)

theEntitys EntityKey (the key) must be preset prior to the attach operation (which will not touch the key). An exception is thrown if an entity with that key is already in the cache. After attach, theEntity is in an Unchanged EntityState (the state). theEntity is presumed to exist in the persistent store; a subsequent change and save will translate to an update statement. 256 | P a g e

After a successful attach, a reference to theEntity is a reference to the entity with that key in the managers EntityCache. Contrast this with the effect of anEntityManager.Imports(new [] {anEntity}) as discussed below. theEntity must be in the Detached state prior to the operation. An exception is thrown if theEntity is other than in Detached state prior to the operation. After attach, related entities are implicitly associated with theEntity automatically; for example, if anOrder with Id==22 is attached and there are OrderDetails with parent OrderId==22, then after the attach, anOrder.OrderDetails returns these details and any one of them will return anOrder in response to anOrderDetail.Order. The sequence of attachments is not important; OrderDetails may be added prior to the parent Order. Attach has no effect on theEntityManagers QueryCache.

AddEntity behaves the same way as AttachEntity except as follows:


After add, theEntity is in an Added state theEntity is presumed to be new and to be absent from in the persistent store; a save will translate to an insert statement. If the key for this type is auto-generated (e.g., backed by an auto-increment field in the database), the existing key will be set to a generated temporary key, replacing the prior key value.

The following is true regarding detaching anEntity:


After detach, anEntity enters the Detached state no matter what its prior state. Detaching an Order does not detach its child OrderDetails; they remain orphaned in the cache. The sequence of detachments is not important; an Order may be detached prior to detaching its child OrderDetails. Detach has no effect on theEntityManagers QueryCache.

EntityManager.Imports is another way of populating an EntityManager with a collection of entities that may have come from anywhere (including hand-rolled). Here's how you might "import" a single stub entity:

C#

theEntityManager.Imports(new [] {theEntity}) ;

Imports differs from AttachEntity in that:


It requires a MergeStrategy to tell it what to do if an entity with the same key as "theEntity" already exists in the cache. It merges "theEntity" into the cache based on the MergeStrategy It makes a clone of "theEntity" and adds that clone to the EntityCache ... unless "theEntity" happens to already be in the cache in which case it is ignored ... which means that Using our example and assuming that "theEntity" was not already in the manager, the entity instance in the cache is not the same as the entity instance you imported, although their keys are equal; the following is true:

C#

theEntity != theManager.FindEntity(theEntity.EntityAspect.EntityKey)

257 | P a g e

A "clone" is a copy of an entity, equivalent to calling the following:

C#
((ICloneable)theEntity).Clone();

This is a copy of the entity, not of its related entities.

Filtering Queries
DevForce provides an extension method, Filter(), that can be used to superimpose one or more independently defined filter conditions upon an existing query. Filter() differs from Where() in that it can apply a condition defined independent of the targetted query. Filter()s primary motivating use case is the need to apply server-side filters to submitted queries in a handler for the Server.Fetching event; though it is perfectly possible to use it in other contexts. For example, suppose your applications database includes data for customers worldwide, but that a given Sales Manager only works with data for customers from his region. Instead of baking the region condition into every query for Customers throughout your application, you could implement a ServerFetching handler that imposes the condition upon any query for customers made while that Sales Manager is logged in. The usefulness of Filter() becomes even more apparent when you need to apply filters in a global way for more than one type. There are four overloads of Filter(), two of which are generic, and two of which are not. Each pair includes one overload that takes a Func<T> and another that takes an EntityQueryFilterCollection (each of whose members is a Func<T>). The generic versions normally get used client-side, because they normally operate upon an EntityQuery<T>, whereupon.NET uses type inference to get T and route the call through the generic signature. The non-generic versions are necessary because, server-side, DevForce has access only to an EntityQuery, not an EntityQuery<T>; that being a consequence of the .NET constraint that generic types cant be passed in event arguments. Lets look at some examples:

C#

var query = _em1.Territories.Where(t => t.Id > 100); var newQuery = query.Filter((IQueryable<Territory> q) => q.Where(t => t.Description.StartsWith("M")));

In this example we have used the overload of Filter which is non-generic, and which takes as its argument a Func delegate. Said delegate takes an IQueryable<T> -- essentially a list of items of type T and returns an IQueryable<T>. The IQueryable<T> that goes in is the one defined by the variable query, defined as
258 | P a g e

C#

_em1.Territories.Where(t => t.Id > 100)

The one that comes out is the one that went in minus those Territories whose Description property value begins with the letter M. In the first example, above, our filter applies to the querys root type, Territory. We arent limited to that: we can also apply filters to other types used in the query. Consider the following:

C#

var q1 = _em1.Customers.SelectMany(c => c.OrderSummaries .Where(o => o.ShipCity.StartsWith("N")) ); var q1a = q1.Filter((IQueryable<OrderSummary> q) => q.Where(o => o.Freight > maxFreight));

The root type for this query is Customer, but the query projects OrderSummaries as its output, and it is against OrderSummaries that we apply our filter. Again we use the non-generic form of Filter; and again, the overload that takes a Func<T> argument. This time the filter imposes a condition upon the values of the OrderSummary.Freight property. Without the filter we would have retrieved all OrderSummaries having a ShipCity whose name begins with N; with the filter, not only must the name begin with N, but the Freight property value must exceed the value maxFreight. Lets look at another example of filtering one some type other than the querys root type:

C#

var q1 = _em1.Customers.Where(c => c.OrderSummaries.Any(o => o.ShipCity.StartsWith("N"))); var q1a = q1.Filter((IQueryable<OrderSummary> q) => q.Where(o => o.Freight > maxFreight));

In the absence of the filter, the above query would retrieve Customer objects: specifically, Customers having at least one Order whose ShipCity begins with the letter N. The filter potentially reduces the set of Customers retrieved by imposing an additional condition on their related OrderSummaries (again, on the value of their Freight property). Now lets look at a use of Filter() involving conditions on more than a single type.

C#

var eqFilters = new EntityQueryFilterCollection(); eqFilters.AddFilter((IQueryable<Customer> q) => q.Where(c => c.Country.StartsWith("U"))); eqFilters.AddFilter((IQueryable<OrderSummary> q) => q.Where(o => o.OrderDate < new DateTime(2009, 1, 1))); var q0 = _em1.Customers.Where(c => c.OrderSummaries.Any(o => o.ShipCity.StartsWith("N"))); var q1 = q0.Filter(eqFilters);

259 | P a g e

In the above snippet, we instantiate a new EntityQueryFilterCollection, to which we then add two individual filters, each of which is a Func<T>. The first filter added imposes a condition on the Customer type; the second imposes a condition on the OrderSummary type. Note that we could now apply these filters to any query whatsoever. If the targetted query made use of the Customer type, the condition on Customers would apply; if it made use of the OrderSummary type, the condition on OrderSummaries would apply. If it made use of both, as does our example q0, both conditions would apply. A filter is also applied directly to any clause of a query that returns its targetted type. Thus, the effect of the two filters defined above, applied against query q0, is to produce a query that would look like the following if written conventionally:

C# var q0 = _em1.Customers .Where(c => c.Country.StartsWith("U")) .Where(c => c.OrderSummaries .Where(o => o.OrderDate < new DateTime(2009, 1, 1)) .Any(o => o.ShipCity.StartsWith("N")));

Query Inversion in More Detail


The discussion here expands upon that in the section InversionMode earlier in this chapter. It is provided as a supplement for a deeper understanding of the topic.

Interaction of the FetchStrategy and the InversionMode


Consider the query shown below. For this query, we have custom-baked a QueryStrategy so we can experiment with various FetchStrategies and InversionModes. The collection against which the query is directed is _Em1.Customers; but then it uses the SelectMany() method to project Order objects into the result set. Since its return type is different from the type contained in the collection first referenced, the query is non-invertible.

260 | P a g e

C#

var query = _Em1.Customers .Where(c => c.CustomerID == "CONSH") .SelectMany(c => c.Orders); QueryStrategy aQueryStrategy = new QueryStrategy(FetchStrategy.DataSourceThenCache, MergeStrategy.PreserveChanges, InversionMode.On); query.QueryStrategy = aQueryStrategy; foreach (Order anOrder in query) { System.Diagnostics.Debug.WriteLine(anOrder.OrderDate.ToString()); } Assert.IsTrue(query.ToList().Count > 0, "should return orders");

VB

In our initial run, we have the InversionMode set to On. Because DevForce is unable to invert the query, a QueryInversionServerException is thrown, with the following message: This query is not automatically invertible and cannot be executed unless either its QueryInversionMode is set to 'Manual' or its FetchStrategy is set to Optimized, DataSourceOnly or CacheOnly. If we change the InversionMode to Try and rerun the query, it runs without an exception, but the Assert test fails, because no Orders were included in the result set. Why? Because changing the InversionMode from On to Try didnt alter the fact that the query couldnt be inverted; it just told DevForce not to worry about that fact. The result set returned with a FetchStrategy of DataSourceThenCache is only that obtained in a final query against the cache, after entities retrieved from the data source have been placed there. Since the query was not invertible, no Customer objects were retrieved into the cache, and that final query returns an empty result. Suppose now we set the FetchStrategy to DataSourceAndCache. Now references to the Order objects retrieved from the data source are included in the result set. A second application of the query, this time against the cache, may or may not pick up additional Orders59. But in any event, the final result set will contain references to the in-cache Orders that are linked to the specified Customer. This will be true even if, at the end of the process, there are still no Customer objects in the cache! When a query cannot be inverted, a FetchStrategy other than DataSourceThenCache should be used. Table 15 shows the combinations of FetchStrategy and InversionMode that lead to exceptions. Note that these exceptions are designed to prevent you from receiving query results that, although they may look perfectly valid, are not!

59

It will pick up additional Orders if there are Orders in the cache that are (a) linked to Customer CONSH, and (b) either do not exist in the data source, or are not linked to Customer CONSH in the data source

261 | P a g e

Table 15. FetchStrategy x InversionMode - Exception Behavior

FetchStrategy CacheOnly DataSourceOnly DataSourceOnly DataSourceOnly DataSourceOnly DataSourceThenCache DataSourceThenCache DataSourceThenCache DataSourceThenCache Optimized Optimized Optimized Optimized DataSourceAndCache DataSourceAndCache DataSourceAndCache DataSourceAndCache

InversionMode NA On Try Off Manual On Try Off Manual On Try Off Manual On Try Off Manual

QueryInversionServerException Never If query requires inversion and cannot be inverted Never Never Never If query requires inversion and cannot be inverted If query requires inversion and cannot be inverted If query requires inversion Never If query requires inversion and cannot be inverted Never Never Never If query requires inversion and cannot be inverted Never Never Never

Only queries that either have been inverted or do not require inversion are saved in the query cache.

Turning a Non-Invertible Query on Its Head


Note that the previous query (for Orders placed by Customer CONSH) can be rewritten as follows:

C# VB

var query = _Em1.Orders .Where(o => o.Customer.CustomerID == "CONSH");

This form of the query, unlike the other one, is invertible.

A Special Case: Using the Skip() Method on an EntityQuery


The query below uses the DataSourceOnly QueryStrategy in combination with a call to Skip().

262 | P a g e

C#

EntityQuery<Customer> customersQuery = _Em1.Customers .Where(c => c.ContactTitle == "Sales Representative") .OrderBy(c => c.CompanyName); customersQuery.QueryStrategy = QueryStratey.DataSourceOnly; // <--note! ICollection<Customer> customers = customersQuery.Skip(5).Take(5).ToList();

VB

You can easily get results that are not what you would expect if you do not specify the QueryStrategy when using Skip. Suppose we omitted the statement in the above example that specifies the QueryStrategy:

C#

EntityQuery<Customer> customersQuery = _Em1.Customers .Where(c => c.ContactTitle == "Sales Representative") .OrderBy(c => c.CompanyName); ICollection<Customer> customers = customersQuery.Skip(5).Take(5).ToList();

VB

In the above case, DevForce would use the EntityManagers default QueryStrategy, which (unless you had changed it) would be QueryStrategy.Normal. Recall that QueryStrategy.Normal uses a FetchStrategy of DataSourceThenCache, and that the latter returns a list of references obtained in a final, cache-only query. So heres the flow of events for the above query. (Assume an empty cache as a starting point.)
1. 2. 3. 4. Query is submited to the EntityManager. EntityManager checks the query cache to see if query has been submitted before. It finds that it has not. EntityManager submits query against the data source, which returns five Customers, which are placed in the cache. EntityManager submits the query again, this time against the cache (so that it will incorporate any Customers who have been added locally but have not yet been saved to the data source).

The second query, against the cache, skips the five Customers it finds there, and upon attempting to take the next five, discovers that there are no more. It therefore returns 0 Customers. Although this isnt, technically, a case of a failed query inversion, the result and the reason for it are clearly similar to that. The only real advice here is that, if youre using Skip(), you should either use a FetchStrategy of DataSourceOnly, or make good and certain that you understand FetchStrategies in detail.

DataSourceThenCache Versus DataSourceAndCache


The distinction between the DataSourceThenCache and DataSourceAndCache strategies is subtle but important in the case of queries that must process non-targeted types and are therefore subject to query inversion. Suppose you were to submit the following query:
263 | P a g e

C#

var query = em1.Customers .Where(c => c.Orders .Any(o => o.OrderDate.HasValue == true && o.OrderDate.Value.Year == 1997));

VB

This query targets Customers but must process Orders to find the correct set of Customers. DevForce would have no difficulty inverting this query, but suppose you submitted it with an InversionMode of Off and a FetchStrategy of DataSourceThenCache. The InversionMode setting would mean that only Customer objects were retrieved into the cache: no Order objects. Great! you say. Thats all I wanted: Customers. But even though you have the desired Customers in your cache, you dont yet have references to them. How does DevForce get these references? Because of the FetchStrategy you specified, DevForce now resubmits your query, this time against the cache; and the set of references to Customer objects that it will return will be entirely determined by the Customers that meet the query criteria when the query is resubmitted against the cache. But wait! There is no guarantee that the cache contains the same Order objects that were found in the data source; it will, in fact, contain no Order objects at all unless some other, unrelated operation that was previously executed caused some to be retrieved. Therefore the set of Customers found by the query when submitted against the cache may be very different from the set found when it was submitted against the data source. Indeed, the set may be empty. You may get references to no Customers or some Customers, but there is no guarantee, and indeed little likelihood, that youll get references to all of the Customers retrieved by your query from the data source. If, on the other hand, you submitted your query with a FetchStrategy of DataSourceAndCache, youll get want you wanted: all Customers in the data source who meet your conditions, as well as all Customers that exist only in your local cache that meet those conditions. With that FetchStrategy, DevForce performs a union of the references obtained by the two query submissions. The DataSourceAndCache FetchStrategy does have some drawbacks which well discuss momentarily. Generally speaking, it is the appropriate FetchStrategy only in the following circumstance: 1. 2. Your query will use related objects; You want to include in the result set references to entities that exist in the cache but which have not yet been persisted to the database; DevForce cant invert the query; and You cant write an equivalent query that is invertible.

3. 4.

The reason that DataSourceThenCache is the preferred FetchStrategy for other circumstances is that, under certain circumstances, DataSourceAndCache can produce confusing results. Suppose you have some Customer objects in the cache, including Customer XYZ, and you submit a DataSourceAndCache query for Customers with Orders in the current year. Customers meeting this condition are fetched from the data source into the cache, and merged there with Customers already residing in the cache with a

264 | P a g e

MergeStrategy of PreserveChanges. Meanwhile DevForce hangs on to a list of references to the objects just fetched. Now it so happens that Customer XYZ, who was in the cache already, had (during the current application session) just cancelled their one and only order for the current year. The Order was marked for deletion, but this change had not been committed to the database when the query was submitted. So, based on the state of data in the data source, Customer XYZ met the query conditions and was retrieved, and a reference to their object in the cache was included in the set returned by the query against the datasource. DevForce continued on, resubmitting the query against the cache. This time Customer XYZ did not make the cut because, according to the data in the cache, they did not have a current year Order. No reference to their in-cache object was included in the list of pointers resulting from the query against the cache. But DataSourceAndCache, DevForce then performed a UNION of the references obtained in the query against the data source and those obtained in the query against the cache. A reference to cached Customer XYZ therefore ended up in the result set returned by the query. Your app happily filled a datagrid with the returned Customers, and there sat Customer XYZ, even though they (quite visibly) did not have an order in the current year! Can a phone call from your end user be far away? The DataSourceThenCache FetchStrategy, by contrast, would have retrieved, from the data source and into the cache, whatever data met the query conditions. It would then have submitted the query against the cache, and only the Customers meeting the specified condition in that final query would have been included in the returned result set. Customer XYZ, having been found to have no current year Order, would have been excluded, properly.

Transactional Queries
DevForce query requests are atomic: the developer can issue only one (synchronous) query request at a time. But when the request resolves into multiple SQL queries, they can all be performed together within the same transaction. Individual query requests resolve into several SQL queries when the query has includes that fetch related objects or when the query includes one or more sub-queries and query inversion is turned on. When the root query is performed transactionally, both the main select and the selection of related entities occur within transactional boundaries.

DevForce developers can set the transaction isolation level on individual commands
Developers can set the transaction isolation level for individual queries and saves.

Implementation

265 | P a g e

There is a TransactionSettings class and a TransactionSettings property on both the SaveOptions and QueryStrategy classes. The TransactionSettings class provides the ability to dynamically set: whether or not to use the Microsoft Distributed Transaction Coordinator the Transaction Isolation level of a Save or Query. This provides in effect

the Transaction timeout to be applied to a Save or Query Note: For the current version Transaction isolation levels and timeouts can only be applied if the DTC is turned on. Note: The Default Transaction Isolation Level for Saves is Serialized; for queries it is ReadCommitted. Note: Non-locking queries can be implemented by setting the TransactionSettings.IsolationLevel to ReadUncommitted.

DevForce and Data Sources Deep Dive


There are potentially many data sources at play in a DevForce application. Data sources can be databases or, using DevForce POCO support, any type of data source, including web services. Any design-time access of a database is initiated by Visual Studios Entity Data Model Designer during your design session using that tool. That designer adds a connectionString element to the app.config file, placing it in the same project as the EDMX.. This app.config contains connection information to the database used by the EDM designer for its design work. The DevForce OM Extension to the EDM Designer adds a DataSourceKey attribute to the EDMX. If you open the EDMX using an XML editor, youll see that the DevForce-added attributes are prefixed with the namespace qualifier of ib10. By default, the DataSourceKey is given the same name as the EF Entity Container Name, and thus points to a connection string in the app.config of the Model project.

XML

266 | P a g e

<connectionStrings> <add name="NorthwindIBEntities" connectionString=" " providerName="System.Data.EntityClient" /> </connectionStrings>

The EDM Designer updated an app.config for design time support. At run time, DevForce will use the DataSourceKey value to find the connection information to the run time database. To do so, the connection information must be available in the app.config of the executable project (or web.config), not the Model project. DevForce will search for an edmKey or connectionString with the same name as the DataSourceKey + DataSourceExtension. We havent previously mentioned EdmKeys, but they encapsulate connection information, and are located within the IdeaBlade Configuration section. An EdmKey contains connection information, as a connectionString will, but also additional DevForce attributes, such as logTraceString, which writes the EF-generated SQL to the debug log. You must ensure that either an edmKey or connectionString is available at run time in the appropriate config file.

DevForce also allows you to omit connection information from the run time configuration altogether and supply the connection string dynamically, using the IDataSourceKeyResolver interface. More information on implementing this interface is available later in this document.

DataSourceKeys, DataSourceKeyResolvers, and DataSourceExtensions


A DataSourceKey is a symbolic representation of a data source used by the DevForce EntityManager and associated with the Entity objects it retrieves, updates, and creates. Every Entity has a DataSourceKeyName attribute that identifies its symbolic Datasource60. This key name is hard-coded into the business class at the time the latter is generated by DevForce. Recall that a DomainModel, and therefore an EntityManager can access multiple data sources. A given EntityManager might, for example, access a SQL Server database, an Oracle database, and a web service, mapping business classes from each and joining all into a single transactional unit. Each of those three datasources gets a distinct DataSourceKey, and entities generated from each of them get assigned the name of that DataSourceKey. But what if you need multiple versions of those three data sources? For example, you might have Development, Test, Stage, and Production versions of the same three-datasource set. The data sources in all four versions would have the same schemas, but different content. For example, data in the development and test data sources might be scrubbed so as to eliminate security issues during relatively unprotected use; data in the development data sources might be lightweight compared to that
60

You can find this on an entity instance as its EntityAspect.EntityMetadata.DataSourceKeyName property

267 | P a g e

in the Test data sources; and so forth. All four versions of a given database (schema) in a set of data sources would be identified with the same DataSourceKey and all would map to the same set of business classes, so that an application consuming their data would be indifferent to which of the physical instances of that schema it accessed in any given launch. DevForce uses a string called a DatasourceExtension to discriminate between alternative instances of a given data schema. You supply these extensions in the edmKeys (or connectionStrings) that you configure in the app.config file, by adding them to the name attribute of the edmKey, separating them from the DataSourceKey Name by an underscore character (_). At runtime, to obtain the data required for business objects of a designated type (e.g, Employees), DevForce connects to an actual data source by consulting a DataSourceKeyResolver. The DataSourceKeyResolver combines the DataSourceKeyName associated with the desired Entity type with a DataSourceExtension (supplied by the requesting EntityManager) and returns a DataSourceKey object. That DataSourceKey object contains all the information required to connect to an actual, deployed data source.

EntityManagers and DataSourceExtensions


Every EntityManager gets associated at instantiation with a DataSourceExtension. You can see this clearly in the following code statement which uses an overload of the EntityManager constructor that specifies the extension explicitly (as Development):

C#

DomainModelEntityManager mgr = new DomainModelEntityManager(true, "Development");

The extension determines which version e.g., Development, Test, Stage, or Production of the data source(s) actually gets accessed by the EntityManager. Expressed another way: the Extension identifies a collection of one or more data sources, each the repository of a set of tables or web services that map to business object classes, 61 which will be accessed by a given EntityManager.

In the illustration below, all four of the DS#1 data sources would have the same DataSourceKeyName. The same could be said for the DS#2 and DS#3 data sources. On the other hand, the set of data sources accessed by a single EntityManager would comprise a DS#1, a DS#2, and a DS#3. But which copy of DS#1, a DS#2, and DS#3 should be used? That would be determined by the DataSourceExtension with which the EntityManager was associated at instantiation.

61

The default extension, incidentally, is no extension at all.

268 | P a g e

Now lets look at DataSourceKey names and extensions as they appear in edmKeys in an App.config file. Listing 3 is an excerpt from an app.config file containing multiple DataSourceKeys with different key names and extensions. For clarity, weve made sure the name attribute is the first attribute listed for the the <edmKey> element. Note that each DataSourceKey, in addition to containing a connection string, also includes probe assembly names for assemblies that hold auxiliary classes for id generation, authentication, event handling, and the like.62
Listing 3. Extract of app.config file with multiple DataSourceKeys

XML

<edmKeys> <!-- Production databases --> <edmKey name="NorthwindIB_Release" connection="metadata=res://ServerModelNorthwindIB/ServerModelNorthwindIB.csdl|res://Serve rModelNorthwindIB/ServerModelNorthwindIB.ssdl|res://ServerModelNorthwindIB/ServerModelNor thwindIB.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=ProductionDBMS_A;Initial Catalog=NorthwindIB;Integrated Security=True;MultipleActiveResultSets=True&quot;" logTraceString="false" tag=""> </edmKey> <edmKey name="Aw2000_Release" connection="metadata=res://ServerModelAw2000/ServerModelAw2000.csdl|res://ServerModelAw20 00/ServerModelAw2000.ssdl|res://ServerModelAw2000/ServerModelAw2000.msl;provider=System.D ata.SqlClient;provider connection string=&quot;Data Source=ProductionDBMS_B;Initial Catalog=AdventureWorks2000;Integrated Security=True;MultipleActiveResultSets=True&quot;" logTraceString="false" tag=""> </edmKey>

<!-- Development databases --> <edmKey name="NorthwindIB_Development" connection="metadata=res://ServerModelNorthwindIB/ServerModelNorthwindIB.csdl|res://Serve rModelNorthwindIB/ServerModelNorthwindIB.ssdl|res://ServerModelNorthwindIB/ServerModelNor thwindIB.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=DevelopmentDBMS_A;Initial Catalog=NorthwindIB;Integrated Security=True;MultipleActiveResultSets=True&quot;" logTraceString="false" tag=""> </edmKey> <edmKey name="Aw2000_Development" connection="metadata=res://ServerModelAw2000/ServerModelAw2000.csdl|res://ServerModelAw20

62

It may also contain a <tag>, where you can put any sort of string-value custom information you desire. At runtime you can access the information placed there via the Tag property of a DataSourceKey object -- which you can get from the DataSourceKeys collection of a DataSourceKeyResolver object.

269 | P a g e

00/ServerModelAw2000.ssdl|res://ServerModelAw2000/ServerModelAw2000.msl;provider=System.D ata.SqlClient;provider connection string=&quot;Data Source= DevelopmentDBMS_B;Initial Catalog=AdventureWorks2000;Integrated Security=True;MultipleActiveResultSets=True&quot;" logTraceString="false" tag=""> </edmKey> </edmKeys>

In the above excerpt from an app.config file, edmKeys are present for two databases (NorthwindIB and Adventureworks2000). Two versions (Development and Release) are maintained of these databases. Instantiating an EntityManager and specifying a DataSourceKey Extension of Release...

mPersMgr = new DomainModelEntityManager(true, "Release");

would cause all data accesses for entities based on the databases to go against the sources named in the edmKeys that have the suffix _Release in their name attribute. For example, data for classes mapped to tables in the NorthwindIB database would be retrieved from the copy of that database running on the ProductionDBMS_A instance of SQL Server; data for classes mapped to the AdventureWorks2000 database would be retrieved from the copy of that database running on the ProductionDBMS_B instance of SQL Server. Were an EntityManager to be instantiated with the extension Development, different copies of the two databases would be accessed. Note the following points: 1. The DataSourceKey Names and DataSourceKey Extensions are case insensitive. 2. In the name attribute of the edmKey element, the Datasource Extensions are always preceded by an underscore _ character. If you wished to establish one or the other set of databases as the default say, the Development versions then you could include in the <edmKeys> section of the app.config an additional pair of edmKeys with no extensions specified in their names, as shown below. The information in these keys would be used by any EntityManager instantiated with no DataSourceExtension specified. (This time, for brevity, weve snipped out the detail for the connection attribute value.)
Listing 4. Extract of app.config file with multiple DataSourceKeys

XML

<!-- Default databases --> <edmKey name="NorthwindIB" connection="..." logTraceString="false" tag=""> </edmKey> <edmKey name="Aw2000" connection="..." logTraceString="false" tag=""> </edmKey>

270 | P a g e

Tenant Extensions
Extensions are also a good way to segment data sources by client in a multi-tenant application. Multitenant applications are typical of Application Service Provider (ASP) scenarios in which each customers data is managed in isolated data sources. When the user logs in, the application identifies the users parent customer and knows which set of Datasources is appropriate for that user. The application can then instantiate an EntityManager that draws upon just those data sources.
The DataSourceExtension is the ideal representation for a customer-specific data source set as in this depiction of a three-tenant scenario with customers A, B, and C:

Multi-Part Extensions
DataSourceExtensions may have multiple parts, permitted an even more sophisticated scheme for selected a data source instance at runtime. Consider the following edmKeys in an app.config file (connection value and probe assembly section removed for brevity):

XML

<edmKeys> <!-- Production databases --> <edmKey name="Acmetest2_SQLSRVR_OLE1" connection="... " logTraceString="false" tag=""> ... </edmKey> <edmKey name="Acmetest2_SQLSRVR_OLE2" connection="..." logTraceString="false" tag=""> ... </edmKey> <edmKey name="Acmetest2_SQLSRVR" connection="... " logTraceString="false" tag=""> ... </edmKey> <edmKey name="Acmetest2" connection="..." logTraceString="false" tag=""> ... </edmKey> </edmKeys>

271 | P a g e

Note that the first two edmKey names contain two underscores. These delimit multi-part DataSourceExtensions. Were you to instantiate an EntityManager as follows:

C#

mPersMgr = new DomainModelEntityManager(true, "SQLSRVR_OLE1");

you would get the database identified in the first edmKey in the above excerpt. On the other hand, if you wrote this statement:

C#

mPersMgr = new DomainModelEntityManager(true, "SQLSRVR_FOO");

then the DataSourceKeyResolver, being unable to locate an edmKey with both parts of the extension matching, would resolve the database to the one identified with the third edmKey, named AcmeTEst2_SQLSRVR. It would do so because it finds a match on the first part of the extension. If the DataSourceKeyResolver can find no edmKey with an extension that matches at least on the first part of the extension submitted, it will throw an exception. Thus, the following statement, containing a misspelled first part of the extension, will result in an exception. It will find no matching set of extensions; no matching first extension; and will not default to the extensionless key AcmeTest2: C#
mPersMgr = new DomainModelEntityManager(true, "SQLSVRR_OLE1");

Extensions and EntityServers


Lets stick with the multi-tenant, ASP scenario for awhile. When the application client determines the customer, it creates an EntityManager dedicated to the data sources applicable to that customer by including the customers DatasourceExtension name in the constructor. C#
msManager = new DomainModelEntityManager(true, "A"); // Connect to customer "A"

Now the client application tries to login or fetch entities with this EntityManager. The EntityManager contacts the EntityService. The EntityService checks among its EntityServers for one that is associated with extension A. It doesnt find one so it creates a new EntityServer instance for extension A and adds it to its collection. This EntityServer now serves every EntityManager presenting the A extension.

272 | P a g e

When the EntityService encounters EntityManagers with unknown extensions B and C for example , it creates more EntityServers. The three-tenant scenario could look like this:

Dynamic DataSourceKeys and the DataSourceKeyResolver


Every entity has a DataSourceKeyName which identifies its symbolic data source. There should be at least one real data source somewhere that holds the data source object to which the entity is mapped. The DataSourceKeyName helps DevForce find it.
The DataSourceKeyName property of the entity reveals this name; for example: anEmployee.EntityAspect.EntityMetadata.DataSourceKeyName.

DevForce connects an actual data source at runtime by asking a DataSourceKeyResolver for the DataSourceKey that corresponds to the key name.
To be more precise, it returns an object that implements IDataSourceKey. There are two implementations of this interface at the moment, the EdmKey, and the ClientEdmKey. EdmKey provides access and management information for relational databases. ClientEdmKey has no dependency on the Entity Framework or its data sources.

A DataSourceKeyResolver has a single method, GetKey(KeyName, KeyExtension). The KeyName is the symbolic data source name that we see inscribed in the entitys DataSourceKeyName property. The KeyExtension, as we have seen, is an optional string for differentiating among multiple keys each referring to a distinct runtime data source. DevForce uses its own DefaultDataSourceKeyResolver unless we provide an alternative. The default version looks for either an edmKey or connectionString in the application configuration. The App.config is a fixed file that must reside in a known place. That means the key information must be comparatively static as well. True, the configuration file does not have to be compiled into the application63. DevForce will prefer a loose version of the file in the executables directory. We make our change, drop it into the executables

63

DevForce supports either embedded or loose configuration files.

273 | P a g e

directory, and DevForce will prefer that version over any other. No re-compilation or major redeployment required. We may need more flexibility or security than the configuration file affords in situations such as the following:
The connection facts change periodically and we cant count on redeploying the updated configuration file. The connection facts are different for different users of the application. The connection facts must not reside in a text file; they must be delivered to the application at runtime after authenticating the user.

Custom DataSourceKeyResolver
Fortunately, it is easy to write a custom DataSourceKeyResolver that does exactly what you want it to do.
Pick a project to hold your key resolver, e.g. DomainModel Add the following references to that project:
IdeaBlade.EntityModel IdeaBlade.Core

Write a class that implements IDataSourceKeyResolver. Decorate the class with the SerializableAttribute ([Serializable] in C#, in VB).
<Serializable()>_

Implement your version of GetKey(KeyName, KeyExtension) to handle the keys you want to manage. Return null (Nothing in VB) if you want the DefaultDataSourceKeyResolver to determine the key.

C#

using IdeaBlade.EntityModel; using IdeaBlade.Core; namespace AppHelper { [Serializable] class MyDataSourceKeyResolver : IDataSourceKeyResolver { public IDataSourceKey GetKey(string keyName, string keyExtension, bool onServer) {

if (!onServer) {return null;}


Console.WriteLine("Shot ya with my resolver"); // Demo code. // Build your own ClientEdmKey starting with the following // return new MakeClientEdmKey(keyName, theConnectionString) return null; // Didn't build key; DefaultDataSourceKeyResolver takes over } } }

VB
Imports IdeaBlade.EntityModel

274 | P a g e

Imports IdeaBlade.Core <Serializable()> _ Public Class MyDataSourceKeyResolver : Implements IDataSourceKeyResolver Public Function GetKey(ByVal keyName As String, _ ByVal keyExtension As String, _ ByVal onServer As Boolean ) _ As IDataSourceKey Implements IDataSourceKeyResolver.GetKey

If !onServer Then Return null


Console.WriteLine("Shot ya with my resolver") ' Demo code. ' Build your own ClientEdmKey starting with the following ' Return New MakeClientEdmKey(keyName, theConnectionString) Return Nothing ' Didn't build key; DefaultDataSourceKeyResolver takes over End Function End Class

THE NET RESULT OF KEY LOOKUP MUST DELIVER A KEY ON BOTH CLIENT AND SERVER. However, in ntier, the client should not provide connection info in the key. Note that GetKey receives a boolean onServer parameter that indicates whether GetKey() is operating on the server or client.

Multiple Application Environments


Many IT shops prescribe separate Development, QA, Test, Stage, and Production environments. Each version of the application works its way through a testing gauntlet from the developer environment to ultimate production release. Suppose our application refers to a database data source called default. Its data source key is default. The application will use this key at runtime to find a data source configuration in the application configuration file (IdeaBlade.ibconfig). The data source configuration is very simple for the development environment. The development deployment puts all tiers on the PC. The default development configurations connection string points to a database on the PC. The QA environment, on the other hand, has a 3 tier deployment with separate machines for client, business object server, and database. This requires many changes to the default configuration including a different connection string that points to the QA database. We really need a separate default configuration for QA. In fact, we need five default configurations in the application configuration file. The symbolic data source, default, doesnt change as we cross environments. The business objects associated with the default data source should be indifferent to configuration differences. The executing environment, on the other hand, has to know which of the default configuration to use. DevForce provides data source key extensions to help distinguish the five default data source configurations. By convention, the data source configuration name is the data source key name followed optionally by an underscore _ and data source key extension.
275 | P a g e

In our example, the configurations could be named Default_Development, Default_QA, etc. When the application launches, it determines its runtime environment and then tells the EntityManager to connect to its data source(s) using the extension to find the appropriate data source configuration information64. If we execute in development, we initialize the EM with Development and it adds the _Development suffix to default. If the EntityManager (and, later, the EntityServer) cannot find a data source configuration named Default_Development, it will look for one named default before giving up.

Multiple EntityManager Instances


Most applications only need a single EntityManager instance. A EntityManager instance can hold every entity we need in a single cache even entities that persist to different data sources.
Accordingly, when we write EntityManager we mean an instance; we say EntityManager class when referring to the class rather than the instance.

We can create new instances and there are scenarios for which this is useful. Perhaps we have a long-running query or series of queries that should run in a background thread without blocking the UI. Maybe we want to poll for changes to a set of entities or be on the look-out for certain conditions in the database. Our implementation should use a different EntityManager in the background thread so as not to conflict with the main manager in the UI thread. When the background process completes, the call-back method can pause the UI, import data from the second manager, alert the user, and resume the UI.

Life with two EntityManagers


Each EntityManager has its own entity cache. An entity instance in one cache is not the same as an entity instance in another cache even when the two instances have identical primary keys. Entities with duplicate keys cannot exist within a single cache. There can be only one Employee object with Id = 42 in a given cache. However, after reading Employee #42 into EM A and EM B, there are two Employee objects in the application that have Id = 42. This is fine as long as we are aware of it. Think of the two EntityManager caches as separate clients. When A changes Employee #42, this has no immediate effect on Bs copy of Employee #42. If A saves the changes, Bs copy is no longer current with respect to the data source. If B then makes changes and tries to save, B gets a concurrency violation. These rules apply whether A and B are two end users on different PCs or two EntityManagers in the same application.

64

Entities in the EM may map to more than one data source. The EM will suffix each data source key name with the same extension.

276 | P a g e

Miscellaneous observations
Different EntityManagers do not interact. It is possible and useful to import entities from one EM to the other.

Logging In a Second EntityManager Based on the Credentials of Another EntityManager


The ability to create a second EntityManager that is logged with the same credentials as the first facilitates scenarios in which the application creates a second context for editing. Changes in this second context are isolated from the main context and can be saved or canceled without unintended effects on entities in the primary PM. The EntityManager has a copy constructor for this purpose.
C#
EntityManager Pm2 = new EntityManager(Pm1);

VB

The new EntityManager (Pm1) will have the same settings and credentials as its prototype (Pm1) but without any data. Its login state will be the same as its prototype. The second EM must connect to the database in order to save changed entities. It can only connect if it is logged in. Without the ability to login the second EM at its creation, we would have to preserve the users original credentials in some safe place in memory. This would be both inconvenient and discomforting, as one can never be quite certain that a rogue module can be prevented from acquiring those credentials and misusing them. It is best to forget about them as soon as possible. The new constructor permits you to do so.

Multi-Threading in a DevForce App


Lets begin our discussion of multi-threading with a definition of thread-safety:
For a class to be thread-safe, it first must behave correctly in a single-threaded environment. If a class is correctly implemented, which is another way of saying that it conforms to its specification, no sequence of operations (reads or writes of public fields and calls to public methods) on objects of that class should be able to put the object into an invalid state, observe the object to be in an invalid state, or violate any of the class's invariants, preconditions, or postconditions. Furthermore, for a class to be thread-safe, it must continue to behave correctly, in the sense described above, when accessed from multiple threads, regardless of the scheduling or interleaving of the execution of those threads by the runtime environment, without any additional synchronization on the part of the calling code. The effect is that operations on a thread-safe object will appear to all threads to occur in a fixed, globally consistent order. The relationship between correctness and thread safety is very similar to the relationship between consistency and isolation used when describing ACID (atomicity, consistency, isolation, and durability) transactions: from the

277 | P a g e

perspective of a given thread, it appears that operations on the object performed by different threads execute sequentially (albeit in a nondeterministic order) rather than in parallel.
65

The DevForce EntityManager is safe for multithreaded read operations. If you attempt writes to a single EntityManager from multiple threads, you must synchronize the write operations yourself. For us to make the EntityManager thread-safe for write operations would require that we make thread-safe every method therein and every method of the business objects it manages, including property setters. This would increase the EntityManagers complexity and degrade its performance significantly. Every user of the EntityManager, and every use thereof, would incur the performance penalty, whether such users and uses required thread-safety or not. At least 90% of the use cases that people submit to us for multi-threading involve retrieving data while other operations proceed. For this we have provided Asynchronous Queries. You call the EntityManagers ExecuteQueryAsync() method, and we take care of putting the data retrieval operation on a separate thread so that the rest of your application can continue processing. Any number of such asynchronous queries can be launched simultaneously. Does this mean that you cant do multithreading (other than by using Asynchronous Queries) in a DevForce application? No, it does not. It just means that you should never share a single EntityManager, or any of the entities it manages in its cache, across multiple threads. Let us repeat:
Never share a EntityManager across more than one thread. Never share entities from a given EntityManager in more than one thread.

Note that the problems that occur with multi-threaded applications are, by their very nature, timingdependent and difficult to diagnose, reproduce, and test for. Your multi-threaded process can work successfully for long periods of time, then fail catastrophically when two or more inconsistent changes happen to be made simultaneously. You should definitely not count on this failure occurring at a convenient time! If Youre Determined To Do Multi-Threading Be sure you really need multiple threads. Remember, if all you want to do is fetch data asynchronously, you will be fully satisfied with Asynchronous Queries. Dont mess around with multi-threading if this is all you want to do. Use caution when writing any multi-threaded app. Don't be lulled into a false sense of confidence just because it is easy to spawn a BackgroundWorker. Multi-threading is still hard. The BackgroundWorker made the syntax easy: it did not make good multi-threaded design easier!

65

Excerpted from Characterizing Thread Safety by Brian Goetz, available on the web at:
http://www-128.ibm.com/developerworks/java/library/j-jtp09263.html

278 | P a g e

If youre new to multi-threaded programming, work with someone who has significant prior experience doing it, if at all possible. If you cant arrange that, do some serious reading and study on the topic before attempting it on a critical application.
If your multi-threaded aspirations involve DevForce business objects: Use a different EntityManager in each thread. Such EntityManagers can do anything a normal EntityManager can do; they can fetch (both synchronously and asynchronously), save, and so forth. Never use EntityManager.DefaultManager when multi-threading the DefaultManager is global to the AppDomain and will be shared among any threads in which it is used. Never communicate entities across thread boundaries. If the caller must know about some entities, send a list of EntityKeys across the thread boundary in a call back. Alternatively, you could bury EntitySets in a call back to serialize copies of entities across the thread boundary.

Batching Asynchronous Tasks


DevForce includes two classes, the AsyncSerialTask and the AsyncParallelTask, that permit you to define and execute asynchronously, in series or in parallel, a collection of linked actions. Each method uses a single callback to handle all processing results, and each provides the ability to specify an ExceptionHandler to provide a single point of error handling.

AsyncSerialTask
The AsyncSerialTask provides you with a mechanism to define a sequence of linked actions, each of which can be performed synchronously or asynchronously. To use the feature, you first create the root task in the sequence, and then add actions to it, until you have a sequence ready for launch via the Execute method. The AsyncSerialTask allows you easily to link a series of actions, passing the output from the previous action as input to the next action. Without the AsyncSerialTask, you would need to issue each asynchronous action separately and in the handler for the completed action launch the next action in the sequence. The AsyncSerialTask takes care of this housekeeping for you. It allows you to pass an argument into the task sequence when execution begins, and to specify a single handler when the entire sequence completes. You can specify an ExceptionHandler to provide a single point of error handling. Note that the entire sequence is not executed as a group on a worker thread. Instead, as each action is serially executed, if the action is asynchronous then a worker thread is started for it; when the action completes its results are returned back to the main thread, which then continues with the next action in the sequence. Note that if you add only synchronous actions and functions to the AsyncSerialTask the entire sequence will execute synchronously. Use the AsyncParallelTask rather than the AsyncSerialTask if you can execute all actions simultaneously.

C#

public void SampleAsyncTask() {

279 | P a g e

DomainModelEntityManager mgr = new DomainModelEntityManager(); // Let's take a series of "actions" all performed synchronously. // Login - if ok, then: // - Run a query for customers // - Modify the retrieved data // - Save changes // It might look like this: if (mgr.Login(new LoginCredential("demo", "demo", "earth"))) { var customers = mgr.Customers.Where(c => c.Country == "USA").ToList(); customers.ForEach(c => c.Country = "US"); SaveResult sr = mgr.SaveChanges(); Debug.Assert(sr.Ok); } // // // // // // // // // // // // // // // Now assume that some of these actions should be done asynchronously. In Silverlight, any actions which go to the BOS - such as query and save must be performed asynchronously. The AsyncSerialTask let's you group a series of actions to be performed together. Without this, you would need to issue each async call separately, and in the handler for the completed action fire off the next action. The AsyncSerialTask does this for you. The same actions with the AsyncSerialTask: - Login asynchronously - When login completes, run an async query to retrieve customers - When the query completes, modify the retrieved data - Save these changes asynchronously (You should also use an ExceptionHandler to trap errors, but we've removed that to make this sample a bit easier to read.) Here's how you might build this task:

AsyncSerialTask.Create("ASimpleTask") .AddAsyncLogin(mgr, new LoginCredential("demo", "demo", "earth")) .AddAsyncQuery(loginArgs => mgr.Customers.Where(c => c.Country == "USA")) .AddAction(fetchArgs => { var customers = fetchArgs.Result; customers.ForEach(c => c.Country = "US"); }) .AddAsyncSave(mgr) .Execute(null, (completionArgs) => { SaveResult sr = completionArgs.Result.Result; Debug.Assert(sr.Ok); }); }

AsyncParallelTask
The AsyncParallelTask allows you to create a set of asynchronous actions, execute them in parallel, and provide a single callback to handle all processing results. To use the feature, you first create a task, and then add asynchronous actions to it until you have a set ready for launch via the Execute method. In the absence of the AsyncParallelTask you would need to issue multiple asynchronous method calls and provide handlers for each. Instead, the AsyncParallelTask takes care of much of this housekeeping for you. It allows you to pass an argument to each action in the task, and to specify a single handler when the entire task completes. You can also specify an ExceptionHandler to provide a single point of error handling.
280 | P a g e

Each action is executed on a separate worker thread. The completion action is called on the main thread once all actions have completed. If you've specified a callback for an asychronous action, that callback will also be called on the main thread. The Execute call returns immediately after starting all of the specified parallel actions. Use the AsyncSerialTask rather than the AsyncParallelTask if you need to link the outputs from one action to the inputs to the next, or to mix asynchronous and synchronous actions.

C#

public void SampleAsyncTask() { DomainModelEntityManager mgr = new DomainModelEntityManager(); // Let's take a few "actions" performed asynchronously: // - Run a query for all customers // - Run a query for all employees mgr.ExecuteQueryAsync<Customer>(mgr.Customers, cb => { if (cb.Error != null) { Debug.WriteLine(cb.Error.Message); } else { cb.Result.ForEach(c => Debug.WriteLine(c.CompanyName)); } }, null); mgr.ExecuteQueryAsync<Employee>(mgr.Employees, cb => { if (cb.Error != null) { Debug.WriteLine(cb.Error.Message); } else { cb.Result.ForEach(e => Debug.WriteLine(e.LastName)); } }, null); // Since these async actions both essentially run in parallel, let's // combine them into a single task: AsyncParallelTask.Create() .AddExceptionHandler(args => Debug.WriteLine(args.Exception.Message)) .AddAsyncQuery(1, x => mgr.Customers) .AddAsyncQuery(2, x => mgr.Employees) .Execute(cb => { ((EntityFetchedEventArgs <Customer>)cb.CompletionMap[1]) .Result.ForEach(c => Debug.WriteLine(c.CompanyName)); ((EntityFetchedEventArgs <Employee>)cb.CompletionMap[2]) .Result.ForEach(e => Debug.WriteLine(e.LastName)); }); }

Service Oriented Architecture


We are sometimes asked whether DevForce is a Service Oriented Architecture (SOA). DevForce applications can be SOA in several respects. First, DevForce applications are .NET applications, which means it is very easy to build web services into the application. Second, we can expose all or part of the business object model as a web service, which means external applications and non-.NET clients can take advantage of the hard work we put into our model.
281 | P a g e

On the other hand, SOA is easy to abuse. It does not belong everywhere and it is an especially unfortunate choice for cross-tier data transfers in an n-tier application. The difference between n-tier and Service Oriented Architectures are vitally important and worth at least some discussion such as were about to have now. SOA design emphasizes loose coupling and a contract between a client and a service. The client should know as little as possible about the service internals and engage with the service only through a message-like interface having a simple protocol. The interface should be course grained, meaning that we expect to get a lot done with each service method call and we dont over-task the interface with fine details. We are often aware of the boundary between the client and service. The services of an SOA application do not belong to that application. In principle, the services are designed independently of any particular SOA application and could be accessed by any authorized client. An n-tier application, by contrast, has logical layers that are tightly coupled. The layers tend to have many, fine grained interface points. The layers are designed to work together as a single, operating whole. It is a secondary benefit if a tier can serve another application through the same interface. SOA proponents emphasize the importance of the contract between client and server. But SOA can only ensure the consistency of the interface points. It cant ensure that the semantics implied in the interface are actually the same on both sides of the fence. A program manager on Microsofts CLR team made an analogous point in a commentary on the difficulty of choosing between defining classes and interfaces:
I often hear people saying that interfaces specify contracts. I believe this is a dangerous myth. Interfaces, by themselves, do not specify much beyond the syntax required to use an object. The interface-as-contract myth causes people to do the wrong thing when trying to separate contracts from implementation, which is a great engineering practice. Interfaces separate syntax from implementation, which is not that useful, and the myth provides a false sense of doing the right engineering. In reality, the contract is semantics, and these can actually be nicely expressed with some implementation.[emphases ours] Krzysztof Cwalina, [Framework Design, 80]

N-tier applications can impose much stricter contracts than SOA applications. They can enforce common semantics by requiring both sides to implement the contract by using the same object classes. A DevForce application forces the type on the server tier to be exactly the same as the type on the client tier. This is known as type fidelity. Hiding implementation details is as essential to n-tier design as it is to SOA design; each layer should know as little as possible about the design and works of the other layers. But an n-tier application can and should impose cross-tier requirements if these help realize application objectives. For example, we can require that the data access tier communicate with a UI tier via .NET remoting rather than Web

282 | P a g e

services if this makes the application less complex and perform better; such objectives may matter far more to the customer right now than exposing the business object model as a service66. An n-tier application can also be an application with a Service Oriented Architecture. The application may implement some number of features by invoking a Web service or by embedding a Web service within an object wrapper. The tier may expose some of the applications own functionality as Web services. In such cases, the application is communicating externally via the service. Cross-tier interactions, on the other hand, are communications within the application. Let not blind obedience to SO orthodoxy triumph over rational choice.

POCO Support in DevForce


In addition to objects based on classes inheriting from IdeaBlade.EntityModel.Entity and generated by the DevForce OM Extension, , you can also use Plain Old CLR Objects (POCOs) with DevForce. The class for your object must be deployed on both the client and the server and must be contained in one of the assemblies routinely searched by DevForce. An additional class (or classes), a POCO service provider, must be supplied server-side, marked with the EnableClientAccess attribute. This class will contain methods to perform the server-side CRUD operations for your custom objects. The names for the methods may either conform to flexible naming conventions (to be discussed below), or they can be named anything you desire and marked with an attribute to indicate their purpose. For convenience, you will probably also want to deploy a client-side class containing extension methods for the EntityManager, so that you can refer to your POCO objects in LINQ queries in a manner similar to that which you use with your DevForce Entities. If the objects have an identified (primary) key value, they will operate fully as first-class citizens in the DevForce local cache. That means, among other things, that they can be:
Stored in the cache; Queried against and retrieved from the cache; Updated in the cache; Created in the cache; and of course Saved from the cache.

Objects that do not have an identified key can still be retrieved, but will not be stored in the DevForce cache. DevForces support for POCO objects follows Microsoft RIA (Rich Internet Application) standards, and uses RIA attributes and naming conventions, to the extent that those are available. As those standards

66

This decision does not prevent us from exposing the business object model as a Web service later. Even then we could retain our remoting interface within the application.

283 | P a g e

and facilities evolve or are fleshed out, the implementation in DevForce will be enhanced or migrated to maintain maximum compatibility with the RIA standards.

Examples of POCO Classes


Here is a class representing a State of the United States. At runtime, you might, for example, creating instances of this class using data from an XML data file deployed on the server:
C#
using using using using System; System.Collections.Generic; System.Linq; System.ComponentModel.DataAnnotations;

namespace AppWithPocos.Pocos { public class State { public State() { } //[Key] public string Abbrev { get { return _abbrev; } set { _abbrev = value; } } public string Name { get { return _name; } set { _name = value; } } public bool Lower49 { get { return _lower49; } set { _lower49 = value; } } public long Population { get { return _population; } set { __population = value; } } #region Private Fields string _abbrev; string _name; bool _lower49; long _population; #endregion Private Fields } }

284 | P a g e

Examples of a POCO Service Provider Class


This POCO Service Provider class can be named anything and you may have many such classes but must be flagged with the attribute [EnableClientAccess]. All such classes will be deployed and used server-side only.
C#
using using using using using using using using using using using using using using System; System.Collections.Generic; System.Linq; System.Web; System.ComponentModel; System.Text; IdeaBlade.EntityModel; IdeaBlade.Core; System.Reflection; IdeaBlade.Core.DomainServices; AppWithPocos.Pocos; System.Xml; System.ComponentModel.DataAnnotations; System.Runtime.Serialization;

namespace AppWithPocos { [EnableClientAccess] public class PocoServiceProvider { public PocoServiceProvider() { } #region State //[RequiresAuthentication] // Uncomment this attribute will require "real" authentication public IEnumerable<State> GetStates() { IEnumerable<State> states = ReadStatesData("states.xml"); return states; } private static IEnumerable<State> ReadStatesData(string fileName) { // Create an isntance of XmlTextReader and call Read method to read the file XmlTextReader textReader = new XmlTextReader(fileName); textReader.Read(); List<State> states = new List<State>(); while (textReader.Read()) { State aState = new State(); textReader.MoveToElement(); if (textReader.Name == "State") { aState.Abbrev = textReader.GetAttribute("Abbrev").Trim(); aState.Name = textReader.GetAttribute("Name").Trim(); aState.Lower49 = Convert.ToBoolean(Convert.ToInt32(textReader.GetAttribute("Lower49"))); states.Add(aState); } } return (IEnumerable<State>)states; } #endregion State } }

285 | P a g e

Note the GetStates() method. This method retrieves the data requested in a query (which is usually submitted from the client). It follows a naming convention similar to the one supported under RIA services, where the name consists of a prefix that is one of a number of synonyms for retrieve (here, Get) and a suffix that is the pluralized name of a POCO type (here, States). The prefixes may be any of the following: Get Fetch Find Query Retrieve Select

When the above naming convention is used, a query can be constructed client-side with an expression such as the following:
C#
new EntityQuery<State>("States", anEntityManager);

Alternatively, the server method can be adorned with the QueryAttribute.


C#
[Query] public IEnumerable<State> ReturnAllStates() { IEnumerable<State> states = ReadStatesData("states.xml"); return states; }

In that case, when called client-side the exact name used for the query must be supplied:
C#
new EntityQuery<State>("ReturnAllStates", anEntityManager);

Query Methods with Parameters


Methods with parameters are supported as well. For example, an additional overload to the GetStates() method above that only returns states with a population size greater than a size passed in might be written as shown below. (Naturally, this would require that the internal ReadStatesData() method be modified as well.)

C#

public IEnumerable<State> GetStates(long minPopulationSize) { IEnumerable<State> states = ReadStatesData("states.xml", minPopulationSize);

286 | P a g e

return states; }

On the client side, parameters may be specified by using one of the EntityQuery.AddParameter() overloads. The following snippet calls the parameterized GetStates() method to return just those states with a population greater than one million people: C#
var query = new EntityQuery<State>("States", anEntityManager); query.AddParameter(1000000); // see

Any number of query parameters are permitted, and the standard .NET overload resolution rules apply. This means that the order and type of the parameters are checked to find appropriate matches, with type coercion occurring as required.

When to Use Parameterized Query Methods Rather Than LINQ Expressions


The following two queries will return the same results:

C#

var query1 = new EntityQuery<State>("States", anEntityManager); query.Where(state => state.Population > 1000000);

var query2 = new EntityQuery<State>("States", anEntityManager); query.AddParameter(1000000);

Furthermore, in both cases, the restriction to those states with greater than one million population occurs on the server, not the client. So the question arises: is one to be preferred over the other? The answer usually depends upon how the server-side method itself is implemented. In general, unless the server-side method can internally use the query parameter to restrict its own query against some back-end data store, query parameters have no advantage over LINQ query restrictions. In fact, LINQ queries are far more flexible and intuitive to work with under most circumstances. Nevertheless, there will be cases where a back-end data stores ability to optimize some queries will yield sufficient performance improvement to justify the use of query parameters. For example, consider the Windows file systems ability to search for files, given a path and wildcards. While the same result could be accomplished via a server-side method that returned all of the files in the file system and then iterated over them to locate a desired set of files, it would likely be faster to call the file system directly with the path and wildcard restrictions.

287 | P a g e

Example of a Client-Side Class Containing Extension Methods for the EntityManager


The following class, deployed client-side, can provide further help in putting your POCO objects on an equal footing with your DevForce-generated objects. First, look at the class; then below, well show what its presence permits in terms of data retrieval syntax:
C#
using System; using System.Collections.Generic; using System.Linq; //using System.Web; using System.Diagnostics; using System.Text; using System.Linq.Expressions; using IdeaBlade.EntityModel; using IdeaBlade.EntityModel.Extensions; using IdeaBlade.Core; //using AppWithPocos.Pocos; using DomainModel; using AppWithPocos.Pocos; namespace AppWithPocos { public static class EmExtensions { #region State public static EntityQuery<State> States(this EntityManager em) { return new EntityQuery<State>("States", em); } #endregion State } }

Because there are now static property named States and Foos available that return EntityQuery<State> and EntityQuery<Foo>, respectively, you can now order the retrieval of States and Foos with the following syntax:
C#
_mgr.ExecuteQueryAsync<State>(_mgr.States() .Where(s => s.Lower49, GotStates, null);

This is very similar to what you do with DevForce entities, except that, since States() is an extension method, so you will have to include the parentheses -- _mgr.States() -- which you do not have to do when referencing the generated properties of the EntityManager that return EntityQuery<T>.

288 | P a g e

Note that you can extend the queries with clauses with the full set of LINQ extension methods -- such as Where(), used above -- just as you can do with ordinary queries.67

Obtaining an EntityAspect Property on Your POCO Object


Objects from DevForce-generated Entity classes have an EntityAspect property through which a number of important operations and useful pieces of metadata can be obtained. Custom objects can benefit from the same facilities by one of two methods:
Implementing a very simple interface, IHasEntityAspect, or Wrapping them at runtime

The latter method preserves their DevForce-ignorant POCO status, but will not provide equal performance with the former method. Getter and Setter can be placeholders in the IHasEntityAspect implementation:
C#
namespace AppWithPocos.Pocos { public class State:IHasEntityAspect { [snip] #region IHasEntityAspect Members public EntityAspect EntityAspect { get; set; } #endregion

To wrap an Entity at runtime for the purpose of getting at the EntityAspect property, call the static method EntityWrapper.Wrap():
C#
var stateWithAspect = EntityWrapper.Wrap(aState);

Having done that, you can then ask questions like the following
C#
Bool isModified = stateWithAspect.EntityAspect.IsModified();

and of course, use any of the other facilities of EntityAspect. (Those are documented in the Developers Guide chapter Class Libraries.)

67

As of DevForce 5.1.1, the Include() syntax on a POCO entity query is not yet implemented. The call will compile but will not yet do anything. This will be corrected in a later release.

289 | P a g e

Data Contract Serializer (DCS) versus .NET Data Contract Serializer (NDCS)
The .NET framework includes several important serialization classes. For the purpose of your DevForce apps, two of these are of particular importance: the Data Contract Serializer (DCS) and the .NET Data Contract Serializer (NDCS). DevForce will use one of these serializers in two situations: 1) in transmitting requests and data in an n-tier application, and 2) when saving and restoring the EntityCacheState. By default, DevForce uses the DataContractSerializer (DCS). This has important implications, since this serializer must be told in advance which types will be serialized, i.e., the known types of the application. In WinClient applications, you may instead use the NetDataContractSerializer (NDCS), by changing a setting in the IdeaBladeConfig. Your POCO classes need to be serializable, just as DevForce entities are by default. Your POCO classes also need to be identified as known types when using DCS, so that the serializer can use them. If your POCO class contains a KeyAttribute, DevForce will automatically identify the class as a known type. Heres a POCO class that does have key:
C#
public class State { public State() { }

[Key]
public string Abbrev { get { return _abbrev; } set { _abbrev = value; } } public string Name { get { return _name; } set { _name = value; } } }

VB

When your class is defined without a key, you need to explicitly tell DevForce that the type should be identified to the serializer as a known type. There are several ways to do this; one way is to attribute the class with the DevForce DiscoverableType attribute:
C#

Using IdeaBlade.EntityModel; [DiscoverableType(DiscoverableTypeMode.KnownType)]


public class State { public State() {

290 | P a g e

} public string Abbrev { get { return _abbrev; } set { _abbrev = value; } } public string Name { get { return _name; } set { _name = value; } } }

VB

A second method is to implement the (empty) interface IKnownType:


C#

Using IdeaBlade.EntityModel;
public class State { public State() }

: IKnownType

public string Abbrev { get { return _abbrev; } set { _abbrev = value; } } public string Name { get { return _name; } set { _name = value; } } }

The first method is preferable, but the second method can be useful if you will have many classes that inherit from a base class, and you wish only to mark the base class. A third option is to implement the DevForce IKnownTypeProvider interface and return your list of known types:

291 | P a g e

C#

public class KnownTypeProvider : IKnownTypeProvider { public IEnumerable<Type> AddKnownTypes() { var list = new Type[] { typeof(OrderRequestInfo), typeof(OrderResponseInfo), typeof(List<long>) }; return list; } }

Attributing a Type Where the Signature of Any DataMember-Attributed Member Indicates a Different Type Than Is Actually Returned
The KnownType attribute is used by the DCS to mark up a type with information about any of its serializable members where the declared type of the member is different from the actual type of the member. Suppose, for example, that your State class includes a Cities property which is typed as an object but actually returns a List<Cities>. This will confuse the serializer and make it serialize the property improperly. To enable the property to be serialized properly, you must attribute it as follows:

C#

[DataContract] [KnownType(typeof(List<City>)]
public class State { public State() {} [... snip] [DataMember] public object Cities { get { return _cities; } set { _cities = value; } } #region Private Fields List<City> _cities; #endregion Private Fields }

292 | P a g e

POCO Save mechanisms


DevForce provides two different mechanisms for saving POCO objects. These will be referred to as either adapter-based or convention-based implementations. By default, DevForce will attempt to locate an adapter-based implementation; if unsuccessful, it will then look for a convention-based implementation. The implementation mechanism can also be specified explicitly by setting the PocoSaveMode property of the SaveOptions instance passed into the EntityManager.SaveChanges call:

C#

var so = new SaveOptions(); so.PocoSaveMode = PocoSaveMode.UseEntitySaveAdapter; myEntityManager.SaveChanges(so);

293 | P a g e

The PocoSaveMode is an enumeration with the following structure:

C#

[/// <summary> /// Determines how to discover any custom server side POCO save methods. /// </summary> public enum PocoSaveMode { /// <summary> /// Use an EntitySaveAdapter subclass if found, otherwise use save methods discovered via convention. /// </summary> Default = 0, /// <summary> /// /// </summary> UseEntitySaveAdapter = 1, /// <summary> /// /// </summary> UseMethodsDiscoveredViaConvention = 2

Adapter-Based Saves
The adapter-based mechanism requires the existence of a server-side class that inherits from the IdeaBlade.EntityModel.Server.EntityServerPocoSaveAdapter. If such a class is found, a single instance of this class will be created for each SaveChanges call, and the appropriate methods corresponding to each insert, update, or delete will be called for each entity to be saved. A single null-parameter constructor is required. Note that a single insert, update, or delete method handles saves for every entity type, so if save logic needs to be different for different types, the type of the entity passed in will need to be inspected and the execution branched appropriately.

294 | P a g e

The following bare-bones version of an EntitySaveAdapter implementation shows the methods you have available to override:

C#

public class PocoSaveAdapter : EntityServerPocoSaveAdapter {

public PocoSaveAdapter() { }

public override void BeforeSave( System.Collections.IEnumerable entities, SaveOptions saveOptions) { }

public override void AfterSave() { }

public override void InsertEntity(object entity) { }

public override void UpdateEntity(object entity, object originalEntity) { }

public override void DeleteEntity(object entity) { } }

Convention-Based Saves
The convention-based mechanism requires the existence of a server-side class with the [EnableClientAccess] attribute. This class will include insert, update, and delete methods named according to the conventions defined below. It must also include a single null-parameter constructor.
295 | P a g e

If such a class is found, a single instance of it will be created for each SaveChanges call, and the appropriate methods corresponding to each insert, update, or delete will be called for each entity to be saved. For each type for which a persistence operation is provided, up to three methods must be written. The name of the method must begin with one of the prefixes defined below. The method must also conform to the signature defined below. If you do not expect to save objects that require one or more of these signatures, then they do not have to be implemented. In other words, if you never plan to delete Foo type objects, then you do not need to implement the Delete method. Insert Method
Prefixes: Insert, Add, or Create Signature: void InsertX(T entity), where T is an entity Type

Update Method
Prefixes: Update, Change, or Modify Signature: void UpdateX(T current, T original), where T is an entity Type

Delete Method
Prefixes: Delete or Remove Signature: void DeleteX(T entity), where T is an entity Type

Below is an example implemention showing the use of the required conventional method names and signatures. InsertFoo could equally be named AddFoo or CreateFoo; UpdateFoo could be named ChangeFoo or ModifyFoo; and so forth.

296 | P a g e

C#

[EnableClientAccess]
public class PocoSaveAdapterUsingConventions {

public PocoSaveAdapterUsingConventions() { }

public void InsertFoo(Foo entity) { // insert logic for any Foo entities }

public void InsertBar(Bar entity) { // insert logic for any Bar entities }

public void UpdateFoo(Foo current, Foo original) { // update logic for any Foo entities }

public void UpdateBar(Bar current, Bar original) { // update logic for any Bar entities }

public void DeleteFoo(Foo entity) { // update logic for any Foo entities }

public void DeleteBar(Bar entity) { // update logic for any Bar entities }

297 | P a g e

Summary Things to Remember When Using POCOs in Your DevForce App


POCO classes must be included in both the server- and client-side assemblies. Put them in the server-side project and link them into the client-side project. Include a POCO ServiceProvider class server-side. Include EntityManager extensions client-side. You can use all forms of LINQ query against these objects. If your POCO class has a primary key (designated with the [Key] attribute, instances of it will be stored in the EntityManager cache, and can be updated, deleted, or inserted there. If your POCO class has no key, instances can be retrieved but will not be placed in the EntityManager cache. Implement IHasEntityAspect to get EntityAspect property and associated functionality on your almost POCO entity.

298 | P a g e

IdeaBlade DevForce

Validation Through Verification

Validation Through Verification

Validation Through Verification ............................................................................................... 299


Introduction ....................................................................................................................................................... 299 DevForce Verification ....................................................................................................................................... 300 Getting Started ..................................................................................................................................................... 301 Validation-Related Settings In the Entity Data Model Designer ....................................................................... 301 Generated Property Code ................................................................................................................................... 302 A (Very) Brief Overview of Verification Mechanics ......................................................................................... 306 VerifierOptions .................................................................................................................................................... 306 Verification in the User Interface ....................................................................................................................... 311 Verification in WPF and Silverlight .................................................................................................................. 311 Verification and WinForm User Interfaces ........................................................................................................ 311

Introduction
Validation is the process of evaluating input and judging it valid or invalid. Such evaluation subjects the input to a battery of validation rules that evaluate the input in the appropriate context. For example, if the user enters a committed delivery date we might want to ensure that:
the committed delivery date is reasonable in the abstract, e.g., occurs in the future; it is possible to deliver on that date given the availability of the desired products, and the currently selected shipping method, and whether there is enough time to prepare the goods for shipping; the order is shippable, e.g. the customers credit has been verified, the address is legitimate, and the total is within the limits authorized for this user.

Clearly such rules can be complex, involving not only the input value itself but also the state of the target object (the order), facts about related objects (customer, shipper, product), and aspects of the environment during the validation (time of day, the users role). User input validation gets most of the attention but we need to validate programmatic inputs as well. That delivery date could as easily be set by business logic or a web service request as it is by a wayward click on a calendar control. The rules are the same for everyone, human and machine.
Validation is hard to do well especially as the application grows and validation criteria change. Common failings include: Missing and incorrect validity checks

IdeaBlade DevForce

Validation Through Verification

Inconsistent checking Failure to validate at the right times Poor communication with end-users Inadequate mechanisms for correcting mistakes.

Enterprise application developers are looking for a robust validation system that operates consistently and reliably across a large application. Robust validation cuts both vertically and horizontally:
We validate vertically when we validate several times in multiple layers of the application. We want to validate in the client UI layer so we can give immediate feedback to the user. We may need to validate again when we save, even though the objects we save are no longer on screen. We may even need to validate again on the server side to protect against misadventure coming from outside the relative safety of the hosted environment. We validate horizontally when we apply the same mechanisms uniformly across all modules of the application. If the user can set the delivery date on any of several screens, the same rules ought to apply unless, of course, there is something special about a particular screen.

DevForce Verification
Verification is IdeaBlades answer to the challenges of validation. Verification is a collection of interoperating validation components that are both easy to use and capable of handling sophisticated scenarios. The developer can: Write rules of any complexity. The developer can draw upon pre-defined rules (required value, range check, field length) or write custom rules of any complexity, including rules that compare multiple fields and span multiple objects. Generate validity checking into business objects automatically via the DevForce Object Mapper. Validate any kind of object, not just objects that derive from base business classes. Trigger validity checking at any time such as upon display, before save, or when setting properties. The engine can fire pre-set to block critically errant data from entering the object or fire post-set to accommodate temporarily invalid values. The UI can inspect the engine for rules of interest, fire them, and adjust the display accordingly. It could color a text box, for example, or hide a portion of the form until applicable criteria were met. Display a localized message in the UI without special programming. The UI could display just the validation failed message but it might also show warnings or ok messages and it might supplement the message be re-directing the application focus to the offending object and property. Each rule returns a rich, extensible object with all the information necessary for the developer to deliver a helpful response. Discover rules in the code or retrieve them at runtime from a central store. The engine automatically discovers rules in the code and can acquire rules defined externally in configuration XML, a database, or some other store of rules. The application can inspect, add, and remove rules at any time. Leverage rules inheritance. Rules defined in base classes propagate to their derived classes where they are inherited or overridden. Adjust validation behavior based on a custom validation context. The developer must have the flexibility to convey custom information to the validation process to cope with the variety of situational factors that arise in real applications. Inspect and intervene as the engine validates. The application can monitor the engines progress and interrupt, modify, or terminate a validation run at any point.

IdeaBlade DevForce

Validation Through Verification

Verification Versus Validation


The DevForce validation mechanism is called Verification and all of its components are named with some variation on this word. We mean to try neither your patience nor your vocabulary. We would call our offering validation if we could. However, Microsoft uses the term validation throughout .NET. It appears in Windows Presentation Foundation (WPF) and Windows Workflow Foundation (WWF) namespaces and in the Enterprise Library as well. Microsoft also uses the following class names:
ValidationError, ValidationErrorCollection, ValidationManager, ValidationResult, ValidationRule, ValidationStatus, ValidationType

IdeaBlade is integrating DevForce with Microsofts WPF and WWF. You are likely doing the same. We will all become confused if we cannot easily distinguish among the same or very similar names. So Verification it is. We will continue to say validation when we speaking in general terms; we will use the term verification (and its variants) when we refer specifically to the DevForce classes located in the IdeaBlade.Verification namespace.

Whither WPF Validation?


We cant leave this digression without a parting comment about validation in Microsofts Windows Presentation Foundation. WPF validation concentrates on presentation of validation results within a WPF user interface. This is a vital aspect of any validation strategy. At present, most applications punish the user for the developers own design failings. We need better UIs and better means to guide users rather than humiliate them. DevForces verification concentrates on the validation process. It complements WPF by producing the rich validation results necessary to deliver an effective user experience. We will address the integration of these mechanisms in a separate document.

Getting Started
The easiest point of entry to DevForce verification is through the DevForce Object Mapper.
Well assume that you are familiar with the Object Mapper and have an existing, working application with its own business object model. See the Hello DevForce topic document in the DevForce Learning Resources under Introduction to DevForce for a walk-through of the Object Mapper.

Validation-Related Settings In the Entity Data Model Designer


1. 2. Open an Entity Data Model in the Visual Studio Entity Data Model designer. Display the Properties panel and then click in white space in the designer window to display the properties of the ConceptualEntityModel. Note the two validation-related properties: Generate Validation Attributes

IdeaBlade DevForce

Validation Through Verification

Generate Verification Attributes

The Validation Attributes refer to the .NET validation attributes those used by Silverlight. The Verification Attributes refer to the DevForce verification attributes used by DevForce to control the behavior of its verification types.

3.

You can control whether verification is performed before proposed new values are pushed into a business object property, after this is done, or both by means of a VerifierOptions type. An instance of the type is available as a property on the VerifierEngine class via DefaultVerifierOptions; and also on the Verifier, VerifierArgs, and VerifierResults classes as VerifierOptions.

Generated Property Code


Here well show you the results of the four possible combinations of values for the two validationrelated code generation properties.
We recommend that you use only use DevForce verification attributes (the first combination documented below) unless you have a compelling reason to do otherwise (e.g., you have a large amount of code that already uses the .NET attributes and facilities). DevForce verification is a superset of the .NET variety, and it will be less confusing to your development team if only one style of attribute is used.

Generate (DevForce) Verification Attributes Only

IdeaBlade DevForce

Validation Through Verification

Here is the FirstName property of an Employee object as generated with the settings shown above:

C#
#region CompanyName property /// <summary>Gets or sets the CompanyName. </summary> [Bindable(true, BindingDirection.TwoWay)] [Editable(true)] [Display(Name="CompanyName", AutoGenerateField=true)] [IbVal.ValidateProperty]

[IbVal.StringLengthVerifier(MaxValue=40, IsRequired=true, ErrorMessageResourceName="Customer_CompanyName")]


[DataMember] public string CompanyName { get { return PropertyMetadata.CompanyName.GetValue(this); } set { PropertyMetadata.CompanyName.SetValue(this, value); } } #endregion CompanyName property

IbVal is an alias for the IdeaBlade.Validation namespace, defined at the top of the code file. The IbVal.ValidateProperty attribute tells both DevForce and Silverlight to check for verifiers when this property gets set, and to run any whose settings define them as appropriate. The IbVal.StringLengthVerifier sets a maximum length on the (text) value, and its IsRequired argument declares the property non-nullable. Generate (.NET) Validation Attributes Only

IdeaBlade DevForce

Validation Through Verification

Here is the generated code that the above settings in the EDM designer: C#
#region CompanyName property /// <summary>Gets or sets the CompanyName. </summary> [Bindable(true, BindingDirection.TwoWay)] [Editable(true)] [Display(Name="CompanyName", AutoGenerateField=true)] [IbVal.ValidateProperty]

[Required()] [StringLength(40)]
[DataMember] public string CompanyName { get { return PropertyMetadata.CompanyName.GetValue(this); } set { PropertyMetadata.CompanyName.SetValue(this, value); } } #endregion CompanyName property

DevForce still generates the IbVal.ValidateProperty attribute to tell both DevForce and Silverlight to check for verifiers when this property gets set, and to run any whose settings define them as appropriate. However, the non-nullability (i.e., Required) and string length constraints are specified a bit differently than was done when using DevForce attributes. DevForce can make use of either style of validation attribute. Its own versions provide richer capabilities than the .NET counterparts, but if you need your code to use the .NET attributes for reasons of your own, DevForce cooperates.

Generate Both (DevForce) Verification and (.NET) Validation Attributes This combination of settings actually causes exactly the same code to be generated as choosing to Generate (DevForce) Verification Attributes alone does. Thats because weve been able to coerce Silverlight into respecting our attributes (as we do theirs), so that generating their attributes is actually not necessary in order to use the .NET validation facilities and get them to work as you want them to.

IdeaBlade DevForce

Validation Through Verification

C#
#region CompanyName property /// <summary>Gets or sets the CompanyName. </summary> [Bindable(true, BindingDirection.TwoWay)] [Editable(true)] [Display(Name="CompanyName", AutoGenerateField=true)] [IbVal.ValidateProperty] [IbVal.StringLengthVerifier(MaxValue=40, IsRequired=true, ErrorMessageResourceName="Customer_CompanyName")] [DataMember] public string CompanyName { get { return PropertyMetadata.CompanyName.GetValue(this); } set { PropertyMetadata.CompanyName.SetValue(this, value); } } #endregion CompanyName property

Generate Neither (DevForce) Verification Nor (.NET) Validation Attributes Here is the generated code we get after setting both Generate Validation Attributes and Generate Verification Attributes to false:

C#

#region CompanyName property /// <summary>Gets or sets the CompanyName. </summary> [Bindable(true, BindingDirection.TwoWay)] [Editable(true)] [Display(Name="CompanyName", AutoGenerateField=true)] [IbVal.ValidateProperty] [DataMember] public string CompanyName { get { return PropertyMetadata.CompanyName.GetValue(this); } set { PropertyMetadata.CompanyName.SetValue(this, value); } } #endregion CompanyName property

DevForce still indicates (via the [IbVal.ValidationProperty] attribute) that validation should be run against the property; so if you define any custom verifiers using other mechanisms (to be discussed shortly) they will get exercised. However, there is now no indication from the property attributes that the property either requires a value, or is limited to any particular length.

IdeaBlade DevForce

Validation Through Verification

A (Very) Brief Overview of Verification Mechanics


What youve seen thus far in this document are verifiers defined with property attributes. You get these for free with DevForce. Simply by flipping a switch in the Entity Data Model Designer (actually, its set ON by default!) you get all of the basic constraints defined on column values by the backing database carried through to your business classes in the code that the IdeaBlade OM Designer Extension generates. This, however, is by no means the extent of the validation capabilities that DevForce provides. Well describe the Verification system in detail later in this document, but first, so you dont lose the forest for the trees, lets take a moment for a high-level overview of how the system works, what you can do with it, and how you do it. Creating a Verifiers Collection for a Type When one of your business types is first instantiated during an application session, a VerifierEngine is invoked to discover all verifiers applicable to that type. Some of these verifiers are defined using property attributes, as youve seen; but verifiers may also be defined in .NET code -- in very flexible and powerful ways! and even in XML. The VerifierEngine discovers all of the verifiers, however encoded, and creates an in-memory collection of them. They are thenceforward available for it to call upon whenever needed. Execution Modes for a Verifier Each of the verifiers has its own properties which tell the VerifierEngine when it should be run. For example, you can define a verifier so that it runs before a proposed new property value is pushed into the business object; or after; or even both (though that is unusual). You also want most verifiers to run whenever an entire instance of a type is being validated. To specify these things, you specify the ExecutionModes on an instance of the VerifierOptions type.

VerifierOptions
The VerifierOptions type collects a number of settings that affect a verifiers behavior, such as when it it is run and how it handles exceptions. An instance of the type is available as a property on the VerifierEngine class via DefaultVerifierOptions; and also on the Verifier, VerifierArgs, and VerifierResults classes as VerifierOptions. The properties of VerifierOptions are detailed in the folowing table:

IdeaBlade DevForce

Validation Through Verification

VerifierOptions has the following properties:

Property
ErrorContinuationMode

Description Gets or sets whether a failure in this verifier should stop the execution of the remainder of the batch in which this verifier is executing. [Enum] Used to determine whether the Entity should throw errors during a property verification or raise the errors thru the INotifyDataErrorInfo interface instead. [Enum] Gets or sets the conditions under which a verifier is executed. See the material immediately below the table for more information about these. [Enum] Returns a version of this VerifierOptions with its raw values. May be used to determine which properties are inherited. [Enum] [bool] Whether to treat verifier warnings as errors. [bool] [Verifier]

System Default Value VerifierErrorContinuationMode.Continue

ErrorNotificationMode

VerifierErrorNotificationMode.Notify

ExecutionModes

VerifierExecutionModes.InstanceAndOnAfterSetTriggers

RawVerifierOptions

ShouldExitOnBeforeSetError TreatWarningsAsErrors Verifier

false false

IdeaBlade DevForce

Validation Through Verification

The system default values shown in the table above are the default settings for the properties of VerifierEngine.DefaultVerifierOptions. All VerifierOptions properties can be inherited from a parent class and, by default, do so. VerifierEngine is parent to any Verifiers contained within it; Verifier is parent to any VerifierResults resulting from its execution. If VerifierEngine.DefaultVerifierOptions is changed, then by default, every Verifier and VerifierResult will inherit this change. But since Verifier and VerifierResult have their own VerifierOptions property, any aspect of VerifierOptions can be overwritten for any given Verifier or VerifierResult. All enum-valued properties on VerifierOptions may be set to an enum value of Inherit to indicate inheritance. Properties that are of type bool? may be set to null. More Detail on ExecutionModes The determination of when and how a verifier is executed is controlled via the IdeaBlade.Validation.VerifierOptions.ExecutionModes and IdeaBlade.Validation.Verifier.TriggerLinks properties on each verifier. A verifier can be executed either
1. 2. in response to a single change (e.g., of a property value); or in the context of verifying an entire object.

The first type of verification listed above is known as a triggered verification. The second is known as an instance verification. We would apply a triggered verification when (for example) a user was changing, or had just changed, the HireDate on an employee. At that time we would only want to run those verification tests whose outcome we feel may have been affected by this specific change. Our goal would be to provide the end user with instant feedback about their change. In most cases, it will never be easier for them to correct a mistake than immediately after making it! Triggered verifications can be subdivided into before and after triggering categories. A BeforeSet verification is applied before a proposed new property value is actually pushed into the business object. It prevents invalid data from ever getting into the business object. An AfterSet verification is applied after the new value is pushed into the business object. It lets the change go in, but raises an immediate flag. Instance verification describes an operation that completely verifies an entire entity, applying all relevant validation rules, whether those rules apply to individual properties of that entity, to a combination of its properties (the validity of whose values must be assessed in relation to each other), or in some other way to the entity as a whole. We might perform an instance verification, for example, on a particular instance of an Employee before permitting it to be saved to the database. This would likely require performing verification tests on a number of individual properties of the employee object; it might also require testing the state of related objects (such as the Orders written by that Employee). Only if the entire suite of tests required to fully validate the Employee were passed would we give the okay to save it to the database.

308 | P a g e

IdeaBlade DevForce

Validation Through Verification

Note that instance verification would be unnecessary if we could really be sure that every change affecting our Employee would be addressed by an appropriate set of triggered verifications. In practice this can be very difficult, or even impossible, to ensure. What if, for example, our Employee is changed by some other application which doesnt apply the same rules that our does? What if it is changed directly in the database by someone with access to that? Even when we can guarantee that neither of those things happen, the mechanisms by which a given entity can be changed inside a single application can become quite complex over time. For all of those reasons, developers commonly perform instance verification at such key junctures as when an entity is submitted for saving. Its an important last line of defense against invalid data. It is common for a given verifier to be applicable during instance verification as well as during triggered verification. Every verifier comes with information about the situations in which it should be executed, via its VerifierOptions.ExecutionModes property. That property takes values from a VerifierExecutionModes enumeration whose choices permit you to make your verifier run in any and all of the circumstances you deem appropriate. The values in the VerifierExecutionModes enumeration, and their impacts, are as follows:

Enum Value
All68

Instruction to the VerifierEngine Run during instance verification and in response to designated triggers (both before the proposed value is pushed into the business object, and also after it has been pushed into the business object). Do not run. Inherit the ExecutionModes setting from the types parent (Verifier if a VerifierResult, and VerifierEngine if a Verifier) Run during instance verification. Run during instance verification and in response to designated triggers, after the proposed value has been pushed into the business object. Run during instance verification and in response to designated triggers, before the proposed value is pushed into the business object. Run in response to designated triggers, after the proposed value has been pushed into the business object. Run in response to designated triggers, before the proposed value is pushed into the business object.

Disabled Inherit Instance InstanceAndOnAfterSetTriggers

InstanceAndOnBeforeSetTriggers

OnAfterSetTriggers OnBeforeSetTriggers

68

This setting is somewhat unusual, but because a verifier has access to information about the triggering context in which it is being run (i.e., before or after), it is possible to include within a single verifier logic that will only be applied in one context or another. From this arises the possibility that you, the developer, might want it to run in both triggering contexts.

309 | P a g e

IdeaBlade DevForce

Validation Through Verification

Settings of InstanceAndOnAfterSetTriggers and InstanceAndOnBeforeSetTriggers are by far this most common (after Inherit). All is a particularly uncommon setting because it orders triggered execution both before and immediately after a new value is pushed into a business object. However, a verifier, as it so happens, has access to information about the triggering context in which it is being run (i.e., before or after), so it is possible to include within a single verifier logic that will only be applied in one context or another. From this arises the possibility that you, the developer, might want such a verifier to run in both triggering contexts. BeforeSet Versus AfterSet Execution This topic deserves special discussion, as the choice between these two execution modes can be one of your most difficult ones. All other things being equal, it is desirable never to allow invalid values into a business object in the first place. If all things were equal, one would use OnBeforeSetTriggers or InstanceAndOnBeforeSetTriggers as the ExecutionModes for all property-level verifiers. However, in practice, these settings can cause problems, especially before an application has been fully fleshed out. For example, in the user interface, a BeforeSet verifier can demand that the end user fix a bad value entered for a property right then and there, before moving on to any other work -- even if she hasnt the faintest idea what to change the bad value to. There are ways to handle such issues in a manner thats friendly to the end user; but in order not to force you to solve such problems right out of the chute we have set the default value for ExecutionModes to VerifierExecutionModes.InstanceAndOnAfterSetTriggers.

Defining Triggers for a Verifier


Consider a constraint that specifies that the HireDate for an Employee must be later than the Employees BirthDate. That seems a pretty reasonable requirement, but suppose when the user enters a HireDate of today, this rule is found to have been violated. But the Employee really was hired today, and the problem is that the BirthDate previously entered for the Employee was in error, specifying the year 2015 when it should have said 1985. If we prevent the new HireDate value from being entered into that property, well subject the user to a lot of unnecessary work. Shell have to clear the new HireDate, even though it is entirely correct, and then go fix the BirthDate value, and then come back and re-enter the new HireDate value, even though she got it right the first time. Users dont have a lot of tolerance for this sort of thing! Its confusing, irritating, or often both. A verifier that applies to BirthDate doesnt have to be triggered by a change to that property. You can, instead or additionally specify that you want it called when the value of HireDate is changed. You can even set it to be triggered by a change to a property of some other type; and not just any instance of that type, but one that happens to be specifically related. For example, suppose you have a rule that says that the OrderDate on an Order taken by Employee X must be greater than or equal to that Employees HireDate. You could define this rule (as a verifier) on the Employee type, but specify that it should be triggered not only by a change to Employee.HireDate, but equally by a change to the OrderDate property of any Order written by that Employee!

310 | P a g e

IdeaBlade DevForce

Validation Through Verification

Pre-Defined Verifiers
DevForce ships with a number of pre-defined verifiers that can be subclassed in flexible ways: examples include the DateTimeRangeVerifier, the PhoneNumberVerifier, the NamedRegexPatternVerifier, the StringLengthVerifier, the RequiredValueVerifier, and a number of others. In addition, it defines a generic DelegateVerifier<T> that provides unlimited verification capabilities: you can use it to define any sort of rule you need or can imagine. Youll find examples of all of these types of verifiers in the sample code solutions included in the Validation area of the Learning Resources.

Verification in the User Interface


Now that the application is detecting invalid data and throwing exceptions, we had better think about how we want to handle those exceptions and tell the user what is going on.

Verification in WPF and Silverlight


IdeaBlade.Core.ComponentModel.INotifyDataErrorInfo has been implemented in DF 2010 with the same semantics as the similarly named class in the Silverlight CLR version of System.ComponentModel. This interface, in turn, is implemented by the DevForce EntityWrapper, from which all of your DevForcegenerated Entity types derive. Because of the implementation of this interface by Entity, it is now possible for you to configure your entities to collect validation errors rather than throw exceptions. (You may also do both, if you wish.)

Verification and WinForm User Interfaces


The following material is specific to WinForm user interfaces. UI Lockup
The UI is going to lock up the moment the user enters an invalid value into a verified UI control. That is any data entry control: TextBox, DataPicker, ComboBox, etc. The user will not be able to leave that control until she enters a value that passes validation not even to close the form. In this illustration, the user cleared the Last Name. The last name is required. The form displays an error bullet and prevents the user from moving out of the textbox.

How does the user recover?

311 | P a g e

IdeaBlade DevForce

Validation Through Verification

If this were a grid, she could press the *Esc+ key; it is standard for grid controls to restore the previous value when the user presses escape. How many users know that? In any case, this TextBox is not in a grid and pressing [Esc] does nothing but ring an annoying bell. The user can press the standard key chord for undo: Ctrl+Z. How many users know that? No, the most users will just keep entering new values until they find one that lets them out of the field. Needless to say, a UI should apply the lock up enforcement technique sparingly. In the authors opinion, it makes sense only for
a value the user must know and is sure to know a value that must be correct immediately and at all times.

Dosage of a dangerous prescription drug would fit this bill. Few other properties qualify. Unlock the UI with AutoValidate Recall that the DevForce Entity.BeforeSetValue and Entity.AfterSetValue methods raise a VerifierResultException when the property fails validation. This exception bubbles up and out of the property setter.69 Data binding traps the exception70 and responds by locking up the form. Fortunately, WinForms .NET 2.0 makes it easy to change this response.
The key is the System.Windows.Forms.UserControl.AutoValidate property which takes one of the System.Windows.Forms.AutoValidate enumerations. AutoValidate
Inherit

Description Do what the parent UserControl does. The parent is the UserControl that contains this UserControl. This is the default for new UserControl instances. If there is no parent, the value is the default, EnablePreventFocusChange.

EnablePreventFocusChange EnableAllowFocusChange Disable

Prevents the user from leaving the control until the value passes validation. Validate but permit the user to leave the control if validation fails. Does not validate. Generally not a good choice.
71

Inherit is the default value for all new UserControls

. Inherit means that the UserControl is governed by the AutoValidate setting of its parent UserControls, the UserControl that contains it.

69 70 71

Thanks to the System.Diagnostics.DebuggerNonUserCodeAttribute that decorates the setter. During the data binding Validate event raised when the user attempts to leave the TextBox.
UserControl is the base class for developer designed screens. System.Windows.Form inherits from UserControl. Individual UI widgets such as TextBox do not inherit from UserControl.

312 | P a g e

IdeaBlade DevForce

Validation Through Verification

The outer UserControl, typically a Form, doesnt have a parent so it is governed by the EnablePreventFocusChange setting. If we never change the AutoValidate property on any UserControl, our application is governed by the setting in the Form which, as we have seen, is EnablePreventFocusChange, the setting that locks up the form. All UserControls within the Form are inheriting this behavior. If we change the Forms AutoValidate property to EnableAllowFocusChange, the widgets on the Form will no longer lock up when the setter throws an exception. Neither will widgets on the contained UserControls because they inherit the parent Forms setting. So the quick answer to UI lockup:
Change the Forms AutoValidate property to EnableAllowFocusChange C#
this.AutoValidate = System.Windows.Forms.AutoValidate.EnableAllowFocusChange; // Can move

Visual Basic
me.AutoValidate = _ System.Windows.Forms.AutoValidate.EnableAllowFocusChange ' Can move

Improving the Users Experience


EnableAllowFocusChange and BeforeSet Triggers AutoValidate.EnableAllowFocusChange works great for property verifiers governed by BeforeSet triggers.
The user can move out of the TextBox. Yet she can still see the error bullet protesting the lack of a last name.

The TextBox remains cleared so we can see that there is a problem or rather that there was a problem, that our intent to clear the name was invalid. The LastName property itself was never actually changed. A BeforeSet trigger prevents the property setter from updating the object. At the moment there is a discrepancy between the business object property value and the corresponding widget control display property on screen 72.

72

We could set the DevForce BindingDescriptor.CancelEditOnError for the binding to LastName to true; this would immediately restore the TextBoxs display of the original value. The author dislikes that choice because it obscures what the user was trying to do by replacing the users data entry. She sees a warning about a problem that is no longer the problem.

313 | P a g e

IdeaBlade DevForce

Validation Through Verification

We can see reveal the discrepancy and cure it by scrolling off of the Nancy employee and then returning to her. The TextBox refreshes with her current LastName property value which remains Davolio. EnableAllowFocusChange and AfterSet Triggers The behavior is different for verifiers evaluated in response to AfterSet triggers. If we had a LastNameRequiredVerifier and set its ExecutionModes to InstanceAndOnAfterSetTriggers, the LastName property value would be empty, just as it appears in the TextBox. A AfterSet trigger causes validation after the property has been set with the proposed value. We can confirm this by scrolling off of the Nancy employee and then returning to her. The TextBox remains blank. The current LastName property value is empty. However, we are no longer aware of the latent validation error. Our application does not validate the Employee upon display and that might be a user experience problem73. At least it is not a data integrity problem or doesnt have to be. We must assume that the application follows our advice and ensures that every entity must survive instance verification before it can be saved. We further assume that the application has some mechanism to display errant entities and their problems. Perhaps a simple MessageBox will do. This Employee will not survive validation, will not be saved, and the user will be told why. Questionable User Experience This approach may be viable if little time can pass between data entry and instance verification. Some applications attempt a save whenever the user moves off the current screen. The user will never lose sight of the LastName error bullet and the save effort will reveal all latent problems with this employee. Many applications delay save and allow the user to move around among entities with pending changes. Thats how our tutorial works. Users can make a change to Nancy, scroll to Andrew and make changes to him, then scroll back to Nancy to continue her updates. In this kind of workflow, the user may not remember that there is a problem with the Nancy object for minutes or hours. When the application finally tells the user about this problem, the mental context is long gone and the application will be perceived to be unfriendly.

73

We could write code to perform instance validation whenever the Employee changed. We could capture the VerifierResults and display them as well as light up bullets next to each widget. The code is not hard to write but its not utterly trivial either. Well describe an approach that achieves something of that effect using a different technique.

314 | P a g e

IdeaBlade DevForce

Validation Through Verification

There is another, potentially greater risk. The user may make a critical business decision base upon what is visible on the screen. That data could be in error. The user wont know it if she scrolled off and then back on to the record. If this risk is serious, the application must behave differently whenever the UI displays a new object a new Employee in our example. Instance Verification Upon Display One approach would be to perform instance verification whenever the currently displayed object is changed.

315 | P a g e

IdeaBlade DevForce

Validation Through Verification

Validation Through Verification - Advanced

Validation Through Verification - Advanced ........................................................................... 316


Introduction ....................................................................................................................................................... 316 Verification Types Overview .............................................................................................................................. 317 Main Verification Classes.................................................................................................................................. 317 Verifiers ............................................................................................................................................................. 319 VerifierResult .................................................................................................................................................... 322 Triggers.............................................................................................................................................................. 324 VerifierEngine ................................................................................................................................................... 326 PropertyValueVerifiers ...................................................................................................................................... 328 Verification Deep Dive ........................................................................................................................................ 332 Verifiers ............................................................................................................................................................. 333 Verifier Result ................................................................................................................................................... 338 Triggers.............................................................................................................................................................. 341 VerifierEngine ................................................................................................................................................... 350 Invoking Verification........................................................................................................................................... 355 Instance Verification .......................................................................................................................................... 357 Trigger Verification: BeforeSet and AfterSet .................................................................................................... 358 Monitor Execution with the VerifierBatchInterceptor ....................................................................................... 362

Introduction
The basics of DevForces validation facilities are covered in the separate topic document Validation Through Verification. This document picks up where that one leaves off, delving deeper into the Verification types and mechanics.

316 | P a g e

IdeaBlade DevForce

Validation Through Verification

Verification Types Overview


Most Verification classes are defined under the IdeaBlade.Validation namespace and deployed in a single DevForce class library, IdeaBlade.Validation.dll.74 This section is a guide to the key Verification constructs in that library.

Main Verification Classes

74

There are some legacy classes having to do with WinForm support that are defined in IdeaBlade.Verification.dll. You will not need these if doing WPF or Silverlight applications.

317 | P a g e

IdeaBlade DevForce

Validation Through Verification

These are the main classes, the classes at the heart of the DevForce Verification paradigm. Type
VerifierEngine

Description A VerifierEngine maintains a list of Verifier instances in its VerifierCollection and executes them at the appropriate times, accumulating their VerifierResults in its VerifierResultCollection. A Verifier should not run independently. Rather it should be evaluated by a VerifierEngine and the caller should reap the results from the engines Execute method when it finishes. A Verifier validates the state of an object. This is the abstract base class for a family of verifiers described below. execution produces a VerifierResult object. This object, in addition to signaling validation success or failure, contains detailed information about the outcome and the context of the verifiers execution.
Verifier

Verifier VerifierCollection VerifierResult VerifierResultCollection

VerifierOptions

instance contains settings that determine many aspects of a verifiers behavior, such as when it it is run and how it handles exceptions.
A VerifiersOptions

TriggerItem

A TriggerItem identifies something like an event. When the VerifierEngine executes in the context of this event, it evaluates all verifiers attached to that TriggerItem. A property is the most commonly encountered TriggerItem. The setting of that property is the associated event. The engine looks for all verifiers that are attached to that property in the right way and executes them.

TriggerLink

Triggered verifiers have one or more TriggerLinks, each of them connecting the verified object to a TriggerItem. The TriggerLink specifies both the TriggerItem and a path back to the verified object. In the case of a simple property it is the very short path from the property to the instance as in the path from Employee.FirstName to Employee. Developers can write complex paths that navigate from a triggering event on an object that is far removed from the object being validated. For example, changing a customers credit limit property (the trigger) could stimulate verification of all outstanding orders (the verified objects) related to (the path to) that customer.

The Verification types are all interrelated, but we separate them by category for explanatory purposes.

Verifiers VerifierResult Triggers VerifierEngine

The Verifier class and its supporting types.


VerifierResult

and related types. and related types.

TriggerItem, TriggerLink,

The VerifierEngine and its supporting types

318 | P a g e

IdeaBlade DevForce

Validation Through Verification

PropertyValueVerifiers

Describes the pre-defined verifiers for the most commonly encountered validation cases.

Verifiers
A Verifier validates the state of an object. A Verifier can only run after it is instantiated by a VerifierEngine. The Verifier class is the abstract base class for derived verifiers that are attuned to specific validation tasks; we cover these derived classes in a separate section below.
The following table highlights significant members of the Verifier class.

Class Member
AddTrigger, AddTriggers ApplicableType DefaultSortOrder Description ExecutionModes GetDisplayName

Description Adds a TriggerLink to the verifier. The verifier subsequently responds to events associated with the TriggerItem in that TriggerLink. The verifier validates objects of this type Static property that reveals the SortOrder given to new verifiers by default. The description of the verifier as displayed to the user. The situations in which the verifier should run. The value combines flags from the VerifierExecutionModes enumeration. Returns a name for the member of a type as it should be displayed to the user. First Name might be the display name for the FirstName property of an Employee. The method is often used to construct the Description from a message template. The integer position of this Verifier in its engines list. Runs the method that indicates if this Verifier applies to a given instance when run in a particular context. Returns a VerifierApplicabilityresult. Set to one of the VerifierOnErrorMode enumerations (Stop, Continue) that tells the engine whether it should stop or continue verifying if this verifier produces an errant VerifierResult. Continue is the default. Removes a TriggerLink from the verifier. The verifier no longer responds to events associated with the TriggerItem in that TriggerLink. The engine executes verifiers in sorted order. The engine sorts verifiers first by this SortValue and, when those are the same, by the order in which the verifiers were added to the engine (InitializationOrder). Thus, the developer can influence verifier processing order by setting this SortValue. Returns the TriggerLinks attached to this verifier.

InitializationOrder IsApplicable

OnErrorMode

RemoveTrigger, RemoveTriggers

SortValue

TriggerLinks

319 | P a g e

IdeaBlade DevForce

Validation Through Verification

Class Member
VerifierArgs VerifierEngine VerifierOptions

Description Configuration data for the Verifier. Every Verifier is created with a VerifierArgs instance, either explicitly or implicitly. The engine to which the verifier is attached. The set of VerifierOptions that will determine the behavior of this verifier. Any options not set at this level will inherit their settings from the parent VerifierEngine. For Internal Use. This method implements the verifiers core validation test and can be overridden by a derived class. The method is public so that a VerifierEngine can call it at the appropriate time. Developers cannot call it directly it will throw an exception.

Verify

The following is list of types that are closely related to Verifier. The list is (mostly) alphabetical to make it easier to locate a type and for lack of more compelling organizational principle. Type
ApplicabilityConstraint(Of T)

Description that determines if a Verifier applies to a particular object given the current TriggerContext and VerifierContext. It returns a VerifierApplicability object. T is the type of the verified object.
Delegate

The developer can invoke this constraint directly by calling Verifier.IsApplicable.


DelegateVerifier(Of T)

A harness for a custom verifier that validates an object of type T. The developer can build almost any kind of verifier with an instance of this class. The developer writes the validation test inside a VerifierCondition delegate (hence the name) and includes a reference to the delegate method in the DelegateVerifier constructor. This verifier can be configured with triggers in the same way as all other verifiers.

DelegatePropertyValueVerifier (Of T)

A harness for a custom property verifier that validates an object of type T. Used to build a verifier triggered by a single property with the purpose of evaluating the proposed or actual value of that property. The developer implements the validation test inside a method that conforms to the ValueVerifierCondition delegate and passes a reference to the method in the DelegatePropertyValueVerifier constructor. The verifier behaves like any of the predefined PropertyValueVerifier classes described below.

PropertyValueVerifierAttribute

Each of the PropertyValueVerifiers can be specified declaratively by decorating a property with the corresponding PropertyValueVerifierAttribute. An object passed to a Verifier when it is triggered by a TriggerItem. The object provides the Verifier with information about what triggered it. Trigger classes are covered separately below.

TriggerContext

320 | P a g e

IdeaBlade DevForce

Validation Through Verification

Type
ValueVerifierCondition(Of T)

Description
Delegate that determines if a value passes its verifier VerifierResult. T is the type of the verified object.

test. It returns a

The developer can supply such a delegate as an argument to the constructor of a DelegatePropertyValueVerifier.
Verifier

Abstract base class for a family of Verifiers. A Verifier validates the state of an object and returns a VerifierResult containing detailed information about the validation outcome and the context of the verifiers execution. Strongly typed abstract subclass of Verifier where T is the type of the verified object. The Verifier determines if it applies to an object it is validating based on an ApplicabilityConstraint. That constraint method returns an object of this type which contains both a VerifierApplicabilityCode and an optional message. An enumeration of result codes emerging from evaluation of an ApplicabilityConstraint. These args carry configuration data for a Verifier. Every Verifier is created with a VerifierArgs instance, either explicitly or implicitly; every Verifier retains a reference to that instance. This is also the base class for a family of VerifierArgs classes, each strongly typed to fit closely with it corresponding Verifier class. The ListVerifier has its ListVerifierArgs for example. Abstract base class for a family of Attribute classes that enable declaration of a Verifier by decoration with an attribute. For example, we can declare that the FirstName property has a StringLengthVerifier by adorning it with the StringLengthVerifierAttribute. A collection of Verifier instances. The collection implements many of the features of List<Verifier> and, most importantly, many Find overloads to facilitate extraction of Verifier subset collections. that implements a validation test on an object of type T. This is the beating heart of the developers custom DelegateVerifier.
Delegate

Verifier(Of T) VerifierApplicability

VerifierApplicabilityCode VerifierArgs

VerifierAttribute

VerifierCollection

VerifierCondition(Of T) VerifierContext VerifierOnErrorMode

The VerifierEngine executes a Verifier in a particular context and makes this context available to the Verifier as it executes. The enumeration (Stop, Continue) that tells the engine whether it should stop or continue verifying if this verifier produces an errant VerifierResult. The developer can set the Verifier.OnErrorMode to a value from this enumeration. The exception thrown when the Verifier itself fails to execute properly, i.e. when the Verifier throws an exception; that exception is included in the InnerException. Not to be confused with the VerifierResultException.

VerifierException

321 | P a g e

IdeaBlade DevForce

Validation Through Verification

Type
VerifierExecutionModes

Description A flag enumeration (Disabled, Instance, OnAfterSetTriggers, OnBeforeSetTriggers) that describes the situations in which a Verifier can run. The VerifierEngine, while executing in one of these situations, runs the verifiers that have a matching ExecutionModes flag. The developer can set a Verifier to run in multiple situations by setting its ExecutionModes to a combination of these flags constructed by oring them together. The VerifierExecutionModes enumeration exposes several of the most common combinations (e.g. All which translates to Instance | OnAfterSetTriggers | OnBeforeSetTriggers ).

VerifierResult
Verifier execution produces a VerifierResult object

. This object, in addition to signaling validation success or failure, contains detailed information about the outcome and the context of the verifiers execution.

75

75

We may say casually that a Verifier returns a VerifierResult but this is not strictly correct and might mislead the developer into improper use of verifiers. While it is true that the Verifier.Verify method returns a VerifierResult, that method executes only one part of the Verifiers validation logic and should not be called by developer code. The Verifier should be executed by a VerifierEngine which stores the VerifierResult in its own results collection. The developer retrieves those results from the engine.

322 | P a g e

IdeaBlade DevForce

Validation Through Verification

The following table highlights significant members of the VerifierResult class. Class Member
Description IsOk ResultCode TargetInstance TriggerContext Verifier VerifierContext VerifierOptions

Description Description of this result. The text is usually fashioned in a form suitable for display when the result is not Ok. True if the result has one of the Ok VerifierResultCodes. The VerifierResultCode for this result. The object that was verified. The TriggerContext in which the verifier was executed. The Verifier whose execution produced this result. The VerfifierContext in which the verifier was executed. The set of VerifierOptions that will determine the behavior of this verifier. Any options not set at this level will inherit their settings from the parent VerifierEngine.

The following are the important types that are most closely related to VerifierResult. Type
VerifierResultCode

Description Enumeration summarizing the result of verification in a single value. While there are several codes, each is a flavor of a binary outcome: success (Ok) or failure (Error). The codes at this writing are: Error, ErrorInsufficientData, Ok, OkNotApplicable, OkWarning.

VerifierResultCollection

The VerifierEngine accumulates a collection of VerifierResult instances which it returns as a VerifierResultCollection from its Execute method. The collection implements most of the features of Collection<VerifierResult> and, importantly, many Find overloads to facilitate extraction of VerifierResult subset collections. The caller of the VerifierEngine may want to throw an exception if it detects an errant VerifierResult. The VerifierResultException is a strongly-typed exception for this purpose; it can report the initial errant VerifierResult as well as a VerifierResultCollection of other results that may be useful to a handler of the exception. The Entity.BeforeSetValue and Entity.AfterSetValue methods are examples of VerifierEngine callers that throw VerificationResultExceptions. The Verifier and the VerifierEngine do not themselves throw this exception; they merely report errors by providing VerifierResults. Do not confuse the VerificationResultException with the VerificationException. A Verifier or VerifierEngine will throw a VerificationException when the verifier fails to execute properly. Improper verifier execution is not the same as an invalid object condition.

VerifierResultException

323 | P a g e

IdeaBlade DevForce

Validation Through Verification

Type
VerifiersErrorsResource.resx

Description A resource file of predefined error message templates. This resource file contains the message templates for constructing VerifierResult descriptions. The developer can substitute a different .NET ResourceManager that governs a wider set of message templates and resources files for different locales; the developers main resource file must contain definitions for all of the message names defined in the VerifiersErrorsResource.resx. The DevForce distribution includes this resource file as a starting place for the developers own resource file (and satellite translation files).

Triggers
Evaluation of a Verifier may be triggered by one or more events.
Events is in quotes because the mechanism, while it feels like an event, does not use the .NET event. The exact mechanism is introduced here and covered more extensively elsewhere in this document.

Setting a property is likely the most commonly encountered trigger. Setting Employee.FirstName, for example, could trigger evaluation of a Verifier that checked if the FirstName string value is present and not longer than thirty characters. The Verifier that checks the FirstName string length can be evaluated independently of any trigger. It could be evaluated during validation of an Employee instance76. But we often want to verify the value the moment the user enters the text. Accordingly, the developer attaches a trigger to that Verifier a trigger bound to the Employee.FirstName property.

TriggerItem
DevForce represents the triggering Employee.FirstName property as a TriggerItem. A TriggerItem is little more than a .NET Type and the name of some member on that type. If a TriggerItem represents a property, the member name is the property name.

TriggerLink
It will not always be enough just to know the TriggerItem. We may have to find our way back from the trigger to the object being verified. This is easy when the TriggerItem refers to a property of the object being verified. If the trigger is Employee.FirstName and the Verifier targets the Employee object, it is obvious that the way back from the property to Employee involves no effort at all: the triggering object and the verified object are the same.

76

Evaluation in this situation is called Instance Verification.

324 | P a g e

IdeaBlade DevForce

Validation Through Verification

On the other hand, we may want to evaluate the verifier when a value changes on some different object. For example, we may want to verify that an Orders total price is still valid if the price of any of its OrderDetail items goes up. The OrderDetail is not the same object as the Order we need to verify. The TriggerLink provides the path from the OrderDetail whose price changed to its parent Order which must be verified. The TriggerLink holds both the end point (the TriggerItem for OrderDetail.Price) and the method to navigate from the trigger object (OrderDetail) to the object to verify (Order). This method is called the TriggerTargetNavigator.
Well cover all of this in greater depth later; for now we look at the classes and other types involved in triggering execution of a Verifier. Type
TriggerContext

Description An object passed to a Verifier when it is triggered by a TriggerItem. The object provides the Verifier with information about what triggered it. A TriggerItem identifies something like an event. A A TriggerItem is defined by the Type of the triggering object and the name of a member on that type that does the triggering.

TriggerItem

TriggerLink

A TriggerLink specifies both the TriggerItem and a path back to the verified object. The path is implemented by a TriggerTargetNavigator method. That method is null when the triggering object and the object to be verified are the same as they are when we trigger a verifier for Employee.FirstName when the user sets that property. The navigator could be a.NET property path (e.g. a PropertyDescriptor). It could also be a custom method capable of bridging the two object types; see TriggerTargetNavigator.

TriggerTargetNavigator TriggerTiming

for navigating from a TriggerItem to the object being verified. See TriggerLink.
Delegate

An enumeration available within the TriggerContext. It indicates when a verifier was triggered. There are two choices: BeforeSet and AfterSet. Properties are the most common triggers so a TriggerTiming typically indicates whether the verifier was evaluated before or after the property was set.

325 | P a g e

IdeaBlade DevForce

Validation Through Verification

VerifierEngine
Verifiers do not execute themselves77. They are executed by a VerifierEngine instance. Each engine maintains a list of Verifier instances and evaluates them at the appropriate times based on a variety of factors that include (but are not limited to) properties of the verifiers themselves.

77

You should not call the Verifier.Verify method directly even though it is public. That method performs some but not all ! of the validation work and will throw an exception if called outside a VerifierEngine.

326 | P a g e

IdeaBlade DevForce

Validation Through Verification

The details of the engine are covered elsewhere in this chapter. Here are the types most relevant to understanding it. Type
VerifierCollection

Description The engine maintains a collection of Verifier instances, accessible via one of the GetVerifier method overloads. The collection implements most of the features of List<Verifier> and, importantly, many Find overloads to facilitate extraction of Verifier subset collections. The VerifierEngine executes a Verifier in a particular context. The engine creates an instance of a VerifierContext before every validation run (a batch) and makes it available to the verifiers in that run. Each verifier can both see and modify the context. The developer can activate a VerifierBatchInterceptor delegate method that can see and modify the context. The context includes a great deal of useful information including a reference to the engine itself, the BatchId of the engines current validation run, the VerifierResultCollection of results accumulated so far, the currently executing Verifier, and a CustomContext object supplied by the developer.

VerifierContext

VerifierEngineCreatedEventArgs

provided to a VerifierEngine.VerifierEngineCreated event handler. The VerifierEngine class raises this static event after creating a new VerifierEngine instance. The developer can attach a handler to consistently configure every new instance.
EventArgs

VerifierEngine. PropertyNameTranslator

Delegate method that takes a type and a string (presumed to be the property name) and returns the string that will be injected into the message produced by the verifier. A verifier description or message should appear in the users preferred language. The message templates can be localized but they often have a placeholder for the property name. The {0} in the message {0} is required will be filled by a property name at runtime. This name should be localized as well. The VerifierEngine.PropertyNameToDisplayNameTranslator property takes such a delegate. Delegate method called by the VerifierEngine after every verifier evaluation in a batch and once more at the end of the batch. A resource file of predefined error message templates. The exception thrown when the Verifier itself fails to execute properly within the engine, i.e. when the Verifier throws an exception; that exception is included in the VerifierException.InnerException. Not to be confused with the VerifierResultException. A flag enumeration (Disabled, Instance, OnAfterSetTriggers, OnBeforeSetTriggers) that describes the situations in which a Verifier can run. The VerifierEngine, while executing in one of these situations, runs the verifiers that have a matching ExecutionModes flag.

VerifierEngine. VerifierBatchInterceptor VerifiersErrorsResource VerifierException

VerifierExecutionModes

327 | P a g e

IdeaBlade DevForce

Validation Through Verification

Type
VerifierOnErrorMode

Description The developer can set the Verifier.OnErrorMode to a value from this enumeration (Stop, Continue). The value tells the VerifierEngine whether it should continue (the default) or stop verifying if this verifier reports that its validation failed. The result of executing a Verifier. It contains detailed information about the outcome and the context of the verifiers execution The VerifierEngine accumulates a collection of VerifierResult instances which it returns as a VerifierResultCollection from its Execute method. The collection implements most of the features of Collection<VerifierResult> and, importantly, many Find overloads to facilitate extraction of VerifierResult subset collections. Args of the VerifierEngine.VerifiersChanged event, raised when a verifier is added to or removed from the engine or a trigger is added to or removed from a verifier already held by the engine. Enumeration of the types of changes reported in
VerifiersChangedEventArgs

VerifierProviderAttribute VerifierResult VerifierResultCollection

VerifiersChangedEventArgs

VerifiersChangedType

PropertyValueVerifiers
The class diagram for Verifier and its derived classes as of this writing looks like this:

328 | P a g e

IdeaBlade DevForce

Validation Through Verification

Most of the verifier classes are PropertyValueVerifiers. A PropertyValueVerifier tests a property value. Technically, it is a Verifier attached to single TriggerItem which is a property on the object being validated. The value to test may be the proposed property value (prior to the property set) or the current value (after the property was set). Many application validations are property validations and most of these resolve into some variation of just a few kinds of verifier: required, range or length, and membership in a list. Attribute Classes Verifiers can be prescribed programmatically and added to the VerifierEngine at runtime. It is sometimes convenient to prescribe them programmatically by adorning properties with attributes. The DevForce includes a number of PropertyValueVerifierAttribute classes to facilitate this approach. The DevForce Verification library covers many of these verifiers and attributes; of course you can easily extend them or write your own. Null property values We have to check for null before we can test a property value. In many cases, null values are not permitted. Rather than oblige the developer to specify both a RequiredValueVerifier and the verifier of interest, all PropertyValueVerifiers include an IsRequired parameter; the base, abstract PropertyValueVerifier evaluates IsRequired before handing the value on to the derived verifier classes. The outcome of the test is often arbitrary in the face of a null value; is a null BirthDate before or after the minimum date in a range check? You should be sure you know how the verifier handles nulls.
The following table highlights significant members that are specific to the PropertyValueVerifier class. Type
DisplayName

Description The displayable name of this verifier; this is typically the display name for the property it verifies. See also the Verifier.GetDisplayName method. Returns the value of this property as it currently is in the object being verified. This value could be compared to the proposed value if the verifier is executing in a BeforeSet context. Returns true if a property value is required (if it cannot be null). The .NET PropertyDescriptor for the property it verifies. Returns the strongly type PropertyVerifierArgs that configure this verifier.

GetPropertyValue

IsRequired PropertyDescriptor TypeVerifierArgs

329 | P a g e

IdeaBlade DevForce

Validation Through Verification

The following are types closely related to this class and its derived classes. Type
DelegatePropertyValueVerifier (Of T)

Description The foundation of a custom property verifier. The developer implements the validation test inside a ValueVerifierCondition delegate of his own devising. It behaves otherwise like any of the predefined property verifiers. A Regex expression for use with the RegexVerifier. You can use one of the pre-named static patterns or create your own named Regex pattern. Verifiers that apply to a single property of an object. The DevForce predefined PropertyValueVerifiers, as of this writing, are:
DateTimeRangeVerifier DecimalRangeVerifier DelegatePropertyValueVerifier(Of T) DoubleRangeVerifier Int32RangeVerifier Int64RangeVerifier ListVerifier RangeVerifier RegexVerifier RequiredValueVerifier StringLengthVerifier

NamedRegexPattern

PropertyValueVerifier

PropertyValueVerifierAttribute

Each of the PropertyValueVerifiers can be specified declaratively by decorating a property with the corresponding PropertyValueVerifierAttribute.
DateTimeRangeVerifierAttribute DecimalRangeVerifierAttribute DelegatePropertyValueVerifierAttribute DoubleRangeVerifierAttribute Int32RangeVerifierAttribute Int64RangeVerifierAttribute RangeVerifierAttribute RegexVerifierAttribute RequiredValueVerifierAttribute StringLengthVerifierAttribute

RangeVerifier(Of T)

A generic range Verifier where T is the type of value tested (not the type of the verified object). A range verifier accepts arguments specifying minimum and maximum (either optional) and whether the range includes or excludes either end point.
Delegate that determines if a value passes its verifier VerifierResult. T is the type of the verified object.

ValueVerifierCondition(Of T)

test. It returns a

The developer can supply such a delegate as an argument to the constructor of a DelegatePropertyValueVerifier.

330 | P a g e

IdeaBlade DevForce

Validation Through Verification

Adding Attributed Verifiers to Generated Properties Previously you saw an example of the code generated by the Object Mapper for a string-valued property, FirstName. The property definition was decorated with a StringLengthVerifier :

C#

[StringLengthVerifier(MaxValue=30, IsRequired=true)] public String FirstName { ... } <StringLengthVerifier(MaxValue:=30, IsRequired:=True)> _ Public ReadOnly Property FirstName() As String ... End Property

VB

To add an attributed verifier to a custom property defined in your developer partial class, you would simply add the appropriate attribute such as the StringLengthVerifier attribute shown above to the property definition. Clearly you cant do the same for properties defined in the designer code file generated by the DevForce Object Mapper. That file, and the code in it, belongs to the Object Mapper, which reserves the write to overwrite it whenever ordered to do so. Nevertheless, you can still apply your own attributed verifiers to generated properties. You do this by means of a buddy class that partners with your business class and contributes additional metadata to it. In the example below, weve added such a buddy class to the developer partial class file for the Customer type, Customer.cs. In the buddy class, weve decorated the static property CompanyName with the StringLengthVerifier, assigning our own MaxValue, which is more restrictive than the one generated for CompanyName in the designer code file.

331 | P a g e

IdeaBlade DevForce

Validation Through Verification

C#

...

using IbVal = IdeaBlade.Validation; using DataAnnot = System.ComponentModel.DataAnnotations;


namespace DomainModel {

[DataAnnot.MetadataType(typeof(CustomerMetadata))]
public partial class Customer : IdeaBlade.EntityModel.Entity { ... } /// <summary> /// The buddy class for Customer /// </summary> public class CustomerMetadata { /// <summary> /// Override CompanyName to make it required. /// </summary>

[IbVal.StringLengthVerifier(MaxValue = 10, IsRequired = true)]


public static string CompanyName; } }

VB

... Imports IbVal = IdeaBlade.Validation Imports DataAnnot = System.ComponentModel.DataAnnotations Namespace DomainModel

<DataAnnot.MetadataType(GetType(CustomerMetadata))> _
Partial Public Class Customer Inherits IdeaBlade.EntityModel.Entity ... End Class ''' <summary> ''' The buddy class for Customer ''' </summary> Public Class CustomerMetadata ''' <summary> ''' Override CompanyName to make it required. ''' </summary>

<IbVal.StringLengthVerifier(MaxValue := 10, IsRequired := True)> _


Public Shared CompanyName As String End Class End Namespace

Important!! Note, in order that the Verification engine should be aware of the Customer types buddy class, that we have decorated the Customer class with the MetadataType attribute from the System.ComponentModel.DataAnnotations namespace. Dont forget to do that: otherwise the verifiers defined in your buddy class will not be enforced!

Verification Deep Dive


Now that we have toured the Verification types we are ready to look more closely at the major types.

332 | P a g e

IdeaBlade DevForce

Validation Through Verification

Verifiers
We use an instance of the DevForce abstract Verifier class to implement a validation rule.
Verifier = Validation Rule

A verifiers primary task is to render judgment on the validity of an object. It isnt suppose to change the object, just evaluate it and pronounce the object valid or invalid. Thats a big job too big for any one verifier instance78. So we create lots of verifiers each of which limits itself to evaluating one aspect of an object such as the string length of a single property. Each verifier produces a VerifierResult object which, at its most basic, (a) indicates success (Ok) or failure (Error) and (b) provides a message (the VerifierResult.Description) for display to a user. An object is valid if the accumulated results of individual verifiers are all ok. The DevForce Verification library contains several predefined Verifier subclasses79 as well as several higher level abstract classes that allow developers to construct their own verifiers. Verifiers dont execute on their own. They have to be evaluated by a VerifierEngine which means we have to tell the engine about them by registering configured instances of some verifier class with the engine. While we can register verifier instances programmatically, it is often more convenient to let the VerifierEngine discover them a process well get to when we consider the engine in detail. For now well talk about registration as if we always took an active hand in it. Each verifier has an ApplicableType which is the type of object that the verifier can verify. Verifiers with an ApplicableType of a .NET base type are presumed to be applicable to all subclasses of that base type. The VerifierEngine ensures that verifiers registered for a base class are propagated automatically the verifier collection of all derived types. Imagine that you had an abstract class called Produce and a bunch of subclasses Carrot, Apple, Potato. When you attach a verifier to Produce.Name, that same verifier applies to Carrot.Name, 80 Apple.Name, and Potato.Name .

Verifier Execution
A verifier cannot be executed until it has been added to a VerifierEngine. An individual verifier instance can be attached to only one VerifierEngine at a time.

78 79 80

While it is possible to write a single super verifier that does it all, it would be unwise to do so. See the class diagram above. It is the same verifier even if Potato.Name overrides Produce.Name. The developer can remove or replace the propagated verifier for Potato.Name by manipulating the Potato verifiers after they have been built. Carrot.Name and Apple.Name will be unaffected.

333 | P a g e

IdeaBlade DevForce

Validation Through Verification

Verifiers are executed in the order that they were added to the VerifierEngine. It is possible to modify the order by setting the SortValue property on each verifier.
A VerifierEngine runs in one of three Execution Modes at a given time. How we call it determines the mode. Instance Verification BeforeSet Trigger Verification AfterSet Trigger Verification

We cover these modes in detail in the Invoking Verification section. The point to note here is that the engine will only evaluate the verifiers that are configured to run in a compatible execution mode. Thus, if the engine is running in BeforeSet Trigger mode and the verifiers ExecutionModes = InstanceAndOnAfterSetTriggers, the verifier will not be evaluated; it will be evaluated when the engine runs in either instance or AfterSet trigger mode. When a VerifierEngine evaluates a verifier it calls two verifier methods: IsApplicable and Verify.
C#
public virtual VerifierApplicability IsApplicable( Object pItemToVerify, TriggerContext pTriggerContext, VerifierContext pVerifierContext); public abstract VerifierResult Verify( Object pItemToVerify, TriggerContext pTriggerContext, VerifierContext pVerifierContext);

Visual Basic
Public Overridable Function IsApplicable( _ ByVal pItemToVerify As Object, _ ByVal pTriggerContext As TriggerContext, _ ByVal pVerifierContext As VerifierContext) _ As VerifierApplicability Public MustOverride Function Verify(ByVal pItemToVerify As Object, _ ByVal pTriggerContext As TriggerContext, _ ByVal pVerifierContext As VerifierContext) _ As VerifierResult

334 | P a g e

IdeaBlade DevForce

Validation Through Verification

Observe that engine calls both methods with the same inputs Parameter
pItemToVerify pTriggerContext

Description The object instance to verify. Its type will be the same as or a descendent of the ApplicableType of the verifier. A TriggerContext object that describes how the verifier was triggered a topic covered elsewhere in this chapter. Note that this value is null (Nothing in VB) when the verifier was not triggered (i.e., during instance verification).

pVerifierContext

IsApplicable
The execution cost of some verifiers may be high. We dont want to pay that cost if the verifier does not apply in the present circumstances. The developer can specify an IsApplicable method to short-circuit unnecessary verifier evaluation. For example, most validations are irrelevant if the object is marked for delete. We might test for that in our IsApplicable method. The VerifierEngine calls the verifiers IsApplicable method first. The IsApplicable method returns a VerifierApplicability object with a VerifierApplicabilityCode. If the code is Yes the engine continues evaluating the verifier. If the code is anything else, the engine stops evaluating, prepares a VerifierResult for this verifier, and moves on to the next verifier. The VerifierResultCode of the prepared VerifierResult will be an ok code (VerifierResultCode.OkNotApplicable) if the VerifierApplicabilityCode is No. It will be an error code (VerifierResultCode.ErrorInsufficientData) if the VerifierApplicabilityCode is InsufficientData. An applicability test is rarely needed. Accordingly, the base IsApplicable implementation in the abstract Verifier class simply returns VerifierApplicability.Yes.

VerifierContext
The VerifierEngine provides both the IsApplicable and the Verify methods with a VerifierContext defined as follows: This context gives each Verifier information about its calling and executing environment including the engines progress during this particular execution. The context values change over the course of the verification. The VerifierEngine will change them. Each Verifier can change them too. Initializing the VerifierContext The initial VerifierContext is either a context created by the engine or a context provided by the code that puts the engine to work.

335 | P a g e

IdeaBlade DevForce

Validation Through Verification

Such code calls one of the engines Execute methods. There are a number of signatures, as well see later in this chapter, and many of them take a VerifierContext. If the caller provides the context, it will have instantiated the context with this constructor: C#
VerifierContext(VerifierOnErrorMode pOnErrorMode, Object pCustomContext)

Visual Basic
New (ByVal pOnErrorMode As VerifierOnErrorMode, ByVal pCustomContext As Object)

The VerifierOnErrorMode is an enumeration with two values - Stop and Continue meaning Stop verifying if you encounter an error and keep verifying until there are no more verifiers to evaluate81. The CustomContext can be any kind of object. It is a mechanism to enable the calling code to communicate situational information to the verifiers that know how to interpret that information. If the caller does not provide a VerifierContext, the VerifierEngine constructs one from its own resources: the VerifierEngine.DefaultOnErrorMode and the VerifierEngine.DefaultCustomContext. The application could set these defaults when it creates the engine instance; it can revise them at will. Features of the VerifierContext
Here is the interface of the VerifierContext C#
public class VerifierContext { public Int64 BatchId { get; } public VerifierOnErrorMode OnErrorMode { get; set; } public Object CustomContext { get; set; } public VerifierResultCollection VerifierResults { get; } public object BatchContext { get; set; } public bool EndOfBatch { get; } public Verifier Verifier{ get; } public VerifierEngine VerifierEngine { get; } }

Visual Basic
Public Class VerifierContext Public ReadOnly Property BatchId() As Int64 public Property OnErrorMode() As VerifierOnErrorMode public Property CustomContext() As Object public ReadOnly Property VerifierResults() As VerifierResultCollection public Property BatchContext() As Object public ReadOnly Property EndOfBatch As Boolean public ReadOnly Property Verifier As Verifier public ReadOnly Property VerifierEngine As VerifierEngine

81

An individual verifier can terminate the batch even if the OnErrorMode is Continue.

336 | P a g e

IdeaBlade DevForce

Validation Through Verification

End Class

Lets walk through them quickly. Calling the engines Execute method initiates a new verification batch that lasts for the duration of the methods execution. The engine assigns the batch a unique BatchId. As stated earlier, OnErrorMode returns an enumeration with two values - Stop and Continue meaning Stop verifying if you encounter an error and keep verifying until there are no more verifiers to evaluate. A verifier can change this value at any time. Weve already met the CustomContext containing an arbitrary object defined by the developer and made available either when the engine was called or through its DefaultCustomContext property. Observer that the object can be reset at any time during the batch. The VerifierEngine adds each verifiers VerifierResult to the VerifierResultCollection in the context. Verifiers can see prior results and take action accordingly82. The BatchContext is a means of accumulating and communicating execution state within the batch. It starts null (Nothing in VB). Any verifier can change it, perhaps depositing useful information for downstream verifiers. The EndOfBatch starts false. The VerifierEngine will set it to true after it evaluates the last verifier in the batch. This flag is intended for use by a VerifierEngine.BatchInterceptor, a delegate method called by the engine after it evaluates each verifier and once more at the end of the batch when it sets this EndOfBatch flag to true. The interceptor could perform batch cleanup when it sees the flag set true. The VerifierEngine records the most recently evaluated verifier in the contexts Verifier property. This property is aimed at the VerifierEngine.BatchInterceptor which may need to take some action after the engine evaluates a particular verifier. The VerifierEngine also registers itself in the contexts VerifierEngine property. Verifiers dont need this they know to which engine they belong. The VerifierEngine.BatchInterceptor does not know what engine is running; it can find out by looking at this context property.

Custom Verifiers
The Verification library comes with many predefined verifiers that cover the majority of cases. Of course you have to be able to create your own and you can do so easily. Keep reading and you will see examples. You can find these same examples, in context, in the Learning Unit on Verification that ships with DevForce.

82

They can even manipulate the VerifierResultCollection itself; one hopes they are prudent in doing so.

337 | P a g e

IdeaBlade DevForce

Validation Through Verification

Verifier Result
We expect a verifier to render a binary decision most of the time. Its usually a pass / fail test. Accordingly, every verifier returns a VerifierResult with an IsOk property. Either it is or it isnt.
More nuanced information is also available but there is always a firm yes or no.

If the validation failed we probably want to display a message to the user83 explaining how it failed. The VerifierResult.Description contains the message prepared by the Verifier a message that may have been translated into the local language and culture. The VerifierResult.Description comes from the Verifier.Description by default. The phrase First Name cannot exceed 30 characters serves well both as the description of the Verifier and the message to the user when the entered text exceeds 30 characters.

Customizing the Description


When this is not satisfactory, the developer can customize the message.

Sub-class the Verifier and override the Description property


In this example in which the author wants to drive home the point about keeping the birth date reasonable. The DateTimeRangeVerifier would be fine if not for the message. So the author fills out the DateTimeRangeVerifier and then overrides the Description property. C#
/// <summary>Default Ctor,</summary> /// <remarks> /// BirthDate is not required, /// must be on or after global min date (<see cref="M:MinBirthDate"/>), /// and before today. /// </remarks> public BirthDateRangeVerifier() : base(typeof(Employee), // Type of the object being verified Employee.BirthDateEntityProperty.Name, // Property trigger false,// Non-null value is not required MinBirthDate, true, // starting min date (inclusive) DateTime.Today, false) { } // ending max date (exclusive) public override string Description { // ToDo: Localize get { return "Must be born after " + MinBirthDate.Year.ToString() + "; No time travellers allowed!"; } } }

VB

83

Or perhaps to a log file if we are validating outside of a user interface.

338 | P a g e

IdeaBlade DevForce

Validation Through Verification

Create a Delegate Verifier


One of the easiest ways to create a new verifier is to create an instance of one of the delegate verifiers as we showed above. The description is one of the parameters in their constructors.
C#
public DelegateVerifier(String pDescription, VerifierCondition<T> pVerifierCondition) public DelegatePropertyValueVerifier(String pDescription, String pPropertyName, bool pIsRequired, ValueVerifierCondition<T> pVerifierCondition)

Visual Basic
' DelegateVerifier Constructor Public Sub New (ByVal pDescription As String, _ ByVal pVerifierCondition As VerifierCondition(Of T)) ' DelegatePropertyValueVerifier Constructor Public Sub New (ByVal pDescription As String, _ ByVal pPropertyName As String, _ ByVal pIsRequired As Boolean, ByVal pVerifierCondition _ As ValueVerifierCondition(Of T))

Sub-class the Verifier and override the Verify() method


The Verifier.Verify method returns the VerifierResult picked up by the VerifierEngine. This gives you complete control over the VerifierResult.Description which you can construct dynamically.
This is Verifys signature84. C#
public abstract VerifierResult Verify(Object pItemToVerify, TriggerContext pTriggerContext, VerifierContext pVerifierContext);

Visual Basic
Public MustOverride Function Verify(ByVal pItemToVerify As Object,_ ByVal pTriggerContext As TriggerContext, _ ByVal pVerifierContext As VerifierContext) As VerifierResult

Localization and Internationalization


There are two mechanisms for localizing the messages reported through the VerifierResult
1. 2. Use resource files for the message templates Translate the property name that is injected into the template.

For example, the basic message template, ,0- is required, is ready to use as PropertyRequired message. At runtime we plug First name or Last name or whatever into the slot reserved by ,0-.

84

Although the method is public and it would seem that you can instantiate all of its parameters, you cannot call it yourself; you will get an exception if you try. This is deliberate; DevForce can ensure proper verifier execution only within a VerifierEngine.

339 | P a g e

IdeaBlade DevForce

Validation Through Verification

If the application will be used by non-English speakers, well want to translate the template and well want to translate the property names.

Message Templates
DevForce ships with standard error and warning message templates. The developer can replace them with a completely custom version. The developer creates the .NET resource files for each language85. The only requirement is that at least the default file has an entry for all of the DevForce template keys.
A copy of the DevForce verification resource file is available from IdeaBlade as a starting point for customization.

Visual Studio generates a strongly-typed ResourceManager class to support these custom files. The developer sets the ErrorsResourceManager property of each new VerifierEngine to this ResourceManager as shown.
C#
VerifierEngine engine = new VerifierEngine(); engine.ErrorsResourceManager = myResourceManager;

Visual Basic
Dim engine As VerifierEngine = New VerifierEngine() engine.ErrorsResourceManager = myResourceManager

Property Names
Most common verifiers apply to a single property and inherit from the PropertyValueVerifier. Their message templates have a slot for the property name and the verifier knows how to fill that slot with the property name after it has been translated. The key to the process is the PropertyNameTranslator. The VerifierEngine has a PropertyNameToDisplayNameTranslator property that takes a PropertyNameTranslator delegate defined as follows
C#
public delegate String PropertyNameTranslator( Type pType, String pPropertyName);

Visual Basic
Public Delegate Function PropertyNameTranslator( _ ByVal pType As Type, ByVal pPropertyName As String) As String

The expected implementation takes a type-and-string (e.g. Employee and FirstName) and turns it into a translated string.
85

The .NET practices for localization are beyond the scope of this document.

340 | P a g e

IdeaBlade DevForce

Validation Through Verification

Note that type-and-string also defines a TriggerItem. As with TriggerItem, the string is typically the name of a member of the target type but it doesnt have to be.

With that background we are ready to proceed.


All predefined PropertyValueVerifier subclasses within the Verification library observe the following protocol when preparing a property name for insertion into the template: If the engine has a PropertyNameToDisplayNameTranslator, that method is used to translate the property name. If there is no translator, the verifier tries the value of PropertyValueVerifier.DisplayName. If DisplayName is null, the verifier looks for a .NET DescriptionAttribute adorning the object property. It there is no such attribute, the verifier uses the property name.

This same protocol can be used within a custom verifier, even one multiple slots for multiple property names and values. The translator is not limited to translating property names.

Triggers
Evaluation of a Verifier may be triggered by one or more events.
Events is in quotes because the mechanism, while it feels like an event, does not use the .NET event. The exact mechanism is introduced here and covered more extensively elsewhere in this document.

Setting a property is the most commonly encountered trigger. Setting Employee.FirstName, for example, could trigger evaluation of a Verifier that checked if the FirstName string value is present and not longer than thirty characters. The Verifier that checks the FirstName string length can be evaluated independently of any trigger. It could be evaluated during validation of an Employee instance86. But it is often a kindness to the user if we validate the first name text at the moment she enters it rather than wait for the entire Employee object to be evaluated. Accordingly, the developer attaches a trigger to that Verifier a trigger bound to the Employee.FirstName property. Property validation of this kind - a property Verifier with an attached property trigger - is extremely popular. It is so popular that DevForce provides the PropertyValueVerifier87 and a host of derived verifiers to make it easy to specify property validation.
One approach is to adorn a property with one of the attribute-based versions of the PropertyValueVerifier as we do for the FirstName property in the following example. C#
/// <summary>Gets or sets the FirstName.</summary> [StringLengthVerifier(MaxValue=30, IsRequired=true)]

86 87

Evaluation in this situation is called Instance Verification. The PropertyValueVerifier and its kin are covered below.

341 | P a g e

IdeaBlade DevForce

Validation Through Verification

public virtual System.String FirstName { //

Visual Basic
''' <summary>Gets or sets the FirstName.</summary> <StringLengthVerifier(MaxValue:=30, IsRequired:=True)> _ Public Overridable ReadOnly Property FirstName() As System.String Get '

A VerifierEngine discovers the attribute and the FirstName property it adorns and then adds a StringLengthVerifier, triggered by the FirstName property, to its list of verifiers. Something similar happens when we add the Verifier programmatically to a list of verifiers that we later add to a VerifierEngine.
C#
// Add FirstName StringLengthVerifier to a list of verifiers. verifiers.Add(new StringLengthVerifier( typeof(Employee),"FirstName", true, 1, 30));

Visual Basic
' Add FirstName StringLengthVerifier to a list of verifiers. verifiers.Add(New StringLengthVerifier( _ GetType(Employee), "FirstName", True, 1, 30))

Behind the scenes, DevForce constructs a Verifier that can validate the Employee.FirstName property and arranges for that Verifier to be evaluated when someone tries to set the Employee.FirstName property88. That arrangement is the trigger.

Adding Triggers Explicitly


The predefined PropertyValueVerifiers and their corresponding attribute versions each add a property trigger to a property verifier implicitly (which is to say, automatically). When you create your own verifiers, you may want to add one or more triggers yourself. These you must add explicitly.
It is easy to do with the EntityPropertyDescriptors generated by the Object Mapper89 as we see in this example:
hireDateVerifier.AddTrigger( EntityPropertyDescriptors.Employee.HireDate )

You can add them by string name too.


hireDateVerifier.AddTrigger(HireDate)

This isnt type safe and it assumes that the trigger property is a property of the object to be verified as is usually the case. You can specify the trigger type if you want to do so90.
88 89

It will also be evaluated when the program validates the Employee object (that is, during Instance Verification). You can extend them to include your custom properties.

342 | P a g e

IdeaBlade DevForce

Validation Through Verification

C#
hireDateVerifier.AddTrigger(typeof(Employee), HireDate);

Visual Basic
hireDateVerifier.AddTrigger(GetType(Employee), HireDate)

You may go far with just this much understanding of triggers. On the other hand, you may find you need to dig deeper and then youll want to know about TriggerItem and TriggerLink.

TriggerItem
The TriggerItem represents the triggering Employee.FirstName property.
The Employee.FirstName property serves two roles in our example. It is both the value that is validated by the verifier and it is the thing that can trigger the verifier. We have to distinguish between the two. At the moment, we are interested in the property only in its second role in its capacity as a trigger. Imagine that the verifier didnt look at the first name. Imagine that it performed some other Employee validation such as checking to see if the person is old enough to be an Employee. We could still trigger this verifier every time the user touched the FirstName property. The FirstName property serves in the second role, as trigger, even though it plays no role at all in the validation.

A TriggerItem is little more than a .NET Type and a string called the MemberName. The string is almost always the name of some member on that type. If TriggerItem represents a property, the MemberName is the property name.
While most TriggerItems are properties, it should be clear that we can represent almost any member of a Type as a TriggerItem. We could trigger evaluation of a Verifier with a method as easily as a property. In fact, the MemberName could be an arbitrary string that is not an actual member of the type.

TriggerContext
In the course of evaluating a Verifier, the VerifierEngine calls methods on that verifier. Remember, the VerifierEngine calls these methods. You do not. These methods include: C#
public VerifierApplicability IsApplicable( Object pItemToVerify, TriggerContext pTriggerContext, VerifierContext pVerifierContext); public VerifierResult Verify( Object pItemToVerify, TriggerContext pTriggerContext,

90

Or if you have to do so for reasons that will become clear below.

343 | P a g e

IdeaBlade DevForce

Validation Through Verification

VerifierContext pVerifierContext);

Visual Basic
Public Function Verify( _ ByVal pItemToVerify As Object, _ ByVal pTriggerContext As TriggerContext, _ ByVal pVerifierContext As VerifierContext) As VerifierResult Public Function IsApplicable( _ ByVal pItemToVerify As Object, _ ByVal pTriggerContext As TriggerContext, _ ByVal pVerifierContext As VerifierContext) As VerifierApplicability

Notice that the second parameter is a TriggerContext. The TriggerContext provides the verifier with vital information about how the verifier was triggered.
The engine does not have to be triggered to evaluate the verifier. It could evaluate an entire instance without prompting by a trigger91. The TriggerContext is null in this situation a fact the verifier may use to establish that it was not triggered. The following table highlights the key elements of a TriggerContext: Class Member
ProposedValue

Description Typically a value entered by the user. The value is not yet committed; if the trigger is a property, the property has not yet been set to this value. The ProposedValue is meaningful only when the triggers Timing is BeforeSet. By convention, the verifier evaluates the proposed value. If the value is invalid (per the verifier), the triggering property should discard the proposed value and leave the current property value intact. The Entity.BeforeSetValue observes this convention.

Timing

One of the TriggerTiming enumerations (BeforeSet, AfterSet) that indicate whether the validation occurs before the triggering property performs its task (BeforeSet) or after it has already performed its task (AfterSet). The ProposedValue is meaningful only when the Timing is BeforeSet. The TriggerItem that inspired the VerificationEngine to evaluate the Verifier. The object that pulled the trigger. The Employee instance is the TriggerItemInstance in an Employee.FirstName trigger.

TriggerItem TriggerItemInstance

TriggerLink
We have neglected the TriggerLink to this point, conveniently confining our attention to the TriggerItem.

91

This is called instance verification.

344 | P a g e

IdeaBlade DevForce

Validation Through Verification

As it happens, the TriggerItem alone is insufficient if we are to support a robust validation system. The TriggerItem tells us what kind of object triggered a Verifier. Now we have to find a way back from the object trigger to the object being verified. This is easy when the TriggerItem refers to a property of the object being verified. If the trigger is Employee.FirstName and the Verifier targets the Employee object, it is obvious that the way back from the property to Employee involves no effort at all: the triggering object and the verified object are one and the same. We wouldnt bother with such minutia unless we had grander plans and we do. We would like to trigger evaluation of a Verifier when something happens much farther away. Lets change our example from Employee to Order. Suppose the user increases the quantity of an item on Order, a change that typically increases the total price of the order. Imagine that there is a verifier on the Order that constrains the total allowed amount of any order to a maximum amount, an amount calculated per a rule that factors the role of the user entering the data and the Customers credit limit. This verifier sits on the Order class. We could wait until we validated the entire order before evaluating this verifier. If the change broke the limit, wed tell the user. But it might be better to tell the user right away. It might be better if the change to the OrderDetail.Quantity property triggered the Order verifier immediately.
OrderDetail.Quantity is not a property of Order. It is one hop away, on the navigation path from OrderDetail to Order. In other words, to make this trigger work, the VerifierEngine must be able to

follow the path from the triggering change in Quantity to OrderDetail and from there to Order. Enter the TriggerLink. The TriggerLink includes both the TriggerItem and a method that can navigate from the triggering object to the object to verify, a method known as the TriggerTargetNavigator. In our order example, the navigator could be the method that implements the nested property path from OrderDetail to Order. Verifiers and TriggerLinks We observed earlier that specifying a StringLengthVerifier for FirstName, simultaneously specifies the Employee.FirstName as its TriggerItem. It turns out that we are actually attaching a TriggerLink to the Verifier; the Employee.FirstName is the TriggerItem contained within that link whose other half is the navigator to Employee. When we use any of the PropertyValueVerifiers, we implicitly create a verifier attached to a TriggerLink that refers to the chosen property as its TriggerItem. The DevForce syntax hides the hook-up to make creating the verifier easy.

345 | P a g e

IdeaBlade DevForce

Validation Through Verification

Easy things should be easy. But hard things should be possible and a full appreciation of what is actually happening can open our eyes to more complex scenarios. Lets take a look at some syntax for adding a TriggerLink to a Verifier explicitly. First, the simple case:
C#
TriggerLink aLink = new TriggerLink( new TriggerItem(typeof(Employee), "FirstName"), // TriggerItem null, false); // Navigation aStringLengthVerifier.AddTrigger(aLink);

Visual Basic
Dim aLink As New TriggerLink( _ New TriggerItem(GetType(Employee), "FirstName"), _ Nothing, False) aStringLengthVerifier.AddTrigger(aLink)

The TriggerItem consists of a Type and a property name, just as we expect. The navigator is null (Nothing in VB) because there is no navigation necessary from the object that triggers the verifier to the object that is verified they are the same object. The third Boolean parameter is false because the link does not return a collection and therefore cannot return multiple targets. The meaning of this mysterious option will become clear shortly.
We would never actually add a simple property trigger this way. There is no reason to specify the TriggerLink or even the triggering objects type. There is no navigator and the type of the trigger is the same as the type of the verifier. Instead we would write, in both C# and VB,
aStringLengthVerifier.AddTrigger("FirstName")

Now look at the second case involving Order and OrderDetail:


C#
TriggerLink aLink = new TriggerLink( new TriggerItem(typeof(OrderDetail), "Quantity"), // TriggerItem "Order", false); // Navigation orderTotalPriceVerifier.AddTrigger(aLink);

Visual Basic
Dim aLink As New TriggerLink( _ New TriggerItem(GetType(OrderDetail), "Quantity"), _ "Order", False) orderTotalPriceVerifier.AddTrigger(aLink)

346 | P a g e

IdeaBlade DevForce

Validation Through Verification

This time we have a navigator. The navigator is indicated by the Order property, a property of OrderDetail that returns the Order instance to verify. Apparently DevForce can convert a nested property path into a TriggerTargetNavigator.

How It Works
Here in schematic form is how Verifiers, TriggerItems, and TriggerLinks come together under the control of a VerifierEngine when the triggering object and the verified object are different. Something in the trigger property implementation tells the VerifierEngine to verify92, supplying it with the means to identify the TriggerItem. The VerifierEngine finds a TriggerLink for that TriggerItem and also the Verifier to which that TriggerLink is attached. The VerifierEngine extracts the TriggerTargetNavigator and calls it, passing the trigger object as a parameter. The trigger object is the OrderDetail in our example. The navigator returns the object to verify (the Order). The VerifierEngine confirms that the object to verify is of the correct type (i.e., it matches the Verifier.ApplicableType). The VerifierEngine executes the Verifier, passing the trigger information (a TriggerContext) as one of the parameters.

Triggering Multiple Verifiers We said that the OrderTotalPrice verifier consults the customers credit limit when determining if the total price of the order is valid. If the user changes the customers credit limit, she could render the order valid or invalid. Not just one order either. She could change the validity of all of the customers outstanding orders.
We might want to draw attention to this by adding a Customer.CreditLimit trigger to the OrderTotalPrice verifier. Heres some syntax: C#
TriggerLink aLink = new TriggerLink( new TriggerItem(typeof(Customer), "CreditLimit"), // TriggerItem "Customer.Orders", // Navigation true); // true = returns multiple targets orderTotalPriceVerifier.AddTrigger(aLink);

Visual Basic
Dim aLink As New TriggerLink( _ New TriggerItem(GetType(Customer), "CreditLimit"), _ "Customer.Orders", _ True) orderTotalPriceVerifier.AddTrigger(aLink)

Note that this time the third argument of the TriggerLink constructor is True. We had to add an additional argument to signal that this TriggerLink could return multiple objects to verify93. The

92 93

Well investigate how to engage the VerifierEngine in just a few moments. If we said False, the link would return a single target object a collection of Order. The OrderTotalPrice verifier applies to a single Order instance, not a collection. There is a type mismatch between the verifier and the (collection) object returned

347 | P a g e

IdeaBlade DevForce

Validation Through Verification

VerifierEngine will execute the OrderTotalPrice verifier for each of the customer orders. If there are

twenty customer orders, there will be twenty VerifierResults.

TriggerLinks and Performance


We typically dont worry about how long it takes to set a property. Now that weve introduced triggers that can provoke a series of verifications, we should pause and reflect. The navigator in this last example invoked the Orders property of a Customer instance. That Customer may have thousands of orders, none of them in the entity cache. Calling Customer.Orders in this situation usually means a trip to the data store. The UI could stall noticeably while DevForce runs out to the server to fetch the orders. The developer must be aware of this possibility if she is going to write fancy triggers like this one. She may want to confine it to entities in the cache or only retrieve the orders that are still open94. The Customer.Orders property cant be changed. Fortunately, there is an alternative.

TriggerTargetNavigator Delegate
In our previous TriggerLink examples we specified the navigator with a nested property path. We could have used a TriggerTargetNavigator delegate, defined as follows.
public delegate Object TriggerTargetNavigator(Object pInstance); Public Delegate Function TriggerTargetNavigator(ByVal pInstance As Object) _ As Object

Its a simple method that takes one object the triggering object and returns another object the object to verify95. Here is the same TriggerLink, rewritten to use a TriggerTargetNavigator delegate method called aCustomerOrdersNavigator. C#
TriggerLink aLink = new TriggerLink( new TriggerItem(typeof(Customer), "CreditLimit"), // TriggerItem aCustomerOrdersNavigator, // Navigation delegate true); // true = returns multiple targets orderTotalPriceVerifier.AddTrigger(aLink);

Visual Basic
Dim aLink As New TriggerLink( _ New TriggerItem(GetType(Customer), "CreditLimit"), _ aCustomerOrdersNavigator, _ True) orderTotalPriceVerifier.AddTrigger(aLink)

from the TriggerLink; the VerifierEngine will raise a VerifierException indicating that the verifiers execution failed.
94 95

There is no point in verifying closed orders. Remember that this object can be a collection of objects. The boolean TriggerLink.ReturnsMultipleTargets property tells the VerifierEngine whether to verify the items in the collection individually (true) or as a single object (false).

348 | P a g e

IdeaBlade DevForce

Validation Through Verification

This method could use DevForce persistence operations to do the navigation but it doesnt have to. It can have any implementation that returns objects that match the verifiers target object type, the value of Verifier.ApplicableType.
Do not use an asynchronous delegate. Validation is not workflow. Validation is an inherently synchronous operation and the VerifierEngine is not thread safe. The navigator must return an object to verify; the application must pause until that object becomes available.

PropertyDescriptor Syntax
We have shown the TriggerItem in its native form as a .NET Type and a member name. The PropertyDescriptor alternative may be easier to enter, easier to read, and is certainly more typesafe because the developer does not have to code the member name as a string.
Heres how to add the simple property trigger in a single statement using the PropertyDescriptor notation in either C# or Visual Basic:
aStringLengthVerifier.AddTrigger(Employee.PathFor(e=>e.FirstName))

Heres how to add the TriggerLink with PropertyDescriptor notation. C#


TriggerItem item = new TriggerItem(typeof(Customer), Customer.CreditLimtEntityProperty.Name); orderTotalPriceVerifier.AddTrigger( new TriggerLink(item, // TriggerItem Customer.PathFor(c=>c.Orders), // Navigation True)); // true = returns multiple targets Dim item As New TriggerItem(typeof(Customer), Customer.CreditLimtEntityProperty.Name) orderTotalPriceVerifier.AddTrigger( new TriggerLink(item, TriggerItem Customer.PathFor(c=>c.Orders), Navigation True)) true = returns multiple targets

VB

Non-Property Triggers
We tend to discuss triggers as if they were always property triggers. They usually are. But they dont have to be. It takes a TriggerItem to trigger verification. The TriggerItem consists of a Type and a String called the MemberName. The MemberName could be any string. Usually it is a property name but it need not be. It could be a method name. It could be a string with no intrinsic meaning at all.

349 | P a g e

IdeaBlade DevForce

Validation Through Verification

The VerifierEngine uses the type-and-string to find verifiers to evaluate. It is as if the engine had a dictionary of TriggerItems, each leading to a TriggerLink and each link leading to a Verifier96. The reality of the MemberName is irrelevant from this perspective. Any block of code can trigger verification. All it has to do is call a VerifierEngine in a trigger-like way as discussed in the section Invoking Verification. The DevForce Object Mapper generates property setter code that calls a VerifierEngine in a triggerlike way. You do the same when you write your own custom settable properties. You could put the same call logic inside a method. For example, you might trigger Order verification inside methods that add or remove OrderDetail items so that you can immediately test the effect of adds and deletes on the total price of an order. TriggerTiming (BeforeSet, AfterSet) is a convention that you should follow but can adapt to your purpose. Your AddOrderDetail method could trigger verification in a BeforeSet manner before adding the new item97. If validation fails, the method could discard the item before it did any harm.

VerifierEngine
The VerifierEngine is the primary entry point for verification services. An application may have any number of VerifierEngines although most will only need one. Each VerifierEngine contains a list of verifiers and a set of methods that allow collections of these verifiers to be evaluated sequentially against an instance of a .NET class.
The verified object could be a DevForce business object but it doesnt have to be. The object can be of any concrete type.

Each verifier produces a VerifierResult. The engine accumulates these results in a VerifierResultCollection as it proceeds and returns the entire collection as its own result.

Adding Verifiers to a VerifierEngine


Verifiers can be added to a VerifierEngine in two ways:

The engine can discover them automatically by inspecting the .NET types for verifier attributes. The developer can add them programmatically.

The application can combine these methods.

96

Actually, a TriggerItem could lead to multiple TriggerLinks and each of those links could be attached to multiple Verifiers. A single TriggerItem can launch an avalanche of verifications. You must supply a ProposedValue. It can be any kind of object such as the item to be added. It could be null.

97

350 | P a g e

IdeaBlade DevForce

Validation Through Verification

We got a taste of verifier discovery in the Getting Started started section. Well cover it in more depth shortly. Programmatic management of an engines verifiers is straightforward via the AddVerifier and RemoveVerifier methods. Verifiers can be added or removed from a VerifierEngine at any time. The engine raises a VerifiersChanged event when verifiers are added or removed. available on the verifier engine and will inform any subscriber of the addition or removal of any verifier98.

Verifier Discovery
The VerifierEngine always discovers verifiers in the types it is asked to verify99. When a VerifierEngine attempts to verify an instance of a type it has not seen before, it probes the type reflectively, looking for verifiers. The probing strategy is as follows.
Start with the most senior base class in the types inheritance chain. Look for instances of the VerifierAttribute100 class on members of that base class. These define the attributed verifiers. Look for a static method decorated with the VerifierProviderAttribute101; Such a method must take a single parameter of type object this is the VerifierProviderContext and it must return an IEnumerable(Of Verifier). The engine calls the VerifierProvider and adds the Verifier instances returned by that method to its list of verifiers for the base type. Find the next class in the types inheritance chain and return to step #2. Stop when have descended to the type that initiated the discovery process.

We have seen the attribute verifiers earlier.


A VerifierProvider might look like this: C#
#region Verification #region GetVerifiers Method /// <summary>Get Verifiers.</summary> /// <param name="pVerifierProviderContext">Context in which these Verifiers are retrieved.</param> /// <returns>The verifiers.</returns> [VerifierProvider] public static IEnumerable<Verifier> GetVerifiers(Object pVerifierProviderContext) {

98 99

The event is also raised when triggers are added or removed from a verifier that has been registered in the engine. Automatic discovery is not always a good thing, and developers can disable an engines automatic discovery. An engine with automatic discovery disabled can still perform discovery when asked to do so. DevForce provides a number of common verifiers in attribute form all of which descend from VerifierAttribute. The developer can add custom VerifierAttribute subclasses just as he can add custom Verifiers. Actually, there can be more than one such method in the class and the VerifierEngine will call each one.

100

101

351 | P a g e

IdeaBlade DevForce

Validation Through Verification

List<Verifier> verifiers = new List<Verifier>(); verifiers.Add(GetHireDateRangeVerifier()); verifiers.Add(new BirthDateRangeVerifier()); verifiers.Add(GetBornBeforeHiredVerifier()); verifiers.Add(GetPhoneNumberVerifier(Employee.HomePhoneEntityProperty)); return verifiers; } #endregion #region Hire Date Verifier /// <summary>Get a GetHireDateRangeVerifier.</summary> /// <remarks> /// Demonstrates building a highly focused verifier /// by encapsulation a standard verifier /// and its configuration. /// </remarks> private static Verifier GetHireDateRangeVerifier() { Verifier v = new DateTimeRangeVerifier( typeof(Employee), // Type of the object being verified Employee.HireDateEntityProperty.Name, // Property trigger false, // Non-null value is not required MinHireDate, true, // starting min date (inclusive) MaxHireDate, false); // ending max date (exclusive) return v; } private static DateTime MinHireDate { get { return new DateTime(1990, 1, 1); } } private static DateTime MaxHireDate { get { return DateTime.Today.AddMonths(1); } } #endregion #region BirthDateRangeVerifier inner class /// <summary>Get the minimum BirthDate allowed.</summary> private static DateTime MinBirthDate { get { return new DateTime(1900, 1, 1); } } /// /// /// /// /// /// /// /// /// /// /// <summary>BirthDate Range Verifier</summary> <remarks> Illustrates changing the error messaging for a particular property. Have to subclass to take control of the messaging. Here the message is statically known so we override <see cref="M:Description"/>; if it were dynamic or if <see cref="T:DateTimeRangeVerifier"/> constructed the message dynamically, we would have overridden <see cref="M:VerifyValue"/> and manipulated the message while creating the <see cref="T:VerifierResult"/>.

352 | P a g e

IdeaBlade DevForce

Validation Through Verification

/// </remarks> private class BirthDateRangeVerifier : DateTimeRangeVerifier { /// <summary>Default Ctor,</summary> /// <remarks> /// BirthDate is not required, /// must be on or after global min date (<see cref="M:MinBirthDate"/>), /// and before today. /// </remarks> public BirthDateRangeVerifier() : base( typeof(Employee), // Type of the object being verified Employee.BirthDateEntityProperty.Name, // Property trigger false,// Non-null value is not required MinBirthDate, true, // starting min date (inclusive) DateTime.Today, false) { } // ending max date (exclusive) public override string Description { // ToDo: Localize get { return "Must be born after " + MinBirthDate.Year.ToString() + "; No time travellers allowed!"; } } } #endregion #region Born Before Hired Verifier /// <summary>Get a BornBeforeHiredVerifier.</summary> /// <remarks> /// Demonstrates comparing two property values /// by creating an instance of a /// <see cref="T:DelegateVerifier{TVerifiedObject}"/>. /// </remarks> private static Verifier GetBornBeforeHiredVerifier() { // ToDo: localize description string description = "Must be born before hired."; DelegateVerifier<Employee> v = new DelegateVerifier<Employee>(description, BornBeforeHiredCondition); v.AddTriggers(Employee.BirthDateEntityProperty.Name, Employee.HireDateEntityProperty.Name); v.ExecutionModes = VerifierExecutionModes.InstanceAndOnAfterSetTriggers; return v; } /// <summary> /// The <see cref="T:VerifierDelegate{TVerifiedObject}"/> /// for the <see cref="M:GetBornBeforeHiredVerifier"/>. /// </summary> private static VerifierResult BornBeforeHiredCondition( Employee pEmp, TriggerContext pTriggerContext, VerifierContext pVerifierContext) { if (pTriggerContext != null && // We are not checking the proposed value because don't expect to call it BeforeSet pTriggerContext.Timing == TriggerTiming.BeforeSet) { throw new VerifierException("BornBeforeHired verifier not implemented for

353 | P a g e

IdeaBlade DevForce

Validation Through Verification

BeforeSet"); } return new VerifierResult(pEmp.BirthDate < pEmp.HireDate); } #endregion #region Phone Number Verifier /// <summary>Get a GetPhoneNumberVerifier.</summary> /// <remarks> /// Encapsulates a standard RegexVerifier, subclassed so the description can be customized. /// </remarks> private static Verifier GetPhoneNumberVerifier(EntityProperty pPhoneEntityProperty) { return new PhoneNumberVerifier( pPhoneEntityProperty.EntityType, // Type of object being verified pPhoneEntityProperty.Name, // Trigger false, // Non-null value is not required NamedRegexPattern.USPhone); // Regex pattern to use } private class PhoneNumberVerifier : RegexVerifier { public PhoneNumberVerifier(Type pApplicableType, string pPropertyName, bool IsRequired, NamedRegexPattern pattern) : base( pApplicableType, pPropertyName, IsRequired, pattern ) { } public override string Description { get { return base.Description + " including area code [e.g., (206)555-1212, 206-555-1212, or 206.555.1212]."; } } } #endregion #endregion

VB

VerifierProviderContext
Observe that a VerifierProvider method has an object parameter called the VerifierProviderContext. This is an arbitrary object, open to the developers imagination. The VerifierEngine will pass it along to each provider.

354 | P a g e

IdeaBlade DevForce

Validation Through Verification

The engine acquires this context object in one of two ways: From the VerifierEngine.DefaultVerifierProviderContext which the developer must have initialized before the engine starts its discovery process. As the second argument to VerifierEngine.DiscoverVerifiers(Type, Object). This is a method that forces verifier discovery for the given type. The VerifierProviderContext object could be anything. It could be a pre-calculated list of verifiers for the type. It could include the VerifierEngine itself so that the VerifierProvider can inspect and manipulate the other verifiers for this type.
The application must set the VerifierEngines DefaultVerifierProviderContext or call its DiscoverVerifiers method early. The engine starts auto discovery as soon as it receives a request to verify an instance of a type. That discovery could fail or populate the engine with the wrong verifiers if the developer doesnt make these calls first.

Recommended Verifier Loading Approach


We recommend that most applications rely on automatic discover to build up a VerifierEngines list of verifiers. It is ok to add or remove verifiers from a VerifierEngine programmatically outside of the class being verified but you should have a good reason for the extra and unexpected complexity. Some business requirements call for configurable validation rules. Verifiers can be represented in metadata, saved to storage, retrieved when the application starts, and plugged in to a VerifierEngine.

Configuring New VerifierEngines Consistently


While most applications will have only one VerifierEngine, there are good use cases for having two or more. Wherever there are multiple engines there arises the need to ensure that they are all configured consistently and appropriately. We dont want a rogue programmer blithely instantiating new engines that lack a DefaultVerifierProviderContext or are missing some other critical setting. The application can attach a handler to the static event, VerifierEngineCreated, on the VerifierEngine class. The event is raised whenever there is a newly created engine. The new engine is passed in the VerifierEngineCreatedEventArgs so that the handler can configure it.

Invoking Verification
Verifiers do not execute themselves nor can they be executed on their own. They must belong to a

(single) VerifierEngine and rely on that engine to make them do their validation work.

355 | P a g e

IdeaBlade DevForce

Validation Through Verification

A VerifierEngine doesnt verify on its own either. Something has to tell it to verify. DevForce shouldnt perform any operation unless it is asked to do so. Verification is a potentially costly operation. Perhaps as important, DevForce would not know what to do when it was done verifying. Only the application developer can know when to verify and what to do with the results. DevForce does provide an easy way to automate trigger verification of the properties of business objects. The developer simply launches the Object Mapper and turns Verification on102. The Object Mapper generates setter code to call the VerifierEngine at the appropriate time.
It is still up to the developer to invoke verification at other key moments in the application such as: Verification of entities just before they are saved. Trigger verification of custom, settable properties of business objects. Verification upon business object fetch or merge. Trigger verification of non-business objects.

Fortunately, there are .NET events for all of the key business object moments and trigger verification of nonbusiness objects looks just like trigger verification of business objects. In every case, the developer calls one of the VerifierEngine.Execute overloads. The public Execute methods available at this time fall into three Execution Modes: Instance Verification BeforeSet Trigger Verification AfterSet Trigger Verification

Well examine each mode in this following segments. Well learn how calling the VerifierEngines Execute method determines whether it will perform instance, BeforeSet, or AfterSet verification. Before we do, it is important to remember that we do not call Verifiers; the VerifierEngine does that. When we tell it to execute in one of the three modes, it will iterate over its internal list of registered verifiers, evaluating each verifier that is enabled for the current mode.
A Verifier will only be evaluated if its Verifier.ExecutionModes matches the current mode!

For example, if a verifiers ExecutionModes = VerifierExecutionModes.Disabled, the verifier wont be evaluated at all, no matter how we call the VerifierEngine. Keep this in mind as you review the scenarios below.

102

We saw how to do this in the Getting Started section.

356 | P a g e

IdeaBlade DevForce

Validation Through Verification

Instance Verification
The following are the VerifierEngine.Execute overloads for instance verification: Execute Overload Instance Verification
1 2 3 Execute(object pInstance) Execute(object pInstance, VerifierContext pVerifierContext) Execute(object pInstance, IEnumerable<Verifier> pVerifiers, VerifierContext pVerifierContext)

Description

Validate an instance within the default VerifierContext Validate an instance within a particular
VerifierContext

Validate an instance with just the given list of 103 Verifiers . Validate within a particular VerifierContext.

The Instance Verification Execute overloads validate an entire instance. The VerifierEngine finds the Verifiers for the instance type keeps only those with the Instance flag set in their Verifier.ExecutionModes sorts them in execution order104 and evaluates them sequentially.

VerifierContext
Every verifier receives a VerifierContext object during its evaluation. The simplest Execute, which accepts only the object to verify, passes along a VerifierContext constructed by the VerifierEngine. The other signatures take a custom VerifierContext argument which the engine modifies before handing to the verifiers. One of the signatures lets you specify which verifiers the VerifierEngine should evaluate. These verifiers must be registered with the VerifierEngine and their Verifier.ApplicableType must match the type of the verified object.

When and Where to Verify an Instance


The business requirements dictate when and where to verify an instance. Many applications provide the ability to validate an entity at any time and then ensure that every entity passes validation before it can be saved. Accordingly, this author recommends:
Prepare business objects for instance verification Inject a BaseEntity in the Entity Data Model Designer Write a VerifyInstance method in that BaseEntity

Verify instances in your handler of the EntityManager.Saving event Make sure you have such a handler on every EntityManager

103 104

All of the verifiers must have been registered with this engine or else the Execute method returns an exception. Verifiers are sorted by Verifier.SortValue ; ties are broken by the order in which they were loaded into the engine (Verifier.InitializationOrder).

357 | P a g e

IdeaBlade DevForce

Validation Through Verification

Iterate through the entities to be saved, calling VerifyInstance on each one Accumulate the VerifierResults from each call Cancel the save if there are any VerifierResults. Report these results to the user.

These are basic techniques taught in the code samples in the DevForce Learning Resources, demonstrated in DevForce movies, and incorporated (albeit in enriched form) in DevForce reference applications.
Here is a simplified example of a VerifyInstance method. C#
/// <summary>Validate object for all instance Verifiers.</summary> protected virtual VerifierResultCollection VerifyInstance() { return this.VerifierEngine.Execute(this); }

Visual Basic
''' <summary>Validate object for all instance Verifiers.</summary> Protected Overridable Function VerifyInstance() As VerifierResultCollection Return Me.VerifierEngine.Execute(Me) End Function

Observe that each instance has access to a VerifierEngine; this is the VerifierEngine that belongs to its EntityManager.

Trigger Verification: BeforeSet and AfterSet


Should we validate a value before we set the property or after we set the property? There is no universally correct answer to this question.

BeforeSet Triggers
Some bad values should never enter the object. If the object property concerned the dosage level of a drug, wed want to prevent entry of an invalid value. Ten thousand milligrams of something could be fatal. We have to block that at the moment of data entry. We dont want the user to be able to move until the problem is corrected. We certainly dont want that dosage to appear in the business object ever not even in cache. This is the right place for BeforeSet trigger verification. In BeforeSet verification, the VerifierEngine receives a proposed value from the caller. The engine creates a TriggerContext with TriggerContext.Timing set to TriggerTiming.BeforeSet. It embeds the proposed value in the TriggerContext.ProposedValue. Then it makes calls on the verifier(s) linked to the trigger, passing in this TriggerContext so that the verifier (a) knows how it was triggered and (b) the value it should test. By convention, the code that asks for BeforeSet trigger verification should examine the VerifierResultCollection returned from the engine before doing anything more with the proposed value. If the results collection contains an errant result if VerifierResultCollection.AreOk is false the code should discard the proposed value.

358 | P a g e

IdeaBlade DevForce

Validation Through Verification

The following are the VerifierEngine.Execute overloads for BeforeSet trigger verification: BeforeSet Trigger Execute Signatures
1 Execute(object pTriggerItemInstance, String pMemberName, Object pProposedValue)

Description Perform BeforeSet validation of verifiers on all objects that are linked to the pMemberName property of the pTriggerItemInstance. The property will be set to the pProposedValue unless the validation fails. Verifiers receive the default VerifierContext. We say that the property caused a BeforeSet trigger validation

Execute(object pTriggerItemInstance, String pMemberName, Object pProposedValue, VerifierContext pVerifierContext)

Perform BeforeSet validation of verifiers on all objects that are linked to the pMemberName property of the pTriggerItemInstance. Verifiers receive the given VerifierContext. Perform BeforeSet validation of verifiers on all objects that are linked to the given PropertyDescriptor which translates to a property of the pTriggerItemInstance. Verifiers receive the default VerifierContext. Perform BeforeSet validation of verifiers on all objects that are linked to the given PropertyDescriptor which translates to a property of the pTriggerItemInstance. Verifiers receive the given VerifierContext. Perform BeforeSet validation of verifiers on all objects that are linked to the given TriggerItem. Verifiers receive the default VerifierContext. Perform BeforeSet validation of verifiers on all objects that are linked to the given TriggerItem. Verifiers receive the given VerifierContext.

Execute(object pTriggerItemInstance, PropertyDescriptor pDescriptor, Object pProposedValue)

Execute(object pTriggerItemInstance, PropertyDescriptor pDescriptor, Object pProposedValue, VerifierContext pVerifierContext)

Execute(object pTriggerItemInstance, TriggerItem pTriggerItem, Object pProposedValue)

Execute(object pTriggerItemInstance, TriggerItem pTriggerItem, Object pProposedValue, VerifierContext pVerifierContext)

Setting a BeforeSet Trigger


The natural place to trigger a BeforeSet validation is inside the setter of the property, before writing the incoming value into the object. The code should provide the incoming value as the ProposedValue parameter. The code may include a VerifierContext if it will help the triggered Verifier do its job but the context is optional and may be null. The VerifierEngine provides the Verifier with a TriggerContext object that (a) alerts the Verifier to the fact that it was triggered and (b) provides the contextual information it needs to do its evaluation, including the proposed value in this BeforeSet case.
See the TriggerContext section for more information.

359 | P a g e

IdeaBlade DevForce

Validation Through Verification

If the verification fails if any BeforeSet Verifier produces an errant VerifierResult the property must do something. The .NET framework development guidelines suggest that it should throw an exception. There is a VerifierResultException105 for this purpose.
A VerifierResultException should not terminate the application. DevForce handles the exception gracefully when it occurs during data binding; see the Error! Reference source not found. section. The developer must handle a VerifierResultException thrown outside of data binding.

Entity.BeforeSetValue
The Object Mapper generates an Entity.BeforeSetValue method that adheres to this recommendation precisely106. The method is virtual; developers can override it in a base entity class if they want different behavior or if they want to augment it with other behavior such as error logging.

AfterSet Triggers
Life and death properties are relatively rare. It is usually ok if the property value is invalid while the user is working with the object. We want the user to know the value is invalid. We want to block every attempt to save invalid data. But we can tolerate bad values for a while. For example, the employees home city may be a required value. We may not be able to save the employee record until we have a complete and valid home address. We want the application to tell us about the omission in time to correct it. On the other hand, it isnt going to harm anything if it stays blank while the user is entering new employee information. If the user mistakenly enters the wrong city, she should be able to clear it. She may not know the name of the correct city; it is better to leave the city blank than to leave the incorrect city in place. This is fine as long as we prevent the user from saving the address.
Summarizing the requirement: Permit entry of an invalid value but advise the user of that fact. Prevent saving of an object with an invalid value and tell the user about that.

The rule manifested in the Verifier - is the same in both cases. How we validate and what we do with the result depends upon the context. We covered the second scenario - block the save when we discussed instance validation above. We want AfterSet triggered validation to handle the first scenario. AfterSet means that the property has already been set with the incoming, invalid value from the user by the time we validate. There is no proposed value to worry about. We still want to validate the (now current) property value and tell the user if there is a problem.
105 106

Its constructor accepts a VerifierResultsCollection parameter that handlers can interpret and present intelligently. The appendix discusses the implementation of BeforeSetValue in detail.

360 | P a g e

IdeaBlade DevForce

Validation Through Verification

The following are the VerifierEngine.Execute overloads for AfterSet trigger verification: AfterSet Trigger Execute Signatures
1 Execute(object pTriggerItemInstance, String pMemberName)

Description Perform AfterSet validation of verifiers on all objects that are linked to the pMemberName property of the pTriggerItemInstance. Verifiers receive the default VerifierContext. We say that the property caused a AfterSet trigger validation

Execute(object pTriggerItemInstance, String pMemberName, VerifierContext pVerifierContext)

Perform AfterSet validation of verifiers on all objects that are linked to the pMemberName property of the pTriggerItemInstance. Verifiers receive the given VerifierContext. Perform AfterSet validation of verifiers on all objects that are linked to the pMemberName property of the pTriggerItemInstance. Verifiers receive the given VerifierContext. Perform AfterSet validation of verifiers on all objects that are linked to the given TriggerItem. Verifiers receive the given VerifierContext.

Execute(object pTriggerItemInstance, PropertyDescriptor pPropertyDescriptor, VerifierContext pVerifierContext)

Execute(object pTriggerItemInstance, TriggerItem pTriggerItem, VerifierContext pVerifierContext)

As always we must tell the VerifierEngine to perform verification. The VerifierEngine will give the triggered Verifier a TriggerContext just as it did for the BeforeSet trigger but this time there will be no proposed value; the verifier may have to fish the value out of the object. That shouldnt be hard. The Verifier typically knows the property it verifies and this property is usually the same property that triggered verification. A First Name StringLengthVerifier that is triggered by input of first name text will know how to examine the FirstName property of the Employee instance it verifies.
DevForce removes the guess work if the Verifier inherits from PropertyValueVerifier (as StringLengthVerifier does). Every subclass of PropertyValueVerifier has a virtual VerifyValue method that receives both the instance to verify and the value to verify.

It is slightly trickier if the instance triggering the verifier is different from the object instance verified. We encountered such a case when we considered a TotalPriceVerifier on Order that is triggered by a change to the price of one of its OrderDetails. Fortunately, the Orders TotalPriceVerifier can use the TriggerContext.TriggerItem.MemberName (UnitPrice) to dig the changed price value out of the TriggerContext.TriggerItemInstance (the OrderDetail instance). Relatively few verifiers involve such circuitous triggering. The vast majority of verifiers are PropertyValueVerifiers whose triggering and verified instances are the same object.

361 | P a g e

IdeaBlade DevForce

Validation Through Verification

Which leaves us with the small problem of invoking the VerifierEngine at the right time. As this is a AfterSet trigger, we should call the engine immediately after the line that pushes the incoming value into the trigger object.

Entity.AfterSetValue
That is what the Object Mapper does when it inscribes an Entity.AfterSetValue method into the generated property code 107. What happens if the verification fails? We invoked the verifier for a reason, presumably to alert the user to a problem. The AfterSetValue throws a VerifierResultException just as the BeforeSetValue does. DevForce and .NET handle this just fine if the exception occurs within data binding. The developer must handle the exception if it occurs anywhere else.
AfterSetValue is virtual so developers can override it in a base entity class if they want different

behavior. Well consider an alternative implementation in the Error! Reference source not found. section.
Remember that you can delay telling the user about invalid input and rely upon instance verification to catch it just before save. You wont need AfterSet triggers if you go this route.

The Role of the Object Mapper


As we just noted, the Object Mapper includes the Entity.BeforeSet and the Entity.AfterSet methods in the code it generates for properties unless you specify otherwise. It also generates an Args parameter for those methods that specifies whether verification should be invoked BeforeSet and AfterSet. By default, it is invoked in both situations.

Writing Verified Custom Business Object Properties


Developers often write custom business object properties. Such properties are usually ReadOnly, which is to say, they have a getter but no setter. Trigger validation is a non-issue if there is no setter. When the developer needs to write a settable property, her code probably should parallel the code generated by DevForce.

Monitor Execution with the VerifierBatchInterceptor


Some applications need to monitor the progress of a VerifierEngines execution and intervene at certain points. The VerifierEngine.BatchInterceptor is the way to do it. The engine calls the interceptor after evaluating each Verifier giving all of the visibility and opportunity it needs.
107

The appendix discusses the implementation of AfterSetValue in detail.

362 | P a g e

IdeaBlade DevForce

Validation Through Verification

An interceptor is a method that conforms to the VerifierBatchInterceptor delegate signature:


C#
public delegate VerifierOnErrorMode VerifierBatchInterceptor( Object pInstance, TriggerContext pTriggerContext, VerifierContext pVerifierContext);

Visual Basic
Public Delegate Function VerifierBatchInterceptor( _ ByVal pInstance As Object, _ ByVal pTriggerContext As TriggerContext, _ ByVal pVerifierContext As VerifierContext) As VerifierOnErrorMode

Because the interceptors parameters are the same as the parameters of the Verifier methods, IsApplicable and Verify(), it has the same visibility into the verification process as they do.
The interceptor can see the last verifier evaluated by looking at the VerifierContext.Verifier. review and edit the accumulating VerifierResultCollection by looking at the VerifierContext.VerifierResults. terminate the current batch at any time by returning VerifierOnErrorMode.Stop. post-process the VerifierResults when the batch is done because the engine will call it one last time with the VerifierContext.EndOfBatch flag set true. The following example shows how one could use an interceptor to curb run-away validations. In this case, it terminates the batch on the third error: C#
VerifierEngine engine = new VerifierEngine(); engine.BatchInterceptor = MyBatchInterceptor; private VerifierOnErrorMode MyBatchInterceptor( Object pInstance, TriggerContext pTriggerContext, VerifierContext pVerifierContext) { if ( pVerifierContext.VerifierResults.Errors.Count > 2 ) { pVerifierContext.VerifierResults.Add( new VerifierResult(false,"More than 2 errors encountered")); return VerifierOnErrorMode.Stop; } else { return VerifierOnErrorMode.Continue; } }

Visual Basic
Dim engine As New VerifierEngine() engine.BatchInterceptor = AddressOf MyBatchInterceptor Private Function MyBatchInterceptor( _ ByVal pInstance As Object, _ ByVal pTriggerContext As TriggerContext, _ ByVal pVerifierContext As VerifierContext) As VerifierOnErrorMode If pVerifierContext.VerifierResults.Errors.Count > 2 Then

363 | P a g e

IdeaBlade DevForce

Validation Through Verification

pVerifierContext.VerifierResults.Add( _ New VerifierResult(False,"More than 2 errors encountered")) Return VerifierOnErrorMode.Stop Else Return VerifierOnErrorMode.Continue End If End Function

364 | P a g e

IdeaBlade DevForce Developers Guide

DevForce Silverlight Apps

DevForce Silverlight Apps

Features described in the section are included with the DevForce Silverlight product.

DevForce Silverlight Apps ......................................................................................................... 365


Overview - What is DevForce Silverlight? ........................................................................................................ 365 Creating a DevForce Silverlight Application .................................................................................................... 366 Silverlight Deployment Steps ............................................................................................................................ 366 Questions and Answers...................................................................................................................................... 367 Troubleshooting ................................................................................................................................................. 368

Overview - What is DevForce Silverlight?


DevForce Silverlight allows you to deliver line of business applications in the browser with the kind of responsiveness users expect from a desktop application. Developed for Microsoft Silverlight, the browser plug-in which powers rich application experiences, it allows you to leverage your existing DevForce experience with new tools and techniques to build serious applications. A few things to note about Silverlight, and thus about DevForce Silverlight:
Silverlight is inherently n-tier. The client application executes in a sandbox on the browser, and must communicate with a service to retrieve and save data. The DevForce Silverlight Business Object Server (BOS) provides that service, and allows you quickly to have a Silverlight application retrieving and saving to a database, using the domain model and business objects you're already familiar with. Silverlight is inherently asynchronous. To avoid blocking the browser, Silverlight requires that all service communications be performed asynchronously. This can be a bit challenging at first, but DevForce Silverlight provides an asynchronous API very similar to the standard synchronous API, plus additional features to make asynchronous programming as easy as possible.

In DevForce Silverlight, you have the EntityManager to hold your client-side entity cache and communicate with the BOS, just like you would in a standard DevForce application. The Domain Model is actually shared between the two environments, and DevForce handles the movement of your business objects between tiers. You use the standard EntityQuery syntax to build true LINQ queries, which can be directed against a back-end data source or against the local DevForce cache. Your queries run asynchronously against back-end data sources, or synchronously against the local cache.

365 | P a g e

IdeaBlade DevForce Developers Guide

DevForce Silverlight Apps

Key to it all is the shared domain model. The domain model used by the Silverlight application is the same domain model used on the server, or in any .NET DevForce application: not an anemic object model with an unfamiliar API. You can add business logic - via custom methods and properties, DevForce property interceptors, and DevForce verification - to your shared domain model. You can also choose to deploy logic which is applicable to the client-side or server-side only.

Creating a DevForce Silverlight Application


You can use several different approaches to create a Silverlight application with DevForce:
1. Use the DevForce Silverlight Application template.

You can find this project template by choosing File - New Project' or 'File - Add - New Project' in Visual Studio. The template is in the DevForce 2010 folder under both the Visual C# and Visual Basic project types. Its use will result in the creation of both the Silverlight and web application projects for your DevForce Silverlight application.108 From here you can work on UI and domain model features, or reference already created projects. You'll use the Visual Studio Entity Data Model designer to create the domain model in the web application project. DevForce will generate the code for that model, and will then automatically add it to the Silverlight project as linked code so that it will be compiled into that assembly as well.
2. Use the standard Silverlight application template.

The standard Visual Studio template for a Silverlight application will create both the Silverlight and web application (or web site) projects. If you want the web application to host your BOS, you will need to do the following:
Add the EntityService.svc and EntityServer.svc files to the project; Add all necessary IdeaBlade references; and Modify the web.config to include the appropriate settings for the BOS.

You can find samples of the EntityService.svc, EntityServer.svc, and web.config files in the folder LearningResources\110_Deployment\Snippets\IIS Files, installed by DevForce within the DevForce 2010 installation folder (typically installed under C:\Program Files). As with option #1, you will use the Visual Studio Entity Data Model (EDM) designer to create the domain model. DevForce will generate an object model from the EDM and link that object model to the Silverlight project.

Silverlight Deployment Steps


Please see the Deployment topic document (in the Deployment section of the Learning Resources) for detailed instructions and information about deploying Silverlight apps.

108

Note that the Silverlight assembly will ultimately be deployed client-side; the web assembly server-side.

366 | P a g e

IdeaBlade DevForce Developers Guide

DevForce Silverlight Apps

Questions and Answers


1. What is the "linked" Silverlight domain model?

In order to provide a single "shared" domain model which can be used between application tiers, DevForce Silverlight creates two versions of the model - one compiled with .NET assemblies and one compiled with Silverlight assemblies. These two versions actually reference the same code files, and use the "linked" file feature of Visual Studio so that only a single copy of any file is required. The IdeaBlade OM Designer Extension (see Tools / Extension Manager from the Visual Studio main menu after installing DevForce) performs this linkage for you: it will generate the domain model files into the .NET (web) project, and then create links to these files in the Silverlight project. The result is that the domain model is available to both environments: a Customer class is the same whether its defined in the client Silverlight application, or the server domain model assembly. One additional requirement also ensures that the types in your domain model can be shared: both the namespace and assembly names must be the same for the two assemblies holding the domain model.
2. Where is the debug log?

A "client-side" debug log is not provided in DevForce Silverlight. A debug log is generated on the BOS server, but it contains the usual server-side messages. You can implement a custom ITraceLogger if you need Silverlight tracing messages logged.
3. Do I have to host the BOS from IIS? And must it be the same web site that's serving the Silverlight application? You can still host the BOS from either the console (ServerConsole.exe) or Windows Service (ServerService. exe) in DevForce Silverlight. You can also host the BOS from a different web site than the Silverlight application. In both scenarios you need to ensure that a policy file is in place to avoid getting a cross-domain access error. You'll find a sample clientaccesspolicy.xml file in the LearningResources\110_Deployment\Snippets\Silverlight folder installed by DevForce, along with a readme explaining how to deploy the file. 4. Can a single BOS support both Silverlight and .NET client applications at the same time? Yes it can, providing that your license allows it. 5. How can I bind anonymously typed objects in my Silverlight application? The DynamicTypeConverter converts anonymously-typed objects to dynamically-typed objects for binding in Silverlight applications. Use the Convert(IEnumerable) method to convert one or more instances of an anonymous type to corresponding instances of a DevForce dynamic type. A DevForce "dynamic type" is a System.Type created dynamically at runtime. Generally the primary use for this conversion is in Silverlight applications, which do not support data binding to anonymous types. Projection queries are one common example in which return data will be anonymously-typed. 6. How can I customize the communications channel to the BOS? For example, I need to set higher timeout values and add security.

The default configuration used by DevForce uses HTTP binding, binary encoding, and a MaxReceivedMessageSize set to the maximum value (2G), with all other attributes defaulting. To override the DevForce defaults you can add a ServiceReferences.ClientConfig file to your Silverlight

367 | P a g e

IdeaBlade DevForce Developers Guide

DevForce Silverlight Apps

application, or implement a custom ServiceProxyEvents class. More information on sub-classing ServiceProxyEvents is available in the API documentation. A sample ServiceReferences.ClientConfig is provided with the DevForce installation, in the 110_Deployment\Snippets\Silverlight folder. Unless youre familiar with these files, its best to copy the sample into your project and customize that. To use: include the file in the Silverlight application project and mark it as Content. Remember that both the client and server configurations must be compatible for communications to succeed, so you will likely need to modify your web.config file also. The 110_Deployment\Snippets\Sample N-tier config files folder contains samples showing different communications configurations.

Troubleshooting
1. You attempt to Connect to the BOS from the Silverlight client and receive the exception "An error occurred while trying to make a request to URI 'http://localhost:9009/EntityService.svc'" Connection errors can have many causes, but the first thing to check, especially in a new application using the ASP.NET Development Server, is that the Silverlight application is actually "served" by the web application. You can see this by looking at the address bar in the browser. If it doesn't start with "http://" then the application is instead loading from the file system. Why is this a problem? Because, for security reasons, a Silverlight application cannot make service requests unless served by a web server. In DevForce Silverlight this means that the application cannot connect to, or make other requests of, the BOS; thus, data cannot be retrieved from or saved to the back-end data source.

The problem is easily remedied by ensuring that the web application project is always the startup project in your solution.
2. "*** License violation *** - 'Distributed BOS' not supported with the current license: StandardEF"

You must have a license for DevForce Silverlight in order to develop Silverlight applications with DevForce. The Silverlight samples in the Learning Units were created with a license key authorized for Silverlight, and you'll be able to run the samples as long as you don't regenerate the domain model. Once you regenerate the model with your license key, the sample will stop working due if your key does not authorize DevForce Silverlight.
3. I get the following exception when trying to fetch: "Unable to locate type: XX.YY"

This not-so-friendly message may be caused by a type name mismatch between your .NET and Silverlight domain model assemblies. DevForce will seamlessly transmit entities between the Silverlight and BOS tiers, but it does this using what is essentially a "shared" domain model. DevForce expects to see entities having the same fully-qualified type name, for instance "DomainModel.Customer, DomainModel, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null", in both the .NET and Silverlight assemblies holding the model. This is why DevForce attempts to keep the assembly and namespace names in sync between the two projects, since without this type name equality, entities cannot move between tiers. This restriction will likely be removed in later releases of DevForce Silverlight. To fix the exception,

368 | P a g e

IdeaBlade DevForce Developers Guide

DevForce Silverlight Apps

ensure that the assembly and namespace names of the two projects containing the domain model are identical.
4. Why aren't my breakpoints working?

This has nothing to do with DevForce, but we run into it from time to time. Double-check the Web properties on the web application project, and ensure that both the ASP.NET and Silverlight debuggers are checked.
5. Your application was running initially and then crashes after a few minutes with an exception message such as: Object reference not set to an instance of an object.. ---> System.NullReferenceException: Object reference not set to an instance of an object.

You may have encountered a problem that occurs when the IIS application pool has recycled. One of the best ways to insure this does not happen is to create a new application pool that does not recycle on a time limited basis and then assign your application to that pool.
6. Your application had been running and then crashes after you make a change to one or more of the files in the application directory. The exception includes this message: Could not load file or assembly 'App_Web_.........

You may have encountered a problem that occurs when files in the application folder no longer match the compiled version located in the Temporary ASP.NET Files folder. You can force a rebuild of your application by deleting the bin folder and then replacing it with a copy, or by running the aspnet_compiler.exe command with the -c switch. You can find the command by first browsing to the folder %SystemRoot%\Microsoft.NET\Framework\ and then opening the v2.0.xxxxx subfolder (the numbers after v2.0 can vary) . Here is an example using the virtual directory name of the application: aspnet_compiler v /MyApp -c

7.

FIPS Compliance

If your Silverlight application will be served from a web server on which FIPS (Federal Information Processing Standards) compliance is enforced, you will need to make the following changes to both the web.config and startup pages. In the web.config, you must set debug to false when FIPS is enabled. This is true even during development: you cannot set debug to true with FIPS enabled!

XML

<system.web> <!-Set compilation debug="true" to insert debugging symbols into the compiled page. Because this affects performance, set this value to true only during development. --> <compilation debug="false">

369 | P a g e

IdeaBlade DevForce Developers Guide

DevForce Silverlight Apps

If you use an .html page (ex: index.html) instead of an .aspx page to host the Silverlight application, you will need to delete:

XML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

in order to be compatible with Firefox.

8.

How to tell if the BOS is running.

Youve received an error message from your client application stating, The remote server returned an error: Not Found. This is a communications error which occurs when the WCF client application is unable to complete a handshake with the server. There are, unfortunately, a myriad of reasons why this might occur, but one of the first things to check is whether the service is actually running. You can do this easily: open the web browser and point it to the URL being used by the client application. For example, if the client app.config contains the following

C#

<objectServer remoteBaseURL="http://localhost" serverPort="9009" serviceName="EntityService.svc/sl" />

or no app.config is present, then open the web browser to http://localhost:9009/EntityService.svc . (Note that the final /sl is not included when browsing for the service description.) If the service is running, you will see a Service description page generated by WCF. If, instead, you see a page showing error information, then you know the service cannot be started and that your application will be unable to run. Usually the error message on the page has helpful diagnostic information.
9. Known Issues (Silverlight-Only)

The Copy Local property on DevForce references in the web project must be set to true for the apps to run properly. This setting is required to allow the DevForce WCF services (defined in the *.svc files) to be compiled correctly. If not set, the services will not start, the client application will be unable to connect to the server, and you will see an error message as follows: The remote server returned an error: NotFound. If the service is unavailable, then also make sure that the endpoint bindings match between client and server.

370 | P a g e

IdeaBlade DevForce Developers Guide

DevForce Silverlight Apps

When you begin your Silverlight solution using the DevForce Silverlight Application project template, several DevForce assemblies are added as references to the web project; and for all, CopyLocal is set to true. However, if you manually add or modify references, you may see that the property is initially set to false (which is the Visual Studio default). Always check this property when you see the above error.

371 | P a g e

IdeaBlade DevForce

Web Applications

Web Applications

Web Applications........................................................................................................................ 372


State of the Release Candidate Documentation ................................................................................................. 372 Introduction ....................................................................................................................................................... 372 The DevForce ASPDataSource Component ...................................................................................................... 372 Using the ASPDataSource in Development ...................................................................................................... 373 Overridable Methods for Select, Update, Insert, and Delete ............................................................................. 373 The EntityAdapterManager Class ...................................................................................................................... 374 The Configure Data Source Wizard ................................................................................................................... 375 Parameter Collection Editor .............................................................................................................................. 375 Retrieving Schema Information ......................................................................................................................... 376 Third Party Support ........................................................................................................................................... 376

State of the Release Candidate Documentation


We are working hard to update all of our documentation from DevForce 2009 to DevForce 2010, .NET 4, and Silverlight 4. During this conversion, you may find some sections that are out of date, but you should be able to get many of the examples to work, with small modifications, by checking against the API Documentation for the current method signatures.

Introduction
DevForce provides object mapping and a persistence framework that are as useful in web applications as in WinForm and WPF applications. For a web application, you deploy DevForce server-side. Communication between the client and server tiers occurs between the users browser and IIS; serverside, requests for data are handed off to a two-tier DevForce application which communicates with the back end data sources.

The DevForce ASPDataSource Component


The datasource controls provided with ASPNET, notably the Object DataSource, are not designed to work with objects that contain business logic, making them unsatisfactory for work with IdeaBlade

372 | P a g e

IdeaBlade DevForce

Web Applications

business objects. By contrast, the DevForce ASPDataSource control is an ASP.NET data source control designed to work with rich business objects. The ASPDataSource control is used at both run time and design time. Data binding occurs at run time. At design time, functionality has been provided to allow Visual Studio 2005 developers to graphically create web pages using common controls like the .NET DetailsView and GridView and third-party controls like the Developer ExpressASPxGridView and ASPxPivotGrid, and the Infragistics UltraWebGrid.

Using the ASPDataSource in Development


The UI developer drags an ASPDataSource control onto a Web Form and interacts with it. In Visual Studio, the ASPDataSourceDesigner exposes a Configure Data Source Wizard to permit the developer to configure an instance of the ASPDataSource. When a control such as a .NET GridView bound to an ASPDataSource control requests schema information about its data source, the schema information is created and returned. This allows information to be returned not only about the simple properties backed by table columns, but also about custom computed properties. At runtime, the Web Form interacts with the ASPDataSource to perform the actual databinding. This work is handled by the ASPDataSourceView, an instance of which is managed by the ASPDataSource control.

Overridable Methods for Select, Update, Insert, and Delete


Every ASP.NET DataSource control, whether provided in Visual Studio (e.g., the ObjectDataSource) or by a third party vendor, allows the developer to specify methods and parameters for Select, Update, Insert, and Delete methods. In many implementations, this is done by having the user select from a dropdown list supplied by a wizard (as provided by the .NET ObjectDataSource) or by having the developer type in the method name into a property sheet for the DataSource control (as done with the previous DevForceDataSource). The required parameters for the CRUD method were specified in the code and were collected at runtime using a ParameterCollection Editor. We found that typing in the CRUD method name and then making sure that the parameters specified in the code matched the parameters collected by the Parameter Colllection Editor was a process that was both error-prone and difficult to debug. For the ASPDataSource, we decided to take a different approach. We have declared four abstract methods for Select, Update, Insert, and Delete which must be overridden by the developer to be used. By requiring the developer to use overridable methods, many incorrectly written methods produce compilation errors rather than runtime errors, and mysterious reflection errors are avoided. Here are the signatures for the four abstract methods:

373 | P a g e

IdeaBlade DevForce
C#

Web Applications

public virtual IEnumerable SelectEntities(IOrderedDictionary parameters, DataSourceSelectArguments pSelectArgs)

public virtual int InsertEntity(IOrderedDictionary parameters, IDictionary newvalues)

public virtual int UpdateEntity(IOrderedDictionary parameters, IDictionary keys ,IDictionary values, IDictionary oldvalues)

public virtual int DeleteEntities(IOrderedDictionary parameters, IDictionary keys, IDictionary oldvalues)

The EntityAdapterManager Class


The code for each instance of an ASPDataSource control is contained in an EntityAdapterManager class. In the Instructional Unit for this feature, there are three ASPDataSource controls: mEmployeeSelectorAdapterManager, a DropDown control, mEmployeeEditorAdapterManager, a DetailsView control; and

mOrderAdapterManager, a GridView control. Every instance of an EntityAdapterManager has a read-only EntityTypeName (e.g, Employee or Order) and an AdapterManagerAssemblyName (e.g., Web.Model, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null)

374 | P a g e

IdeaBlade DevForce

Web Applications

The Configure Data Source Wizard


To prevent errors caused by mistyping the name of the EntityAdapterManager class, the developer is encouraged to use a wizard. The ASPDataSource control has a wizard that provides a dropdown giving the developer the choice of which EntityAdapterManager to use.

After the developer has selected an EntityAdapterManager, the EntityTypeName and EntityAssemblyName properties are computed.

Parameter Collection Editor


The ASPDataSource control uses a Parameter Collection Editor to configure the parameters for each CRUD method. For example, in the Instructional Unit, the EmployeeEditorAdapterManager needs the ID of the currently selected employee in the DropDown list at the top of the Web Form to be able to perform its SelectEntities method.

375 | P a g e

IdeaBlade DevForce

Web Applications

Retrieving Schema Information


The ASPDataSource supplies schema information to the web control to which it is attached. A DetailsView control, for example, needs to know the name, datatype, and other property information about the fields of the Entity that it is editing. A GridView needs to know the same kind of information for the columns in its GridView. To signal its ability to supply this information, an ASPDataSource control communicates its CanRefresh property. Then, the WebControl calls into the ASPDataSource control to invoke the GetFields() function to get this schema information. Once the Web Control has this information, it can display this information to the UI developer.

Third Party Support


The ASPDataSource control works well with a variety of .NET Web Controls such as the DropDownList, DetailsView, and GridView; it works equally well with well-known third-party web controls such as the ASPxGridView and ASPxPivotGrid from Developer Express, and the UltraWebGrid from Infragistics. We suspect that the ASPDataSource will work with many other third-party controls that we have not specifically tested.

376 | P a g e

IdeaBlade DevForce

Business Object Server

Business Object Server

Business Object Server

377

Business Object Server Architecture ................................................................................................................. 377 EntityService Startup and Shutdown ................................................................................................................. 381 EntityServer Startup and Shutdown ................................................................................................................... 381 Remote Service Method Call (RSMC) Methods ............................................................................................... 382 Push Notification ............................................................................................................................................... 384 BOS Hosting Details ............................................................................................................................................ 385 The DevForce Client ............................................