Onion architecture in the development of cross platform applications. Part 2. Domain and Application

Onion architecture in the development of cross platform applications. Part 2. Domain and Application

21 April 2021

.NET

Share:

Hello!

This is a continuation of the first article about onion architecture in the development of cross-platform applications.

From the theory to the practice. For a closer look at onion architecture, let's create a solution called MediaStoreApp.

Create a new solution folder. Let's call it Domain. Then add a new project to the folder. Select the Class Library type as the project type, and specify MediaStoreApp.Domain.Core as its name.

Let's add a class representing the MediaItem to the new project, which will represent the Domain Model:

namespace MediaStoreApp.Domain.Core
{
    public class MediaItem
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        public decimal Price { get; set; }
    }
}

Then add a new project to the Domain folder, also of the Class Library type, and specify MediaStoreApp.Domain.Interfaces as its name. Then add a reference to the above-defined project to this project MediaStoreApp.Domain.Core and add a new interface as well:

using System;
using System.Collections.Generic;
using MediaStoreApp.Domain.Core;

namespace MediaStoreApp.Domain.Interfaces
{
    public interface IMediaStoreRepository
    {
        IEnumerable<MediaItem> GetMediaItems();
        MediaItem GetMediaItem(int id);
        void Create(MediaItem item);
        void Update(MediaItem item);
        void Delete(int id);
        void Save();
    }
}

This interface constitutes the Domain Services layer and depends on the Domain Model layer.

When creating an application architecture, one must understand that the actual number of levels here is rather arbitrary. Depending on the scale of the tasks, there may be more or fewer levels. However, it is important to understand the very principle that we have the domain model at the center, and everything else depends on them. Each outer level can depend on the inner one, but not vice versa.

Onion Architecture

In the above diagram, between the external layer and the Domain Services layer, there is another layer of APIs or application business logic interfaces - the Application Services layer. This layer can include the interfaces of the helper classes. For example, the purchase of a media item can be an object that, depending on the method of payment (credit card, PayPal, etc), can include one or another functionality. Therefore, we will add a new folder to the solution, which we will call Services. Add a new project of the Class Library type to this folder, which we will call MediaStoreApp.Services.Interfaces.

And add the IOrder interface to this project:

using MediaStoreApp.Domain.Core;
namespace MediaStoreApp.Services.Interfaces
{
    public interface IOrder
    {
        void MakeOrder(IEnumerable<MediaItem> mediaItems);
    }
}

This project also has a dependency on the project classes MediaStoreApp.Domain.Core. And the IOrder interface, which represents the purchase and checkout process, uses these classes in the method MakeOrder(). It is assumed that the list of objects of the purchased media items is passed to the method.

In the next article, we'll create Infrastructure and UI levels.

Related:

Onion architecture in the development of cross platform applications. Part 1. Overview

Onion architecture in the development of cross platform applications. Part 1. Overview

Overview of onion architecture. Creating an cross-platform application with the onion architecture

Creating screen brightness service with .NET 5

Creating screen brightness service with .NET 5

Creating WindowsService to adjust screen brightness on laptops

An error has occurred. This application may no longer respond until reloaded. Reload 🗙