dependency-injection - 我可以在 CaSTLe Windsor 中为代理类型定义自定义属性吗

标签 dependency-injection inversion-of-control castle-windsor castle castle-dynamicproxy

我有一个类,我用 CaSTLe Dynamic Proxy 代理它。我想向代理方法(未在代理类中定义)添加一些自定义属性。这可能吗。

我想要这个,因为我想为我的应用程序的服务层生成 ASP.NET Web API 层。我代理了服务(从 ApiController 和其他 IMyService 接口(interface)继承),它工作得很好,但我想将 WebAPI 特定属性添加到这个新创建的 Dynamic 类,因此 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();
    }
}

认为我有一个由 MyServiceImpl 实现的 IMyService。我想制作一个 Api Controller ,以便能够从 Web 使用此服务。
但是如你所见,api Controller 只是真实服务的代理。那么,我为什么要写它呢?我可以使用城堡温莎动态创建它。

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

所以,除了这个问题。我想知道是否可以将属性添加到同步代理类的方法中。

最佳答案

再看看那个项目
https://github.com/aspnetboilerplate/aspnetboilerplate/issues/55
我也想知道如何,这样我就可以在 IService 上定义 RoutePrefix 和在 Action 上定义 Route。
幸运的是,我终于知道如何为代理定义自定义属性了。

public class CustomProxyFactory : DefaultProxyFactory
{
    #region Overrides of DefaultProxyFactory

    protected override void CustomizeOptions(ProxyGenerationOptions options, IKernel kernel, ComponentModel model, object[] arguments)
    {
        var attributeBuilder = new CustomAttributeBuilder(typeof(DescriptionAttribute).GetConstructor(new[] { typeof(string) }), new object[] { "CustomizeOptions" });
        options.AdditionalAttributes.Add(attributeBuilder);
    }

    #endregion
}

/// <summary>
/// 用户信息服务
/// </summary>
[Description("IUserInfoService")]
public interface IUserInfoService
{
    /// <summary>
    /// 获取用户信息
    /// </summary>
    /// <param name="name"></param>
    /// <returns></returns>
    [Description("IUserInfoService.GetUserInfo")]
    UserInfo GetUserInfo([Description("IUserInfoService.GetUserInfo name")] string name);
}

/// <summary>
/// 用户信息服务
/// </summary>
[Description("UserInfoService")]
public class UserInfoService : IUserInfoService
{
    /// <summary>
    /// 获取用户信息
    /// </summary>
    /// <param name="name"></param>
    /// <returns></returns>
    [Description("UserInfoService.GetUserInfo")]
    public virtual UserInfo GetUserInfo([Description("UserInfoService.GetUserInfo name")] string name)
    {
        return new UserInfo { Name = name };
    }
}

using DescriptionAttribute = System.ComponentModel.DescriptionAttribute;
[TestFixture]
public class AttributeTests
{
    /// <summary>
    /// Reference to the Castle Windsor Container.
    /// </summary>
    public IWindsorContainer IocContainer { get; private set; }
    [SetUp]
    public void Initialize()
    {
        IocContainer = new WindsorContainer();
        IocContainer.Kernel.ProxyFactory = new CustomProxyFactory();
        IocContainer.Register(
            Component.For<UserInfoService>()
                .Proxy
                .AdditionalInterfaces(typeof(IUserInfoService))
                .LifestyleTransient()
            );

    }

    /// <summary>
    /// 
    /// </summary>
    [Test]
    public void GetAttributeTest()
    {
        var userInfoService = IocContainer.Resolve<UserInfoService>();
        Assert.IsNotNull(userInfoService);
        var type = userInfoService.GetType();
        Assert.IsTrue(type != typeof(UserInfoService));
        var attribute = type.GetCustomAttribute<DescriptionAttribute>();
        Assert.IsTrue(attribute != null);
        Trace.WriteLine(attribute.Description);

        var method = type.GetMethod("GetUserInfo");
        attribute = method.GetCustomAttribute<DescriptionAttribute>();
        Assert.IsTrue(attribute != null);
        Trace.WriteLine(attribute.Description);

        var parameter = method.GetParameters().First();
        attribute = parameter.GetCustomAttribute<DescriptionAttribute>();
        Assert.IsTrue(attribute != null);
        Trace.WriteLine(attribute.Description);
    }
}

关于dependency-injection - 我可以在 CaSTLe Windsor 中为代理类型定义自定义属性吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18735262/

相关文章:

c# - 获取使用依赖注入(inject)的类的实例

java - Spring - 具体类中的@scheduled方法未运行

windows-phone-7 - NuGet 中的 CaSTLe Windsor 引用在 Windows Phone 7 项目中不起作用

c# - 使用 CaSTLeWindsor 解决依赖关系

angular - 循环依赖注入(inject) Angular 2

android - Dagger:如果我每次都*想要*一个新实例怎么办?

java - 为 Jersey 资源提供 lambda 上下文

asp.net-mvc - MVC3 与 MVC 涡轮

dependency-injection - 使用 .Net 和程序集引用进行控制反转

dependency-injection - 使用 CaSTLe Windsor 子容器来解析具有特定实例的类型