Dynamic Service Brokers are used when we want to create a re-usable Broker that can expose different instances of the same technology. They are Dynamic because the broker will discover the Schema of the provider when a Service Instance of the Broker is registered or refreshed.
Here is an example: suppose you wanted to create a Service Broker that can interact with MySQL databases. The Broker will always interact with the same technology (MySQL), but we cannot predict what the underlying Schema of a particular instance of a MySQL database will look like. You therefore need to create a Dynamic Service Broker that will query a particular instance of a MySQL database when a Service Instance is registered or refreshed. The discovery process will use some kind of query to determine the various tables, views and procedures available in that MySQL database, and then describe these in Service Object terms.
A Dynamic Service Broker is implemented very similarly to a Static Service broker, but with one key difference: instead of decorating the Classes, Properties and Methods that you want to add to your Service Broker, you will dynamically generate Service Objects and add Properties and Methods when the DescribeSchema() method is called.
Custom Brokers are standard .NET Class libraries (.dlls). If you have an existing class that you want to expose as a Service Broker, you can edit the code in your class and add the necessary implementation or alternatively reference your assembly in a new class project. If you are referencing a custom assembly, remember that you will need to copy the referenced assembly to the K2 server along with the Service Broker .dll file. (For a Dynamic Service Broker, the chances are good that you will need to reference an external assembly, so keep this dependency in mind when deploying.)
Add a reference to the .NET library SourceCode.SmartObjects.Services.ServiceSDK.dll which is located by default in C:\Program Files (x86)\K2 blackpearl\Bin. This file is available on the K2 server and in a machine where the K2 design tools are installed. When deploying your project you don’t need to copy this assembly to the K2 server, since it already exists on the K2 server.
After adding the reference, import the SourceCode namespaces that you will need to use in your project:
Next inherit the base class SourceCode.SmartObjects.Services.ServiceSDK.ServiceAssemblyBase in your project. We recommend creating a separate .cs class for the base class to separate the Broker code implementation from the class you will expose as a ServiceObject.
When you inherit this base class in your class, you will override three basic methods for your service broker: GetConfiguration, DescribeSchema and Execute.
Example: Importing namespaces and inheriting from the base class:
The next step is to add any configuration settings to your broker. Dynamic Service Brokers usually need additional configuration settings, since you would normally be able to register the same Service Type against different instances of the same Provider technology.
You add configuration values by overriding and implementing the base class' GetConfigSection() method. For each configuration setting in your Service Broker, add it to the Service Configuration items. Examples below.
You can add as many configuration settings as you like; just keep adding to the Service.ServiceConfiguration collection. At runtime, you can read the configuration values like this:
Example: Implementing configuration settings:
In a dynamic Service Broker, you will generate Service Objects dynamically in the DescribeSchema() method of the ServiceAssemblyBase base class. This method is called when you register or refresh a Service Instance, and it is used to list the available Service Objects for a Service Broker. Remember that you will manually register or refresh a Service Instance to call this method: K2 does not "poll" or otherwise automatically update the list of Service Objects in a Service Broker. This is especially important to know when your Providers Schema is dynamic: you may need to refresh your Service Instance from time to time to pick up changes to the Providers Schema.
K2 will not automatically update SmartObjects that use the Provider. If the Schema changes significantly, especially if Properties or Methods are changed or removed, there is a real possibility that existing SmartObjects can be broken. Generally, adding properties or methods is safe, but remember that you will need to update your SmartObject definitions manually if you want to take advantage of any new Properties or Methods in your Provider.
We recommend implementing the DescribeSchema in two separate methods. The override implementation of DescribeSchema() gathers configuration settings, sets up Type Mappings and sets up the Service Instance after the Service Objects have been discovered and added. The actual discovery of the Schema is performed by a helper method. The helper method will discover the Objects in the Provider and then add Service Objects for each object that we want to expose in the Service Broker.
Example: Implementing the DescribeSchema method and adding type mappings:
The actual discovery of the Schema is performed by a helper method. The helper method will discover the Objects in the Provider and then add Service Objects for each object that we want to expose in the Service Broker. In the example below, we are querying a .NET DataSet to determine the Service Objects that should be added. For a different provider, the discovery process may be different.
Example: Adding Service Objects:
Example: Adding Service Object Properties:
Example: Adding Service Object Methods:
Example: Adding the generated Service object to the ServiceObjects collection:
Finally, override and implement the Execute() method in the ServiceAssemblyBase base class. This method is called at runtime whenever a SmartObject interacts with your Service Broker. Note that, regardless of the method called this is always the entry point into your Broker, so you will need to examine the request to determine which Service Object and Method was requested, and what the input properties, parameters and return properties are. You will then usually call out to helper methods to perform the interaction with the Provider.
Example: The Execute() method (Note: only the Read method is shown here):
Video | Links | Learn | Support |
No videos found for this article K2 on YouTube
No Additional links found for this article
No self-learning content for this article Try some scenarios...
No relevant support links available for this article
|