...
This method is invoked when the service is stopping.
Common Mistakes
Some of the more common mistakes which can cause significant issues are:
Blocking a SQL Transaction with a UI prompt
An entire organisation can be ground to a halt by displaying a messagebox or dialog at the wrong time.
If a plugin is awaiting user interaction and there are un-committed transactions pending, then other users will be blocked from reading or writing to the same tables or pages of the database.
The SaveEnding event of all business logic objects is raised when the business logic has created a SQL Transaction, issued SQL inserts, updates or deletes and not yet committed the transaction.
...
Understanding Business Logic Behaviours
When writing plugins to interact with business logic objects, particularly handling events raised by these objects, it is important to understand the behaviours and sequence of events.
The Save
When the Save() method of a business logic object is invoked, the following occurs in order:
If no SQL Transaction is already started, one is started
The SaveStart event is raised. It is safe to perform long running actions and UI interactions
SQL commands are issued to INSERT, UPDATE and DELETE
The SaveEnding event is raised. It is unsafe to perform long running actions and UI interactions
The SQL transaction, if started by this business logic object is committed
The SaveEnd event is raised. It is safe to perform long running actions and UI interactions
If any exception is thrown during the save (even by a plugin) then if the business logic object had started a transaction, then it will RollBack that transaction and all SQL commands issued since the transaction started will be undone.
Plugins that wish to use the same SQL transaction to update data to ensure consistency should do this in either the SaveStart or SaveEnding events. If using the SaveEnding event then ensure no user interaction is made.
The Read
When the Read(string RecID) of a business logic object is invoked, the following occurs in order:
The ReadStart event is raised
The Clear() method is invoked to clear contents of properties, private members and collections - this will in turn raise the ClearStart and then ClearEnd events.
The data is read
The ReadEnd event is raised
JiwaCollections
All collections or lists in Jiwa are of type JiwaCollection. Each item in the collection inherit from the JiwaCollectionItem class.
Sales order lines, purchase order lines, debtor delivery addresses, Bill input items are all JiwaCollections.
There are several events of JiwaCollections which can be subscribed to, via the public property of the business logic object.
Note that the JiwaCollection events are not raised during the normal read of a business logic object.
Adding
The Adding event is raised when an item is being added to the collection. A CancelEventArgs argument allows this add to be cancelled.
Added
The Added event is raised when an item is added to the JiwaCollection. The item argument is the item which was added.
The code below shows a handler for a sales order lines, displaying a message box of the PartNo property of the sales order line when added to the collection.
Code Block | ||
---|---|---|
| ||
public class FormPlugin : System.MarshalByRefObject, JiwaFinancials.Jiwa.JiwaApplication.IJiwaFormPlugin
{
public override object InitializeLifetimeService()
{
// returning null here will prevent the lease manager
// from deleting the Object.
return null;
}
public void SetupBeforeHandlers(JiwaFinancials.Jiwa.JiwaApplication.IJiwaForm JiwaForm, JiwaFinancials.Jiwa.JiwaApplication.Plugin.Plugin Plugin)
{
}
public void Setup(JiwaFinancials.Jiwa.JiwaApplication.IJiwaForm JiwaForm, JiwaFinancials.Jiwa.JiwaApplication.Plugin.Plugin Plugin)
{
JiwaFinancials.Jiwa.JiwaSalesUI.SalesOrder.SalesOrderEntryForm salesOrderForm = (JiwaFinancials.Jiwa.JiwaSalesUI.SalesOrder.SalesOrderEntryForm)JiwaForm;
salesOrderForm.SalesOrder.SalesOrderLines.Added += delegate(JiwaFinancials.Jiwa.JiwaSales.SalesOrder.SalesOrderLine salesOrderLine)
{
System.Windows.Forms.MessageBox.Show(String.Format("Added '{0}'", salesOrderLine.PartNo));
};
}
} |
Changed
The Changed even is raised when a property if the item changes. The PropertyChangedEventArgs argument has a PropertyName property which contains the name of the property which changed.
Removing
The Removing event is raised when an item is being removed from the JiwaCollection.
Removed
The Removed event is raised when an item is removed from the JiwaCollection.
Common Mistakes
Some of the more common mistakes which can cause significant issues are:
Blocking a SQL Transaction with a UI prompt
An entire organisation can be ground to a halt by displaying a messagebox or dialog at the wrong time.
If a plugin is awaiting user interaction and there are un-committed transactions pending, then other users will be blocked from reading or writing to the same tables or pages of the database.
The SaveEnding event of all business logic objects is raised when the business logic has created a SQL Transaction, issued SQL inserts, updates or deletes and not yet committed the transaction.
Awaiting user interaction, or any long running operation - such as I/O or external API’s should not be done in handlers of the SaveEnding event.
DRY (Don’t Repeat Yourself)
If there is code that is going to be needed by multiple plugins don’t repeat the code in multiple places, instead create a plugin and create a public class within this plugin to hold the code and then add a plugin reference to each of the plugins that need to access this code and call this new class. By doing this you don’t have to maintain multiple copies of the same code.