Click here to Skip to main content
15,896,118 members
Articles / Database Development / SQL Server

TDD using SqlCE and NHibernate

Rate me:
Please Sign up or sign in to vote.
4.42/5 (7 votes)
10 Apr 2007CPOL4 min read 55.9K   410   28  
This article walks through setting up a SqlCE test bench when using TDD
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Reflection;
using System.Text;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Tool.hbm2ddl;

namespace Samples.SqlCe.Tests {

    /// <summary>
    /// SqlCEDBHelper courtesy of Ayende Rahien from Rhino.Commons.Helpers
    /// Full code can be found here: https://svn.sourceforge.net/svnroot/rhino-tools/trunk/rhino-commons/Rhino.Commons/Helpers/SqlCEDbHelper.cs
    /// </summary>
    internal static class SqlCEDbHelper {
        private static string engineTypeName = "System.Data.SqlServerCe.SqlCeEngine, System.Data.SqlServerCe";
        private static Type type;
        private static PropertyInfo localConnectionString;
        private static MethodInfo createDatabase;
    
        internal static void CreateDatabaseFile(string filename) {
            if (File.Exists(filename))
                File.Delete(filename);

            if (type == null) {
                type = Type.GetType(engineTypeName);
    			localConnectionString = type.GetProperty("LocalConnectionString");
                createDatabase = type.GetMethod("CreateDatabase");
            }
    		object engine = Activator.CreateInstance(type);
    		localConnectionString
                .SetValue(engine, string.Format("Data Source='{0}';", filename), null);
    		createDatabase
                .Invoke(engine, new object[0]);
    	}
    }

    /// <summary>
    /// The code below was also supplied by Ayende Rahien from Rhino.Commons.ForTesting
    /// You can find the complete code here: https://svn.sourceforge.net/svnroot/rhino-tools/trunk/rhino-commons/Rhino.Commons/ForTesting/NHibernateEmbeddedDBTestFixtureBase.cs
    /// Ayende has more code in the version in his repository, and you can
    /// expand alot more here, but for the sake of argument only the basics are here
    /// </summary>
    public class EmbeddedTestBase {
        public static string DatabaseFilename = "TempDB.sdf";

        protected static ISessionFactory sessionFactory;
        protected static Configuration configuration;

        /// <summary> 
        /// Initialize NHibernate and builds a session factory 
        /// Note, this is a costly call so it will be executed only one. 
        /// </summary> 
        protected void FixtureInitalize(params Assembly[] assemblies) {
            if (sessionFactory != null)
                return;

            Hashtable properties = new Hashtable();
            properties.Add("hibernate.connection.driver_class", "NHibernate.Driver.SqlServerCeDriver");
            properties.Add("hibernate.dialect", "NHibernate.Dialect.MsSqlCeDialect");
            properties.Add("hibernate.connection.provider", "NHibernate.Connection.DriverConnectionProvider");
            string connectionString = string.Format("Data Source={0};", DatabaseFilename);
            properties.Add("hibernate.connection.connection_string", connectionString);
            properties.Add("hibernate.show_sql", "true");
            properties.Add("hibernate.connection.release_mode", "on_close");

            configuration = new Configuration();
            configuration.Properties = properties;
            foreach (Assembly assembly in assemblies) {
                Console.WriteLine("Adding Assembly:" + assembly);
                configuration = configuration.AddAssembly(assembly);
            }
            sessionFactory = configuration.BuildSessionFactory();

        }

        public void SetupDb() {
            SqlCEDbHelper.CreateDatabaseFile(DatabaseFilename);
            try {
                new SchemaExport(configuration).Execute(false, true, false, true);
            }
            catch (Exception e) {
                Console.WriteLine("Error Message:" + e);
            }
        }

        public ISession CreateSession() {
            SetupDb();
            return sessionFactory.OpenSession();
        }

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions