private IEnumerable <string> GetAppContextObjectNameListFromType(Type type)
        {
            var objectNameList = _applicationContext.GetObjectNamesForType(type);

            if (objectNameList.Count > 1)
            {
                throw new NotSupportedException("The case where there is more than one object for a single type is " +
                                                "not yet supported");
            }

            return(objectNameList);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Gets the Spring IDbProvider given the ISessionFactory.
        /// </summary>
        /// <remarks>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.</remarks>
        /// <param name="sessionFactory">The session factory.</param>
        /// <returns>The corresponding IDbProvider, null if no mapping was found.</returns>
        /// <exception cref="InvalidOperationException">If DbProviderFactory's ApplicaitonContext is not
        /// an instance of IConfigurableApplicaitonContext.</exception>
        public static IDbProvider GetDbProvider(ISessionFactory sessionFactory)
        {
            ISessionFactoryImplementor sfi = sessionFactory as ISessionFactoryImplementor;

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

#if NET_1_1
                    DriverBase db = cp.Driver as DriverBase;
#else
                    IDriver db = cp.Driver;
#endif
                    if (db != null)
                    {
                        Type hibCommandType = db.CreateCommand().GetType();

                        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 = Spring.Data.Common.DbProviderFactory.GetDbProvider(providerName);
                                return(prov);
                            }
                        }
                    }
                    else
                    {
                        log.Info("Could not derive IDbProvider from SessionFactory");
                    }
                }
            }
            return(null);
        }