You can use the legacy MonoAndroid sample to run a model-driven Android client against a persistence server and verify that changes made on the phone and in a WPF client are shared.
What this sample demonstrates
The sample demonstrates three parts of an MDriven application working against the same persisted data:
- A MonoAndroid phone application that creates and queries model objects.
- A PersistenceServer that provides the shared persisted data.
- A WPF client that displays and edits the same information.
For example, you can create a Class1 object on the phone, refresh the WPF client, and see the newly created object. You can also change data in the WPF client, refresh on the phone, and query the updated server data.
This page describes the legacy MonoAndroid sample. For Xamarin-based mobile development, see Documentation:MDriven In Xamarin and Documentation:Getting MDriven benefits on devices.
Prepare the solution
- Open the solution file (
.sln) supplied with the ECO sample. - When Visual Studio reports missing projects, acknowledge the message.
- Read the sample's
ReadMe. It explains why projects are missing from the solution. - Remove the missing projects from the solution.
- Add references to the assemblies required by
MonoAndroidApplication1:- In the reference dialog, choose Browse.
- Locate the assemblies supplied in
PhoneBuilds.zip. - Add the required assemblies as references to
MonoAndroidApplication1.
- Configure the solution so that the required startup project starts when you debug. The demonstration requires the persistence server to be available before the phone client queries or updates data.
Run the Android client
- Press F5 to start debugging.
- When prompted to select an emulator image, choose MonoAndroid_API_10.
- Wait for the emulator to start, then select the deployed application.
The phone application opens with buttons that query and update the shared model data.
The persistence server presents the root directory of its persisted data. The accompanying WPF application is a second client for the same data and displays the information in the database.
Use the phone application and the WPF application together:
- In the phone application, create an object with the second button.
- Switch to the WPF application.
- Refresh the WPF application to retrieve the latest persisted data.
- Confirm that the object created on the phone is visible.
- Make a change in the WPF application.
- Return to the phone application and use its refresh/query button.
- Confirm that the phone now reports the updated server state.
A refresh matters because each client must retrieve current data from the persistence service before it can show changes committed by the other client.
Phone application code
Refresh and query the server
The first button refreshes the local state from the persistence service, runs an OCL query through the OCL persistence service, and counts all Class1 instances through the extent service.
void button_Click(object sender, EventArgs e)
{
EcoServiceHelper.GetPersistenceService(_ecospace).Refresh(true);
Button button = FindViewById<Button>(Resource.Id.MyButton);
IObjectList list = EcoServiceHelper.GetOclPsService(_ecospace).Execute(
"Class1.allinstances->select(a|a.Class2->notempty)");
int noOfC1ThatHasC2s = list.Count;
string extra = "";
if (list.Count > 0)
extra = list[0].GetValue<Class1>().Attribute1;
int noOfC1 = EcoServiceHelper.GetExtentService(_ecospace)
.AllInstances<Class1>()
.Count;
button.Text = "db updated, Now C1 Total: " + noOfC1.ToString() +
", Total of C1 that has C2's " + noOfC1ThatHasC2s.ToString() +
" " + extra;
}
The query
Class1.allinstances->select(a|a.Class2->notempty)
selects every Class1 instance whose Class2 association is not empty. The button text then reports both:
- The total number of
Class1objects. - The number of
Class1objects that have at least one relatedClass2object.
OCL is the expression language used in the query. Consult the available MDriven API reference for service and type details: Documentation:Api documentation.
Create and persist an object
The second button creates a new strongly typed Class1 object in the application's Ecospace, assigns Attribute1, and commits the change with UpdateDatabase().
void button2_Click(object sender, EventArgs e)
{
Button button2 = FindViewById<Button>(Resource.Id.MyButton2);
Class1 c1 = new Class1(_ecospace);
c1.Attribute1 = "Created on android by mono " +
DateTime.Now.ToLongTimeString();
button2.Text = "c1 created";
_ecospace.UpdateDatabase();
}
For example, if the button is pressed at 14:30:00, the new object's Attribute1 value includes Created on android by mono 14:30:00. After UpdateDatabase(), refresh the WPF client to see the object there.
Services used by the sample
| Service access | Purpose in this sample | Example |
|---|---|---|
GetPersistenceService(_ecospace)
|
Retrieves the persistence service used to refresh data from the shared server. | Refresh(true) retrieves current persisted state before querying it.
|
GetOclPsService(_ecospace)
|
Retrieves the service that executes an OCL query against persisted objects. | Finds Class1 objects with a non-empty Class2 association.
|
GetExtentService(_ecospace)
|
Retrieves the service that enumerates all instances of a model class. | AllInstances<Class1>().Count returns the total number of Class1 objects.
|
Troubleshooting
If the phone application cannot retrieve or update shared data, first verify that the persistence server is running and that both clients use the same server data. Then refresh the client that should display changes made by the other client.
If Visual Studio cannot load the solution because projects are missing, remove those projects as described by the sample ReadMe and ensure that MonoAndroidApplication1 has references to the assemblies from PhoneBuilds.zip.
