Search Results for

    Show / Hide Table of Contents

    For the Save Extender Add New Items project you will need the following references In this example the company entity will be used.

    using System.Text;
    using LemonEdge.API.Core.Data;
    using LemonEdge.API.Core.Extensions.SaveExtension;
    using LemonEdge.API.Descriptors;
    using LemonEdge.API.Entities.DataIntegration;
    using LemonEdge.API.Entities.FinancialServices.Products.FX;
    using LemonEdge.Utils.Database;
    using Microsoft.Extensions.Logging;
    

    The IEntityUpdaterSaveExtender allows consumers to hook in to the platform's save context before and after a save has occurred.

    public class AddDataSourceSaveExtender : IEntityUpdaterSaveExtender
    

    This is the contructor for the save extender.

    private static readonly Random random = new((int)DateTime.Now.Ticks);
    private readonly ILogger<AddDataSourceSaveExtender> _logger;
    
    public AddDataSourceSaveExtender(ILogger<AddDataSourceSaveExtender> logger)
    {
        _logger = logger;
    }
    

    The SaveExtenderOptions allows us to modify the behavior of the save extender. SaveExtenderOptions.RunAfterSaveOnSeparateThread will run the OnAfterSave call on a separate thread instead of awaiting the Task. SaveExtenderOptions.IgnoreErrors will ignore any exceptions thrown by the OnBeforeSave or OnAfterSave methods. Otherwise, exceptions will be bubbled up to the caller, potentially short circuiting the save process.

    public SaveExtenderOptions Options => SaveExtenderOptions.Default;
    

    The OnBeforeSave call is executed just prior to a database commit of the entities marked for change by an EntityOperation

    public async Task OnBeforeSave(ISaveContextPrior context)
    {
        try
        {
            const string myDataSourceType = "CurrencyExternalDataSource";
            var dataSourceTypes = await context.Context.GetItems<IDataSourceType>()
                .Where(nameof(IDataSourceType.Name), SQLOperator.Equals, myDataSourceType)
                .Execute(context.Context);
            var dataSourceType = dataSourceTypes.FirstOrDefault();
    
            if (dataSourceType == null)
            {
                _logger.LogWarning("Could not find data source type '{dataSourceType}'", myDataSourceType);
                return;
            }
    
            var inserts = context.GetItems<ICurrency>(EntityOperation.Insert).ToList();
    
            foreach (var item in inserts)
            {
                var descriptor = EntityDescriptorFactory.Instance.GetDescriptor(item);
    
                var dataSourceForEntity = await context.AddNewItem<IDataSource>();
                dataSourceForEntity.DataSourceTypeID = dataSourceType.ID;
                dataSourceForEntity.EntityTypeID = descriptor.TypeID;
                dataSourceForEntity.EntityID = item.ID;
                dataSourceForEntity.SourceID = RandomString(10);
    
                context.UpdateItemProperty(item, nameof(ICurrency.ThreeLetterCode), RandomString(3));
    
                _logger.LogInformation(
                    "Update currency {currencyName} code to {currencyCode} and added data-source ID {dataSourceId}",
                    item.Name, item.ThreeLetterCode, dataSourceForEntity.SourceID);
            }
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "An error occurred during OnBeforeSave");
        }
    }
    

    The OnAfterSave call is exectued just after the database commit of the entities marked for change by an EntityOperation.

    public Task OnAfterSave(ISaveContextAfter context)
    {
        return Task.CompletedTask;
    }
    

    RandomString generates a random string for use in OnBeforeSave() only for the use in the example.

    private string RandomString(int Size)
    {
        var builder = new StringBuilder();
        char ch;
        for (var i = 0; i < Size; i++)
        {
            ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
            builder.Append(ch);
        }
    
        return builder.ToString();
    }
    
    In this article
    Back to top © LemonEdge Technologies. All rights reserved.