.NET 云原生架构师训练营(模块二 基础巩固 MongoDB API重构)--学习笔记

本文涉及的产品
云数据库 MongoDB,独享型 2核8GB
推荐场景:
构建全方位客户视图
简介: - Lighter.Domain- Lighter.Application.Contract- Lighter.Application- LighterApi- Lighter.Application.Tests

2.5.8 MongoDB -- API重构

  • Lighter.Domain
  • Lighter.Application.Contract
  • Lighter.Application
  • LighterApi
  • Lighter.Application.Tests

Lighter.Domain

将数据实体转移到 Lighter.Domain 层

36.jpg

Lighter.Application.Contract

将业务从controller 抽取到 Lighter.Application 层,并为业务建立抽象接口 Lighter.Application.Contract层

37.jpg

IQuestionService

namespace Lighter.Application.Contracts
{
    public interface IQuestionService
    {
        Task<Question> GetAsync(string id, CancellationToken cancellationToken);
        Task<QuestionAnswerReponse> GetWithAnswerAsync(string id, CancellationToken cancellationToken);
        Task<List<Question>> GetListAsync(List<string> tags, CancellationToken cancellationToken, string sort = "createdAt", int skip = 0, int limit = 10);
        Task<Question> CreateAsync(Question question, CancellationToken cancellationToken);
        Task UpdateAsync(string id, QuestionUpdateRequest request, CancellationToken cancellationToken);
        Task<Answer> AnswerAsync(string id, AnswerRequest request, CancellationToken cancellationToken);
        Task CommentAsync(string id, CommentRequest request, CancellationToken cancellationToken);
        Task UpAsync(string id, CancellationToken cancellationToken);
        Task DownAsync(string id, CancellationToken cancellationToken);
    }
}

Lighter.Application

实现业务接口

38.jpg

QuestionService

namespace Lighter.Application
{
    public class QuestionService : IQuestionService
    {
        private readonly IMongoCollection<Question> _questionCollection;
        private readonly IMongoCollection<Vote> _voteCollection;
        private readonly IMongoCollection<Answer> _answerCollection;

        public QuestionService(IMongoClient mongoClient)
        {
            var database = mongoClient.GetDatabase("lighter");

            _questionCollection = database.GetCollection<Question>("questions");
            _voteCollection = database.GetCollection<Vote>("votes");
            _answerCollection = database.GetCollection<Answer>("answers");
        }

        public async Task<Question> GetAsync(string id, CancellationToken cancellationToken)
        {
            // linq 查询
            var question = await _questionCollection.AsQueryable()
                .FirstOrDefaultAsync(q => q.Id == id, cancellationToken: cancellationToken);

            //// mongo 查询表达式
            ////var filter = Builders<Question>.Filter.Eq(q => q.Id, id);

            //// 构造空查询条件的表达式
            //var filter = string.IsNullOrEmpty(id)
            //    ? Builders<Question>.Filter.Empty
            //    : Builders<Question>.Filter.Eq(q => q.Id, id);

            //// 多段拼接 filter
            //var filter2 = Builders<Question>.Filter.And(filter, Builders<Question>.Filter.Eq(q => q.TenantId, "001"));
            //await _questionCollection.Find(filter).FirstOrDefaultAsync(cancellationToken);

            return question;
        }

        public async Task<List<Question>> GetListAsync(List<string> tags, CancellationToken cancellationToken, string sort = "createdAt", int skip = 0, int limit = 10)
        {
            //// linq 查询
            //await _questionCollection.AsQueryable().Where(q => q.ViewCount > 10)
            //    .ToListAsync(cancellationToken: cancellationToken);

            var filter = Builders<Question>.Filter.Empty;

            if (tags != null && tags.Any())
            {
                filter = Builders<Question>.Filter.AnyIn(q => q.Tags, tags);
            }

            var sortDefinition = Builders<Question>.Sort.Descending(new StringFieldDefinition<Question>(sort));

            var result = await _questionCollection
                .Find(filter)
                .Sort(sortDefinition)
                .Skip(skip)
                .Limit(limit)
                .ToListAsync(cancellationToken: cancellationToken);

            return result;
        }

