Пример #1
0
        protected override void BeforeApplyChanges(SaveChangesContext context)
        {
            var tag     = context.SaveOptions.Tag?.ToString();
            var saveMap = context.SaveMap;

            if (tag == "CommentOrderShipAddress.Before")
            {
                var  orderInfos = saveMap[typeof(Order)];
                byte seq        = 1;
                foreach (var info in orderInfos)
                {
                    var order = (Order)info.ClientEntity;
                    AddComment(order.ShipAddress, seq++);
                }
            }
            else if (tag == "UpdateProduceShipAddress.Before")
            {
                var orderInfos = saveMap[typeof(Order)];
                var order      = (Order)orderInfos[0].ClientEntity;
                UpdateProduceDescription(order.ShipAddress);
            }
            else if (tag == "LookupEmployeeInSeparateContext.Before")
            {
                LookupEmployeeInSeparateContext(false);
            }
            else if (tag == "LookupEmployeeInSeparateContext.SameConnection.Before")
            {
                LookupEmployeeInSeparateContext(true);
            }
            else if (tag == "ValidationError.Before")
            {
                foreach (var type in saveMap.Keys)
                {
                    var list = saveMap[type];
                    foreach (var entityInfo in list)
                    {
                        var entity      = entityInfo.ClientEntity;
                        var entityError = new EntityError()
                        {
                            EntityTypeName = type.Name,
                            ErrorMessage   = "Error message for " + type.Name,
                            ErrorName      = "Server-Side Validation",
                        };
                        if (entity is Order)
                        {
                            var order = (Order)entity;
                            entityError.KeyValues    = new object[] { order.OrderID };
                            entityError.PropertyName = "OrderDate";
                        }
                    }
                }
            }
            else if (tag == "increaseProductPrice")
            {
                foreach (var type in saveMap.Keys)
                {
                    if (type == typeof(Category))
                    {
                        foreach (var entityInfo in saveMap[type])
                        {
                            if (entityInfo.EntityState == BreezeEntityState.Modified)
                            {
                                Category category = (entityInfo.ClientEntity as Category);
                                var      products = Products.Where(p => p.CategoryID == category.CategoryID);
                                foreach (var product in products)
                                {
                                    context.AddAdditionalEntity(product, BreezeEntityState.Modified);
                                    var incr = (Convert.ToInt64(product.UnitPrice) % 2) == 0 ? 1 : -1;
                                    product.UnitPrice += incr;
                                }
                            }
                        }
                    }
                }
            }
            else if (tag == "deleteProductOnServer.Before")
            {
                var prodinfo = saveMap[typeof(Product)].First();
                if (prodinfo.EntityState == BreezeEntityState.Added)
                {
                    // because Deleted throws error when trying delete non-existent row from database
                    prodinfo.EntityState = BreezeEntityState.Detached;
                }
                else
                {
                    prodinfo.EntityState = BreezeEntityState.Deleted;
                }
            }
            else if (tag == "deleteSupplierOnServer.Before")
            {
                var product = (Product)saveMap[typeof(Product)].First().ClientEntity;
                var infos   = GetEntityInfos(saveMap, typeof(Supplier));
                var supinfo = infos?.FirstOrDefault();
                if (supinfo != null)
                {
                    supinfo.EntityState = BreezeEntityState.Deleted;
                }
                else
                {
                    // create new EntityInfo for entity that we want to delete that was not in the save bundle
                    var supplier = Session.Get <Supplier>(product.SupplierID);
                    context.AddAdditionalEntity(supplier, BreezeEntityState.Deleted);
                }
            }
        }
Пример #2
0
        protected override void AfterFlushChanges(SaveChangesContext context, List <KeyMapping> keyMappings)
        {
            var saveMap = context.SaveMap;
            var tag     = context.SaveOptions.Tag?.ToString();

            if (tag == "CommentKeyMappings.After")
            {
                foreach (var km in keyMappings)
                {
                    var  realint = Convert.ToInt32(km.RealValue);
                    byte seq     = (byte)(realint % 512);
                    AddComment(km.EntityTypeName + ':' + km.RealValue, seq);
                }
            }
            else if (tag == "UpdateProduceKeyMapping.After")
            {
                if (!keyMappings.Any())
                {
                    throw new Exception("UpdateProduce.After: No key mappings available");
                }
                var km = keyMappings[0];
                UpdateProduceDescription(km.EntityTypeName + ':' + km.RealValue);
            }
            else if (tag == "LookupEmployeeInSeparateContext.After")
            {
                LookupEmployeeInSeparateContext(false);
            }
            else if (tag == "LookupEmployeeInSeparateContext.SameConnection.After")
            {
                LookupEmployeeInSeparateContext(true);
            }
            else if (tag == "deleteProductOnServer")
            {
                var t        = typeof(Product);
                var prodinfo = saveMap[t].First();
                prodinfo.EntityState = BreezeEntityState.Deleted;
            }
            else if (tag != null && tag.StartsWith("deleteProductOnServer:"))
            {
                // create new EntityInfo for entity that we want to delete that was not in the save bundle
                var id      = tag.Substring(tag.IndexOf(':') + 1);
                var product = new Product {
                    ProductID = int.Parse(id)
                };
                context.AddAdditionalEntity(product, BreezeEntityState.Deleted);
            }
            else if (tag == "deleteSupplierAndProductOnServer")
            {
                // mark deleted entities that are in the save bundle
                var t        = typeof(Product);
                var infos    = GetEntityInfos(saveMap, typeof(Product));
                var prodinfo = infos.FirstOrDefault();
                if (prodinfo != null)
                {
                    prodinfo.EntityState = BreezeEntityState.Deleted;
                }
                infos = GetEntityInfos(saveMap, typeof(Supplier));
                var supinfo = infos.FirstOrDefault();
                if (supinfo != null)
                {
                    supinfo.EntityState = BreezeEntityState.Deleted;
                }
            }
        }