Pigeon Agents Development Demo Agents Globals Class - DatasmithSA/Pigeon-Voice-Training GitHub Wiki
Although this class is not strictly necessary it is recommended to have a class holding global data that is accessible to all Pigeon Voice agents in a library. For this demo the purpose of this class is to load the agents' settings file into an AgentsSettings object. We will use a singleton pattern to implement this class i.e. there will only ever be a single instance (object) of this globals class.
After creating an agents library, and creating the Settings Class, follow the instructions below to create the globals singleton class:
- Add a new class in the agents library project and call it AgentsGlobals.
- Add a reference to the Figlut.Server.Toolkit.dll to your agents library project.
- Add a using clause to the top of the new the AgentsGlobals class file: using Figlut.Server.Toolkit.Utilities;
- Below is the code for the complete AgentsGlobals class. The important property to notice is the Settings property. When called the first time by an agent, it will use the Figlut GOC.Instance.GetSettings(true, true) method to read the AgentsSettings.xml file and load it into an AgentsSettings object, accessible via the Settings property of this AgentsGlobals singleton instance. For example an agent could access the connection string settings by calling: AgentsGlobals.Instance.Settings.DatabaseConnectionString. Once the settings file has been loaded the Settings property will simply return the AgentsSettings object every time without reading (deserialising) the file again.
#region Using Directives
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Figlut.Server.Toolkit.Utilities;
#endregion //Using Directives
public class AgentsGlobals
{
#region Singleton Setup
private static AgentsGlobals _instance;
public static AgentsGlobals Instance
{
get
{
if (_instance == null)
{
_instance = new AgentsGlobals();
}
return _instance;
}
}
#endregion //Singleton Setup
#region Constructors
private AgentsGlobals()
{
}
#endregion //Constructors
#region Fields
private AgentsSettings _settings;
#endregion //Fields
#region Properties
public AgentsSettings Settings
{
get
{
if (_settings == null)
{
_settings = GOC.Instance.GetSettings<AgentsSettings>(true, true);
}
return _settings;
}
}
#endregion //Properties
}