我可以在Castle Windsor中为代理类型定义自定义属性

hik*_*kan 8 dependency-injection castle-windsor castle castle-dynamicproxy inversion-of-control

我有一个类,我用Castle Dynamic Proxy代理它.我想为代理方法添加一些自定义属性(在代理类中没有定义).这可能吗.

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

编辑:

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

public interface IMyService
{
    IEnumerable<MyEntity> GetAll();
}

public class MyServiceImpl : IMyService
{
    public IEnumerable<MyEntity> GetAll()
    {
        return new List<MyEntity>(); //TODO: Get from database!
    }
}

public class MyServiceApiController : ApiController,IMyService
{
    private readonly IMyService _myService;

    public MyServiceApiController(IMyService myService)
    {
        _myService = myService;
    }

    public IEnumerable<MyEntity> GetAll()
    {
        return _myService.GetAll();
    }
}
Run Code Online (Sandbox Code Playgroud)

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

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

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

小智 2

再次查看该项目\n https://github.com/aspnetboilerplate/aspnetboilerplate/issues/55 \n我也想知道如何,以便我可以在 IService 上定义 RoutePrefix 和在 Action 上定义 Route。\n幸运的是,我终于知道如何为代理定义自定义属性。

\n\n
public class CustomProxyFactory : DefaultProxyFactory\n{\n    #region Overrides of DefaultProxyFactory\n\n    protected override void CustomizeOptions(ProxyGenerationOptions options, IKernel kernel, ComponentModel model, object[] arguments)\n    {\n        var attributeBuilder = new CustomAttributeBuilder(typeof(DescriptionAttribute).GetConstructor(new[] { typeof(string) }), new object[] { "CustomizeOptions" });\n        options.AdditionalAttributes.Add(attributeBuilder);\n    }\n\n    #endregion\n}\n\n/// <summary>\n/// \xe7\x94\xa8\xe6\x88\xb7\xe4\xbf\xa1\xe6\x81\xaf\xe6\x9c\x8d\xe5\x8a\xa1\n/// </summary>\n[Description("IUserInfoService")]\npublic interface IUserInfoService\n{\n    /// <summary>\n    /// \xe8\x8e\xb7\xe5\x8f\x96\xe7\x94\xa8\xe6\x88\xb7\xe4\xbf\xa1\xe6\x81\xaf\n    /// </summary>\n    /// <param name="name"></param>\n    /// <returns></returns>\n    [Description("IUserInfoService.GetUserInfo")]\n    UserInfo GetUserInfo([Description("IUserInfoService.GetUserInfo name")] string name);\n}\n\n/// <summary>\n/// \xe7\x94\xa8\xe6\x88\xb7\xe4\xbf\xa1\xe6\x81\xaf\xe6\x9c\x8d\xe5\x8a\xa1\n/// </summary>\n[Description("UserInfoService")]\npublic class UserInfoService : IUserInfoService\n{\n    /// <summary>\n    /// \xe8\x8e\xb7\xe5\x8f\x96\xe7\x94\xa8\xe6\x88\xb7\xe4\xbf\xa1\xe6\x81\xaf\n    /// </summary>\n    /// <param name="name"></param>\n    /// <returns></returns>\n    [Description("UserInfoService.GetUserInfo")]\n    public virtual UserInfo GetUserInfo([Description("UserInfoService.GetUserInfo name")] string name)\n    {\n        return new UserInfo { Name = name };\n    }\n}\n\nusing DescriptionAttribute = System.ComponentModel.DescriptionAttribute;\n[TestFixture]\npublic class AttributeTests\n{\n    /// <summary>\n    /// Reference to the Castle Windsor Container.\n    /// </summary>\n    public IWindsorContainer IocContainer { get; private set; }\n    [SetUp]\n    public void Initialize()\n    {\n        IocContainer = new WindsorContainer();\n        IocContainer.Kernel.ProxyFactory = new CustomProxyFactory();\n        IocContainer.Register(\n            Component.For<UserInfoService>()\n                .Proxy\n                .AdditionalInterfaces(typeof(IUserInfoService))\n                .LifestyleTransient()\n            );\n\n    }\n\n    /// <summary>\n    /// \n    /// </summary>\n    [Test]\n    public void GetAttributeTest()\n    {\n        var userInfoService = IocContainer.Resolve<UserInfoService>();\n        Assert.IsNotNull(userInfoService);\n        var type = userInfoService.GetType();\n        Assert.IsTrue(type != typeof(UserInfoService));\n        var attribute = type.GetCustomAttribute<DescriptionAttribute>();\n        Assert.IsTrue(attribute != null);\n        Trace.WriteLine(attribute.Description);\n\n        var method = type.GetMethod("GetUserInfo");\n        attribute = method.GetCustomAttribute<DescriptionAttribute>();\n        Assert.IsTrue(attribute != null);\n        Trace.WriteLine(attribute.Description);\n\n        var parameter = method.GetParameters().First();\n        attribute = parameter.GetCustomAttribute<DescriptionAttribute>();\n        Assert.IsTrue(attribute != null);\n        Trace.WriteLine(attribute.Description);\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n