Partager via


Creating a Custom Rules Store

Retired Content

This content and the technology described is outdated and is no longer being maintained. For more information, see Transient Fault Handling.

patterns & practices Developer Center

The Autoscaling Application Block includes two rules store implementations that you can select from in the block configuration: an XML rules store in Microsoft Azure blob storage and an XML rules store on the local file system. The first is intended for use when you host the block in Azure, the second when you host the block on-premises. Both share the same XML schema.

You can create your own custom rules store, for example to store the rules in a SQL Server database. In this scenario, both the location of the store and the format of the stored rules would differ from the two existing rules store implementations.

A custom rules store implementation must implement the IRulesStore interface shown in the following code sample.

public interface IRulesStore
{
    event EventHandler<EventArgs> StoreChanged;

    IEnumerable<Rule> GetRules();

    IEnumerable<Operand> GetOperands();
}

You should notify the block whenever the contents of your rules store change by using the StoreChanged event so that the block can load the new rule definitions. The GetRules method returns a collection of Rule objects, and the GetOperands method returns a collection of Operand objects.

Note

The block treats the rules store as a read-only store. If you want to provide a mechanism for editing the rules in your store through code, you should design and implement this functionality yourself. The Autoscaling Application Block provides this functionality for the XML rules stores in the RuleSetSerializer class.

If you want to pass custom configuration parameters to your custom rules store, your custom rules store class should have a constructor that takes a single parameter of type NameValueCollection, as shown in the following code sample. Note the use of the ConfigurationElementType attribute to decorate the class.

[ConfigurationElementType(typeof(CustomRulesStoreData))]
public class CustomRulesStore : IRulesStore
{
    public CustomRulesStore(NameValueCollection attributes)
    {
        ...
    }

    public IEnumerable<Rule> GetRules()
    {
        ...
    }

    public IEnumerable<Rules.Conditions.Operand> GetOperands()
    {
        ...
    }

    public event EventHandler<EventArgs> StoreChanged
    {
        ...
    }
}

You must deploy the assembly that implements your custom rules store with the Autoscaling Application Block.

You must tell the Autoscaling Application Block about your custom rules store by using the Enterprise Library configuration tool. The following procedure shows how to configure the block to use a custom rules store.

Configuring the Autoscaling Application Block to use a custom rules store

  1. To change the rules store implementation to use a custom rules store, click the plus sign icon at the top right of the Rules Store panel and then click Set Rules Store.

    Hh680933.DD7077D7E3FFDB91FEE7F1B6E790631C(en-us,PandP.50).png

  2. To store your rules in a custom rules store, click Use Custom Rules Store, and then click Yes to confirm the change. Use the Type Name box to identify the type of your custom rules store implementation.

    Hh680933.F4AD6FE1549E051F69C751DCE016AD46(en-us,PandP.50).png

  3. You can provide any additional configuration data that your custom rules store requires by adding attributes. Each attribute is a key/value pair. The block passes all the key/value pairs to the constructor of your custom rules store class.

    Hh680933.26B72643DF36CDF68BFAEFEB66F1F726(en-us,PandP.50).png

Next Topic | Previous Topic | Home

Last built: June 7, 2012