        public async Task<QuestionAnswerReponse> GetWithAnswerAsync(string id, CancellationToken cancellationToken)
        {
            // linq 查询
            var query = from question in _questionCollection.AsQueryable()
                where question.Id == id
                join a in _answerCollection.AsQueryable() on question.Id equals a.QuestionId into answers
                select new { question, answers };

            var result = await query.FirstOrDefaultAsync(cancellationToken);

            //// mongo 查询表达式
            //var result = await _questionCollection.Aggregate()
            //    .Match(q => q.Id == id)
            //    .Lookup<Answer, QuestionAnswerReponse>(
            //        foreignCollectionName: "answers",
            //        localField: "answers",
            //        foreignField: "questionId",
            //        @as: "AnswerList")
            //    .FirstOrDefaultAsync(cancellationToken: cancellationToken);

            return new QuestionAnswerReponse {AnswerList = result.answers};
        }

        public async Task<Answer> AnswerAsync(string id, AnswerRequest request, CancellationToken cancellationToken)
        {
            var answer = new Answer { QuestionId = id, Content = request.Content, Id = Guid.NewGuid().ToString() };
            _answerCollection.InsertOneAsync(answer, cancellationToken);

            var filter = Builders<Question>.Filter.Eq(q => q.Id, id);
            var update = Builders<Question>.Update.Push(q => q.Answers, answer.Id);

            await _questionCollection.UpdateOneAsync(filter, update, null, cancellationToken);

            return answer;
        }

        public async Task CommentAsync(string id, CommentRequest request, CancellationToken cancellationToken)
        {
            var filter = Builders<Question>.Filter.Eq(q => q.Id, id);
            var update = Builders<Question>.Update.Push(q => q.Comments,
                new Comment { Content = request.Content, CreatedAt = DateTime.Now });

            await _questionCollection.UpdateOneAsync(filter, update, null, cancellationToken);
        }

        public async Task<Question> CreateAsync(Question question, CancellationToken cancellationToken)
        {
            question.Id = Guid.NewGuid().ToString();
            await _questionCollection.InsertOneAsync(question, new InsertOneOptions { BypassDocumentValidation = false },
                cancellationToken);
            return question;
        }

        public async Task DownAsync(string id, CancellationToken cancellationToken)
        {
            var vote = new Vote
            {
                Id = Guid.NewGuid().ToString(),
                SourceType = ConstVoteSourceType.Question,
                SourceId = id,
                Direction = EnumVoteDirection.Down
            };

            await _voteCollection.InsertOneAsync(vote, cancellationToken);

            var filter = Builders<Question>.Filter.Eq(q => q.Id, id);
            var update = Builders<Question>.Update.Inc(q => q.VoteCount, -1).AddToSet(q => q.VoteDowns, vote.Id);
            await _questionCollection.UpdateOneAsync(filter, update);
        }

        public async Task UpAsync(string id, CancellationToken cancellationToken)
        {
            var vote = new Vote
            {
                Id = Guid.NewGuid().ToString(),
                SourceType = ConstVoteSourceType.Question,
                SourceId = id,
                Direction = EnumVoteDirection.Up
            };

            await _voteCollection.InsertOneAsync(vote, cancellationToken);

            var filter = Builders<Question>.Filter.Eq(q => q.Id, id);
            var update = Builders<Question>.Update.Inc(q => q.VoteCount, 1).AddToSet(q => q.VoteUps, vote.Id);
            await _questionCollection.UpdateOneAsync(filter, update);
        }

        public async Task UpdateAsync(string id, QuestionUpdateRequest request, CancellationToken cancellationToken)
        {
            var filter = Builders<Question>.Filter.Eq(q => q.Id, id);

            //var update = Builders<Question>.Update
            //    .Set(q => q.Title, request.Title)
            //    .Set(q => q.Content, request.Content)
            //    .Set(q => q.Tags, request.Tags)
            //    .Push(q => q.Comments, new Comment {Content = request.Summary, CreatedAt = DateTime.Now});

            var updateFieldList = new List<UpdateDefinition<Question>>();

            if (!string.IsNullOrWhiteSpace(request.Title))
                updateFieldList.Add(Builders<Question>.Update.Set(q => q.Title, request.Title));

            if (!string.IsNullOrWhiteSpace(request.Content))
                updateFieldList.Add(Builders<Question>.Update.Set(q => q.Content, request.Content));

            if (request.Tags != null && request.Tags.Any())
                updateFieldList.Add(Builders<Question>.Update.Set(q => q.Tags, request.Tags));

            updateFieldList.Add(Builders<Question>.Update.Push(q => q.Comments,
                new Comment { Content = request.Summary, CreatedAt = DateTime.Now }));

            var update = Builders<Question>.Update.Combine(updateFieldList);

            await _questionCollection.UpdateOneAsync(filter, update, cancellationToken: cancellationToken);
        }
    }
}

