StrataFrame Forum

BO Serialization Examples C#

http://forum.strataframe.net/Topic11855.aspx

By Alex M. Lana - 10/4/2007

Greetings,



I've read several posts about SF serialization, but could not find one complete solution to help me figure out a way of working an online/offline application.

An WMS mobile application or even a simple Point of Sale, that works online with ES, but in case of failure, changes the stream to local storage, binary or XML. Using serialization.



My main question is the approach to BO binding and ES connection status control.

May I have to use 2 BO(online/offline) or may I change the stream type on-the-fly according to ES connection status?



If you could point a pretty simple example in C# will help a lot.

Thanks for the attention,



Alex
By Trent L. Taylor - 10/5/2007

Alex,

There are many different ways to approach this.  First, you could use a local database such as SQLAnywhere while disconnected from the network or internet, talk to a ES server remotely, or simply serialize the BOs to disk the restore them and save once you get back to a network that can access the database.  The problem with the latter is that you may have static tables or information that needs to be queried while in the field.  In this case you would first have to perform a query to populate the business objects before you leave the office.  In this scenario, you would be better off using the ES if there will always be an internet connection available on the laptop, or creating a local SQLAnywhere or SQLExpress database that is used when access to the main database is not available.

If you choose to go down the serialization route, the logic to serialize and deserialize is actually pretty simple.  All business objects have a SerializeToStream and SerializeToByteArray method.  Below is a sample of how to use the SerializeToStream.

void SaveToDisk()
        {
            //-- Establish Locals
            System.IO.FileStream output = new System.IO.FileStream(@"c:\SavedBO.bin",  System.IO.FileMode.Create,  System.IO.FileAccess.ReadWrite);

            //-- Save the BO to disk
            myBO1.SerializeToStream(output);

            //-- Close the stream
            output.Close();
        }
        void ReadFromDisk()
        {
            //-- Establish Locals
            System.IO.FileStream input = new System.IO.FileStream(@"c:\SavedBO.bin",  System.IO.FileMode.Open,  System.IO.FileAccess.Read);

            //-- Read the BO from the saved state into an instance
            myBO1 = (MyBO)MicroFour.StrataFrame.Business.BusinessLayer.DeserializeBusinessObject(input);

            //-- Close the stream
            input.Close();
        }

As for swapping data sources, this can be done very easily.  Simply change the data source that is created in your applications SetDataSOurces (or someplace else if you have the need while the application is already running) in the program.cs file:

if inOffice
   {
   //-- Connect directly to the SQL Server
   DataBasics.DataSources.Add(new SqlDataSourceItem("", "connection string"));
   }
else
   {
   //-- Connect to an Enterprise Server
   DataBasics.DataSources.Add(new EnterpriseDataSourceItem("", "connection string"));
   }
By Alex M. Lana - 10/5/2007

Thanks Trent,

I've managed to store/update/retrieve/synchronize using datasets as encripted XML, loaded on-the-fly onto the BOs.

The question I have is how can I check database connection state with SF layers.

Does SF has something like getConnetionState.

Basically need to catch the DataSource connection error, when remote data is offline, so I can switch to offline datasets.

I was thinking of something like:



TcpClient tcpClient = new TcpClient ();

tcpClient.Connect ("mySQLserver.com", 1433);

//Connection is present

tcpClient.Close();



But would rather not go this way, since I want to control the most using SF.



Thanks for the attention, regards,



Jacob... sorry... Alex
By Greg McGuffey - 10/5/2007

I've checked if the db was available just using a normal ADO.NET Connection object. I retrieved the connection string from the DataLayer.DataSources dictionary, then just attempted to open it (inside a try/catch). This keeps connection info encapsulated within the DataSources dictionary.



This won't work with Enterprise Server, as it isn't using an ADO connection.



Just an idea for you to ponder! BigGrin
By StrataFrame Team - 10/8/2007

With the ES, you could try to do a fill on a business object... something with a WHERE clause of 0=1 so no records are returned is fine.  If you get an exception, then you need to swap to local datasets.  You can do the same for a the SqlDataSourceItem, too, so if you're using ES/Sql and swapping back and forth, then this would be the way to go.