/MongoDbGenericRepositoryPattern-1.0.0

2023 || You can work with asynchronous or non-asynchronous methods. You can use it in your sample projects. It will speed you up. There are crud operations.

Primary LanguageC#MIT LicenseMIT

License

License: MIT

Features

WebApp

"Hello, I would like to talk about the features of MongoDbGenericRepositoryPattern, a NuGet package I wrote."

  • You can work with asynchronous or non-asynchronous methods.
  • You can use it in your sample projects. It will speed you up.
  • There are crud operations.
  • When you configure appsettings and program.cs, it will run smoothly.
  • I am using it in a real microservice project. (own project)
  • More methods will be added.

Errors are corrected as a result of feedback.

MongoDbGenericRepositoryPattern- 1.0.0

A nuget package I wrote to use the generic repository pattern more efficiently.

Version

.net 7.0

Install

  dotnet add package MongoDbGenericRepositoryPattern --version 1.0.0

Use

Appsettings.json

{
  "DatabaseSettings": {
    "ConnectionStrings": "mongodb://localhost:27017",
    "DatabaseName": "ProductDb"
  }
}

Program.cs

builder.Services.AddScoped<ICategoryService, CategoryService>();
builder.Services.AddScoped<IProductRepository, ProductRepository>();
builder.Services.Configure<DatabaseSettings>(builder.Configuration.GetSection("DatabaseSettings"));
builder.Services.AddSingleton<IDatabaseSettings>(sp =>
{
    return sp.GetRequiredService<IOptions<DatabaseSettings>>().Value;
});

Create Repository

* Interface
    public interface IProductRepository :IRepository<Products>
    {
        void AddProduct(Products product);
    }



  *  Concrete  
public class ProductRepository : MongoDbGenericRepository<Products>, IProductRepository
{
    private readonly MongoDbContext _dbContext;

    public ProductRepository(IDatabaseSettings databaseSettings) : base(databaseSettings)
    {
        _dbContext = new MongoDbContext(databaseSettings);
    }

    public void AddProduct(Products product)
    {
        var collection = _dbContext.GetCollection<Products>();
        collection.InsertOne(product);
    }
}


Service

private readonly ICategoryRepository _categoryRepository;
private readonly IMapper _mapper;

public CategoryService(ICategoryRepository categoryRepository, IMapper mapper)
{
    _categoryRepository = categoryRepository;
   _mapper = mapper;
}

  public async Task<ResponseDto<List<CategoryDto>>> GetAllAsync()
{
    var categories = await _categoryRepository.GetAllAsync();
    var categoriesDto = _mapper.Map<List<CategoryDto>>(categories);

    return ResponseDto<List<CategoryDto>>.Success(categoriesDto, 200);
}

Controller

private readonly ICategoryService _categoryService;

public CategoryController(ICategoryService categoryService)
{
    _categoryService = categoryService;
}

[HttpGet("[action]")]
public async Task<IActionResult> GetAll()
{
    var response = await _categoryService.GetAllAsync();
    return CreateActionResultInstance(response);
}

IRepository

Task<bool> ExistsByIdAsync(string id, string type = "object");
IQueryable<T> GetAllQueryable();
Task<IQueryable<T>> GetAllQueryableAsync();
IList<T> GetAll();
Task<IList<T>> GetAllAsync();
IList<T> FilterBy(Expression<Func<T, bool>> filter);
Task<IList<T>> FilterByAsync(Expression<Func<T, bool>> filter);
T GetById(string id, string type = "object");
Task<T> GetByIdAsync(string id, string type = "object");
void InsertOne(T entity);
Task InsertOneAsync(T entity);
void InsertMany(ICollection<T> entities);
Task InsertManyAsync(ICollection<T> entities);
T UpdateOne(T entity, string id, string type = "object");
Task<T> UpdateOneAsync(T entity, string id, string type = "object");
T DeleteOne(Expression<Func<T, bool>> filter);
Task<T> DeleteOneAsync(Expression<Func<T, bool>> filter);
T DeleteById(string id);
Task<T> DeleteByIdAsync(string id);
void DeleteMany(Expression<Func<T, bool>> filter);
Task DeleteManyAsync(Expression<Func<T, bool>> filter);

## Packages

* Mongodb.Bson(2.20.0)
* Mongodb.Driver(2.20.0)
 ### Design Patterns:
    * Generic Repository   
       

                                                                                                                      
   ###    By Abdullah Balikci - berjcode