Data Management. Getting Started

The goal of this topic is to implement simple Export and Import operations in the XAF Main Demo application. By default, the Main Demo application is installed in %PUBLIC%\Documents\DevExpress Demos XX.X\Components\eXpressApp Framework\MainDemo. The application's database contains Contact table populated with objects.

data_management_5

The first task to be implemented is to import the Contact table to the XML Data Set. And the second task is to get the Contact object from the XML Data Set and add it to the application database.

Xafari.Data module supplies ExportOperation and ImportOperation classes which provide the required functionality. Data migration is performed automatically, to execute operation it is necessary to describe a data source and determine some settings. The end user interaction will be provided via a standard mechanism of Actions and Controllers.

Note

The tasks in this topic are quite trivial and has an introductory character. Export and Import operations are a versatile tool and can be used in different scenarios. In simple cases, it is enough to use the existing data sources, but you can adapt to any situation by implementing custom one. There are various advanced ways to run these operations: from the command prompt, in a separate thread etc.

Open the existing XAF Main Demo solution and follow the steps described below.

Required Modules

Add XafariBCModule, XafariXFModule and XafariDataModule to the platform independent MainDemo.Module project. Add Xafari.dll assembly to the References list.

Add XafariBCWinModule to the platform-specific MainDemo.Module.Win module project. Add XafariBCWebModule to the platform-specific MainDemo.Module.Web module project.

Export Operation

In the Solution Explorer, right-click the Controllers folder in the MainDemo.Module project, and choose Add DevExpress Item | New Item..., to invoke DevExpress Template Gallery window. Then select the Vew Controller, specify a ExportImportContactsViewController as the new item's name and click Add Item.

data_management_6

Add two Simple Actions to the ExportImportContactsViewController.

To do this, double-click the MainDemo.Module | Controllers | ExportImportContactsViewController.cs file to invoke the Designer. In the DX.XX.X: XAF Actions section in the Toolbox, drag Simple Action to the Designer.

In the Properties window for Simple Action, set the Name and ID properties to "ExportContactAction". Switch to the Events view in the Properties window, double-click the Execute event to generate event handler.

Similarly, add ImportContactAction. Image below shows Controller's Designer.

data_management_7

Open ExportImportContactsViewController.cs file and modify the auto-generated ExportContactAction_Execute event handler, add the following code:

  • c#
  • VB

private void ExportContactAction_Execute(object sender, SimpleActionExecuteEventArgs e)
{
  // 1. Create and set ExportSettings
  var contactType = typeof(Contact);
  ITypeInfo contactTypeInfo = XafTypesInfo.CastTypeToTypeInfo(contactType);
  ISettings settings = new ExportSettings();
  ISchema schema = new Schema();
  string tableName = "Contact";
  ITypeMap contactTypeMap = new TypeMap(contactTypeInfo.FullName, tableName);
  contactTypeMap.KeyColumnName = "FullName";
  contactTypeMap.KeyColumnType = "System.String";
  contactTypeMap.KeyMemberName = "Oid";
  contactTypeMap.KeyMemberType = contactTypeInfo.FindMember("Oid").MemberType.FullName;
  contactTypeMap.MemberMapping.Add(new MemberMap("FullName", "FULLNAME"));
  contactTypeMap.MemberMapping.Add(new MemberMap("FirstName", "FIRSTNAME"));
  contactTypeMap.MemberMapping.Add(new MemberMap("LastName", "LASTNAME"));
  contactTypeMap.MemberMapping.Add(new MemberMap("Email", "EMAIL"));
  contactTypeMap.MemberMapping.Add(new MemberMap("Birthday", "BIRTHDAY"));
  schema.TypeMapping.Add(contactTypeMap);
  settings.Schema = schema;
  // 2. Create DataSource
  DataSetDataSource dataSource = new DataSetDataSource();
  dataSource.DestinationPath = "d:\\ExportImport\\ContactExport.xml";
  // 3. Execute Export Operation using ExportSettings and DataSource
  var operation = new ExportOperation(Application);
  operation.DataSource = dataSource;
  operation.Settings = settings;
  using (var objectSpace = this.Application.CreateObjectSpace())
  {
    operation.Execute();
  }
}

