Skip to content
  • P
    Projects
  • G
    Groups
  • S
    Snippets
  • Help

丁松杰 / Pole

  • This project
    • Loading...
  • Sign in
Go to a project
  • Project
  • Repository
  • Issues 0
  • Merge Requests 0
  • Pipelines
  • Wiki
  • Snippets
  • Members
  • Activity
  • Graph
  • Charts
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
  • Files
  • Commits
  • Branches
  • Tags
  • Contributors
  • Graph
  • Compare
  • Charts
Switch branch/tag
  • Pole
  • samples
  • apis
  • Backet.Api
  • Grains
  • AddBacketGrain.cs
Find file
BlameHistoryPermalink
  • dingsongjie's avatar
    重构 eventbus 的 代码结构 · 5511b5b8
    dingsongjie committed 5 years ago
    5511b5b8
AddBacketGrain.cs 1.83 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
using Backet.Api.Grains.Abstraction;
using Backet.Api.Infrastructure;
using Orleans;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Backet.Api.Domain.Event;
using Pole.Core.UnitOfWork;
using Pole.EventBus.UnitOfWork;
using Pole.EventBus;

namespace Backet.Api.Grains
{
    public class AddBacketGrain : Grain, IAddBacketGrain
    {
        public async Task<bool> AddBacket(BacketDto backetDto)
        {
            using (var scope = ServiceProvider.CreateScope())
            {
                var unitOfWork = scope.ServiceProvider.GetRequiredService<IUnitOfWork>();
                var dbContext = scope.ServiceProvider.GetRequiredService<BacketDbContext>();
                var bus = scope.ServiceProvider.GetRequiredService<IBus>();
                using (var transaction = await dbContext.Database.BeginTransactionAsync())
                {
                    unitOfWork.Enlist(transaction, bus);
                    Backet.Api.Domain.AggregatesModel.BacketAggregate.Backet backet = new Backet.Api.Domain.AggregatesModel.BacketAggregate.Backet
                    {
                        Id = backetDto.Id,
                        UserId = backetDto.UserId
                    };
                    if (backetDto.BacketItems == null || backetDto.BacketItems.Count == 0) return false;
                    backetDto.BacketItems.ForEach(item =>
                    {
                        backet.AddBacketItem(item.ProductId, item.ProductName, item.Price);
                    });
                    dbContext.Backets.Add(backet);
                    await bus.Publish(new BacketCreatedEvent() { BacketId = backet.Id });
                    await unitOfWork.CompeleteAsync();
                }
                return true;
            }
        }
    }
}