LighterApi

注册服务

Startup

services.AddScoped<IQuestionService, QuestionService>()
        .AddScoped<IAnswerService, AnswerService>();

调用服务

QuestionController

namespace LighterApi.Controller
{
    [ApiController]
    [Route("api/[controller]")]
    public class QuestionController : ControllerBase
    {
        private readonly IQuestionService _questionService;

        public QuestionController(IQuestionService questionService)
        {
            _questionService = questionService;
        }

        [HttpGet]
        [Route("{id}")]
        public async Task<ActionResult<Question>> GetAsync(string id, CancellationToken cancellationToken)
        {
            var question = await _questionService.GetAsync(id, cancellationToken);

            if (question == null)
                return NotFound();

            return Ok(question);
        }

        [HttpGet]
        [Route("{id}/answers")]
        public async Task<ActionResult> GetWithAnswerAsync(string id, CancellationToken cancellationToken)
        {
            var result = await _questionService.GetWithAnswerAsync(id, cancellationToken);

            if (result == null)
                return NotFound();

            return Ok(result);
        }

        [HttpGet]
        public async Task<ActionResult<List<Question>>> GetListAsync([FromQuery] List<string> tags,
            CancellationToken cancellationToken, [FromQuery] string sort = "createdAt", [FromQuery] int skip = 0,
            [FromQuery] int limit = 10)
        {
            var result = await _questionService.GetListAsync(tags, cancellationToken, sort, skip, limit);
            return Ok(result);
        }

        [HttpPost]
        public async Task<ActionResult<Question>> CreateAsync([FromBody] Question question, CancellationToken cancellationToken)
        {
            question = await _questionService.CreateAsync(question, cancellationToken);
            return StatusCode((int) HttpStatusCode.Created, question);
        }

        [HttpPatch]
        [Route("{id}")]
        public async Task<ActionResult> UpdateAsync([FromRoute] string id, [FromBody] QuestionUpdateRequest request, CancellationToken cancellationToken)
        {
            if (string.IsNullOrEmpty(request.Summary))
                throw new ArgumentNullException(nameof(request.Summary));

            await _questionService.UpdateAsync(id, request, cancellationToken);
            return Ok();
        }

        [HttpPost]
        [Route("{id}/answer")]
        public async Task<ActionResult<Answer>> AnswerAsync([FromRoute] string id, [FromBody] AnswerRequest request, CancellationToken cancellationToken)
        {
            var answer = await _questionService.AnswerAsync(id, request, cancellationToken);
            return Ok(answer);
        }

        [HttpPost]
        [Route("{id}/comment")]
        public async Task<ActionResult> CommentAsync([FromRoute] string id, [FromBody] CommentRequest request, CancellationToken cancellationToken)
        {
            await _questionService.CommentAsync(id, request, cancellationToken);
            return Ok();
        }

        [HttpPost]
        [Route("{id}/up")]
        public async Task<ActionResult> UpAsync([FromBody] string id, CancellationToken cancellationToken)
        {
            await _questionService.UpAsync(id, cancellationToken);
            return Ok();
        }

        [HttpPost]
        [Route("{id}/down")]
        public async Task<ActionResult> DownAsync([FromBody] string id, CancellationToken cancellationToken)
        {
            await _questionService.DownAsync(id, cancellationToken);
            return Ok();
        }
    }
}

Lighter.Application.Tests

建立单元测试项目,测试Lihgter.Application(需要使用到xunit、Mongo2go)

39.jpg

Mongo2go:内存级别引擎

访问 Mongo 内存数据库

SharedFixture

