c# - 无法按照教程进行操作:连接到数据存储 [11 of 18] | 初学者系列:使用 mongoDB 的 Web API
问题描述
我是 monngoDB 的初学者,只是创建一个 ASP.NET Web API 来在 MongoDB 数据库中执行 CRUD 操作。我已经能够根据以下 Microsoft 教程创建一个简单的应用程序:beginner's series to webbbApis
在本教程中,我无法跟随并且不知道代码是否打开,
MongoDatabaseOptions.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace ContosoRecipes.Models
{
public class MongoDatabaseOptions
{
}
}
IRecipeDataStore.cs
using ContosoRecipes.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace ContosoRecipes.Data
{
public interface IRecipeDataStore
{
Task<IEnumerable<T>> GetRandomRecipes<T>(int count);
Task<T> GetRecipeById<T>(string id);
Task<IEnumerable<T>> GetRecipesById<T>(string ids);
Task<IEnumerable<T>> SearchRecipesByIngredients<T>(string[] ingredients);
Task<IEnumerable<T>> SearchRecipesByTags<T>(string[] tags);
Task UpdateRecipe(Recipe recipe);
Task RemoveRecipe(string recipe_id);
}
}
MongoRecipeDataStore.cs
using ContosoRecipes.Models;
using Microsoft.Extensions.Options;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace ContosoRecipes.Data
{
public class MongoRecipeDataStore : IRecipeDataStore
{
public MongoRecipeDataStore(IMongoClient mongoClient, IOptions<MongoDatabaseOptions> mongodbOption, IMongoDatabase database)
{
}
public async Task<IEnumerable<T>> GetRandomRecipes<T>(int count)
{
throw new NotImplementedException();
}
public async Task<T> GetRecipeById<T>(string id)
{
throw new NotImplementedException();
}
public async Task<IEnumerable<T>> GetRecipesById<T>(string ids)
{
throw new NotImplementedException();
}
public async Task RemoveRecipe(string recipe_id)
{
throw new NotImplementedException();
}
public async Task<IEnumerable<T>> SearchRecipesByIngredients<T>(string[] ingredients)
{
throw new NotImplementedException();
}
public Task<IEnumerable<T>> SearchRecipesByTags<T>(string[] tags)
{
throw new NotImplementedException();
}
public Task UpdateRecipe(Recipe recipe)
{
throw new NotImplementedException();
}
}
}
还有一点 ServiceCollectionExtensions.cs
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using MongoDB.Bson.Serialization;
using MongoDB.Bson.Serialization.Conventions;
using MongoDB.Driver;
using ContosoRecipes.Models;
namespace CentosoRecipes
{
public static class ServiceCollectionExtensions
{
public static IServiceCollection AddMongoDb(this IServiceCollection services , IConfiguration config)
{
services.Configure<MongoDatabaseOptions>(config.GetSection("MongoDatabase"));
services.AddSingleton<IMongoClient>(provider => new MongoClient(config["MongoDatabase"]));
var pack = new ConventionPack();
pack.Add(new CamelCaseElementNameConvention());
ConventionRegistry.Register("Custom Convention", pack, t => true);
BsonClassMap.RegisterClassMap<Recipe>(cm =>
{
cm.AutoMap();
cm.SetIdMember(cm.GetMemberMap(c => c.Title));
});
return services;
}
}
}
我只是在 Github 上创建了一个存储库: myrepo
也许有人可以提供帮助并为回购做出贡献,这将非常有帮助并且非常感谢。
解决方案
推荐阅读
- r - GCP 上 Rstudio-server 的默认用户名/密码
- kubernetes - 了解删除有状态集
- python - 如何在mac上检测用户登录失败?
- apache-spark - Aws Emr Spark 使用胶水作为配置单元元存储?
- memory - 估计 AWS Neptune 上的图形数据库大小
- flutter - 如何在颤动中创建一个始终粘在页面底部的按钮?
- javascript - 几个图在flot中具有相同的图例
- ais - 如何根据 AIS 140 标准计算校验和
- python - 如何使用 igraph 将 numpy 数组转换为图形?
- rabbitmq - 业务数据在 RabbitMQ 消息中的位置