/// <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 #2
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;
                    }
                }
            }
        }
        /// <summary>
        /// Gets an NHibernate session factory for the specified key.
        /// </summary>
        /// <param name="key">String identifier for the database required.</param>
        /// <returns>An instance object that implements the NHibernate ISessionFactory interface.</returns>
        /// <exception cref="ArgumentNullException">If the key is null.</exception>
        /// <exception cref="ArgumentException">If the key is empty.</exception>
        /// <exception cref="System.Configuration.ConfigurationErrorsException">If the key is a valid string, but it does not appear in the application configuration file.</exception>
        /// <exception cref="System.IO.FileNotFoundException">If the required NHibernate configuration file does not exist for the specified key.</exception>
        public static ISessionFactory GetSessionFactory(string key)
        {
			Cfg cfg = new Cfg();
			DatabaseConfiguration databaseConfiguration = cfg[key];
            return databaseConfiguration.SessionFactory;
        }
        /// <summary>
        /// Gets an NHibernate database connection string for the specified key.
        /// </summary>
        /// <param name="key">String identifier for the database required.</param>
        /// <returns>An ADO.NET connection string.</returns>
        /// <exception cref="ArgumentNullException">If the key is null.</exception>
        /// <exception cref="ArgumentException">If the key is empty.</exception>
        /// <exception cref="System.Configuration.ConfigurationErrorsException">If the key is a valid string, but it does not appear in the application configuration file.</exception>
        /// <exception cref="System.IO.FileNotFoundException">If the required NHibernate configuration file does not exist for the specified key.</exception>
        public static string GetConnectionString(string key)
        {
			Cfg cfg = new Cfg();
            DatabaseConfiguration databaseConfiguration = cfg[key];
            return databaseConfiguration.ConnectionString;
        }