Guest User

Untitled

a guest
Jul 22nd, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.49 KB | None | 0 0
  1. ## Global.asax.cs
  2. protected void Application_Start()
  3. {
  4. var builder = new ContainerBuilder();
  5.  
  6. builder.RegisterModule(new AutofacControllerModule(Assembly.GetExecutingAssembly()));
  7. builder.RegisterModule(new NHibernateModule());
  8. builder.RegisterModule(new AutoMapperModule());
  9.  
  10. _ContainerProvider = new ContainerProvider(builder.Build());
  11. ControllerBuilder.Current.SetControllerFactory(new AutofacControllerFactory(_ContainerProvider));
  12.  
  13. RegisterRoutes(RouteTable.Routes);
  14. }
  15.  
  16. ## NHibernateModule.cs
  17. public class NHibernateModule : Module
  18. {
  19. protected override void Load(ContainerBuilder builder)
  20. {
  21. if (builder == null)
  22. throw new System.ArgumentNullException("builder", "builder is null.");
  23.  
  24. // NHProf utility
  25. NHibernateProfiler.Initialize();
  26.  
  27. // Re-usable instances
  28. builder.RegisterType<MyServiceImpl>().As<IMyService>().InstancePerMatchingLifetimeScope(WebLifetime.Request);
  29. builder.Register(c => new TransactionManager()).InstancePerMatchingLifetimeScope(WebLifetime.Request);
  30.  
  31. // Session-per-Request
  32. builder.Register(c => c.Resolve<ISessionFactory>().OpenSession())
  33. .HttpRequestScoped()
  34. .OnActivated(e =>
  35. { // can wrap transactions here, as opposed to via an action filter; I prefer the action filter.
  36. e.Context.Resolve<TransactionManager>().CurrentTransaction = ((ISession)e.Instance).BeginTransaction();
  37. });
  38.  
  39. // Application scoped SessionFactory instance
  40. builder.Register(c => BuildSessionFactory()).SingleInstance();
  41. }
  42.  
  43. // SessionFactory builder
  44. private ISessionFactory BuildSessionFactory()
  45. {
  46. var cfg = new NHibernate.Cfg.Configuration().Configure(Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "nhibernate.config"));
  47. cfg.SetProperty(NHibernate.Cfg.Environment.ConnectionStringName, ConfigurationManager.ConnectionStrings["Dubskin"].Name);
  48. if (Convert.ToBoolean(ConfigurationManager.AppSettings["GenerateSchema"]))
  49. {
  50. new SchemaExport(cfg).Execute(false, true, false);
  51. }
  52.  
  53. return cfg.BuildSessionFactory();
  54. }
  55. }
  56.  
  57. ## TransactionManager.cs -- would swap this out for an action filter in the future
  58. internal class TransactionManager : IDisposable
  59. {
  60. bool _disposed;
  61.  
  62. public ITransaction CurrentTransaction { get; set; }
  63.  
  64. public void Dispose()
  65. {
  66. if (!_disposed)
  67. {
  68. _disposed = true;
  69.  
  70. if (CurrentTransaction != null)
  71. {
  72. if (HttpContext.Current.Error != null)
  73. CurrentTransaction.Rollback();
  74. else
  75. CurrentTransaction.Commit();
  76. }
  77. }
  78. }
  79. }
  80.  
  81. ## AutoMapperModule.cs -- used for configuring automapped models -> view models on app start.
  82. public class AutoMapperModule : Module
  83. {
  84. protected override void Load(ContainerBuilder moduleBuilder)
  85. {
  86. if (moduleBuilder == null)
  87. throw new ArgumentNullException("moduleBuilder", "moduleBuilder is null.");
  88.  
  89. Mapper.CreateMap<ManageEntityViewModel, Entity>();
  90. }
  91. }
Add Comment
Please, Sign In to add comment