protected void ProcessStateChange(LocalPluginContext context)
        {
            var target = context.GetTarget <dlab_AutoNumbering>();

            switch (target.statecode)
            {
            case dlab_AutoNumberingState.Active:
                // Register Plugin if it doesn't exist
                context.Trace("Processing Active State Request");
                var settings = context.OrganizationService.GetEntity <dlab_AutoNumbering>(context.PrimaryEntity.Id);
                if (settings.dlab_PluginStepId == null || context.SystemOrganizationService.GetEntityOrDefault <SdkMessageProcessingStep>(Guid.Parse(settings.dlab_PluginStepId)) == null)
                {
                    RegisterIncrementor(context);
                }
                break;

            case dlab_AutoNumberingState.Inactive:
                // Unregister Plugin
                context.Trace("Processing In-Active State Request");
                UnregisterIncrementor(context);
                break;

            case null:
                context.Trace("Processing Non-State Related Change");
                AssertNonDuplicateId(context, target);
                break;

            default:
                throw new EnumCaseUndefinedException <dlab_AutoNumberingState>(target.statecode.GetValueOrDefault());
            }
        }
        protected override void ExecuteInternal(LocalPluginContext context)
        {
            var target             = context.GetTarget <Entity>();
            var autoNumberManagers = AutoNumberManagersByEntity.GetOrAddSafe(_settingLock, target.LogicalName,
                                                                             logicalName =>
                                                                             context.SystemOrganizationService.GetEntities <dlab_AutoNumbering>(
                                                                                 dlab_AutoNumbering.Fields.dlab_EntityName, logicalName).
                                                                             Select(s => new AutoNumberManager(s)).ToArray());

            if (autoNumberManagers.Length == 0)
            {
                throw new InvalidPluginExecutionException("No Auto-Number Settings found for Entity " +
                                                          target.LogicalName);
            }

            // ReSharper disable once ForCanBeConvertedToForeach - Not sure if a foreach enumeration is thread safe
            for (var i = 0; i < autoNumberManagers.Length; i++)
            {
                var manager = autoNumberManagers[i];
                // Lock here so the settings entity won't be updated in the middle of processing the record
                lock (manager)
                {
                    SetAutoNumber(context, target, manager);
                }
            }
        }
        protected void UnregisterIncrementor(LocalPluginContext context)
        {
            var settings = context.OrganizationService.GetEntity<dlab_AutoNumbering>(context.PrimaryEntity.Id);
            Guid pluginStepId;
            if (string.IsNullOrWhiteSpace(settings.dlab_PluginStepId) || !Guid.TryParse(settings.dlab_PluginStepId, out pluginStepId))
            {
                return;
            }
            context.SystemOrganizationService.TryDelete(SdkMessageProcessingStep.EntityLogicalName, pluginStepId);

            if (context.Event.Message == MessageType.Delete)
            {
                return;
            }

            // Could be called from deactivate update.  Clear Plugin Setp
            var target = context.GetTarget<dlab_AutoNumbering>();
            target.dlab_PluginStepId = null;
        }
        protected void RegisterIncrementor(LocalPluginContext context)
        {
            var settings = context.CoallesceTargetWithPreEntity<dlab_AutoNumbering>();
            var createMessage = GetCreateMessageId(context);

            var messageProcessingStep = new SdkMessageProcessingStep
            {
                ModeEnum = sdkmessageprocessingstep_mode.Synchronous,
                Name = $"IdGenerator for the {settings.dlab_EntityName} Entity",
                Rank = settings.dlab_PluginExecutionOrder.GetValueOrDefault(1),
                SdkMessageId = createMessage,
                SdkMessageFilterId = GetMessageFilterId(context, settings.EntityName, createMessage.Id),
                EventHandler = GetPluginId(context),
                StageEnum = sdkmessageprocessingstep_stage.Preoperation,
                SupportedDeploymentEnum = sdkmessageprocessingstep_supporteddeployment.ServerOnly
            };

            var target = context.GetTarget<dlab_AutoNumbering>();
            target.dlab_PluginStepId = context.SystemOrganizationService.Create(messageProcessingStep).ToString();
        }