依赖注入 – 我可以在Castle Windsor中为代理类型定义自定义属性

前端之家收集整理的这篇文章主要介绍了依赖注入 – 我可以在Castle Windsor中为代理类型定义自定义属性前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个类,我用Castle Dynamic Proxy代理它.我想为代理方法添加一些自定义属性(未在代理类中定义).这可能吗.

我想要这个,因为我想为我的应用程序的服务层生成ASP.NET Web API层.我代理服务(继承自ApiController和其他IMyService接口),它工作得很好但我想将WebAPI特定属性添加到这个新创建的Dynamic类中,因此Web API框架可以读取它们.

编辑:

如果有人想知道我想要什么,我想详细解释一下.

  1. public interface IMyService
  2. {
  3. IEnumerable<MyEntity> GetAll();
  4. }
  5.  
  6. public class MyServiceImpl : IMyService
  7. {
  8. public IEnumerable<MyEntity> GetAll()
  9. {
  10. return new List<MyEntity>(); //TODO: Get from database!
  11. }
  12. }
  13.  
  14. public class MyServiceApiController : ApiController,IMyService
  15. {
  16. private readonly IMyService _myService;
  17.  
  18. public MyServiceApiController(IMyService myService)
  19. {
  20. _myService = myService;
  21. }
  22.  
  23. public IEnumerable<MyEntity> GetAll()
  24. {
  25. return _myService.GetAll();
  26. }
  27. }

认为我有一个由MyServiceImpl实现的IMyService.我想让一个Api控制器能够从网上使用这项服务.
但是如你所见,api控制器只是实际服务的代理.那么,为什么我要写呢?我可以使用城堡windsor动态创建它.

这是我的想法,几乎在我的新项目(https://github.com/hikalkan/aspnetboilerplate)中完成.但是,如果我需要为api控制器的GetAll方法添加一些属性(例如Authorize),该怎么办呢?我不能直接添加,因为没有这样的类,它是城堡动态代理.

所以,除了这个问题.我想知道是否可以向synamic代理类的方法添加属性.

再次看到那个项目
https://github.com/aspnetboilerplate/aspnetboilerplate/issues/55
我也想知道如何,以便我可以在IService和Route on Action上定义RoutePrefix.
幸运的是,我终于知道如何为代理定义自定义属性.
  1. public class CustomProxyFactory : DefaultProxyFactory
  2. {
  3. #region Overrides of DefaultProxyFactory
  4.  
  5. protected override void CustomizeOptions(ProxyGenerationOptions options,IKernel kernel,ComponentModel model,object[] arguments)
  6. {
  7. var attributeBuilder = new CustomAttributeBuilder(typeof(DescriptionAttribute).GetConstructor(new[] { typeof(string) }),new object[] { "CustomizeOptions" });
  8. options.AdditionalAttributes.Add(attributeBuilder);
  9. }
  10.  
  11. #endregion
  12. }
  13.  
  14. /// <summary>
  15. /// 用户信息服务
  16. /// </summary>
  17. [Description("IUserInfoService")]
  18. public interface IUserInfoService
  19. {
  20. /// <summary>
  21. /// 获取用户信息
  22. /// </summary>
  23. /// <param name="name"></param>
  24. /// <returns></returns>
  25. [Description("IUserInfoService.GetUserInfo")]
  26. UserInfo GetUserInfo([Description("IUserInfoService.GetUserInfo name")] string name);
  27. }
  28.  
  29. /// <summary>
  30. /// 用户信息服务
  31. /// </summary>
  32. [Description("UserInfoService")]
  33. public class UserInfoService : IUserInfoService
  34. {
  35. /// <summary>
  36. /// 获取用户信息
  37. /// </summary>
  38. /// <param name="name"></param>
  39. /// <returns></returns>
  40. [Description("UserInfoService.GetUserInfo")]
  41. public virtual UserInfo GetUserInfo([Description("UserInfoService.GetUserInfo name")] string name)
  42. {
  43. return new UserInfo { Name = name };
  44. }
  45. }
  46.  
  47. using DescriptionAttribute = System.ComponentModel.DescriptionAttribute;
  48. [TestFixture]
  49. public class AttributeTests
  50. {
  51. /// <summary>
  52. /// Reference to the Castle Windsor Container.
  53. /// </summary>
  54. public IWindsorContainer IocContainer { get; private set; }
  55. [SetUp]
  56. public void Initialize()
  57. {
  58. IocContainer = new WindsorContainer();
  59. IocContainer.Kernel.ProxyFactory = new CustomProxyFactory();
  60. IocContainer.Register(
  61. Component.For<UserInfoService>()
  62. .Proxy
  63. .AdditionalInterfaces(typeof(IUserInfoService))
  64. .LifestyleTransient()
  65. );
  66.  
  67. }
  68.  
  69. /// <summary>
  70. ///
  71. /// </summary>
  72. [Test]
  73. public void GetAttributeTest()
  74. {
  75. var userInfoService = IocContainer.Resolve<UserInfoService>();
  76. Assert.IsNotNull(userInfoService);
  77. var type = userInfoService.GetType();
  78. Assert.IsTrue(type != typeof(UserInfoService));
  79. var attribute = type.GetCustomAttribute<DescriptionAttribute>();
  80. Assert.IsTrue(attribute != null);
  81. Trace.WriteLine(attribute.Description);
  82.  
  83. var method = type.GetMethod("GetUserInfo");
  84. attribute = method.GetCustomAttribute<DescriptionAttribute>();
  85. Assert.IsTrue(attribute != null);
  86. Trace.WriteLine(attribute.Description);
  87.  
  88. var parameter = method.GetParameters().First();
  89. attribute = parameter.GetCustomAttribute<DescriptionAttribute>();
  90. Assert.IsTrue(attribute != null);
  91. Trace.WriteLine(attribute.Description);
  92. }
  93. }

猜你在找的设计模式相关文章