ApplicationContext.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Web.Mvc;
  8. using Autofac;
  9. using Autofac.Integration.Mvc;
  10. using GreenTree.Nachtragsmanagement.Data;
  11. using GreenTree.Nachtragsmanagement.Services.DbContext;
  12. using GreenTree.Nachtragsmanagement.Services.Test;
  13. namespace GreenTree.Nachtragsmanagement.Web.Framework
  14. {
  15. public class ApplicationContext
  16. {
  17. #region Statics
  18. /// <summary>
  19. /// Current ApplicationContext
  20. /// </summary>
  21. private static ApplicationContext _current;
  22. /// <summary>
  23. /// Returns the current ApplicationContext or creates one if doesn't exist already
  24. /// </summary>
  25. public static ApplicationContext Current
  26. {
  27. get
  28. {
  29. if (_current == null)
  30. {
  31. _current = new ApplicationContext();
  32. }
  33. return _current;
  34. }
  35. }
  36. #endregion
  37. #region Fields
  38. /// <summary>
  39. /// Returns the application wide app container builder
  40. /// </summary>
  41. private static IContainer _appContainer;
  42. /// <summary>
  43. /// Returns the application wide controller container builder
  44. /// </summary>
  45. private static IContainer _controllerContainer;
  46. #endregion
  47. #region Services
  48. /// <summary>
  49. /// Returns the generel DbContextService
  50. /// </summary>
  51. public DbContextService DbContextService { get; set; }
  52. #endregion
  53. #region Ctor
  54. /// <summary>
  55. /// Initializes a new instance of the ApplicationContext class
  56. /// </summary>
  57. public ApplicationContext()
  58. {
  59. DbContextService = new DbContextService(_appContainer.Resolve<IDbContext>());
  60. }
  61. #endregion
  62. #region Initialization
  63. /// <summary>
  64. /// Registers all service ressources needed by the ApplicationContext
  65. /// </summary>
  66. public static void InitServices()
  67. {
  68. var appBuilder = new ContainerBuilder();
  69. appBuilder
  70. .RegisterInstance(new AppendixObjectContext("Server=localhost\\SQLEXPRESS;Database=Nachtragsmanagement;User Id=nachtragdat;Password=nachtragdat;"))
  71. .As<IDbContext>()
  72. .SingleInstance();
  73. appBuilder
  74. .RegisterType<DbRelationService>()
  75. .As<IDbRelationService>();
  76. appBuilder
  77. .RegisterControllers(Assembly.GetCallingAssembly());
  78. _appContainer = appBuilder.Build();
  79. DependencyResolver.SetResolver(new AutofacDependencyResolver(_appContainer));
  80. }
  81. #endregion
  82. }
  83. }