YDotNet.NoSqlRepository
2.0.0
dotnet add package YDotNet.NoSqlRepository --version 2.0.0
NuGet\Install-Package YDotNet.NoSqlRepository -Version 2.0.0
<PackageReference Include="YDotNet.NoSqlRepository" Version="2.0.0" />
<PackageVersion Include="YDotNet.NoSqlRepository" Version="2.0.0" />
<PackageReference Include="YDotNet.NoSqlRepository" />
paket add YDotNet.NoSqlRepository --version 2.0.0
#r "nuget: YDotNet.NoSqlRepository, 2.0.0"
#:package YDotNet.NoSqlRepository@2.0.0
#addin nuget:?package=YDotNet.NoSqlRepository&version=2.0.0
#tool nuget:?package=YDotNet.NoSqlRepository&version=2.0.0
π NoSql Generic Repository Pattern (MongoDB)
π NoSqlRepository is a super flexible and easy-to-use generic repository for MongoDB in .NET! It offers all the CRUD operations, smooth pagination, and even comes with an extension method to make your MongoDB configuration a breeze. π
π Features
- CRUD Operations: π Create, Read, Update, and Delete documents with ease.
- Pagination: π§βπ» Seamlessly paginate your read operations to fetch data in chunks.
- MongoDB Configuration Extension: π§ Simplify your MongoDB setup with our extension method.
- Super Flexible: πͺ Works with any type of entity, just like magic.
π οΈ Installation
To install NoSqlRepository, simply run the command below in your Package Manager Console:
Install-Package YDotNet.NoSqlRepository
Or, if you prefer using the .NET CLI (for that command line power π₯):
dotnet add package YDotNet.NoSqlRepository
βοΈ Setup
1. Add MongoDB Configuration
Add your MongoDB connection settings in appsettings.json
(or appsettings.Development.json
for development purposes):
{
"MongoDbSettings": {
"ConnectionString": "mongodb://localhost:27017",
"DatabaseName": "YourDatabaseName"
}
}
2. Register MongoDB Services in Program.cs
In Program.cs
(or Startup.cs
for the legacy folks), register MongoDB services:
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Configuration;
using NoSqlRepository;
using MongoDB.Driver;
public class Program
{
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
// Register MongoDb services with the extension method
builder.Services.AddMongoDbExtensions(builder.Configuration);
// Register the repository
builder.Services.AddScoped(typeof(NoSqlRepository<>));
var app = builder.Build();
app.Run();
}
}
π§ Methods
Hereβs a list of all the cool things you can do with NoSqlRepository! π
GetAllAsync()
Fetches all documents from the MongoDB collection.
public async Task<IEnumerable<T>> GetAllAsync()
FindAsync(FilterDefinition<T>? filter, int skip = 0, int limit = 10)
Fetches documents based on a filter, with pagination support! π
public async Task<IEnumerable<T>> FindAsync(FilterDefinition<T>? filter, int skip = 0, int limit = 10)
GetByIdAsync(string id)
Fetch a document by its ID. π΅οΈββοΈ
public async Task<T?> GetByIdAsync(string id)
AddAsync(T entity)
Add a brand-new document into your collection! π
public async Task AddAsync(T entity)
UpdateAsync(string id, T entity)
Update an existing document by its ID. π
public async Task UpdateAsync(string id, T entity)
UpdateAsync(string id, UpdateDefinition<T> update)
Apply a partial update to a document. π οΈ
public async Task<UpdateResult> UpdateAsync(string id, UpdateDefinition<T> update)
DeleteAsync(string id)
Goodbye, document! π
public async Task DeleteAsync(string id)
π± Example Usage
π·οΈ Example Entity
Let's create a simple entity for fun!
public class YourEntity
{
public ObjectId Id { get; set; }
public string Name { get; set; }
}
ποΈ Creating the Repository
Make your custom repository extend the abstract NoSqlRepository<T>
class. Here's how you can build your own repository for the entity:
public class YourEntityRepository : NoSqlRepository<YourEntity>
{
public YourEntityRepository(MongoClient client, IOptions<MongoDbSettings> settings, string? collectionName = null)
: base(client, settings, collectionName) { }
}
π οΈ Using the Repository
Now letβs see how you can inject and use the repository in your service:
public class YourService
{
private readonly YourEntityRepository _repository;
public YourService(YourEntityRepository repository)
{
_repository = repository;
}
// Fetch all entities from the MongoDB collection
public async Task GetAllEntitiesAsync()
{
var entities = await _repository.GetAllAsync();
// Do something with the entities, like logging them
Console.WriteLine(entities);
}
// Create a new entity and add it to the collection
public async Task CreateNewEntityAsync()
{
var newEntity = new YourEntity { Name = "New Entity" };
await _repository.AddAsync(newEntity);
}
}
Here's the updated README with an additional usage section for the RestaurantRepository
example, demonstrating how to access MongoDB collections and perform more extensive operations using the Collection
instance:
π½οΈ Custom Repository Example
π Example: RestaurantRepository
If you want to do some custom queries or more advanced operations, you can easily extend NoSqlRepository and use the protected Collection
instance for direct access to the MongoDB collection. Here's how you can create a custom repository for a Restaurant
entity!
public class RestaurantRepository : NoSqlRepository<Restaurant>
{
public RestaurantRepository(MongoClient client, IOptions<MongoDbSettings> settings, string? collectionName = null)
: base(client, settings, collectionName)
{ }
// Custom method to fetch restaurant names with pagination
public async Task<IEnumerable<string>> GetRestaurantsAsync()
{
// Define filter (Empty means no filter, so we get all restaurants)
var filter = Builders<Restaurant>.Filter.Empty;
// Define projection to only return restaurant names
var projection = Builders<Restaurant>.Projection.Expression(r => r.Name);
// Perform query with pagination: skip 0, limit to 10 results
var restaurantNames = await Collection
.Find(filter)
.Project(projection) // Apply projection to get only the 'Name' field
.Skip(0) // Skip the first 0 records (pagination starting point)
.Limit(10) // Limit to 10 records
.ToListAsync(); // Execute the query asynchronously
// Return the list of restaurant names
return restaurantNames;
}
}
π How It Works
Custom Queries: Use the
Collection
property (inherited fromNoSqlRepository
) for any custom query operations like filtering, projections, and sorting. In this case, theGetRestaurantsAsync()
method retrieves the restaurant names with pagination support.Projection: The method applies a projection to only retrieve the
Name
field from theRestaurant
documents, keeping your queries efficient by avoiding unnecessary fields.Pagination: We use
.Skip(0)
and.Limit(10)
to implement basic pagination. You can adjust theskip
andlimit
values dynamically for more advanced pagination logic.
π§βπ³ Example Entity: Restaurant
For this example, the Restaurant
entity might look like this:
public class Restaurant
{
public ObjectId Id { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public string Cuisine { get; set; }
public bool IsOpen { get; set; }
}
π οΈ Usage
Once your repository is ready, you can use it in your service class or anywhere you need it. Hereβs how you can inject and use your custom RestaurantRepository
:
public class RestaurantService
{
private readonly RestaurantRepository _restaurantRepository;
public RestaurantService(RestaurantRepository restaurantRepository)
{
_restaurantRepository = restaurantRepository;
}
// Example method to fetch restaurant names
public async Task GetRestaurantNamesAsync()
{
var restaurantNames = await _restaurantRepository.GetRestaurantsAsync();
// Do something with the restaurant names, like logging or processing
Console.WriteLine(string.Join(", ", restaurantNames));
}
}
π οΈ Configuration Options
- ConnectionString: The URL to your MongoDB instance (e.g.,
mongodb://localhost:27017
). - DatabaseName: The name of the MongoDB database you want to use. π
π License
This project is licensed under the MIT License. Enjoy and have fun with MongoDB! π
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net8.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.2)
- Microsoft.Extensions.Options (>= 8.0.2)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 8.0.0)
- MongoDB.Driver (>= 3.3.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last Updated |
---|---|---|
2.0.0 | 99 | 4/27/2025 |