Beispiel #1
0
        public async Task <bool> CreateOrderByEF(IOrder order)
        {
            using (var trans = DbContext.Database.BeginTransaction())
            {
                var orderEntity = new Order()
                {
                    ID          = GenerateOrderID(),
                    OrderUserID = order.OrderUserID,
                    OrderTime   = order.OrderTime,
                    OrderItems  = null,
                    ProductID   = order.ProductID // For demo use
                };

                DbContext.Orders.Add(orderEntity);
                await DbContext.SaveChangesAsync();

                // When using EF, no need to pass transaction
                var orderMessage = new OrderMessage()
                {
                    ID          = orderEntity.ID,
                    OrderUserID = orderEntity.OrderUserID,
                    OrderTime   = orderEntity.OrderTime,
                    OrderItems  = null,
                    ProductID   = orderEntity.ProductID // For demo use
                };

                await CapPublisher.PublishAsync(EventConstants.EVENT_NAME_CREATE_ORDER, orderMessage);

                trans.Commit();
            }

            return(true);
        }
        public async Task <bool> CreateOrderByEfAsync(IOrder order)
        {
            using (var trans = DbContext.Database.BeginTransaction())
            {
                var orderEntity = new Order
                {
                    ID          = Guid.NewGuid().ToString(),
                    OrderUserID = order.OrderUserID,
                    OrderTime   = order.OrderTime,
                    OrderItems  = null,
                    ProductID   = order.ProductID
                };

                DbContext.Orders.Add(orderEntity);
                await DbContext.SaveChangesAsync();

                var orderMessage = new OrderMessage()
                {
                    ID          = order.ID,
                    OrderUserID = order.OrderUserID,
                    OrderTime   = order.OrderTime,
                    OrderItems  = null,
                    MessageTime = DateTime.Now,
                    ProductID   = order.ProductID
                };

                await CapPublisher.PublishAsync(EventConstants.EVENT_NAME_CREATE_ORDER, orderMessage);

                trans.Commit();
            }
            return(true);
        }
        public async Task <bool> CreateOrderByDapperAsync(IOrder order)
        {
            using (var conn = new SqlConnection(ConnStr))
            {
                conn.Open();
                using (var trans = conn.BeginTransaction())
                {
                    string sql = @"INSERT INTO [dbo].[Orders](OrderID, OrderTime, OrderUserID, ProductID)
                                                                VALUES(@OrderID, @OrderTime, @OrderUserID, @ProductID)";
                    order.ID = Guid.NewGuid().ToString();
                    await conn.ExecuteAsync(sql, new
                    {
                        OrderId     = order.ID,
                        OrderTime   = DateTime.Now,
                        OrderUserID = order.OrderUserID,
                        ProductID   = order.ProductID
                    }, trans);

                    var orderMessage = new OrderMessage()
                    {
                        ID          = order.ID,
                        OrderUserID = order.OrderUserID,
                        OrderTime   = order.OrderTime,
                        OrderItems  = null,
                        MessageTime = DateTime.Now,
                        ProductID   = order.ProductID
                    };
                    await CapPublisher.PublishAsync(EventConstants.EVENT_NAME_CREATE_ORDER, orderMessage, trans);

                    trans.Commit();
                }
            }
            return(true);
        }
Beispiel #4
0
        public async Task <bool> CreateOrderByDapper(IOrder order)
        {
            using (var conn = new MySqlConnection(ConnStr))
            {
                conn.Open();
                using (var trans = conn.BeginTransaction())
                {
                    // business code here
                    string sqlCommand = @"INSERT INTO orders(OrderID, OrderTime, OrderUserID, ProductID)
                                                                VALUES(@OrderID, @OrderTime, @OrderUserID, @ProductID)";
                    //IDataReader abc = await conn.ExecuteReaderAsync(sqlCommand);
                    //string ab;
                    //while (abc.Read())
                    //{
                    //    ab = abc["a1"].ToString();
                    //}
                    var query = conn.QueryAsync <Order>(sqlCommand);

                    order.ID = GenerateOrderID();
                    await conn.ExecuteAsync(sqlCommand, param : new
                    {
                        OrderID     = order.ID,
                        OrderTime   = DateTime.Now,
                        OrderUserID = order.OrderUserID,
                        ProductID   = order.ProductID
                    }, transaction : trans);

                    // For Dapper/ADO.NET, need to pass transaction
                    var orderMessage = new OrderMessage()
                    {
                        ID          = order.ID,
                        OrderUserID = order.OrderUserID,
                        OrderTime   = order.OrderTime,
                        OrderItems  = null,
                        MessageTime = DateTime.Now,
                        ProductID   = order.ProductID // For demo use
                    };

                    await CapPublisher.PublishAsync(EventConstants.EVENT_NAME_CREATE_ORDER, orderMessage, trans);

                    trans.Commit();
                }
            }

            return(true);
        }
Beispiel #5
0
        public async Task <bool> CreateOrderByDapper(IOrder order)
        {
            using (var conn = new SqlConnection(ConnStr))
            {
                conn.Open();
                using (var trans = conn.BeginTransaction())
                {
                    // business code here
                    string sqlCommand = @"INSERT INTO [dbo].[Orders](OrderID, OrderTime, OrderUserID, ProductID)
                                                                VALUES(@OrderID, @OrderTime, @OrderUserID, @ProductID)";

                    order.ID = GenerateOrderID();
                    await conn.ExecuteAsync(sqlCommand, param : new
                    {
                        OrderID     = order.ID,
                        OrderTime   = DateTime.Now,
                        OrderUserID = order.OrderUserID,
                        ProductID   = order.ProductID
                    }, transaction : trans);

                    // For Dapper/ADO.NET, need to pass transaction
                    var orderMessage = new OrderMessage()
                    {
                        ID          = order.ID,
                        OrderUserID = order.OrderUserID,
                        OrderTime   = order.OrderTime,
                        OrderItems  = null,
                        MessageTime = DateTime.Now,
                        ProductID   = order.ProductID // For demo use
                    };

                    await CapPublisher.PublishAsync(EventConstants.EVENT_NAME_CREATE_ORDER, orderMessage, trans);

                    trans.Commit();
                }
            }

            return(true);
        }
Beispiel #6
0
        public async Task Register(string @event)
        {
            Logger.LogInformation(@event);
            try
            {
                var httpContext = Context.GetHttpContext();
                var query       = httpContext.Request.Query;

                if (!query.TryGetValue("api_key", out var values))
                {
                    throw new ArgumentException("Не найден api_key");
                }

                var apiKey   = values.First();
                var tenantId = DataProtector.Unprotect(apiKey);

                var message = new EventMessage
                {
                    TenantId = Guid.Parse(tenantId),
                    Event    = @event
                };

                var headers = new CapHeaders
                {
                    MessageKey        = tenantId,
                    EnableIdempotence = true
                };

                await CapPublisher.PublishAsync(KafkaTopics.Events, message, headers);
            }
            catch (Exception e)
            {
                Logger.LogError(e.Message);
                throw new Exception("Не удалось обработать эвент", e);
            }
        }