For the Basic Enity CRUD to funtion you need to following reference:
using System.Runtime.CompilerServices;
using LemonEdge.API.Client.CommonUI;
using LemonEdge.API.Core.Context.AddIns;
using LemonEdge.API.Core.Context.AddIns.Enums;
using LemonEdge.ClientService.Host;
using LemonEdge.EntityCRUDServices.Core;
using LemonEdge.Examples.ConnectingToLemonEdge;
using LemonEdge.Modules.AssetManagement.Entities.Funds;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Worker = LemonEdge.Examples.BasicEntityCreation.Worker;
Following from the basic connector, here, we demonstrate simple CRUD operations on core entities in the system. Similar to tne Basic Connector we need run the host.
try
{
// The host reads our application settings and spins everything up: https://learn.microsoft.com/en-us/dotnet/core/extensions/generic-host
var host = Host.CreateDefaultBuilder(args).ConfigureServices(ConfigureServices).Build();
host.Services.InitialiseLemonEdge();
host.Services.GetRequiredService<IAddInLoaderHelper>().Type = FrameworkType.Xamarin; //this will become FrameworkType.None
await host.RunAsync();
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
Just like in the Basic connector we have to register the services but to use CRUD operations we need to add the BaseEntityService. The BaseEntityService is detailed below.
void ConfigureServices(HostBuilderContext hostContext, IServiceCollection services)
{
ForceLoadAddIns();
// LemonEdge services required by an end-client are registered with a call to ConfigureApplication
services.ConfigureLemonEdge(hostContext.Configuration);
// Your own services can be appended here, along with the connector to LemonEdge
services.AddScoped<Connector>();
services.AddSingleton<IConnector>(sp => Connector.Instance ??= sp.GetRequiredService<Connector>());
services.AddSingleton<IBaseEntityService, BaseEntityService>();
services.AddSingleton<IAuthenticationService, AuthenticationService>();
// Finally, we need to define the application host service for start-up
services.AddHostedService<Worker>();
}
This method forces the addin containing IFund to be loaded into memory.
[MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)]
Type[] ForceLoadAddIns()
{
return new[] { typeof(IFund) };
}
Next is the worker it has examples of CRUD operation done on the object of type ICurrency. These are the refernces need for the class
using LemonEdge.API.Client.CommonUI;
using LemonEdge.API.Core.Context;
using LemonEdge.API.Core.Data;
using LemonEdge.API.Entities.FinancialServices.Products.FX;
using LemonEdge.EntityCRUDServices.Core;
using LemonEdge.Examples.ConnectingToLemonEdge;
using LemonEdge.Modules.AssetManagement.Entities.Funds;
using LemonEdge.Utils;
using LemonEdge.Utils.Database;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
The class needs to inherit BackgroundService.
public class Worker : BackgroundService
Constructor receiving dependencies through injection.
connector: The LemonEdge "IConnector" enabling access to the system. entityService: The CRUD service for base entities. lifetime: The "IHostApplicationLifetime" needed to manage application lifetime. logger: The logger to output progress. authenticationService: The service for authenticating to LemonEdge.
Properties
private readonly IConnector _connector;
private readonly IBaseEntityService _entityService;
private readonly IHostApplicationLifetime _lifetime;
private readonly ILogger<Worker> _logger;
private readonly IAuthenticationService _authenticationService;
private UserInfo? _user;
Constructor
public Worker(IConnector connector, IBaseEntityService entityService, IHostApplicationLifetime lifetime, ILogger<Worker> logger, IAuthenticationService authenticationService)
{
_connector = connector;
_entityService = entityService;
_lifetime = lifetime;
_logger = logger;
_authenticationService = authenticationService;
}
ExecuteAsync runs this code to perform the Create operation.
private async Task<ICurrency> Create(CancellationToken cancelToken)
{
// create an entity updater to access the data layer
var context = await _connector.Create();
// create the currency instance
var ccy = await _entityService.Create<ICurrency>(context, _connector.LoggedInUser);
// update the object
ccy.Name = "ZDK";
ccy.ThreeLetterCode = "ZDK";
ccy.Symbol = "?";
// save the changes
await context.SaveChanges(cancelToken);
// return the instance
return ccy;
}
ExecuteAsync runs this code to perform the Read operation.
private async Task<ICurrency?> Read(string code, CancellationToken cancelToken)
{
// create an entity updater to access the data layer
var context = await _connector.Create();
// filter the entity set
var filtered = await _entityService.Read<ICurrency>(context, new QueryableFilter(nameof(ICurrency.ThreeLetterCode), SQLOperator.Equals, code), cancelToken);
// return the first item, if not null
return filtered.FirstOrDefault();
}
ExecuteAsync runs this code to perform the Update operation.
private async Task<ICurrency> Update(Guid id, string newCode, CancellationToken cancelToken)
{
// create an entity updater to access the data layer
var context = await _connector.Create();
// as per Read(...), you could also pass in the id and make the search simpler
var ccy = await _entityService.Update<ICurrency>(context, id);
// update the object
ccy.ThreeLetterCode = newCode;
// save the changes
await context.SaveChanges(cancelToken);
return ccy;
}