namespace Lighter.Application.Tests
{
    public class SharedFixture:IAsyncLifetime
    {
        private MongoDbRunner _runner;
        public MongoClient Client { get; private set; }
        public IMongoDatabase Database { get; private set; }

        public async Task InitializeAsync()
        {
            _runner = MongoDbRunner.Start();
            Client = new MongoClient(_runner.ConnectionString);
            Database = Client.GetDatabase("db");

            //var hostBuilder = Program.CreateWebHostBuilder(new string[0]);
            //var host = hostBuilder.Build();
            //ServiceProvider = host.Services;
        }

        public Task DisposeAsync()
        {
            _runner?.Dispose();
            _runner = null;
            return Task.CompletedTask;
        }
    }
}

QuestionServiceTests

namespace Lighter.Application.Tests
{

    [Collection(nameof(SharedFixture))]
    public class QuestionServiceTests
    {
        private readonly SharedFixture _fixture;

        private readonly QuestionService _questionService;
        public QuestionServiceTests(SharedFixture fixture)
        {
            _fixture = fixture;
            _questionService = new QuestionService(_fixture.Client);
        }

        private async Task<Question> CreateOrGetOneQuestionWithNoAnswerAsync()
        {
            var collection = _fixture.Database.GetCollection<Question>("question");
            var filter = Builders<Question>.Filter.Size(q => q.Answers, 0);
            var question = await collection.Find(filter).FirstOrDefaultAsync();

            if (question != null)
                return question;

            question = new Question { Title = "问题一" };
            return await _questionService.CreateAsync(question, CancellationToken.None);
        }

        private async Task<QuestionAnswerReponse> CreateOrGetOneQuestionWithAnswerAsync()
        {
            var collection = _fixture.Database.GetCollection<Question>("question");
            var filter = Builders<Question>.Filter.SizeGt(q => q.Answers, 0);
            var question = await collection.Find(filter).FirstOrDefaultAsync();

            if (question != null)
                return await _questionService.GetWithAnswerAsync(question.Id, CancellationToken.None);

            // 不存在则创建一个没有回答的问题,再添加一个答案
            question = await CreateOrGetOneQuestionWithNoAnswerAsync();
            var answer = new AnswerRequest { Content = "问题一的回答一" };
            await _questionService.AnswerAsync(question.Id, answer, CancellationToken.None);

            return await _questionService.GetWithAnswerAsync(question.Id, CancellationToken.None);
        }

        [Fact]
        public async Task GetAsync_WrongId_ShoudReturnNull()
        {
            var result = await _questionService.GetAsync("empty", CancellationToken.None);
            result.Should().BeNull();
        }

        [Fact]
        public async Task CreateAsync_Right_ShouldBeOk()
        {
            var question = await CreateOrGetOneQuestionWithNoAnswerAsync();
            question.Should().NotBeNull();

            var result = await _questionService.GetAsync(question.Id, CancellationToken.None);
            question.Title.Should().Be(result.Title);
        }

        [Fact]
        public async Task AnswerAsync_Right_ShouldBeOk()
        {
            var question = await CreateOrGetOneQuestionWithNoAnswerAsync();
            question.Should().NotBeNull();

            var answer = new AnswerRequest { Content = "问题一的回答一" };
            await _questionService.AnswerAsync(question.Id, answer, CancellationToken.None);

            var questionWithAnswer = await _questionService.GetWithAnswerAsync(question.Id, CancellationToken.None);

            questionWithAnswer.Should().NotBeNull();
            questionWithAnswer.AnswerList.Should().NotBeEmpty();
            questionWithAnswer.AnswerList.First().Content.Should().Be(answer.Content);
        }

        [Fact]
        public async Task UpAsync_Right_ShouldBeOk()
        {
            var before = await CreateOrGetOneQuestionWithNoAnswerAsync();
            await _questionService.UpAsync(before.Id, CancellationToken.None);

            var after = await _questionService.GetAsync(before.Id, CancellationToken.None);
            after.Should().NotBeNull();
            after.VoteCount.Should().Be(before.VoteCount+1);
            after.VoteUps.Count.Should().Be(1);
        }

