Skip to content

Classification

Duncan Jones edited this page Oct 10, 2020 · 8 revisions

Classification

A classification is a piece of code that runs over the event stream for an entity in order to decide whether that entity instance is included or excluded from the group rule of the classifier. It can be considered analoguous to a WHERE clause in an SQL query.

For each event in the stream it needs to decide (a) am I interested in this kind of event and if so (b) what does this mean to group classification.

Example

The following classification runs over the event stream for a command to classify if the command is completed.

 /// <summary>
 /// A classification to denote that a command execution has completed
 /// </summary>
 public class Completed
     : ClassificationBase,
     IClassifyEventType<CommandHandler.Events.Completed>
 {
     public Classification.ClassificationResults ClassifyEventInstance(Events.Completed eventInstance)
     {
         return Classification.ClassificationResults.Include;
     }
 }

In addition to returning the current classification of the entity (by reading its event stream) the classification processor also returns values to indicate if the entity was every considered "included" or "excluded" from the classification to allow for business decisions based on "x was ever in grouping y".

A classification is run by a Classification Processor and you can specify the Event Stream and classifier to use by parameter binding with a Classification attribute.

A classification differs from an projection in that it can also accept parameters. This can be used for functionality like "List all accounts where the balance is greater than $1000"

Classification name attribute

A classification can be referred to by its class name or by using a ClassificationName attribute:

    /// <summary>
    /// A classification to denote that a query execution has completed
    /// </summary>
    [ClassificationName("Query Completed") ]
    public class Completed: 
        ClassificationBase,
        IClassifyEventType<Completed>
    {
      // - - - 8< - - - - - - - - 

Get all instances

In order to turn a classification into a group we need to get the complete list of all the Entity Instance Identifier of a particular Entity Type to run a classification over.

The Classification class provides a GetAllInstanceKeys method to do this. For example to list all the bank account numbers:

   [FunctionName("GetAllAccounts")]
   public static async Task<HttpResponseMessage> GetAllAccountsRun(
     [HttpTrigger(AuthorizationLevel.Function, "GET", Route = @"GetAllAccounts/{asOfDate?}")]HttpRequestMessage req,
      string asOfDate,
      [Classification("Bank", "Account", "ALL", nameof(InterestAccruedToday))] Classification clsAllAccounts)
   {
       // Set the start time for how long it took to process the message
       DateTime startTime = DateTime.UtcNow;

       IEnumerable<string> allAccounts = await clsAllAccounts.GetAllInstanceKeys(null);
       System.Text.StringBuilder sbRet = new System.Text.StringBuilder(); 
       if (null != allAccounts )
       {
           foreach (string accountNumber in allAccounts)
           {
               if (! (sbRet.Length == 0))
               {
                   sbRet.Append(",");  
               }
               sbRet.Append(accountNumber);  
           }
       }

       return req.CreateResponse<FunctionResponse>(System.Net.HttpStatusCode.OK,
               FunctionResponse.CreateResponse(startTime,
               false,
               $"Account numbers: {sbRet.ToString()}  "),
               FunctionResponse.MEDIA_TYPE);
   }