The domain model and the data access layer

The domain model of the cart service represents the entities that we require to describe the cart session of a user. Specifically, the domain model of the cart service implements three different entity classes: Cart, CartItem, and CartUser. Like the catalog service, all the entities are stored in the Cart.Domain project, which will be referred to by the Cart.Infrastructure and Cart.API projects. 

Let's start with the CartSession class, which represents a single cart session:

using System;
using System.Collections.Generic;

namespace Cart.Domain.Entities
{
public class CartSession
{
public string Id { get; set; }
public IList<CartItem> Items { get; set; }
public CartUser User { get; set; }
public DateTimeOffset ValidityDate { get; set; }
}
}

The CartSession entity represents a single cart instance that's been created by a user. Therefore, it refers to the CartUser class, which contains the user's information. Besides, the CartSession entity also provides the IList<CartItems> field, which represents the items in the cart and the quantity associated with each item. Let's proceed by also defining the CartItem class:

using System;

namespace Cart.Domain.Entities
{
public class CartItem
{
public Guid CartItemId { get; set; }

public int Quantity { get; set; }

public void IncreaseQuantity()
{
Quantity = Quantity + 1;
}

public void DecreaseQuantity()
{
Quantity = Quantity - 1;
}
}
}

The CartItem class implements a CartItemId field and a Quantity field. Furthermore, it also provides the IncreaseQuantity and DecreaseQuantity fields, which are used to increment and decrement the quantity of a specific item, respectively. Finally, it is possible to determine the CartUser class, which represents the user related to the cart:

namespace Cart.Domain.Entities
{
public class CartUser
{
public string Email { get; set; }
}
}

For demonstration purposes, we are representing CartUser with only one property; this will contain the email of the user. The preceding entities are stored in the Entities folder, inside the Cart.Domain project. 

Once we have defined the domain model, we can proceed with the implementation of the data access abstraction. Specifically, the cart service will use the same access pattern we defined previously for the catalog service in order to get information about the cart sessions:

// src/Cart.Domain/Repositories/ICartRepository.cs

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Cart.Domain.Entities;

namespace Cart.Domain.Repositories
{
public interface ICartRepository
{
IEnumerable<string> GetCarts();
Task<CartSession> GetAsync(Guid id);
Task<CartSession> AddOrUpdateAsync(CartSession item);
}
}

The ICartRepository method implements methods that retrieve and update our data, the GetCarts method retrieves the IDs of the current carts, and the GetAsync method gathers information about a specific cart. Finally, the AddOrUpdateAsync method allows us to update or add a new cart if it's not present in the data store, while ICartRepository defines the operations that are performed by our data store. In the next subsection, we will look at a concrete implementation of the CartRepository class.

This domain model has been simplified to provide an example of a possible implementation of the cart. In a real-world application, we should consider other essential information about the state of a cart.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.116.159