        [Fact]
        public async Task DownAsync_Right_ShouldBeOk()
        {
            var before = await CreateOrGetOneQuestionWithNoAnswerAsync();
            await _questionService.DownAsync(before.Id, CancellationToken.None);

            var after = await _questionService.GetAsync(before.Id, CancellationToken.None);
            after.Should().NotBeNull();
            after.VoteCount.Should().Be(before.VoteCount-1);
            after.VoteDowns.Count.Should().Be(1);
        }

        public async Task UpdateAsync_WithNoSummary_ShoudThrowException()
        {
            var before = await CreateOrGetOneQuestionWithNoAnswerAsync();
            var updateRequest = new QuestionUpdateRequest { Title = before.Title + "-updated" };
            await _questionService.UpdateAsync(before.Id, updateRequest, CancellationToken.None);

            var after = await _questionService.GetAsync(before.Id, CancellationToken.None);
            after.Should().NotBeNull();
            after.Title.Should().Be(updateRequest.Title);
        }

        [Fact]
        public async Task UpdateAsync_Right_ShoudBeOk()
        {
            var before = await CreateOrGetOneQuestionWithNoAnswerAsync();
            var updateRequest = new QuestionUpdateRequest { Title = before.Title + "-updated", Summary ="summary" };
            await _questionService.UpdateAsync(before.Id, updateRequest , CancellationToken.None);

            var after = await _questionService.GetAsync(before.Id, CancellationToken.None);
            after.Should().NotBeNull();
            after.Title.Should().Be(updateRequest.Title);
        }

        [Fact]
        public async Task UpdateAsync_Right_CommentsShouldAppend()
        {
            var before = await CreateOrGetOneQuestionWithNoAnswerAsync();
            var updateRequest = new QuestionUpdateRequest { Title = before.Title + "-updated", Summary = "summary" };
            await _questionService.UpdateAsync(before.Id, updateRequest, CancellationToken.None);

            var after = await _questionService.GetAsync(before.Id, CancellationToken.None);
            after.Comments.Should().NotBeEmpty();
            after.Comments.Count.Should().Be(before.Comments.Count+1);
        }
    }
}

运行单元测试

40.jpg

GitHub源码链接:

https://github.com/MINGSON666/Personal-Learning-Library/tree/main/ArchitectTrainingCamp

