Starts in:
3 DAYS
7 HRS
21 MIN
30 SEC
Starts in:
3 D
7 H
21 M
30 S

There are multiple versions of this document. Pick the options that suit you best.

UI
Database

Web Application Development Tutorial - Part 7: Authors: Database Integration

Introduction

This part explains how to configure the database integration for the Author entity introduced in the previous part.

DB Context

Open the BookStoreMongoDbContext in the MongoDb folder of the Acme.BookStore.MongoDB project and add the following property to the class:

public IMongoCollection<Author> Authors => Collection<Author>();

Implementing the IAuthorRepository

Create a new class, named MongoDbAuthorRepository inside the Acme.BookStore.MongoDB project (in the Authors folder) and paste the following code:

using System;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Collections.Generic;
using System.Threading.Tasks;
using Acme.BookStore.MongoDB;
using MongoDB.Driver;
using MongoDB.Driver.Linq;
using Volo.Abp.Domain.Repositories.MongoDB;
using Volo.Abp.MongoDB;

namespace Acme.BookStore.Authors;

public class MongoDbAuthorRepository
    : MongoDbRepository<BookStoreMongoDbContext, Author, Guid>,
    IAuthorRepository
{
    public MongoDbAuthorRepository(
        IMongoDbContextProvider<BookStoreMongoDbContext> dbContextProvider
        ) : base(dbContextProvider)
    {
    }

    public async Task<Author> FindByNameAsync(string name)
    {
        var queryable = await GetMongoQueryableAsync();
        return await queryable.FirstOrDefaultAsync(author => author.Name == name);
    }

    public async Task<List<Author>> GetListAsync(
        int skipCount,
        int maxResultCount,
        string sorting,
        string filter = null)
    {
        var queryable = await GetMongoQueryableAsync();
        return await queryable
            .WhereIf<Author, IMongoQueryable<Author>>(
                !filter.IsNullOrWhiteSpace(),
                author => author.Name.Contains(filter)
            )
            .OrderBy(sorting)
            .As<IMongoQueryable<Author>>()
            .Skip(skipCount)
            .Take(maxResultCount)
            .ToListAsync();
    }
}
  • Inherited from the MongoDbRepository, so it inherits the standard repository method implementations.
  • WhereIf is a shortcut extension method of the ABP. It adds the Where condition only if the first condition meets (it filters by name, only if the filter was provided). You could do the same yourself, but these type of shortcut methods makes our life easier.
  • sorting can be a string like Name, Name ASC or Name DESC. It is possible by using the System.Linq.Dynamic.Core NuGet package.

See the MongoDB Integration document for more information on the MongoDB based repositories.


Contributors


Last updated: August 05, 2024 Edit this page on GitHub

Was this page helpful?

Please make a selection.

To help us improve, please share your reason for the negative feedback in the field below.

Please enter a note.

Thank you for your valuable feedback!

Please note that although we cannot respond to feedback, our team will use your comments to improve the experience.

In this document
Community Talks

What’s New with .NET 9 & ABP 9?

21 Nov, 17:00
Online
Register Now
Mastering ABP Framework Book
Mastering ABP Framework

This book will help you gain a complete understanding of the framework and modern web application development techniques.

Learn More