Private Sub ExportContactAction_Execute(ByVal sender As Object, ByVal e As SimpleActionExecuteEventArgs)
  ' 1. Create and set ExportSettings
  Dim contactType = GetType(Contact)
  Dim contactTypeInfo As ITypeInfo = XafTypesInfo.CastTypeToTypeInfo(contactType)
  Dim settings As ISettings = New ExportSettings()
  Dim schema As ISchema = New Schema()
  Dim tableName As String = "Contact"
  Dim contactTypeMap As ITypeMap = New TypeMap(contactTypeInfo.FullName, tableName)
  contactTypeMap.KeyColumnName = "FullName"
  contactTypeMap.KeyColumnType = "System.String"
  contactTypeMap.KeyMemberName = "Oid"
  contactTypeMap.KeyMemberType = contactTypeInfo.FindMember("Oid").MemberType.FullName
  contactTypeMap.MemberMapping.Add(New MemberMap("FullName", "FULLNAME"))
  contactTypeMap.MemberMapping.Add(New MemberMap("FirstName", "FIRSTNAME"))
  contactTypeMap.MemberMapping.Add(New MemberMap("LastName", "LASTNAME"))
  contactTypeMap.MemberMapping.Add(New MemberMap("Email", "EMAIL"))
  contactTypeMap.MemberMapping.Add(New MemberMap("Birthday", "BIRTHDAY"))
  schema.TypeMapping.Add(contactTypeMap)
  settings.Schema = schema
  ' 2. Create DataSource
  Dim dataSource As DataSetDataSource = New DataSetDataSource()
  dataSource.DestinationPath = "d:\ExportImport\ContactExport.xml"
  ' 3. Execute Export Operation using ExportSettings and DataSource
  Dim operation = New ExportOperation(Application)
  operation.DataSource = dataSource
  operation.Settings = settings
  Using objectSpace = Me.Application.CreateObjectSpace()
    operation.Execute()
  End Using
End Sub

The code above contains three blocks. The first one creates and initializes the settings object which represents the Export Settings. In this sample, Export Settings specifies the key field and the list of fields to be exported. You can define this object both in code or via the xml-file, it is possible to describes settings object as follows:

  • xml

<?xml version="1.0" ?>
  <ExportSettings>
  <Name>New Settings</Name>
  <Schema>
  <TypeMapping>
  <TypeMap>
  <TypeFullName>MainDemo.Module.BusinessObjects.Contact</TypeFullName>
  <TableName>Contact</TableName>
  <KeyMemberName>Oid</KeyMemberName>
  <KeyMemberType>System.Guid</KeyMemberType>
  <KeyColumnName>FullName</KeyColumnName>
  <KeyColumnType>System.String</KeyColumnType>
  <MemberMapping>
  <MemberMap>
  <MemberName>FullName</MemberName>
  <ColumnName>FULLNAME</ColumnName>
  </MemberMap>
  <MemberMap>
  <MemberName>FirstName</MemberName>
   <ColumnName>FIRSTNAME</ColumnName>
  </MemberMap>
  <MemberMap>
  <MemberName>LastName</MemberName>
  <ColumnName>LASTNAME</ColumnName>
  </MemberMap>
  <MemberMap>
  <MemberName>Email</MemberName>
   <ColumnName>EMAIL</ColumnName>
  </MemberMap>
  <MemberMap>
  <MemberName>Birthday</MemberName>
  <ColumnName>BIRTHDAY</ColumnName>
 </MemberMap>
  </MemberMapping>
  </TypeMap>
  </TypeMapping>
  </Schema>
 </ExportSettings>

Then, the second block defines a Data Source. DataSetDataSource type is shipped by the Xafari.Data module, it only requires to specify the file path.

The final stage is to initialize the Export Operation object and call the Execute() method. Export Operation object requires to set Export Settings and Data Source, they were created in the previous steps.

To see the result, create d:\ExportImport\ folder, run the WinForms application and execute ExportContactAction.

data_management_8

Then, navigate to the d:\ExportImport\ folder and see the ContactExport.xml file, which is the XML Data Set with data from the Contact table.

data_management_9

Examine this file.

Import Operation

This operation needs XML Data Source which contains Contact object for importing. Create a new ContactImport.xml file  and paste the following lines into it:

  • xml

<NewDataSet>
  <xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
  <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
  <xs:complexType>
  <xs:choice minOccurs="0" maxOccurs="unbounded">
  <xs:element name="Contact">
  <xs:complexType>
  <xs:sequence>
  <xs:element name="FULLNAME" type="xs:string" minOccurs="0" />
  <xs:element name="FIRSTNAME" type="xs:string" minOccurs="0" />
  <xs:element name="LASTNAME" type="xs:string" minOccurs="0" />
  <xs:element name="EMAIL" type="xs:string" minOccurs="0" />
  <xs:element name="BIRTHDAY" type="xs:string" minOccurs="0" />
  </xs:sequence>
  </xs:complexType>
  </xs:element>
  </xs:choice>
  </xs:complexType>
  </xs:element>
  </xs:schema>
  <Contact>
  <FULLNAME>NewFirst NewLast</FULLNAME>
  <FIRSTNAME>NewFirst</FIRSTNAME>
  <LASTNAME>NewLast</LASTNAME>
  <EMAIL>new_email@md.com</EMAIL>
  <BIRTHDAY>1/30/2016 12:00:00 AM</BIRTHDAY>
  </Contact>
  </NewDataSet>

