ConnectionInterceptor.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // ***********************************************************************
  2. // Assembly : XYY.Core
  3. // Author : 陈汉林
  4. // Created : 08-24-2016
  5. //
  6. // Last Modified By : 陈汉林
  7. // Last Modified On : 08-24-2016
  8. // ***********************************************************************
  9. // <copyright file="ConnectionInterceptor.cs" company="澳鹏">
  10. // Copyright (c) 澳鹏. All rights reserved.
  11. // </copyright>
  12. // <summary></summary>
  13. // ***********************************************************************
  14. using System;
  15. using System.Collections.Generic;
  16. using System.Data;
  17. using System.Linq;
  18. using System.Text;
  19. using System.Threading.Tasks;
  20. using System.Reflection;
  21. using AspectCore.DynamicProxy;
  22. using System.Collections.Concurrent;
  23. /// <summary>
  24. /// The Infrastructure namespace.
  25. /// </summary>
  26. namespace XYY.Core.Standard.Data.Infrastructure
  27. {
  28. public static class InterceptorAttribut
  29. {
  30. private static readonly ConcurrentDictionary<MethodInfo, object[]>
  31. MethodAttributes = new ConcurrentDictionary<MethodInfo, object[]>();
  32. public static T GetAttribute<T>(this AspectContext context) where T : Attribute
  33. {
  34. MethodInfo method = context.ServiceMethod;
  35. var attributes = MethodAttributes.GetOrAdd(method, method.GetCustomAttributes(true));
  36. var attribute = attributes.FirstOrDefault(x => typeof(T).IsAssignableFrom(x.GetType()));
  37. if (attribute is T)
  38. {
  39. return (T)attribute;
  40. }
  41. return null;
  42. }
  43. }
  44. /// <summary>
  45. /// 拦截器.
  46. /// </summary>
  47. public class ConnectionInterceptorAttribute : AbstractInterceptorAttribute
  48. {
  49. /// <summary>
  50. /// The _unit of work
  51. /// </summary>
  52. /// [FromContainer]
  53. IUnitOfWork _unitOfWork;
  54. /// <summary>
  55. /// The _connection
  56. /// </summary>
  57. IDbConnection _connection;
  58. /// <summary>
  59. /// Initializes a new instance of the <see cref="ConnectionInterceptor"/> class.
  60. /// </summary>
  61. /// <param name="connection">The connection.</param>
  62. /// <param name="unitOfWork">The unit of work.</param>
  63. public ConnectionInterceptorAttribute()
  64. {
  65. //_unitOfWork = unitOfWork;
  66. //_connection = connection;
  67. }
  68. public override Task Invoke(AspectContext context, AspectDelegate next)
  69. {
  70. _unitOfWork = context.ServiceProvider.GetService(typeof(IUnitOfWork)) as IUnitOfWork;
  71. _connection = context.ServiceProvider.GetService(typeof(IDbConnection)) as IDbConnection;
  72. MethodInfo method = context.ServiceMethod;
  73. var nonTrans = context.GetAttribute<NonTransAttribute>();
  74. var isStartWithGet = method.Name.StartsWith("Get", StringComparison.OrdinalIgnoreCase);
  75. if (nonTrans == null && !isStartWithGet)
  76. {
  77. _unitOfWork.Connection = _unitOfWork.Connection ?? _connection;
  78. if (_unitOfWork.Connection.State != ConnectionState.Open && _unitOfWork.Connection.State != ConnectionState.Connecting)
  79. {//初始化连接
  80. _unitOfWork.Connection.Open();
  81. }
  82. //方法不含NonTrans特性,或者方法不是Get开头的,则需要开启事务
  83. try
  84. {
  85. _unitOfWork.Transaction = _unitOfWork.Transaction ?? _unitOfWork.Connection.BeginTransaction();
  86. }
  87. catch
  88. {
  89. //尝试重新开启事务
  90. _unitOfWork.Transaction = _unitOfWork.Transaction ?? _unitOfWork.Connection.BeginTransaction();
  91. }
  92. }
  93. return context.Invoke(next);
  94. }
  95. }
  96. }