"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.
A nuget package I wrote to use the generic repository pattern more efficiently.
.net 7.0
dotnet add package MongoDbGenericRepositoryPattern --version 1.0.0
{
"DatabaseSettings": {
"ConnectionStrings": "mongodb://localhost:27017",
"DatabaseName": "ProductDb"
}
}
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;
});
* 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);
}
}
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);
}
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);
}
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