Skip to content

Business Logic

Dave Walker edited this page Dec 10, 2023 · 1 revision

Overview

The business logic is divided into the following namespaces:

Namespace Contents
FlightRecorder.BusinessLogic.Api External API integrations
FlightRecorder.BusinessLogic.Config Common application settings reader
FlightRecorder.BusinessLogic.Database Logic for reading from/writing to the database
FlightRecorder.BusinessLogic.Factory Factory class for accessing the database logic
FlightRecorder.BusinessLogic.Logging Logging implementation
FlightRecorder.DataExchange.Export CSV-format data export logic
FlightRecorder.DataExchange.Import CSV-format data import logic

Database Management Classes

The logic for accessing the flight recorder data is implemented as a set of management classes, one per entity type, in the FlightRecorder.BusinessLogic.Database namespace.

These classes are marked as internal and for consuming applications the database logic is accessed using an instance of the Flight Recorder "factory" class.

Database Factory

The FlightRecorderFactory class in the FlightRecorder.BusinessLogic.Factory namespace implements a facade that exposes the database management classes via the interfaces they implement and co-ordinates their activity.

The factory is the sole entry point for applications wishing to use the business logic to read from/write to the database.

Note that other logic - such as the external API integrations and data exchange logic - are not fronted by the factory class.

Creating a Flight Recorder Business Logic Factory

  • Reference the FlightRecorder.Entities library
  • Reference the FlightRecorder.BusinessLogic library
  • Reference the FlightRecorder.Data library
  • Create a database context
  • Create a Flight Recorder Factory

The following are the using statements:

using FlightRecorder.BusinessLogic.Factory;
using FlightRecorder.Data;
using FlightRecorder.Entities.Db;

The following code snippet creates the factory:

FlightRecorderDbContext context = new FlightRecorderDbContextFactory().CreateInMemoryDbContext();
FlightRecorderFactory factory = new FlightRecorderFactory(context);

Depending on your application, the context may be injected rather than being created using the context factory.

Reading Data

By way of example, the following reads and displays a list of the second page of manufacturers from the database using a page size of 10:

var manufacturers = await factory.Manufacturers.List(null, 2, 10).ToListAsync();
foreach (var manufacturer in manufacturers)
{
    Console.WriteLine(manufacturer.Name);
}

Writing Data

Again, by way of example, the following adds a new manufacturer to the database:

var manufacturer = await factory.Manufacturers.AddAsync("Airbus");