Creating an in-memory repository

The simplest way to create an in-memory repository is to define a singleton class with a private attribute, which represents a collection of elements. The repository will be initialized as a singleton type; therefore, this specific life cycle guarantees that data will be persistent until the application is restarted. 

At this stage, we don't need to use a repository with a real data source because we only need to focus on the HTTP part of our example and not how the data is stored. Later on in this book, we will take a look closer at the data access part.

First of all, we need a model that represents the data we want to store using our repository. Let's create a new folder, called Models, and create a new class named Order.cs:

using System;
using System.Collections.Generic;

namespace SampleAPI.Models
{
public class Order
{
public Guid Id { get; set; }
public IEnumerable<string> ItemsIds { get; set; }
}
}

Now, we need to define a new interface called IOrderRepository. The interface represents our order repository, and it will be located in a new folder called Repositories:

using System;
using System.Collections.Generic;
using SampleAPI.Models;

namespace SampleAPI.Repositories
{
public interface IOrderRepository
{
IEnumerable<Order> Get();
Order Get(Guid orderId);
void Add(Order order);
void Update(Guid orderId, Order order);
Order Delete(Guid orderId);
}
}

Our interface is implemented by the MemoryOrderRepository class, which provides the concrete logic of our interface:

using System;
using System.Collections.Generic;
using System.Linq;
using SampleAPI.Models;

namespace
SampleAPI.Repositories
{
public class MemoryOrderRepository : IOrderRepository
{
private IList<Order> _orders { get; set; }

public MemoryOrderRepository()
{
_orders = new List<Order>();
}
public IEnumerable<Order> Get() => _orders;

public Order Get(Guid orderId)
{
return _orders.FirstOrDefault(o => o.Id == orderId);
}
public void Add(Order order)
{
_orders.Add(order);
}

public void Update(Guid orderId, Order order)
{
var result = _orders.FirstOrDefault(o => o.Id == orderId);

if (result != null) result.ItemsIds = order.ItemsIds;
}
public Order Delete(Guid orderId)
{
var target = _orders.FirstOrDefault(o => o.Id == orderId);
_orders.Remove(target);

return target;
}
}
}

The MemoryOrderRepository class initializes a private list of the Order type. Furthermore, it also defines some operations that we can use to manipulate the list of orders, that are, the GetAddUpdate, and Delete methods. These methods use the LINQ syntax to act on the list elements. Furthermore, the main collection that's represented by the _orders attribute is declared as private in order to prevent any external access.

Note that each namespace path reflects the structure of the filesystem. For example, the SampleAPI.Repositories namespace reflects the Sample.API/Repositories filesystem path.

Finally, we can proceed by initializing the MemoryOrderRepository implementation as a singleton. To do that, we need to modify the Startup class and add our service to the services collection using the AddSingleton method:

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using SampleAPI.Repositories;

namespace SampleAPI
{
public class Startup
{

public IConfiguration Configuration { get; }

// ..

public void ConfigureServices(IServiceCollection services)
{
services
.AddSingleton<IOrderRepository, MemoryOrderRepository>()
.AddControllers();
}
// ...
}
}
The following example uses the IOrderRepository interface for demonstration and learning purposes. I strongly suggest that you avoid using singleton instances to store data in memory since singleton instances are not persistent storage, and this also causes performance degradation in our application.

In summary, we now have an Order class that describes a single order. The IOrderRepository interface allows us to store and read the data, and it has an in-memory implementation provided by the MemoryOrderRepository type, which uses the memory as a data store. Now, we have all the necessary components so that we can deal with the data and we can proceed by handling the client request through our controller.

..................Content has been hidden....................

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