NAVmoble - the pocket-sized ERP
Optimized for Microsoft Dynamics NAV and Windows Mobile powered devices

Thursday, October 21, 2010

NAVmobile HowTo: Consuming Business Objects

I had recently several requests on writing a little bit more about the NAVmobile customization approach from a dev point of view. So I decided to make a short how-to serie in order to illustrate some of the concepts. Most of it can be found in the documentation, but I hope that I will clarify some of the missing points.

NAVmobile provides the mobile developers with mobile object relational mapping API. It can be used by the developers to consume data stored on the device or data stored on remote storage.
The ORM implementation found in NAVmobile is very useful when dealing with standart CRUD operations over local and remote data in terms of business objects. It has certain limiations mainly because of the resource-related limitations found on the mobile devices these days.
It is not intended to provide complete replacement of the standart ADO .NET implementation found in .NET Compct Framework. However, it gives clean and quick way to deal with the CRUD operations and the various logical and tehcnical challenges related to the synchronization of the data. Each CURD operation through the NAVmobile ORM API performs certain behind-the-scene operations over the locally stored data, which allows later the synchronization engine to properly propagate the changes to the remote side ( the ERP ).
The developer can easily go down to the pure SQL level and deal directly with the mobile storage to perform even advanced data operations.

Local Business Objects
NAVmobile provides framework for bi-directrional data synchronization between the device and virtually any remote data storage ( ussually ERPs like Microsoft Dynamics NAV or Microsoft Dynamics AX ). The existnce of local mobile storage allows the application to be used in occatioanlly connected environments where the data is stored on the device and then synchronized when a connection exists. The developer only needs to declare the business objects and then the NAVmobile framework is taking care of the initial mobile storage provisioning, synchronization and other low level technical tasks.
The business objects must be declared in the NAVmobile business object descriptor file (described in the documentation ) and then a correpsonding .NET Compact Framework class must be created.
Let's pretend we have the following business object declaration:

public class Customer:BusinessObjectBase
{
public string No;
public string Title;
public double CreditBalance;
public double CreditLimit;
}
If we need to fetch all customers with positive credit blance , we can use the following code:
IList customers = BusinessObjectBase.FetchAll("CreditBalance>0");
foreach(Customer customer in customers)
{
...
}
Creating new customer object is easy as:

Customer customer = new Customer();
customer.No="567";
customer.Title="My first customer";
customer.Add();
and then fetching only one customer by primary key can be done like this:
Customer customer = BusinessObjectBase.Fetch("567");
MessageBox.Show(customer.Title);
Updating a customer:
customer.Title="This is not my first customer";
customer.Update();

...and then deleting it:
customer.Delete();

Using this approach the developer can quicky perform CRUD operations over the business objects stored on the mobile device.

Each CRUD operation over the business objects is propagated later through the sync engine to the remote storage ( the ERP ) during the device synchronization session.

Remote Business Objects
Remote business objects can be consumed by using the NAVmobile LiveLinkEngine, which serves as an on-line connection media between the mobile device and the remote storge ( ERP, Microsoft SQL Server , etc. ). This functionality requires the existance of TCP/IP connection between the device and the NAVmobile Services.

Assuming we need to use the same Customer business object from a remote storage , we can use the following code in order to fetch all ther customers:
IList customers = LiveDataLinkService.FetchAll("CreditBalance>0");
foreach(Customer customer in customers)
{
....
}
The FetchAll method will causes the NAVmobile framework to perform invocation of the NAVmobile Services LiveDataLink Web Service in order to fetch the data from the remote sotrage. The data will be dehydrated into the Customer business object collection and ready for local processing.

Addin a new remote customer is easy too:

Customer customer = new Customer();
customer.No="567";
customer.Title="My first customer";
LiveDataLinkService.Add(customer);
The Add method will send the customer business object to the server where it will be stored.
We can use similiar approach to fetch one customer:

Customer customer = LiveDataLinkService.Fetch("567");
MessageBox.Show(customer.Title);

and then to change and update it:
customer.Title="here somes a new name";
LiveDataLinkDataService.Update(customer);

Deleting the customer is also easy:

LiveDataLinkDataService.Delete(customer);


The ORM module can be used also in the NAVmobile templating engine to quickly build mobile reports diplsayed and printed on the device. Local and remote data can be mixed together in a single report. More on this subject later.


Links:

NAVmobile/AXmobile web site

No comments: