首页 > 解决方案 > 无法按照教程进行操作:连接到数据存储 [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

也许有人可以提供帮助并为回购做出贡献,这将非常有帮助并且非常感谢。

标签: c#.netmongodbasp.net-web-apiasp.net-core-webapi

解决方案


推荐阅读