/// <summary>
        /// Delete data (the current instance) using the CSLA Data Portal.
        /// </summary>
        /// <remarks>
        /// This method is called when a Business Object is deferred deleted.
        /// It actually removes the data from the database.
        /// </remarks>
        protected override void DataPortal_DeleteSelf()
        {
            // Get an NHibernate session factory
            ISessionFactory sessionFactory = Cfg.GetSessionFactory(DatabaseKey);

            // Open an NHibernate session from the factory
            using (ISession session = sessionFactory.OpenSession())
            {
                // Begin a transaction on the session
                using (ITransaction transaction = session.BeginTransaction())
                {
                    try
                    {
                        // Delete the current instance (using the open session)...
                        Delete(session);

                        // ...then commit the transaction
                        transaction.Commit();
                    }
                    catch
                    {
                        transaction.Rollback();
                        throw;
                    }
                }
            }
        }
        /// <summary>
        /// Insert or update using NHibernate.
        /// </summary>
        private void DoInsertOrUpdate()
        {
            // Get an NHibernate session factory
            ISessionFactory sessionFactory = Cfg.GetSessionFactory(DatabaseKey);

            // Open an NHibernate session from the factory
            using (ISession session = sessionFactory.OpenSession())
            {
                // Begin a transaction on the session
                using (ITransaction transaction = session.BeginTransaction())
                {
                    try
                    {
                        // Save some data...
                        Save(session);

                        // ...then commit the transaction
                        transaction.Commit();
                    }
                    catch
                    {
                        transaction.Rollback();
                        throw;
                    }
                }
            }
        }
        /// <summary>
        /// Update data using the CSLA Data Portal.
        /// </summary>
        protected override void DataPortal_Update()
        {
            // Get an NHibernate session factory
            ISessionFactory sessionFactory = Cfg.GetSessionFactory(DatabaseKey);

            // Open an NHibernate session from the factory
            using (ISession session = sessionFactory.OpenSession())
            {
                // Begin a transaction on the session
                using (ITransaction transaction = session.BeginTransaction())
                {
                    try
                    {
                        // Stop raising events while the list is modified
                        RaiseListChangedEvents = false;

                        // Save some data...
                        Save(session);

                        // ...then commit the transaction
                        transaction.Commit();
                    }
                    catch
                    {
                        transaction.Rollback();
                        throw;
                    }
                    finally
                    {
                        // Start raising events again
                        RaiseListChangedEvents = true;
                    }
                }
            }
        }
        /// <summary>
        /// Fetch data using the CSLA Data Portal.
        /// </summary>
        /// <param name="criteria">The Business Object criteria to identify what to fetch.</param>
        protected override void DataPortal_Fetch(object criteria)
        {
            // Get an NHibernate session factory
            ISessionFactory sessionFactory = Cfg.GetSessionFactory(DatabaseKey);

            // Open an NHibernate session from the factory
            using (ISession session = sessionFactory.OpenSession())
            {
                // Fetch the Business Object
                Fetch(criteria, session);
            }
        }
Example #5
0
        /// <summary>
        /// Fetch data using the CSLA Data Portal.
        /// </summary>
        /// <param name="criteria">The Business Object criteria to identify what to fetch.</param>
        protected override void DataPortal_Fetch(object criteria)
        {
            // Get an NHibernate session factory
            ISessionFactory sessionFactory = Cfg.GetSessionFactory(DatabaseKey);

            // Open an NHibernate session from the factory
            using (ISession session = sessionFactory.OpenSession())
            {
                // Create the NHibernate criteria interface needed
                ICriteria nhCriteria = session.CreateCriteria(typeof(T));

                // Get the derived class to setup the specific criteria for this BO
                SetNHibernateCriteria(criteria, nhCriteria);

                // Get the list based on the criteria selected
                IList nhibernateList = nhCriteria.List();

                // Now move the references into the CSLA instance
                Add(nhibernateList);
            }
        }
        /// <summary>
        /// Delete data using the CSLA Data Portal.
        /// </summary>
        /// <param name="criteria">The Business Object criteria to identify what to delete.</param>
        /// <remarks>
        /// This method is called from the Business object factory delete method.
        /// It actually removes the data from the database.
        /// </remarks>
        protected override void DataPortal_Delete(object criteria)
        {
            // Get an NHibernate session factory
            ISessionFactory sessionFactory = Cfg.GetSessionFactory(DatabaseKey);

            // Open an NHibernate session from the factory
            using (ISession session = sessionFactory.OpenSession())
            {
                // Begin a transaction on the session
                using (ITransaction transaction = session.BeginTransaction())
                {
                    try
                    {
                        // First load the right BO into memory (this includes any children)...
                        Fetch(criteria, session);

                        // ... then mark it as "old"
                        // Normally the CSLA DataPortal would do this, but as the Fetch() method
                        // has been called directly this needs to be done manually
                        MarkOld();

                        // ... then mark it as deleted (to set the CSLA flags correctly)...
                        Delete();

                        // ...and then delete it (using the open session)
                        Delete(session);

                        // ...then commit the transaction
                        transaction.Commit();
                    }
                    catch
                    {
                        transaction.Rollback();
                        throw;
                    }
                }
            }
        }