//Using EntityFrameworkCore for database operations using Microsoft.EntityFrameworkCore; public class ProductService : IProductService { private readonly DbContext _dbContext; public ProductService(DbContext dbContext) { _dbContext = dbContext; } public async Task AddProductAsync(Product product) { await _dbContext.Products.AddAsync(product); await _dbContext.SaveChangesAsync(); } }
//Using MongoDB.Driver for database operations using MongoDB.Driver; public class UserService : IUserService { private readonly IMongoCollectionIn both examples, ICrudService AddAsync is used to add a new object (Product/User) to the database asynchronously. The package library used in the first example is EntityFrameworkCore, while the second example uses MongoDB.Driver._userCollection; public UserService(IDatabaseSettings settings) { var client = new MongoClient(settings.ConnectionString); var database = client.GetDatabase(settings.DatabaseName); _userCollection = database.GetCollection (settings.UserCollectionName); } public async Task AddUserAsync(User user) { await _userCollection.InsertOneAsync(user); } }