Read Model Store


The primary objective of the read model store is to persist the data in an optimized format, ensuring quicker query responses in comparison to joining data from disparate sources such as tables or documents.

The Crystal Sharp framework provides support for using databases and Elasticsearch as read model stores. The Crystal Sharp framework supports the following read model stores:

Microsoft SQL Server To use Microsoft SQL Server as a read model store.
Oracle To use Oracle as a read model store.
PostgreSQL To use PostgreSQL as a read model store.
MySQL To use MySQL as a read model store.
MongoDB To use MongoDB as a read model store.
Elasticsearch To use Elasticsearch as a read model store.

Following are the NuGet packages for each read model store:

NuGet Package Database
CrystalSharp.MsSql Microsoft SQL Server
CrystalSharp.Oracle Oracle
CrystalSharp.PostgreSql PostgreSQL
CrystalSharp.MySql MySQL
CrystalSharp.MongoDb MongoDB
CrystalSharp.ReadModelStores.Elasticsearch Elasticsearch

Read Model Store - Implementation

In order to implement the read model, it is mandatory to inherit the read model class from ReadModel<TKey>.

Following is the code for the read model class:

public class OrderReadModel : ReadModel<int>
{
    public string OrderCode { get; private set; }
    public decimal TotalPrice { get; private set; }
    public string Customer { get; private set; }
    public string DeliveryAddress { get; private set; }

    public static OrderReadModel Create(Guid globalUId,
        string orderCode,
        decimal totalPrice,
        string customer,
        string deliveryAddress)
    {
        return new OrderReadModel
        {
            GlobalUId = globalUId,
            OrderCode = orderCode,
            TotalPrice = totalPrice,
            Customer = customer,
            DeliveryAddress = deliveryAddress
        };
    }

    public void ChangeDeliveryAddress(string deliveryAddress)
    {
        DeliveryAddress = deliveryAddress;
    }
}

In the above code snippet, the OrderReadModel class inherits from the ReadModel<int>. Here, <int> is the type of the Id property, which is the primary key of the read model in the database.

IMPORTANT

The “Id” property, serving as the primary key for the read model in the database, must have a consistent data type across all models. This requirement is mandatory for ensuring uniformity in the read models.

Read Model Store and MongoDB

In the Crystal Sharp framework, MongoDB uses string as a primary key. Read models that will be stored in MongoDB must have a string type for the Id property, which is the primary key of the read model in the database.

Following code snippet illustrates the read model class for MongoDB:

public class OrderReadModel : ReadModel<string>
{
    public string OrderCode { get; private set; }
    public decimal TotalPrice { get; private set; }
    public string Customer { get; private set; }
    public string DeliveryAddress { get; private set; }

    public static OrderReadModel Create(Guid globalUId,
        string orderCode,
        decimal totalPrice,
        string customer,
        string deliveryAddress)
    {
        return new OrderReadModel
        {
            GlobalUId = globalUId,
            OrderCode = orderCode,
            TotalPrice = totalPrice,
            Customer = customer,
            DeliveryAddress = deliveryAddress
        };
    }

    public void ChangeDeliveryAddress(string deliveryAddress)
    {
        DeliveryAddress = deliveryAddress;
    }
}

In the above code snippet, the OrderReadModel class inherits from the ReadModel<string>. Here, <string> is the type of the Id property, which is the primary key of the read model in the database.

ReadModel<TKey>

The base class ReadModel<TKey> has the following properties that are necessary for the read model store:

public TKey Id { get; private set; } Id column for the table.
public Guid GlobalUId { get; private set; } GlobalUId column for the table.
public EntityStatus EntityStatus { get; private set; } EntityStatus column for the table.
public DateTime CreatedOn { get; private set; } CreatedOn column for the table.
public DateTime? ModifiedOn { get; private set; } ModifiedOn column for the table.

The Crystal Sharp framework manages the properties of the base class ReadModel<TKey> internally when performing create, update, and delete operations.