Exemplo n.º 1
0
        public void UpdateSomeDirAndEmpty()
        {
            var configuration = new Configuration();

            configuration.AddDirectory("C:\\");
            configuration.AddDirectory(string.Empty);

            this.CheckPaths(configuration, "C:\\");
        }
        public void Configure(string configFilePath, List <string> mappingFolders, string databaseDilect, string connectionString = "", bool useNeutralMapping = false)
        {
            Contract.Requires(!string.IsNullOrWhiteSpace(databaseDilect));
            Contract.Requires(!string.IsNullOrWhiteSpace(configFile));
            Contract.Requires(mappingFolders != null && mappingFolders.Count() > 0);

            if (sessionFactory != null)
            {
                return;
            }

            mappingFolders.ForEach(p => p = p.TrimEnd(@"\".ToCharArray()));
            NHibernatePersistenceManager.mappingFolders = mappingFolders;

            configFile = string.Format(@"{0}\{1}.hibernate.cfg.xml", configFilePath.TrimEnd(@"\".ToCharArray()), databaseDilect);
            cfg        = new Configuration();
            cfg.Configure(configFile);

            // in case of having specific queries or mappings for different dialects, it is better (and possible)
            // to develop different maaping files and externalizing queries
            foreach (var mappingFolder in mappingFolders)
            {
                string folder = mappingFolder.TrimEnd(@"\".ToCharArray());
                folder = string.Format(@"{0}\Mappings\{1}", folder, useNeutralMapping ? "Default" : databaseDilect);
                cfg.AddDirectory(new DirectoryInfo(folder));
            }
            if (!string.IsNullOrWhiteSpace(connectionString))
            {
                cfg.SetProperty(NHibernate.Cfg.Environment.ConnectionString, connectionString);
                //cfg.SetProperty(NHibernate.Cfg.Environment.ConnectionString, Util.Decrypt(connectionString));
            }

            Contract.Ensures(cfg != null);
        }
Exemplo n.º 3
0
        public void UpdateFilePath()
        {
            var filePath      = @"C:\\tmp\\tmp.txt";
            var configuration = new Configuration();

            configuration.AddDirectory(filePath);
            this.CheckPaths(configuration, "C:\\tmp");
        }
Exemplo n.º 4
0
        public void AddDirectoryNotExistingPath()
        {
            var path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location),
                                    "Plugins", "SubDir");
            var configuration = new Configuration();

            configuration.AddDirectory(path);
            this.CheckPaths(configuration, path);
        }
Exemplo n.º 5
0
        public void UpdateDirsDirectlyWoSubdirs()
        {
            var basedir = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location),
                                       "Tools");
            var subdir1 = Path.Combine(basedir, "SubDir1");
            var subdir2 = Path.Combine(basedir, "SubDir2");

            this.directories.CreateIfNotExists(basedir);
            this.directories.CreateIfNotExists(subdir1);
            this.directories.CreateIfNotExists(subdir2);

            var configuration = new Configuration();

            configuration.AddDirectory(basedir);
            this.CheckPaths(configuration, basedir);
            configuration.AddDirectory("C:\\");
            this.CheckPaths(configuration, basedir, "C:\\");
            configuration.AddDirectory(subdir2);
            this.CheckPaths(configuration, basedir, "C:\\", subdir2);
        }
Exemplo n.º 6
0
 ISession OpenSession()
 {
     if (SessionFactory == null)
     {
         var cfg  = new Configuration();
         var data = cfg.Configure(
             HttpContext.Current.Server.MapPath(@"~/NHibernate/Configuration/hibernate.cfg.xml"));
         cfg.AddDirectory(new System.IO.DirectoryInfo(HttpContext.Current.Server.MapPath(@"~/NHibernate/Mapping")));
         SessionFactory = data.BuildSessionFactory();
     }
     return(SessionFactory.OpenSession());
 }
Exemplo n.º 7
0
        /// <summary>
        /// Method to create session and manage entities
        /// </summary>
        /// <returns></returns>
        ISession OpenSession()
        {
            if (sessionFactory == null)
            {
                var cgf  = new Configuration();
                var data = cgf.Configure(HttpContext.Current.Server.MapPath(@"Models\NHibernate\Configuration\hibernate.cfg.xml"));
                cgf.AddDirectory(new System.IO.DirectoryInfo(HttpContext.Current.Server.MapPath(@"Models\NHibernate\Mappings")));
                sessionFactory = data.BuildSessionFactory();
            }

            return(sessionFactory.OpenSession());
        }
Exemplo n.º 8
0
        /// <summary>
        /// Abrir sessão para acesso ao banco de dados
        /// </summary>
        public static ISession OpenSession()
        {
            var configuration     = new Configuration();
            var configurationPath = HttpContext.Current.Server.MapPath
                                        (@"~\Config\nhibernate.configuration.xml");

            configuration.Configure(configurationPath);
            string directoryPath = HttpContext.Current.Server.MapPath(@"~\Models\Hbm");

            configuration.AddDirectory(new DirectoryInfo(directoryPath));
            ISessionFactory sessionFactory = configuration.BuildSessionFactory();

            return(sessionFactory.OpenSession());
        }
Exemplo n.º 9
0
        private LoginSessionFactory CreateSessionFactory()
        {
            var context     = DocRepositoryContext.GetInstance();
            var currentUser = Membership.GetUser();

            if (string.IsNullOrEmpty(currentUser?.UserName))
            {
                throw new Exception("Authorization Error");
            }

            string password;

            using (var usersContext = new UsersContext())
            {
                password = usersContext.UserProfiles.Where(up => up.UserId == WebSecurity.CurrentUserId).First()
                           .SqlPassword;
            }

            if (string.IsNullOrEmpty(password))
            {
                throw new Exception("Authorization Error");
            }

            if (context.Owners == null)
            {
                context.UpdateOwners();
            }

            if (context.Owners == null)
            {
                throw new Exception("MSSQL did not provide more than one login");
            }

            if (!context.Owners.Contains(currentUser.UserName))
            {
                context.AddDbUser(currentUser.UserName, password);
            }

            var cgf  = new Configuration();
            var data = cgf.Configure(HttpContext.Current.Server.MapPath(@"\Models\NHibernate\Configuration\user.cfg.xml"));

            cgf.AddDirectory(new System.IO.DirectoryInfo(HttpContext.Current.Server.MapPath(@"\Models\NHibernate\Mappings")));
            cgf.SetProperty(
                "connection.connection_string",
                @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\AppELMA.mdf;" +
                $"User Id ={currentUser.UserName};Password={password};"
                );
            return(new LoginSessionFactory(data.BuildSessionFactory(), currentUser.UserName));
        }
Exemplo n.º 10
0
        public static void InitHibernate()
        {
            Debug.WriteLine("Configuring Hibernate...");
            Log.Info("hello");
            var cfg = new Configuration();

            HbmSerializer.Default.Validate = true;

            HbmSerializer.Default.Serialize(Environment.CurrentDirectory, Assembly.GetExecutingAssembly());
            cfg.AddDirectory(new DirectoryInfo(Environment.CurrentDirectory));

            var sessionFactory = cfg.Configure().BuildSessionFactory();

            SessionFactory = sessionFactory;
            Debug.WriteLine("Created SessionFactory!");
        }
        public override DomainServiceDescriptionProvider CreateProvider(Type domainServiceType, DomainServiceDescriptionProvider parent)
        {
            string        currentDir = AppDomain.CurrentDomain.BaseDirectory;
            Configuration cfg        = null;

            try
            {
                cfg = new Configuration();
                cfg.Configure(_nHibernateConfigurationPath);
                cfg.AddDirectory(new DirectoryInfo(currentDir));
            }
            catch
            {
                throw;
            }

            return(new NHibernateTypeDescriptionProvider(domainServiceType, parent, cfg));
        }
Exemplo n.º 12
0
        protected void AddMappingsFromDirectories()
        {
            try
            {
                System.AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);

                foreach (var dir in Options.MappingDirectories)
                {
                    Configuration.AddDirectory(new System.IO.DirectoryInfo(dir));
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                System.AppDomain.CurrentDomain.AssemblyResolve -= new ResolveEventHandler(CurrentDomain_AssemblyResolve);
            }
        }
Exemplo n.º 13
0
                public static ISession OpenSession()
                
        {
                        var configuration     = new Configuration();
                        var configurationPath = HttpContext.Current.Server.MapPath(@"~\hibernate.cfg.xml");

                        configuration.Configure(configurationPath);
            //configuration.AddFile(HttpContext.Current.Server.MapPath(@"~\AutoGenerated\MappingXML\EmployeeProjectInfo.hbm.xml"));
            //configuration.AddFile(HttpContext.Current.Server.MapPath(@"~\AutoGenerated\MappingXML\Department.hbm.xml"));
            //configuration.AddFile(HttpContext.Current.Server.MapPath(@"~\AutoGenerated\MappingXML\Employee.hbm.xml"));
            //configuration.AddFile(HttpContext.Current.Server.MapPath(@"~\AutoGenerated\MappingXML\Project.hbm.xml"));
            //configuration.AddFile(HttpContext.Current.Server.MapPath(@"~\AutoGenerated\MappingXML\vwEmployeeWorkDetail.hbm.xml"));

            configuration.AddDirectory(new System.IO.DirectoryInfo(HttpContext.Current.Server.MapPath(@"~\AutoGenerated\MappingXML\")));
            //configuration.AddAssembly("TestAppLinq");
                        ISessionFactory sessionFactory = configuration.BuildSessionFactory();
                        return sessionFactory.OpenSession();

                    
        }
Exemplo n.º 14
0
        public void LoadFromFileSystemByInterface()
        {
            var basePath       = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            var libSourcesPath = Path.GetFullPath(Path.Combine(basePath, "..\\..\\..\\Engine\\lib"));
            var pluginDllName  = "TestPlugin0Lib.dll";
            var pluginDirName  = Path.Combine(basePath, "Plugins");

            dirs.CreateIfNotExists(pluginDirName);
            File.Copy(Path.Combine(libSourcesPath, pluginDllName), Path.Combine(pluginDirName, pluginDllName), true);
            var configuration = new Configuration();

            configuration.AddDirectory(pluginDirName);
            var loader = new Loader(configuration);
            var res    = loader.Load <IPlugin>();

            Assert.IsNotNull(res);
            Assert.IsNotEmpty(res);
            Assert.AreEqual(1, res.Count);
            Assert.AreEqual("TestPlugin0Lib.Plugin", res[0].GetType().FullName);
        }
Exemplo n.º 15
0
 /// <summary>
 /// Returns a session factory
 /// </summary>
 /// <param name="hibernateFile">Configuration hibernate file</param>
 /// <param name="hbmDirectory">Directory containing the hbm files</param>
 /// <param name="connectionString">Provide override connection string</param>
 /// <returns>session factory</returns>
 public static ISessionFactory GetFactory(string hibernateFile, string hbmDirectory, string connectionString)
 {
     try
     {
         string absoluteHibernateFile = System.Environment.ExpandEnvironmentVariables(hibernateFile);
         absoluteHibernateFile = Path.GetFullPath(absoluteHibernateFile);
         Configuration config = Configure(new Configuration(), absoluteHibernateFile);
         if (!System.String.IsNullOrEmpty(connectionString))
         {
             config.SetProperty(Environment.ConnectionString, connectionString);
         }
         if (!System.String.IsNullOrEmpty(hbmDirectory))
         {
             hbmDirectory = System.Environment.ExpandEnvironmentVariables(hbmDirectory);
             string absoluteHbmDir = Path.GetFullPath(hbmDirectory);
             config.AddDirectory(new DirectoryInfo(absoluteHbmDir));
         }
         return(config.BuildSessionFactory());
     }
     catch (System.Exception e)
     {
         throw new System.Exception("Error creating NHibernate Session Factory", e);
     }
 }