public void ProductPersistenceTest()
        {
            Guid productId;
              Product product = new Product()
                          {
                            Name = "Stuff",
                            Description = "Cool",
                            UnitPrice = 100M
                          };

              using (var session = SessionFactory.OpenSession())
              using (var tx = session.BeginTransaction())
              {
            productId = (Guid)session.Save(product);
            tx.Commit();
              }

              using (var session = SessionFactory.OpenSession())
              using (var tx = session.BeginTransaction())
              {
            product = session.Get<Product>(productId);
            product.Name = "Junk";
            tx.Commit();
              }
        }
Exemple #2
0
        private static void Main(string[] args)
        {
            XmlConfigurator.Configure();
            var log = LogManager.GetLogger(typeof (Program));

              SetupNHibernateValidator();

            var nhibernateConfig = new Configuration().Configure();
            nhibernateConfig.Initialize();

            ISessionFactory sessionFactory = nhibernateConfig.BuildSessionFactory();

            var schemaExport = new SchemaExport(nhibernateConfig);
            schemaExport.Execute(false, true, false);

            var junk = new Product
                       	{
                       		Name = "Spiffy Junk",
                       		Description = string.Empty,
                       		UnitPrice = -1M
                       	};

              var ve = Environment.SharedEngineProvider.GetEngine();
              var invalidValues = ve.Validate(junk);
              foreach (var invalidValue in invalidValues)
            log.InfoFormat("{0} {1}",
              invalidValue.PropertyName,
              invalidValue.Message);
        }
 public void Bind(Product product)
 {
     this.product = product;
       if (product == null) return;
       txtProductName.Text = product.Name;
       txtDescription.Text = product.Description;
       txtUnitPrice.Text = product.UnitPrice.ToString();
 }
Exemple #4
0
        static void Main(string[] args)
        {
            var kernel = new StandardKernel();
              kernel.Bind<IContextDataProvider>()
            .To<UsernameContextDataProvider>();
              var sl = new NinjectServiceLocator(kernel);
              ServiceLocator.SetLocatorProvider(() => sl);

              var namingStrategy = new NamingStrategy();
              var auditColumnSource = new CtxAuditColumnSource();
              var cfg = new Configuration().Configure();
              new TriggerAuditing(cfg, namingStrategy,
            auditColumnSource).Configure();

              var sessionFaculty = cfg.BuildSessionFactory();

              var se = new NHibernate.Tool.hbm2ddl.SchemaExport(cfg);
              se.Execute(true, true, false);

              var padlock = new Product()
              {
            Name = "Padlock",
            Description = "Secure, weather resistant",
            UnitPrice = 8.36M
              };
              Guid padlockId;

              using (var session = sessionFaculty.OpenSession())
              {
            using (var tx = session.BeginTransaction())
            {
              padlockId = (Guid)session.Save(padlock);
              tx.Commit();
            }
              }

              using (var session = sessionFaculty.OpenSession())
              {
            using (var tx = session.BeginTransaction())
            {
              padlock = session.Get<Product>(padlockId);
              padlock.UnitPrice = 0.10M;
              padlock.Description = "Not so secure, actually.";
              session.SaveOrUpdate(padlock);
              tx.Commit();
            }
              }

              using (var session = sessionFaculty.OpenSession())
              {
            using (var tx = session.BeginTransaction())
            {
              padlock = session.Load<Product>(padlockId);
              session.Delete(padlock);
              tx.Commit();
            }
              }
        }
Exemple #5
0
        static void Main(string[] args)
        {
            log4net.Config.XmlConfigurator.Configure();
              var nhConfig = new Configuration().Configure();
              var sessionFactory = nhConfig.BuildSessionFactory();

              var sessionA = sessionFactory.OpenSession();
              var sessionB = sessionFactory.OpenSession();

              Guid productId;
              Product productA;
              Product productB;

              productA = new Product()
              {
            Name = "Lawn Chair",
            Description = "Lime Green, Comfortable",
            UnitPrice = 10.00M
              };

              using (var tx = sessionA.BeginTransaction())
              {
            Console.WriteLine("Saving product.");
            productId = (Guid) sessionA.Save(productA);
            tx.Commit();
              }

              using (var tx = sessionB.BeginTransaction())
              {
            Console.WriteLine("Changing price.");
            productB = sessionB.Get<Product>(productId);
            productB.UnitPrice = 15.00M;
            tx.Commit();
              }

              using (var tx = sessionA.BeginTransaction())
              {
            Console.WriteLine("Price was {0:c}",
              productA.UnitPrice);

            sessionA.Refresh(productA);

            Console.WriteLine("Price is {0:c}",
              productA.UnitPrice);
            tx.Commit();
              }

              sessionA.Close();
              sessionB.Close();

              Console.ReadKey();
        }
Exemple #6
0
        static void Main(string[] args)
        {
            var cfg = new Configuration().Configure();
              var sessionFaculty = cfg.BuildSessionFactory();

              var se = new NHibernate.Tool.hbm2ddl.SchemaExport(cfg);
              se.Execute(true, true, false);

              var padlock = new Product()
              {
            Name = "Padlock",
            Description = "Secure, weather resistant",
            UnitPrice = 8.36M
              };
              Guid padlockId;

              using (var session = sessionFaculty.OpenSession())
              {
            using (var tx = session.BeginTransaction())
            {
              padlockId = (Guid)session.Save(padlock);
              tx.Commit();
            }
              }

              using (var session = sessionFaculty.OpenSession())
              {
            using (var tx = session.BeginTransaction())
            {
              padlock = session.Get<Product>(padlockId);
              padlock.UnitPrice = 0.10M;
              padlock.Description = "Not so secure, actually.";
              session.SaveOrUpdate(padlock);
              tx.Commit();
            }
              }

              using (var session = sessionFaculty.OpenSession())
              {
            using (var tx = session.BeginTransaction())
            {
              padlock = session.Load<Product>(padlockId);
              session.Delete(padlock);
              tx.Commit();
            }
              }
        }
 protected void btnCancel_Click(object sender, EventArgs e)
 {
     product = null;
       if (Cancelled != null)
     Cancelled(this, new EventArgs());
 }