public static UnitOfWorkTestContext For(PersistenceFramework framwork,
                                                IWindsorContainer container,
                                                UnitOfWorkTestContextDbStrategy dbStrategy,
                                                MappingInfo mappingInfo)
        {
            UnitOfWorkTestContext context = For(framwork, string.Empty, dbStrategy, mappingInfo);

            context.rhinoContainer = container;
            return(context);
        }
        /// <summary>
        /// Throw away all <see cref="UnitOfWorkTestContext"/> objects within <see cref="Contexts"/>
        /// and referenced by <see cref="CurrentContext"/>. WARNING: Subsequent calls to  <see
        /// cref="InitializeNHibernateAndIoC(PersistenceFramework,string,DatabaseEngine,string,MappingInfo)"/>
        /// and all its overloads will now take considerably longer as the persistent framework will
        /// be initialised a fresh.
        /// </summary>
        /// <remarks>
        /// This method should be used vary sparingly. It is highly unlikely that you will need to
        /// call this method between every test.
        /// <para>
        /// Calling this method will dispose of <see cref="UnitOfWorkTestContext"/> objects within
        /// <see cref="Contexts"/>. Each context maintains a reference to a
        ///  <see cref="RhinoContainer"/>. If this container object is
        /// referenced by <see cref="IoC"/>.<see cref="IoC.Container"/> then any subsequent calls to <see
        /// cref="IoC"/>.<see cref="IoC.Resolve(Type)"/> and any of the overloads will throw.
        /// </para>
        /// </remarks>
        public static void DisposeAndRemoveAllUoWTestContexts()
        {
            foreach (UnitOfWorkTestContext context in Contexts)
            {
                context.Dispose();
            }

            CurrentContext      = null;
            IsRunningInTestMode = false;
            Contexts.Clear();
        }
            private void InternalComplete()
            {
                if (string.IsNullOrEmpty(databaseName))
                {
                    databaseName = DeriveDatabaseNameFrom(databaseEngine, mappingInfo.MappingAssemblies[0]);
                }
                UnitOfWorkTestContext context = ioc.GetUnitOfWorkTestContext();

                IsRunningInTestMode = true;

                if (!Equals(context, CurrentContext) || IsInversionOfControlContainerOutOfSynchWith(context))
                {
                    context.InitializeContainerAndUowFactory();
                }
                CurrentContext = context;
                Debug.Print(string.Format("CurrentContext is: {0}", CurrentContext));
            }
            protected internal UnitOfWorkTestContext GetUnitOfWorkTestContext()
            {
                Predicate <UnitOfWorkTestContext> criteria = null;

                if (container == null)
                {
                    criteria = delegate(UnitOfWorkTestContext x)
                    {
                        return(x.Framework == root.PersistenceFramework &&
                               x.RhinoContainerConfigPath == StringOrEmpty(rhinoContainerConfigPath) &&
                               x.DatabaseEngine == root.DatabaseEngine &&
                               x.DatabaseName == root.DatabaseName);
                    };
                }
                else
                {
                    criteria = delegate(UnitOfWorkTestContext x)
                    {
                        return(x.Framework == root.PersistenceFramework &&
                               x.RhinoContainer == container &&
                               x.DatabaseEngine == root.DatabaseEngine &&
                               x.DatabaseName == root.DatabaseName);
                    };
                }


                UnitOfWorkTestContext context = Contexts.Find(criteria);

                if (context == null)
                {
                    UnitOfWorkTestContextDbStrategy dbStrategy =
                        UnitOfWorkTestContextDbStrategy.For(root.DatabaseEngine, root.DatabaseName, root.NHibernateConfigurationProperties);
                    if (container != null)
                    {
                        context = UnitOfWorkTestContext.For(root.PersistenceFramework, container, dbStrategy, root.MappingInfo);
                    }
                    else
                    {
                        context = UnitOfWorkTestContext.For(root.PersistenceFramework, rhinoContainerConfigPath, dbStrategy, root.MappingInfo);
                    }
                    Contexts.Add(context);
                    Debug.Print(string.Format("Created another UnitOfWorkContext for: {0}", context));
                }
                return(context);
            }
        /// <summary>
        /// Initialize NHibernate, build a session factory, and initialize the container.
        /// If <paramref name="rhinoContainerConfig"/> is <see langword="null" /> or <see cref="string.Empty">string.Empty</see>
        /// a <see cref="RhinoContainer">RhinoContainer</see> will not be initialized.
        /// </summary>
        /// <param name="rhinoContainerConfig">The configuration file to initialize a <see cref="RhinoContainer">RhinoContainer</see> 
        /// or <see langword="null" />.</param>
        /// <param name="assemblies">The assemblies to load for NHibernate mapping files.</param>
        public static void FixtureInitialize(string rhinoContainerConfig, params Assembly[] assemblies)
        {
            if (context == null)
            {
                UnitOfWorkTestContextDbStrategy dbStrategy =
                    UnitOfWorkTestContextDbStrategy.For(DatabaseEngine.MsSqlCe, DatabaseFilename);
                context =
                    UnitOfWorkTestContext.For(PersistenceFramework.NHibernate,
                                              rhinoContainerConfig,
                                              dbStrategy,
                                              MappingInfo.From(assemblies));
            }

            if (!string.IsNullOrEmpty(context.RhinoContainerConfigPath))
            {
                if (!IoC.IsInitialized)
                    IoC.Initialize(context.RhinoContainer);
                NHibernateUnitOfWorkFactory hibernateUnitOfWorkFactory = (NHibernateUnitOfWorkFactory)IoC.Resolve<IUnitOfWorkFactory>();
                hibernateUnitOfWorkFactory.RegisterSessionFactory(context.SessionFactory);
            }
        }
        /// <summary>
        /// Initialize NHibernate, build a session factory, and initialize the container.
        /// If <paramref name="rhinoContainerConfig"/> is <see langword="null" /> or <see cref="string.Empty">string.Empty</see>
        /// a <see cref="RhinoContainer">RhinoContainer</see> will not be initialized.
        /// </summary>
        /// <param name="rhinoContainerConfig">The configuration file to initialize a <see cref="RhinoContainer">RhinoContainer</see>
        /// or <see langword="null" />.</param>
        /// <param name="assemblies">The assemblies to load for NHibernate mapping files.</param>
        public static void FixtureInitialize(string rhinoContainerConfig, params Assembly[] assemblies)
        {
            if (context == null)
            {
                UnitOfWorkTestContextDbStrategy dbStrategy =
                    UnitOfWorkTestContextDbStrategy.For(DatabaseEngine.MsSqlCe, DatabaseFilename);
                context =
                    UnitOfWorkTestContext.For(PersistenceFramework.NHibernate,
                                              rhinoContainerConfig,
                                              dbStrategy,
                                              MappingInfo.From(assemblies));
            }

            if (!string.IsNullOrEmpty(context.RhinoContainerConfigPath))
            {
                if (!IoC.IsInitialized)
                {
                    IoC.Initialize(context.RhinoContainer);
                }
                NHibernateUnitOfWorkFactory hibernateUnitOfWorkFactory = (NHibernateUnitOfWorkFactory)IoC.Resolve <IUnitOfWorkFactory>();
                hibernateUnitOfWorkFactory.RegisterSessionFactory(context.SessionFactory);
            }
        }
        /// <summary>
        /// Throw away all <see cref="UnitOfWorkTestContext"/> objects within <see cref="Contexts"/>
        /// and referenced by <see cref="CurrentContext"/>. WARNING: Subsequent calls to  <see
        /// cref="IntializeNHibernateAndIoC(PersistenceFramework,string,DatabaseEngine,string,MappingInfo)"/>
        /// and all its overloads will now take considerably longer as the persistent framework will
        /// be initialised a fresh.
        /// </summary>
        /// <remarks>
        /// This method should be used vary sparingly. It is highly unlikely that you will need to
        /// call this method between every test.
        /// <para>
        /// Calling this method will dispose of <see cref="UnitOfWorkTestContext"/> objects within
        /// <see cref="Contexts"/>. Each context maintains a reference to a 
        ///  <see cref="RhinoContainer"/>. If this container object is
        /// referenced by <see cref="IoC"/>.<see cref="IoC.Container"/> then any subsequent calls to <see
        /// cref="IoC"/>.<see cref="IoC.Resolve(Type)"/> and any of the overloads will throw.
        /// </para>
        /// </remarks>
        public static void DisposeAndRemoveAllUoWTestContexts()
        {
            foreach (UnitOfWorkTestContext context in Contexts)
                context.Dispose();

            CurrentContext = null;
            IsRunningInTestMode = false;
            Contexts.Clear();
        }
 private static bool IsInversionOfControlContainerOutOfSynchWith(UnitOfWorkTestContext context)
 {
     return (IoC.IsInitialized == false) != (context.RhinoContainer == null);
 }
 private static bool IsInversionOfControlContainerOutOfSynchWith(UnitOfWorkTestContext context)
 {
     return((IoC.IsInitialized == false) != (context.RhinoContainer == null));
 }