title
stringlengths
3
46
content
stringlengths
0
1.6k
21:224
The peer reviews check, for instance, the presence of a feature flag while creating a new feature.
21:225
The peer reviews check both unit and functional tests developed during the creation of the new feature.
21:226
21:227
The preceding steps are not exclusively for WWTravelClub. You, as a software architect, will need to define the approach to guarantee a safe CI/CD scenario. You may use this as a starting point. It is worth pointing out that both in Azure DevOps and GitHub, we can completely disable pushing on the master branch, thus forcing the usage of PR for merging modifications on the master branch.
21:228
In the next section, we will start with the actual code, by showing how to choose among various data storage options.
21:229
How to choose your data storage in the cloud
21:230
In Chapter 12, Choosing Your Data Storage in the Cloud, we learned how to use NoSQL. Now we must decide whether NoSQL databases are adequate for our book use case WWTravelClub application. We need to store the following families of data:
21:231
21:232
Information about available destinations and packages: Relevant operations for these data are reads since packages and destinations do not change very often. However, they must be accessed as fast as possible from all over the world to ensure a pleasant user experience when users browse the available options. Therefore, a distributed relational database with geographically distributed replicas is possible but not necessary since packages can be stored inside their destinations in a cheaper NoSQL database.
21:233
Destination reviews: In this case, distributed write operations have a non-negligible impact. Moreover, most writes are additions since reviews are not usually updated. Additions benefit a lot from sharding and do not cause consistency issues like updates do. Accordingly, the best option for this data is a NoSQL collection.
21:234
Reservations: In this case, consistency errors are not acceptable because they may cause overbooking. Reads and writes have a comparable impact, but we need reliable transactions and good consistency checks. Luckily, data can be organized in a multi-tenant database where tenants are destinations since reservation information belonging to different destinations is completely unrelated. Accordingly, we may use sharded Azure SQL Database instances.
21:235
21:236
In conclusion, the best option for data in the first and second bullet points is Cosmos DB, while the best option for the third point is Azure SQL Server. Actual applications may require a more detailed analysis of all data operations and their frequencies. In some cases, it is worth implementing prototypes for various possible options and executing performance tests with typical workloads on all of them.
21:237
In the remainder of this section, we will migrate the destinations/packages data layer we looked at in Chapter 13, Interacting with Data in C# – Entity Framework Core, to Cosmos DB.
21:238
Implementing the destinations/packages database with Cosmos DB
21:239
Let’s move on to the database example we built in Chapter 13, Interacting with Data in C# – Entity Framework Core, and implement this database with Cosmos DB by following these steps:
21:240
21:241
First, we need to make a copy of the WWTravelClubDB project and rename its root folder as WWTravelClubDBCosmo.
21:242
Open the project and delete the Migrations folder since migrations are not required anymore.
21:243
We need to replace the SQL Server Entity Framework provider with the Cosmos DB provider. To do this, go to Manage NuGet Packages and uninstall the Microsoft.EntityFrameworkCore.SqlServer NuGet package. Then, install the Microsoft.EntityFrameworkCore.Cosmos NuGet package.
21:244
Then, do the following on the Destination and Package entities:
21:245
Remove all data annotations.
21:246
Add the [Key] attribute to their Id properties since this is obligatory for Cosmos DB providers.
21:247
Transform the type of the Id properties of both Package and Destination and the PackagesListDTO classes from int to string. We also need to turn the DestinationId external references in Package and in the PackagesListDTO classes into string. In fact, the best option for keys in distributed databases is a string generated from a GUID, because it is hard to maintain an identity counter when table data is distributed among several servers.
21:248
21:249
21:250
In the MainDBContext file, we need to specify that packages related to a destination must be stored inside the destination document itself. This can be achieved by replacing the Destination-Package relation configuration in the OnModelCreating method with the following code:
21:251
builder.Entity<Destination>()
21:252
.OwnsMany(m =>m.Packages);
21:253
21:254
21:255
Here, we must replace HasMany with OwnsMany. There is no equivalent to WithOne since once an entity is owned, it must have just one owner, and the fact that the MyDestination property contains a pointer to the father entity is evident from its type. Cosmos DB also allows the use of HasMany, but in this case, the two entities are not nested one in the other. There is also an OwnOne configuration method for nesting single entities inside other entities.
21:256
Both OwnsMany and OwnsOne are available for relational databases, but in this case, the difference between HasMany and HasOne is that children entities are automatically included in all queries that return their father entities, with no need to specify an Include LINQ clause. However, child entities are still stored in separate tables.
21:257
LibraryDesignTimeDbContextFactory must be modified to use Cosmos DB connection data, as shown in the following code:
21:258
using Microsoft.EntityFrameworkCore;
21:259
using Microsoft.EntityFrameworkCore.Design;
21:260
namespace WWTravelClubDB
21:261
{
21:262
public class LibraryDesignTimeDbContextFactory
21:263
: IDesignTimeDbContextFactory<MainDBContext>
21:264
{
21:265
private const string endpoint = `<your account endpoint>`;
21:266
private const string key = `<your account key>`;
21:267
private const string databaseName = `packagesdb`;
21:268
public MainDBContext CreateDbContext(params string[] args)
21:269
{
21:270
var builder = new DbContextOptionsBuilder<MainDBContext>();
21:271
builder.UseCosmos(endpoint, key, databaseName);
21:272
return new MainDBContext(builder.Options);
21:273
}
21:274
}
21:275
}
21:276
21:277
21:278
Finally, in our test console, we must explicitly create all entity principal keys using GUIDs:
21:279
var context = new LibraryDesignTimeDbContextFactory()
21:280
.CreateDbContext();
21:281
context.Database.EnsureCreated();
21:282
var firstDestination = new Destination
21:283
{
21:284
Id = Guid.NewGuid().ToString(),
21:285
Name = `Florence`,
21:286
Country = `Italy`,
21:287
Packages = new List<Package>()
21:288
{
21:289
new Package
21:290
{
21:291
Id=Guid.NewGuid().ToString(),
21:292
Name = `Summer in Florence`,
21:293
StartValidityDate = new DateTime(2019, 6, 1),
21:294
EndValidityDate = new DateTime(2019, 10, 1),
21:295
DurationInDays=7,
21:296
Price=1000
21:297
},
21:298
new Package
21:299
{
21:300
Id=Guid.NewGuid().ToString(),
21:301
Name = `Winter in Florence`,
21:302
StartValidityDate = new DateTime(2019, 12, 1),
21:303
EndValidityDate = new DateTime(2020, 2, 1),
21:304
DurationInDays=7,
21:305
Price=500
21:306
}
21:307
}
21:308
};
21:309
21:310
21:311
Here, we call context.Database.EnsureCreated() instead of applying migrations since we only need to create the database. Once the database and collections have been created, we can fine-tune their settings from the Azure portal. Hopefully, future versions of the Cosmos DB Entity Framework Core provider will allow us to specify all collection options.
21:312
Finally, the final query (which starts with context.Packages.Where...) must be modified since queries can’t start with entities that are nested in other documents (in our case, Packages entities). Therefore, we must start our query from the unique root DbSet<T> property we have in our DBContext, that is, Destinations. We can move from listing the external collection to listing all the internal collections with the help of the SelectMany method, which performs a logical merge of all nested Packages collections. However, since Cosmos DB SQL doesn’t support SelectMany, we must force SelectMany to be simulated on the client with AsEnumerable(), as shown in the following code:
21:313
var list = context.Destinations
21:314
.AsEnumerable() // move computation on the client side
21:315
.SelectMany(m =>m.Packages)
21:316
.Where(m => period >= m.StartValidityDate....)
21:317
...
21:318
21:319
21:320
The remainder of the query remains unchanged. If you run the project now, you should see the same outputs that were received in the case of SQL Server (except for the primary key values).
21:321
21:322
After executing the program, go to your Cosmos DB account. You should see something like the following:
21:323