Spring.Data.NHibernate.SessionFactoryUtils.GetDbProvider C# (CSharp) Method

GetDbProvider() public static method

Gets the Spring IDbProvider given the ISessionFactory.
The matching is performed by comparing the assembly qualified name string of the hibernate Driver.ConnectionType to those in the DbProviderFactory definitions. No connections are created in performing this comparison.
If DbProviderFactory's ApplicaitonContext is not /// an instance of IConfigurableApplicaitonContext.
public static GetDbProvider ( ISessionFactory sessionFactory ) : IDbProvider
sessionFactory ISessionFactory The session factory.
return IDbProvider
	    public static IDbProvider GetDbProvider(ISessionFactory sessionFactory)
	    {
	        ISessionFactoryImplementor sfi = sessionFactory as ISessionFactoryImplementor;
	        if (sfi != null)
	        {

                IConnectionProvider cp = sfi.ConnectionProvider;
	            if (cp != null)
	            {
                    IConfigurableApplicationContext ctx =
	                    DbProviderFactory.ApplicationContext as IConfigurableApplicationContext;
                    if (ctx == null)
                    {
                        throw new InvalidOperationException(
                            "Implementations of IApplicationContext must also implement IConfigurableApplicationContext");
                    }


                    DriverBase db = cp.Driver as DriverBase;
                    if (db != null)
                    {
                        Type hibCommandType = db.CreateCommand().GetType();

                        IList<string> providerNames = ctx.GetObjectNamesForType(typeof(DbProvider), true, false);
                        string hibCommandAQN = hibCommandType.AssemblyQualifiedName;
                        foreach (string providerName in providerNames)
                        {
                            IObjectDefinition objectdef = ctx.ObjectFactory.GetObjectDefinition(providerName);
                            ConstructorArgumentValues ctorArgs = objectdef.ConstructorArgumentValues;
                            ConstructorArgumentValues.ValueHolder vh = ctorArgs.NamedArgumentValues["dbmetadata"] as ConstructorArgumentValues.ValueHolder;
                            IObjectDefinition od = ((ObjectDefinitionHolder)vh.Value).ObjectDefinition;
                            ConstructorArgumentValues dbmdCtorArgs = od.ConstructorArgumentValues;
                            string commandType = dbmdCtorArgs.GetArgumentValue("commandType", typeof(string)).Value as string;
                            
                            if (hibCommandAQN.Equals(commandType))
                            {
                                IDbProvider prov = DbProviderFactory.GetDbProvider(providerName);
                                return prov;
                            }
                        }
                    }
	                else
                    {
                        log.Info("Could not derive IDbProvider from SessionFactory");
                    }
	            }
	            
	            
	        }
	        return null;
	    }

Usage Example

        /// <summary>
        /// Invoked by an <see cref="Spring.Objects.Factory.IObjectFactory"/>
        /// after it has injected all of an object's dependencies.
        /// </summary>
        /// <remarks>
        /// <p>
        /// This method allows the object instance to perform the kind of
        /// initialization only possible when all of it's dependencies have
        /// been injected (set), and to throw an appropriate exception in the
        /// event of misconfiguration.
        /// </p>
        /// <p>
        /// Please do consult the class level documentation for the
        /// <see cref="Spring.Objects.Factory.IObjectFactory"/> interface for a
        /// description of exactly <i>when</i> this method is invoked. In
        /// particular, it is worth noting that the
        /// <see cref="Spring.Objects.Factory.IObjectFactoryAware"/>
        /// and <see cref="Spring.Context.IApplicationContextAware"/>
        /// callbacks will have been invoked <i>prior</i> to this method being
        /// called.
        /// </p>
        /// </remarks>
        /// <exception cref="System.ArgumentException">
        /// In the event of misconfiguration (such as the failure to set a
        /// required property) or if initialization fails.
        /// </exception>
        public void AfterPropertiesSet()
        {
            if (SessionFactory == null)
            {
                throw new ArgumentException("sessionFactory is required");
            }
            if (this.entityInterceptor is string && this.objectFactory == null)
            {
                throw new ArgumentException("objectFactory is required for entityInterceptorBeanName");
            }

            // Try to derive a DbProvider given the SessionFactory.
            if (this.autodetectDbProvider && DbProvider == null)
            {
                IDbProvider sfDbProvider = SessionFactoryUtils.GetDbProvider(SessionFactory);
                if (sfDbProvider != null)
                {
                    // Use the SessionFactory's DataSource for exposing transactions to ADO.NET code.
                    if (log.IsInfoEnabled)
                    {
                        log.Info("Derived DbProvider [" + sfDbProvider.DbMetadata.ProductName +
                                 "] of Hibernate SessionFactory for HibernateTransactionManager");
                    }
                    DbProvider = sfDbProvider;
                }
                else
                {
                    log.Info("Could not auto detect DbProvider from SessionFactory configuration");
                }
            }
        }
All Usage Examples Of Spring.Data.NHibernate.SessionFactoryUtils::GetDbProvider