相关实践学习
MongoDB数据库入门
MongoDB数据库入门实验。
快速掌握 MongoDB 数据库
本课程主要讲解MongoDB数据库的基本知识,包括MongoDB数据库的安装、配置、服务的启动、数据的CRUD操作函数使用、MongoDB索引的使用(唯一索引、地理索引、过期索引、全文索引等)、MapReduce操作实现、用户管理、Java对MongoDB的操作支持(基于2.x驱动与3.x驱动的完全讲解)。 通过学习此课程,读者将具备MongoDB数据库的开发能力,并且能够使用MongoDB进行项目开发。 &nbsp; 相关的阿里云产品:云数据库 MongoDB版 云数据库MongoDB版支持ReplicaSet和Sharding两种部署架构,具备安全审计,时间点备份等多项企业能力。在互联网、物联网、游戏、金融等领域被广泛采用。 云数据库MongoDB版(ApsaraDB for MongoDB)完全兼容MongoDB协议,基于飞天分布式系统和高可靠存储引擎,提供多节点高可用架构、弹性扩容、容灾、备份回滚、性能优化等解决方案。 产品详情: https://www.aliyun.com/product/mongodb
目录
相关文章
|
22天前
|
运维 Cloud Native 持续交付
深入理解云原生架构及其在现代企业中的应用
随着数字化转型的浪潮席卷全球,企业正面临着前所未有的挑战与机遇。云计算技术的迅猛发展,特别是云原生架构的兴起,正在重塑企业的IT基础设施和软件开发模式。本文将深入探讨云原生的核心概念、关键技术以及如何在企业中实施云原生策略,以实现更高效的资源利用和更快的市场响应速度。通过分析云原生架构的优势和面临的挑战,我们将揭示它如何助力企业在激烈的市场竞争中保持领先地位。
|
20天前
|
Kubernetes Cloud Native 微服务
探索云原生技术:容器化与微服务架构的融合之旅
本文将带领读者深入了解云原生技术的核心概念,特别是容器化和微服务架构如何相辅相成,共同构建现代软件系统。我们将通过实际代码示例,探讨如何在云平台上部署和管理微服务,以及如何使用容器编排工具来自动化这一过程。文章旨在为开发者和技术决策者提供实用的指导,帮助他们在云原生时代中更好地设计、部署和维护应用。
|
1月前
|
Kubernetes Cloud Native 安全
云原生架构的演进与实践
随着云计算技术的不断发展,云原生架构已成为现代软件开发的核心趋势。本文旨在探讨云原生架构的演变历程、核心理念及在实际项目中的应用案例。通过对Kubernetes、Docker等关键技术的分析,结合微服务架构的设计原则,本文将揭示如何构建高效、可扩展且易于维护的云原生应用。
50 10
|
19天前
|
运维 Cloud Native 持续交付
云原生技术深度探索:重塑现代IT架构的无形之力####
本文深入剖析了云原生技术的核心概念、关键技术组件及其对现代IT架构变革的深远影响。通过实例解析,揭示云原生如何促进企业实现敏捷开发、弹性伸缩与成本优化,为数字化转型提供强有力的技术支撑。不同于传统综述,本摘要直接聚焦于云原生技术的价值本质,旨在为读者构建一个宏观且具体的技术蓝图。 ####
|
1月前
|
Kubernetes Cloud Native Docker
云原生之旅:从传统架构到容器化服务的演变
随着技术的快速发展,云计算已经从简单的虚拟化服务演进到了更加灵活和高效的云原生时代。本文将带你了解云原生的概念、优势以及如何通过容器化技术实现应用的快速部署和扩展。我们将以一个简单的Python Web应用为例,展示如何利用Docker容器进行打包和部署,进而探索Kubernetes如何管理这些容器,确保服务的高可用性和弹性伸缩。
|
26天前
|
Cloud Native 持续交付 云计算
云原生技术在现代IT架构中的转型力量####
本文深入剖析了云原生技术的精髓,探讨其在现代IT架构转型中的关键作用与实践路径。通过具体案例分析,展示了云原生如何赋能企业实现更高效的资源利用、更快的迭代速度以及更强的系统稳定性,为读者提供了一套可借鉴的实施框架与策略。 ####
24 0
|
29天前
|
消息中间件 运维 Cloud Native
云原生架构下的微服务优化策略####
本文深入探讨了云原生环境下微服务架构的优化路径,针对服务拆分、通信效率、资源管理及自动化运维等核心环节提出了具体的优化策略。通过案例分析与最佳实践分享,旨在为开发者提供一套系统性的解决方案,以应对日益复杂的业务需求和快速变化的技术挑战,助力企业在云端实现更高效、更稳定的服务部署与运营。 ####
|
1月前
|
Cloud Native 持续交付 云计算
深入理解云原生技术及其在现代IT架构中的应用
在数字化浪潮的推动下,云原生技术已成为企业转型的关键。本文将通过浅显易懂的语言和生动的比喻,带领读者探索云原生的核心概念、优势以及如何在企业中实现云原生架构。我们将一起揭开云原生的神秘面纱,了解它如何助力企业快速适应市场变化,提升业务的灵活性和创新能力。
|
1月前
|
Cloud Native Devops 持续交付
云原生架构的演进与实践
本文深入探讨了云原生架构的核心概念、技术组件及其在现代软件开发中的应用。通过分析容器化、微服务、持续集成/持续部署(CI/CD)等关键技术,揭示了这些技术如何共同促进应用程序的灵活性、可扩展性和高可用性。文章还讨论了云原生架构实施过程中面临的挑战和最佳实践,旨在为开发者和企业提供一套实用的指导方针,以便更有效地利用云计算资源,加速数字化转型的步伐。
46 5
|
1月前
|
消息中间件 监控 Cloud Native
云原生架构下的数据一致性挑战与解决方案####
在数字化转型加速的今天,云原生架构以其轻量级、弹性伸缩和高可用性成为企业IT架构的首选。然而,在享受其带来的灵活性的同时,数据一致性问题成为了不可忽视的挑战。本文探讨了云原生环境中数据一致性的复杂性,分析了导致数据不一致的根本原因,并提出了几种有效的解决策略,旨在为开发者和企业提供实践指南,确保在动态变化的云环境中保持数据的完整性和准确性。 ####