Guest User

Untitled

a guest
May 26th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.81 KB | None | 0 0
  1. using System.Diagnostics;
  2. using FluentNHibernate.Cfg;
  3. using FluentNHibernate.Cfg.Db;
  4. using NHibernate;
  5. using NHibernate.ByteCode.Castle;
  6. using NHibernate.Context;
  7. using Webdevelopment.Hif.Data.Maps;
  8.  
  9. namespace WebApp.App_Code
  10. {
  11. public class NhibernateInit
  12. {
  13. public static ISessionFactory SessionFactory { get; private set; }
  14.  
  15. // TODO: this should be moved in an easy way to configurate from outside
  16. public static void Setup()
  17. {
  18. InitProfiler();
  19. SessionFactory =
  20. Fluently.Configure().Database(MsSqlConfiguration.MsSql2008
  21. .ConnectionString(
  22. c => c.Server("192.168.1.7").Database("appexnet").Username("appexnet").Password("SQL4dev"))
  23. .ShowSql().FormatSql()
  24. .CurrentSessionContext("web")
  25. .ProxyFactoryFactory<ProxyFactoryFactory>())
  26. .Mappings(m => m.FluentMappings.AddFromAssemblyOf<AdvertiserCategoryClassMap>())
  27. .BuildSessionFactory();
  28. }
  29.  
  30. public static void DetachSession()
  31. {
  32. var session = WebSessionContext.Unbind(SessionFactory);
  33. if (session != null)
  34. {
  35. if (session.Transaction != null && session.Transaction.IsActive)
  36. session.Transaction.Commit();
  37. session.Close();
  38. }
  39. }
  40.  
  41. public static void AttachSession()
  42. {
  43. var session = SessionFactory.OpenSession();
  44. WebSessionContext.Bind(session);
  45. session.BeginTransaction();
  46. }
  47.  
  48. [Conditional("DEBUG")]
  49. private static void InitProfiler()
  50. {
  51. HibernatingRhinos.Profiler.Appender.NHibernate.NHibernateProfiler.Initialize();
  52. }
  53. }
  54. }
Add Comment
Please, Sign In to add comment