IConfiguration vs IOptions NET
Synchronous and Asynchronous in .NET Core
Model Binding and Validation in ASP.NET Core
ControllerBase vs Controller in ASP.NET Core
ConfigureServices and Configure methods
IHostedService interface in .NET Core
ASP.NET Core request processing
| Unit of Work Patterns | Database Migrations and Seeding | |
NoSQL Integration in .NET Core (MongoDB & Redis) |
NoSQL databases provide flexible, schema-less, and high-performance data storage options. In .NET Core, two popular NoSQL databases are:
// Install-Package MongoDB.Driver
using MongoDB.Bson;
using MongoDB.Driver;
using System;
using System.Threading.Tasks;
public class Product
{
public ObjectId Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
}
public class MongoExample
{
public static async Task Main()
{
var client = new MongoClient("mongodb://localhost:27017");
var database = client.GetDatabase("ShopDb");
var collection = database.GetCollection<Product>("Products");
// Insert
await collection.InsertOneAsync(new Product { Name = "Laptop", Price = 1200 });
// Read
var products = await collection.Find(new BsonDocument()).ToListAsync();
products.ForEach(p => Console.WriteLine($"{p.Name} - {p.Price}"));
}
}
// Install-Package StackExchange.Redis
using StackExchange.Redis;
using System;
using System.Threading.Tasks;
public class RedisExample
{
public static async Task Main()
{
var redis = await ConnectionMultiplexer.ConnectAsync("localhost:6379");
var db = redis.GetDatabase();
// Set
await db.StringSetAsync("product:1", "Laptop");
// Get
var value = await db.StringGetAsync("product:1");
Console.WriteLine($"Product: {value}");
}
}
- MongoDB is best for flexible, document-based storage. - Redis is best for caching, real-time performance, and ephemeral data. - In many systems, they are used together: MongoDB as the primary store, Redis as a high-speed cache.
| Unit of Work Patterns | Database Migrations and Seeding | |