ApplicationContext.cs 2.2 KB

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