AOP:权限、缓存示例

只有代码

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 using System.Threading.Tasks;
  6 
  7 using Microsoft.Practices.Unity;
  8 using Microsoft.Practices.Unity.InterceptionExtension;
  9 
 10 namespace AOPStudy
 11 {
 12     class Program
 13     {
 14         static void Main(string[] args)
 15         {
 16             var container = new UnityContainer()
 17                 .AddNewExtension<Interception>()
 18                 .RegisterType<ICacheManager, CacheManager>()
 19                 .RegisterType<IPermissionService, PermissionService>()
 20                 .RegisterType<IAuthenticationService, AuthenticationService>()
 21                 .RegisterType<IApplicationService, ApplicationService>();
 22 
 23             container
 24                 .Configure<Interception>()
 25                 .SetInterceptorFor<IApplicationService>(new InterfaceInterceptor())
 26                 .SetInterceptorFor<IPermissionService>(new InterfaceInterceptor());
 27 
 28             var applicationService = container.Resolve<IApplicationService>();
 29 
 30             Console.WriteLine("************************");
 31             applicationService.DoSometing();
 32             Console.WriteLine("************************");
 33             applicationService.DoSometing();
 34         }
 35     }
 36 
 37     public interface IApplicationService
 38     {
 39         void DoSometing();
 40     }
 41 
 42     public class ApplicationService : IApplicationService
 43     {
 44         [Permission]
 45         public void DoSometing()
 46         {
 47             //此处执行业务逻辑
 48         }
 49     }
 50 
 51     public class PermissionHandler : ICallHandler
 52     {
 53         private IAuthenticationService _AuthenticationService;
 54 
 55         public PermissionHandler(IAuthenticationService authenticationService)
 56         {
 57             _AuthenticationService = authenticationService;
 58         }
 59 
 60         public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
 61         {
 62             var userId = new Guid("58E4E105-96E9-42BE-83E4-01B6010D04BB");//真实项目中从Session中获取
 63             var permissionId = Guid.NewGuid();//真实项目中从自定义Attribute中获取
 64 
 65             if (!_AuthenticationService.HasPermission(userId, permissionId))
 66             {
 67                 Console.WriteLine("您没有权限");
 68                 //真实项目中需要抛出自定义异常
 69             }
 70             else
 71             {
 72                 Console.WriteLine("您有权限");
 73             }
 74 
 75             return getNext()(input, getNext);
 76         }
 77 
 78         public int Order { get; set; }
 79     }
 80 
 81     public class PermissionAttribute : HandlerAttribute
 82     {
 83         public override ICallHandler CreateHandler(IUnityContainer container)
 84         {
 85             return container.Resolve<PermissionHandler>();
 86         }
 87     }
 88 
 89     public class CacheHandler : ICallHandler
 90     {
 91         private ICacheManager _CacheManager;
 92 
 93         public CacheHandler(ICacheManager cacheManager)
 94         {
 95             _CacheManager = cacheManager;
 96         }
 97 
 98         public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
 99         {
100             string key = input.Arguments[0].ToString();//真实项目设置自己的策略
101 
102             if (!_CacheManager.Has(key))
103             {
104                 _CacheManager.Add(key, getNext()(input, getNext));
105                 Console.WriteLine("只会执行一次");
106             }
107 
108             return _CacheManager.Get(key) as IMethodReturn;
109         }
110 
111         public int Order { get; set; }
112     }
113 
114     public class CacheAttribute : HandlerAttribute
115     {
116         public override ICallHandler CreateHandler(IUnityContainer container)
117         {
118             return container.Resolve<CacheHandler>();
119         }
120     }
121 
122     public class Permission
123     {
124         public Guid PermissionId { get; set; }
125     }
126 
127     public interface IPermissionService
128     {
129         IEnumerable<Permission> FindPermissionByUserId(Guid userId);
130     }
131 
132     public class PermissionService : IPermissionService
133     {
134         [Cache]
135         public IEnumerable<Permission> FindPermissionByUserId(Guid userId)
136         {
137             return new List<Permission> { new Permission() { PermissionId = Guid.NewGuid() } };
138         }
139     }
140 
141     public interface IAuthenticationService
142     {
143         bool HasPermission(Guid userId, Guid permissionId);
144     }
145 
146     public class AuthenticationService : IAuthenticationService
147     {
148         private IPermissionService _PermissionService;
149 
150         public AuthenticationService(IPermissionService permissionService)
151         {
152             _PermissionService = permissionService;
153         }
154 
155         public bool HasPermission(Guid userId, Guid permissionId)
156         {
157             return _PermissionService.FindPermissionByUserId(userId).Any(x => x.PermissionId == permissionId);
158         }
159     }
160 
161     public interface ICacheManager
162     {
163         void Add(string key, object value);
164 
165         bool Has(string key);
166 
167         object Get(string key);
168     }
169 
170     public class CacheManager : ICacheManager
171     {
172         private static Dictionary<string, object> _Values = new Dictionary<string, object>();
173 
174         public void Add(string key, object value)
175         {
176             _Values.Add(key, value);
177         }
178 
179         public bool Has(string key)
180         {
181             return _Values.ContainsKey(key);
182         }
183 
184         public object Get(string key)
185         {
186             return _Values[key];
187         }
188     }
189 }
原文地址:https://www.cnblogs.com/happyframework/p/2937736.html