top of page

Code Examples

These are some of the simplest examples of my code. I intended to highlight only one aspect in these examples. Some of them are small parts from real projects, while others were specially created for showcasing.

Web API Example

This is an example of my Web API application implementation, featuring exception handling and processing, model validation and RESTful support.

There is a part of the real large-scale project. For demonstration purposes in the WebAPI, I've selected three controllers: two for managing similar entities—Revoked Devices and Registered Devices—and one for Vendors, which is common to both device types.

  • The library WebApiExample.Core has defined interfaces to manage data with a source (in the real project this is typically a database, but not included in this example). It includes DTO classes for manipulating this data, as well as classes for paging, filtering and exceptions that services may throw. For demonstration purposes, only two exceptions are included: one for when an entity is not found, and another for when an entity already exists."

  • The library Web.Framework.Api contains additional code for API functionality, including exception handlers (EntityAlreadyExistsAttribute, EntityNotFoundAttribute etc), a common error model to provide additional error information to client app, static strings and classes for validating data passed from client app (utilizing third-party NuGet package FluentValidation).

  • The entry point application WebApiExample consists of controllers with API methods, including exception handling with the defined handlers, Swagger integration for API documentation, and other relevant functionalities.

Generic Interfaces with implementation Example

This example demonstrates working with generic interface and its implementation, emphasizing abstraction over concrete real types of entities. It abstracts specific entity details and passes concrete classes to another service responsible for interacting with actual entities and saving them to a database. The example encapsulates common logic for retrieving files, processing, parsing, and subsequently passing them to the save service.

There is a part of the real large-scale project. which processing 2 types of files from an external source. Initially, the files are saved to Azure storage (this part is not shown here). Subsequently, the files undergo parsing, processing and potentially saving to a local DB depending on subsequent actions (this part is not shown here)

  • There are 2 types of files with similar structure, but slight differences. An abstract class named AbstractDeviceDto contains all common fields, shared between them. Classes RegisteredDeviceDto and RevokedDeviceDto are descendances of this abstract class.

  • We need to monitor new records that have arrived since the last update, identify modifications, deletions, and potential duplicates (this part is not shown here). For this purpose, we have classes CompareAbstractDevicesResultDto and CompareResultItemModifiedDto.

  • The IDevicesGetFromFilesAndSaveDataDbWorkflow interface is generic because its implementation is identical for both registered and revoked devices. It includes a single method, Workflow, which incapsulates the logic for retrieving data, parsing, processing and saving to the DB. This method is designed to be executed as an atomic operation, ensuring that client code calls only this method to complete the operation.

  • The implementation of the Workflow method retrieves all unprocessed files (regardless of device type, as it operates on abstractions), then selects the oldest file (n the real project, additional actions are performed here, so initially, a list is retrieved instead of just the oldest file), parses it and passes to another service for saving to the DB.

Prisoner's dilemma game

Implementation of prisoner's dilemma game

bottom of page