# Workflow Module

#### WARNING

Starting with DevExpress Version 25.1, you should migrate to the new Llamachant.ExpressApp modules instead. Support for this module will end soon.

**Supported Platforms:** ✅ WinForms, ✅ WebForms, ✅ Blazor

This module allows you to create workflows that send emails, send reports, and invoke methods on objects.

It supports scheduled or event-triggered execution depending on your workflow configuration.

## Getting Started

1. Install `LlamachantFramework.Workflow.XPO` or `LlamachantFramework.Workflow.EF` from NuGet into your solution
2. Include the correct module in the `RequiredModuleTypes` collection
3. (Optional) Configure `TemplateFormattingFactory.SetDefaultFormattingType` for complex HTML templates

```csharp
   public override void Setup(XafApplication application) {
       base.Setup(application);

this.RequiredModuleTypes.Add(typeof(LlamachantFramework.Workflow.WorkflowModuleXPO));
       // OR
       this.RequiredModuleTypes.Add(typeof(LlamachantFramework.Workflow.WorkflowModuleEF));

TemplateFormattingFactory.SetDefaultFormattingType(TemplateFormattingFactory.TemplateFormattingType.DoubleBrace);
   }
   ```

#### IMPORTANT

**Workflow Engine Required** – Create a service, application, or logic that leverages the Workflow Service Engine for execution.

## Entity Framework Core-Based Applications

If using EF Core, add the following `DbSet` entries in your `DbContext`:

```csharp
   public class MySolutionEFCoreDbContext : DbContext {
       public DbSet<WorkflowDefinition> WorkflowDefinition { get; set; }
       public DbSet<WorkflowInstance> WorkflowInstance { get; set; }
       public DbSet<WorkflowEmailSettings> WorkflowEmailSettings { get; set; }
       public DbSet<WorkflowEmailTemplate> WorkflowEmailTemplates { get; set; }
   }
   ```

#### TIP

For EF: Encrypt `WorkflowEmailSettings.StoredPassword` field using your preferred provider.

## Setting up the Workflow Email Settings

The Workflow Email Settings allow SMTP configuration for sending workflow-triggered emails.

You can implement `IWorkflowEmailService` or use default settings.

## Workflow Email Templates

Create email templates and embed object data directly into HTML-based messages.

Use expressions like `{{FirstName}}` or `{{Address.State.Name}}`.

#### TIP

For double brace support, configure `TemplateFormattingFactory.SetDefaultFormattingType`.

## Workflow Definitions

Carefully configure frequency, conditions, and actions for your workflows to avoid errors like repeated execution or accidental deletion.

## Creating a Workflow Definition

Select a frequency:

- **One Time**
- **On Action Executed**
- **Always**

Actions available:

- **A**: Run this workflow against individual objects
- **B**: Invoke a method
- **C**: Send an email
- **D**: Attach a report
- **E**: Workflow Instances Grid

## Sample Scenarios

### Welcome Email

Send a one-time email when a record is created.

### Send a Report on a Schedule

Schedule and deliver a report via PDF or Excel.

### Update an Object

Use workflow to move an object into a new state.

### Run a Workflow When an Action is Clicked

Define a public `ActionName` and attach it to a button or action in the UI.

## Using Your Own Email Service

Extend `IWorkflowEmailService` and register your service in the module setup.

#### IMPORTANT

Register your service in `WorkflowModule.EmailServiceType` and related types.

```csharp
   public override void Setup(XafApplication application) {
       base.Setup(application);
       application.SetupComplete += Application_SetupComplete;
   }
   
   private void Application_SetupComplete(object sender, EventArgs e) {
       var application = (XafApplication)sender;
       var module = LlamachantFramework.Workflow.WorkflowModule.FindModule(application.Modules);

module.EmailServiceType = typeof(DummyEmailService);
       module.EmailSettingsType = typeof(DummyEmailSettings);
       module.WorkflowDefinitionType = typeof(MyWorkflowDefinition);
       module.WorkflowInstanceType = typeof(MyWorkflowInstance);
   }
   ```
   
   ```csharp
   public class DummyEmailService : IWorkflowEmailService {
       public byte[] GetExportedReport(...) => new byte[0];
       public void SendEmail(...) { }
   }
   ```

## Extending Our Email Service

```csharp
   public class ExtendedEmailService : WorkflowEmailService {
       public override void SendEmail(...) {
           base.SendEmail(...);
       }
   }
   ```

#### WARNING

Starting with DevExpress Version 25.1, you should migrate to the new Llamachant.ExpressApp modules instead. Support for this module will end soon.
