Show/Hide Toolbars

TMS XData Documentation

Navigation: Entity Model

Using TXDataModelBuilder

Scroll Prev Top Next More

XData server uses an entity model to specify the data it will publish through the Rest/Json interface. To work properly, the server module (TXDataServerModule object) needs an entity model, represented by a TXDataAureliusModel to work properly.

 

However, you can create the TXDataSeverModule without explicitly providing an entity model. In this case, the server will create one internally, based on the default TMappingExplorer, using the rules of equivalence between Aurelius mapping (specified in the mapping explorer) and the entity model. That's the most straightforward and common way to use it, and you will rarely need to create an entity model yourself.

 

In the case you want to build your own model and create a server based on it (for example, when you don't want classes and fields to have their "T" and "F" prefix removed from their names), you can do it using the TXDataModelBuilder.

 

The following example creates a model based on the default mapping explorer, however it sets properties UseOriginalClassNames and UseOriginalFieldNames to true to avoid the model builder to remove the "T" prefix from class names when naming entity types, and remove "F" prefix from field names when naming properties and navigation properties.

 

uses
  {...}, XData.Aurelius.ModelBuilder, XData.Aurelius.Model, 
  XData.Server.Module, Aurelius.Mapping.Explorer;
 
var
  Builder: TXDataModelBuilder;
  Model: TXDataAureliusModel;
  Explorer: TMappingExplorer;
  Module: TXDataServerModule;
begin
  // model uses a TMappingExplorer instance with Aurelius mapping
  // this example doesn't explain how to obtain one, please refer to Aurelius documentation
  Explorer := TMappingExplorer.DefaultInstance; 
  Model := TXDataAureliusModel.Create(Explorer);
  try
    Builder := TXDataModelBuilder.Create(Model);
    try
      Builder.UseOriginalClassNames := true;
      Builder.UseOriginalFieldNames := true;
      Builder.Build;
    finally
      Builder.Free;
    end;
  except
    Model.Free; // destroy the model in case an error occurs
    raise;
  end;
 
  // create the module using the created model.
  Module := TXDataServerModule.Create(MyServerUrl, MyConnectionPool, Model);  
end;

 

When adding service operations, to the model, the model builder will only take into consideration those service interfaces that belong to the same model of the TMappingExplorer instance you are using. For example, if your TMappingExplorer represents the model "Sample", only service contracts marked for the "Sample" model will be included in the entity model.

 

You can use the methods and properties of TXDataModelBuilder to better define your XData Model:

 

TXDataModelBuilder class

 

Declared in Unit

 

XData.Aurelius.ModelBuilder

 

Properties

 

Name

Description

UseOriginalClassNames: Boolean

By default XData removes the leading "T" of the class name to define the entity set name. So a class TCustomer will correspond to the entity set "Customer". If you don't want this behavior and wants the entity set name to be exactly the name of the class, set this property to true.

UseOriginalFieldNames: Boolean

By default XData removes the leading "F" of a mapped field name to define the property name of an entity type. So a field named "FName" will correspond to a property "Name" in JSON responses. If you don't want this behavior and wants the property name to be exactly the name of the field, set this property to true.

UseOriginalContractNames: Boolean

By default XData removes the leading "I" of the interface name to define the url segment of a service operation. So an interface ITestService will correspond to a service URL which path segment will beign with "TestService". If you don't want this behavior and wants the service operation base URL to be exactly the name of the interface, set this property to true.

 
Methods

 

Name

Description

function AddEntitySet(AClass: TClass): TXDataEntitySet

Creates a new entity set based on the class defined by AClass. The class must be an Aurelius entity. This will automatically create the entity type associated with the entity set.

procedure AddService<T>

Adds a new service operation contract to the model. Example:

 

Model.AddService<IMyService>;

procedure RemoveEntitySet(AClass: TClass);

Use this to remove an entity set already created by the model. This is useful when you want to use the automatic creation of entity sets, based on Aurelius entities, but then you want to remove a few entity sets from the model.