Up EntityManager
DevForce 2010 Resource Center » DevForce development » Start » EntityManager » Multiple models per manager

Multiple models per manager

Last modified on August 15, 2012 17:20

The EntityManager can pool entities from many models with multiple data sources. The EntityManager is not limited to the entities of one model or one database. A single EntityManager instance can

  • contain POCO entities and regular DevForce entities from multiple models simultaneously.
  • query entities from different models.
  • save changed entities to different data sources in a single transaction; DevForce uses a distributed transaction if the data sources support it.

By default, when DevForce generates the base code for a model, it also code generates a custom EntityManager that contains pre-defined queries for entities in that model (e.g. entityManager.Customers where entityManager = new NorthwindIBEntities()).

However, these pre-defined queries are just for syntactic convenience, and in fact, any EntityManager can query for entities from any model. Instead of using the pre-defined property for the query, you just call GetQuery with the entity type that you want:

C#
    manager = new EntityManager(); // Creates an "untyped" EntityManager
                                  // - but you can use any EntityManager
   var query = manager.GetQuery<Customer>(); // "Get all customers" query
   query.ExecuteAsync();
   // listen for completed event and do something
VB
    manager = new EntityManager() ' Creates an "untyped" EntityManager
                                 ' - but you can use any EntityManager
   dim query = manager.GetQuery<Customer>() ' "Get all customers" query
   query.ExecuteAsync()
   ' listen for completed event and do something

The entity types are tied to a particular data source by its DataSourceKey name. The EntityManager and EntityServer learn from the entities they see which data sources are involved in a requested operation.

That model-specificity of entities is apparent in their generated class code; notice the DataSourceKeyName attribute adorning the Customer class:

C#
IbEm.DataSourceKeyName(@"NorthwindEntities")]
public partial class Customer : IbEm.Entity {}
VB
<IbEm.DataSourceKeyName("NorthwindEntities")>
Partial Public Class Customer Inherits IbEm.Entity
End Class

DevForce generates a strongly-typed subclass of the EntityManager such as NorthwindIBEntities that appears to be tied to a single model because it has supplemental properties and methods that are specific to one model. Managers are typically used with one model and these extra member make it convenient to work with that model. But, like every EntityManager, the derived manager can contain entities from multiple models, query from multiple models, and save to multiple databases.

DevForce code generation does add a DataSourceKeyName attribute to the derived EntityManager. The attribute is notional and in no way restricts that EntityManager's ability to work simultaneously with entities from multiple models.
Tags:
Created by DevForce on March 24, 2011 10:54

This wiki is licensed under a Creative Commons 2.0 license. XWiki Enterprise 3.2 - Documentation. Copyright © 2015 IdeaBlade