Aucune description

NHibernateConfiguration.cs 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. //
  2. // NHibernateConfiguration.cs
  3. //
  4. // Author:
  5. // Bryan Allred <bryan.allred@gmail.com>
  6. //
  7. // Copyright (c) 2011 Bryan Allred
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining a copy
  10. // of this software and associated documentation files (the "Software"), to deal
  11. // in the Software without restriction, including without limitation the rights
  12. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. // copies of the Software, and to permit persons to whom the Software is
  14. // furnished to do so, subject to the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be included in
  17. // all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. // THE SOFTWARE.
  26. using FluentNHibernate.Cfg;
  27. using FluentNHibernate.Cfg.Db;
  28. using NHibernate;
  29. using NHibernate.Cfg;
  30. using NHibernate.Tool.hbm2ddl;
  31. namespace WhiteNoise.Domain.NHibernate
  32. {
  33. /// <summary>
  34. /// NHibernate configuration.
  35. /// </summary>
  36. public class NHibernateConfiguration
  37. {
  38. /// <summary>
  39. /// The connection string.
  40. /// </summary>
  41. private readonly string connectionString;
  42. /// <summary>
  43. /// The provider.
  44. /// </summary>
  45. private readonly string provider;
  46. /// <summary>
  47. /// Initializes a new instance of the <see cref="WhiteNoise.Domain.NHibernate.NHibernateConfiguration"/> class.
  48. /// </summary>
  49. /// <param name='connectionString'>
  50. /// Connection string.
  51. /// </param>
  52. /// <param name='provider'>
  53. /// Provider.
  54. /// </param>
  55. public NHibernateConfiguration(string connectionString, string provider)
  56. {
  57. this.connectionString = connectionString;
  58. this.provider = provider;
  59. }
  60. /// <summary>
  61. /// Creates the database configuration.
  62. /// </summary>
  63. /// <returns>
  64. /// The database configuration.
  65. /// </returns>
  66. /// <param name='connectionString'>
  67. /// Connection string.
  68. /// </param>
  69. /// <param name='provider'>
  70. /// Provider.
  71. /// </param>
  72. public static IPersistenceConfigurer CreateDatabaseConfiguration(string connectionString, string provider)
  73. {
  74. switch (provider)
  75. {
  76. case "MsSql2008":
  77. case "System.Data.SqlClient":
  78. return MsSqlConfiguration.MsSql2008
  79. .ConnectionString(connectionString).ShowSql();
  80. case "Firebird":
  81. return new FirebirdConfiguration()
  82. .ConnectionString(connectionString);
  83. case "MySql":
  84. return MySQLConfiguration.Standard
  85. .ConnectionString(connectionString);
  86. case "MsSqlCe":
  87. return MsSqlCeConfiguration.Standard
  88. .ConnectionString(connectionString);
  89. case "SQLite":
  90. return SQLiteConfiguration.Standard
  91. .ConnectionString(connectionString);
  92. case "JetDriver":
  93. return JetDriverConfiguration.Standard
  94. .ConnectionString(connectionString);
  95. case "PostgreSQL":
  96. return PostgreSQLConfiguration.Standard
  97. .Raw("hbm2ddl.keywords","none")
  98. .ConnectionString(connectionString);
  99. default:
  100. return MsSqlConfiguration.MsSql2008
  101. .ConnectionString(connectionString);
  102. }
  103. }
  104. /// <summary>
  105. /// Creates the session factory.
  106. /// </summary>
  107. /// <returns>
  108. /// The session factory.
  109. /// </returns>
  110. public ISessionFactory CreateSessionFactory()
  111. {
  112. Configuration configuration = Fluently.Configure()
  113. .Database(CreateDatabaseConfiguration(this.connectionString, this.provider))
  114. .Mappings(m => m.FluentMappings.AddFromAssemblyOf<NHibernateConfiguration>())
  115. .BuildConfiguration();
  116. // Update the schema.
  117. new SchemaUpdate(configuration).Execute(true, true);
  118. return configuration.BuildSessionFactory();
  119. }
  120. }
  121. }