コード例 #1
0
        public int Save(SimulationModel model)
        {
            ProjectBroker    projectBroker    = new ProjectBroker();
            SupplierBroker   supplierBroker   = new SupplierBroker();
            ShopBroker       shopBroker       = new ShopBroker();
            BuyerBroker      buyerBroker      = new BuyerBroker();
            ConnectionBroker connectionBroker = new ConnectionBroker();

            //najpierw zapis podczas ktorego zbieram
            using (IDbConnection connection = new ConnectionProvider().GetConnection(true))
            {
                IDbTransaction transaction = connection.BeginTransaction();
                try
                {
                    if (model.Project.Id != 0)
                    {
                        buyerBroker.RemoveNotExistingFromProject(connection, model.Project.Id, model.GetListOfBuyersIds());
                        shopBroker.RemoveNotExistingFromProject(connection, model.Project.Id, model.GetListOfShopsIds());
                        supplierBroker.RemoveNotExistingFromProject(connection, model.Project.Id, model.GetListOfSuppliersIds());
                        connectionBroker.RemoveNotExistingFromProject(connection, model.Project.Id, model.GetListOfConnectionsIds());
                    }

                    projectBroker.Save(connection, model.Project, transaction);

                    foreach (Buyer buyer in model.Buyers)
                    {
                        buyer.ProjectId = model.Project.Id;
                        buyerBroker.Save(connection, buyer, transaction);
                    }

                    foreach (Shop shop in model.Shops)
                    {
                        shop.ProjectId = model.Project.Id;
                        shopBroker.Save(connection, shop, transaction);
                    }

                    foreach (Supplier supplier in model.Suppliers)
                    {
                        supplier.ProjectId = model.Project.Id;
                        supplierBroker.Save(connection, supplier, transaction);
                    }

                    foreach (Connection conn in model.Connections)
                    {
                        conn.ProjectId = model.Project.Id;
                        conn.ActorAId  = conn.ActorA.Id;
                        conn.ActorBId  = conn.ActorB.Id;
                        connectionBroker.Save(connection, conn, transaction);
                    }

                    transaction.Commit();
                }
                catch (Exception)
                {
                    transaction.Rollback();
                    throw;
                }
            }
            return(1);
        }