Advertisement
Guest User

Untitled

a guest
Oct 27th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 KB | None | 0 0
  1.  
  2. public class RequestDatabaseFactory
  3. {
  4. public static SqlConnection Connection
  5. {
  6. get
  7. {
  8. if (HttpContext.Current.Items.Contains("RequestDatabaseFactoryConnection"))
  9. return HttpContext.Current.Items["RequestDatabaseFactoryConnection"] as SqlConnection;
  10.  
  11. return null;
  12. }
  13. set
  14. {
  15. HttpContext.Current.Items["RequestDatabaseFactoryConnection"] = value;
  16. }
  17. }
  18.  
  19. public static SqlConnection Get()
  20. {
  21. if (null == Connection)
  22. {
  23. Connection = new SqlConnection("connStringNameInWebconfig");
  24. Connection.Open();
  25. }
  26.  
  27. return Connection;
  28. }
  29.  
  30. public static void Close()
  31. {
  32. if (Connection != null)
  33. Connection.Close();
  34. }
  35.  
  36. public static SqlCommand CreateCommand()
  37. {
  38. var ret= new SqlCommand();
  39. ret.Connection = Get();
  40. return ret;
  41. }
  42. }
  43.  
  44. public class ComponentA
  45. {
  46. public object GetA()
  47. {
  48. var myCmd = RequestDatabaseFactory.CreateCommand();
  49. return null;
  50. }
  51. }
  52.  
  53. public class ComponentB
  54. {
  55. public object GetB()
  56. {
  57. var myCmd = RequestDatabaseFactory.CreateCommand();
  58. return null;
  59. }
  60. }
  61.  
  62. void main()
  63. {
  64. var ca = new ComponentA();
  65. var cb = new ComponentB();
  66.  
  67. ca.GetA();
  68. cb.GetB();
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement