using Microsoft.EntityFrameworkCore; using System.Threading.Tasks; using MyProject.Models; public class MyService : ICrudService { private readonly MyDbContext _dbContext; public MyService(MyDbContext dbContext) { _dbContext = dbContext; } public async TaskGetAsync(int id) { return await _dbContext.MyModels.FindAsync(id); } }
using MongoDB.Driver; using System.Threading.Tasks; using MyProject.Models; public class MyService : ICrudService { private readonly IMongoCollectionIn this example, we have a class called "MyService" which implements the ICrudService interface and uses the IMongoCollection to retrieve data from a MongoDB database. The GetAsync method takes a string parameter representing a name and returns a single MyModel object that matches the name provided._collection; public MyService(IMongoDatabase database) { _collection = database.GetCollection ("myCollection"); } public async Task GetAsync(string name) { var filter = Builders .Filter.Eq("Name", name); return await _collection.Find(filter).FirstOrDefaultAsync(); } }