Save this file to the d:\ExportImport\ folder. This path will be used in import settings.

As you can see, the structure of this file is similar to the structure of the resulting ContactExport.xml file for the Export Operation.

Open ExportImportContactsViewController.cs file and modify the auto-generated ImportContactAction_Execut event handler, add the following code:

  • c#
  • VB

private void ImportContactAction_Execute(object sender, SimpleActionExecuteEventArgs e)
{
  var contactType = typeof(Contact);
  ITypeInfo contactTypeInfo = XafTypesInfo.CastTypeToTypeInfo(contactType);
  ISettings settings = new ImportSettings();
  ISchema schema = new Schema();
  string tableName = "Contact";
  ITypeMap contactTypeMap = new TypeMap(contactTypeInfo.FullName, tableName);
  contactTypeMap.KeyColumnName = "FullName";
  contactTypeMap.KeyColumnType = "System.String";
  contactTypeMap.KeyMemberName = "Oid";
  contactTypeMap.KeyMemberType = contactTypeInfo.FindMember("Oid").MemberType.FullName;
  contactTypeMap.MemberMapping.Add(new MemberMap("FullName", "FULLNAME"));
  contactTypeMap.MemberMapping.Add(new MemberMap("FirstName", "FIRSTNAME"));
  contactTypeMap.MemberMapping.Add(new MemberMap("LastName", "LASTNAME"));
  contactTypeMap.MemberMapping.Add(new MemberMap("Email", "EMAIL"));
  contactTypeMap.MemberMapping.Add(new MemberMap("Birthday", "BIRTHDAY"));
  schema.TypeMapping.Add(contactTypeMap);
  settings.Schema = schema;
  DataSetDataSource dataSource = new DataSetDataSource();
  dataSource.SourcePath = "d:\\ExportImport\\ContactImport.xml";
  var operation = new ImportOperation(Application);
  operation.DataSource = dataSource;
  operation.Settings = settings;
  using (var objectSpace = this.Application.CreateObjectSpace())
  {
    operation.Execute();
  }
}

Private Sub ImportContactAction_Execute(ByVal sender As Object, ByVal e As SimpleActionExecuteEventArgs)
  Dim contactType = GetType(Contact)
  Dim contactTypeInfo As ITypeInfo = XafTypesInfo.CastTypeToTypeInfo(contactType)
  Dim settings As ISettings = New ImportSettings()
  Dim schema As ISchema = New Schema()
  Dim tableName As String = "Contact"
  Dim contactTypeMap As ITypeMap = New TypeMap(contactTypeInfo.FullName, tableName)
  contactTypeMap.KeyColumnName = "FullName"
  contactTypeMap.KeyColumnType = "System.String"
  contactTypeMap.KeyMemberName = "Oid"
  contactTypeMap.KeyMemberType = contactTypeInfo.FindMember("Oid").MemberType.FullName
  contactTypeMap.MemberMapping.Add(New MemberMap("FullName", "FULLNAME"))
  contactTypeMap.MemberMapping.Add(New MemberMap("FirstName", "FIRSTNAME"))
  contactTypeMap.MemberMapping.Add(New MemberMap("LastName", "LASTNAME"))
  contactTypeMap.MemberMapping.Add(New MemberMap("Email", "EMAIL"))
  contactTypeMap.MemberMapping.Add(New MemberMap("Birthday", "BIRTHDAY"))
  schema.TypeMapping.Add(contactTypeMap)
  settings.Schema = schema
  Dim dataSource As DataSetDataSource = New DataSetDataSource()
  dataSource.SourcePath = "d:\ExportImport\ContactImport.xml"
  Dim operation = New ImportOperation(Application)
  operation.DataSource = dataSource
  operation.Settings = settings
  Using objectSpace = Me.Application.CreateObjectSpace()
    operation.Execute()
  End Using
End Sub

The differences between Export Operation and Import Operation are insignificant. ImportContactAction_Execut event handler creates settings object of the ImportSettings type, but parameters are identical to the ExportSettings. DataSetDataSource  object requires to set SourcePath property. Then the Import Operation object is created and the Execute() method is called.

Run the application (whether Windows Forms or ASP.NET) and click ExportContactAction button.

data_management_10

Then, click the Refresh button. You will see a new Contact object imported from XML Data Source.

data_management_11