NoSQL Data Access with ASP.NET Core

Explore how to integrate NoSQL databases like MongoDB, Cosmos DB, and Redis into your ASP.NET Core applications for flexible and scalable data management.

Understanding NoSQL Databases

NoSQL (Not Only SQL) databases offer alternatives to traditional relational databases, excelling in scenarios requiring high scalability, flexible schemas, and handling large volumes of unstructured or semi-structured data. Common types include document, key-value, wide-column, and graph databases.

Integrating MongoDB with ASP.NET Core

MongoDB is a popular document-oriented NoSQL database. We'll use the official MongoDB C# Driver to interact with it.

1. Installation

Install the MongoDB C# Driver NuGet package:

dotnet add package MongoDB.Driver

2. Configuration

Configure your MongoDB connection string in appsettings.json:

{
  "MongoDB": {
    "ConnectionString": "mongodb://localhost:27017",
    "DatabaseName": "MyApplicationDB"
  }
}

3. Creating a Repository (Example: Product)

Define your model and a repository class:

public class Product
{
    public ObjectId Id { get; set; }
    public string Name { get; set; }
    public decimal Price { get; set; }
}

public class ProductRepository
{
    private readonly IMongoCollection _products;

    public ProductRepository(IOptions<MongoDbSettings> settings)
    {
        var client = new MongoClient(settings.Value.ConnectionString);
        var database = client.GetDatabase(settings.Value.DatabaseName);
        _products = database.GetCollection<Product>("Products");
    }

    public async Task<List<Product>> GetAllAsync()
    {
        return await _products.Find(_ => true).ToListAsync();
    }

    public async Task<Product> GetByIdAsync(string id)
    {
        var filter = Builders<Product>.Filter.Eq("_id", new ObjectId(id));
        return await _products.Find(filter).FirstOrDefaultAsync();
    }

    public async Task CreateAsync(Product product)
    {
        await _products.InsertOneAsync(product);
    }

    public async Task UpdateAsync(string id, Product product)
    {
        var filter = Builders<Product>.Filter.Eq("_id", new ObjectId(id));
        await _products.ReplaceOneAsync(filter, product);
    }

    public async Task DeleteAsync(string id)
    {
        var filter = Builders<Product>.Filter.Eq("_id", new ObjectId(id));
        await _products.DeleteOneAsync(filter);
    }
}

public class MongoDbSettings
{
    public string ConnectionString { get; set; }
    public string DatabaseName { get; set; }
}

In Startup.cs or Program.cs (for .NET 6+), register the settings and repository:

services.Configure<MongoDbSettings>(Configuration.GetSection("MongoDB"));
services.AddSingleton<ProductRepository>();

Other NoSQL Options

Learn about integrating with other NoSQL databases:

Key Considerations: When choosing a NoSQL database, consider your application's data structure, scalability needs, query patterns, and consistency requirements. Each NoSQL type has its strengths and weaknesses.