zoukankan      html  css  js  c++  java
  • 思考一种好的架构(八)

    库存业务服务库:

     [Table("Stock")]
        public class StockEntity
        {
            [ColumnAttribute(IsPrimaryKey = true)]
            [AutoIncrement]
            public int ID { get; set; }
    
            /// <summary>  
            /// 商品ID
            /// </summary>
            public int ProductID { get; set; }
    
            /// <summary> 
            /// 数量
            /// </summary>
            public int Number { get; set; }
        }
     public class StockServer : IStock
        {
            IDbContext DC;
            public StockServer(IDbContext DC)
            {
                this.DC = DC;
            }
    
            public IQuery<StockDTO> GetSingle(int productID)
            {
                return DC.Query<StockEntity>().Where(x => x.ProductID.Equals(productID)).Select(x=>new StockDTO() { 
                    ID=x.ID,
                    Number=x.Number,
                    ProductID=x.ProductID
                });
            }
    
            public bool ReducedInventory(int productID, int number)
            {
                var result = DC.Update<StockEntity>(x => x.ProductID.Equals(productID) && x.Number > 0, d => new StockEntity()
                {
                    Number = d.Number - number
                });
                if (result > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
    
            }
    
            public IQuery<StockDTO> StockNumberQuery(int? minNumber, int? maxNumber)
            {
                var result = DC.Query<StockEntity>()
                    .EmptyCheck(minNumber, x => x.Number >= minNumber)
                    .EmptyCheck(maxNumber, x => x.Number <= maxNumber);
                return result.Select(x => new StockDTO()
                {
                    ID = x.ID,
                    Number = x.Number,
                    ProductID = x.ProductID
                });
            }
        }
      public static void ConfigureServices(IServiceCollection services)
            {
                services.AddTransient<IStock, StockServer>();
            }
    
            public static void Configure(IApplicationBuilder app, IHostingEnvironment env)
            {
                
            }

    上节有提到库存服务,这节不再赘述

    订单服务:

     [APIRouth("Order")]
        [ApiController]
        public class OrderController : Controller
        {
            IOrder orderServer;
            public OrderController(IOrder orderServer)
            {
                this.orderServer = orderServer;
            }
           
            [HttpGet]
            [Command]
            public IActionResult GenerateOrder(int productID, int number)
            {
                var result = orderServer.GenerateOrder(productID, number);
                return Json(result);
            }
        }
     [ColumnAttribute(IsPrimaryKey =true)]
            [AutoIncrement]
            public int ID { get; set; }
    
            /// <summary>
            /// 商品ID
            /// </summary>
            public int ProductID { get; set; }
    
            /// <summary>
            /// 数量
            /// </summary>
            public int Number { get; set; }
    
            /// <summary>
            /// 创建时间
            /// </summary>
            public DateTime CreateTimer { get; set; }
    public class OrderServer : IOrder
        {
            IDbContext DC;
            IProduct product;
            IStock stock;
            IMediator mediator;
            public OrderServer(IDbContext DC, IProduct product, IStock stock, IMediator mediator)
            {
                this.mediator = mediator;
                this.product = product;
                this.DC = DC;
                this.stock = stock;
            }
            public bool GenerateOrder(int productID, int number)
            {
                var productDto = product.GetSingle(productID);
                if (productDto == null)
                {
                    throw new Exception("商品未找到");
                }
                if (!stock.ReducedInventory(productID, number))
                {
                    throw new Exception("库存不足");
                }
                if (new Random().Next(100) > 50)
                {
                    throw new Exception("测试回滚库存");
                }
                mediator.Publish(new OrderGenerationEvent(DateTime.Now, number) { });
                return true;
            }
    Command特性头表示这次请求时写操作请求

    这里有提到mediator,事件总线(EventBus)

    之前的CQRS也有提及,事件总线具体后续再说

     public static class Startup
        {
            public static void ConfigureServices(IServiceCollection services)
            {
                services.AddTransient<IOrder, OrderServer>();
            }
    
            public static void Configure(IApplicationBuilder app, IHostingEnvironment env)
            {
                
            }
        }

    订单服务 依赖:

     库存服务 依赖:

    这一节没啥好说的

  • 相关阅读:
    MCMC等采样算法
    【模拟退火】旅行商问题
    团队项目博客汇总
    2018年春季学期-助教总结
    linux简单命令常用随记
    记一次计算机网络作业
    [东北师大软工]Week2-作业2:个人项目实战 初步测试结果
    为什么你学过Java却忘光了——记第一次助教同学见面会
    ahk打印成pdf记录
    PSP总结报告
  • 原文地址:https://www.cnblogs.com/AnAng/p/12611391.html
Copyright © 2011-2022 走看看