示例#1
0
        public static void SaveSnack(SnackViewModel snackViewModel)
        {
            var sessionFactory = SessionFactoryFactory.GetFactory();

            using (var session = sessionFactory.OpenSession())
            {
                using (var trans = session.BeginTransaction())
                {
                    var snackModel = SnackMap.VmToModel(snackViewModel);
                    session.SaveOrUpdate(snackModel);
                    trans.Commit();
                }
            }
        }
        private void AddSnackCommand_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            // check if inputs filled
            if (string.IsNullOrWhiteSpace(snackNameInput.Text) || string.IsNullOrWhiteSpace(snackPriceInput.Text) || string.IsNullOrWhiteSpace(snackQuantityInput.Text))
            {
                MessageBox.Show("Bitte alle Werte angeben");
                return;
            }

            // check if values correct
            double snackPrice    = 0;
            int    snackQuantity = 0;

            if (!double.TryParse(snackPriceInput.Text, out snackPrice))
            {
                MessageBox.Show("Snackpreis ist ungültig");
                return;
            }

            if (!int.TryParse(snackQuantityInput.Text, out snackQuantity))
            {
                MessageBox.Show("Anzahl ist ungültig");
                return;
            }

            // add
            var newSnack = new SnackViewModel {
                Name = snackNameInput.Text, Quantity = snackQuantity, PricePerUnit = snackPrice
            };

            this.Snacks.Add(newSnack);

            Persister.SaveSnack(newSnack);

            // avoid INotifyProperty-Madness
            this.OverAllSumTextBlock.Text = this.OverallSum.ToString("#,##0.00 €;(#,##0.00) €");
        }
 public static Snack VmToModel(SnackViewModel vm)
 {
     return(new Snack {
         Date = vm.Date, Name = vm.Name, PricePerUnit = vm.PricePerUnit, Quantity = vm.Quantity
     });
 }