Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I define custom attributes for proxy type in Castle Windsor

I have a class that I proxy it with Castle Dynamic Proxy. I want to add some custom Attributes to proxy methods (which is not defined in proxied class). Is this possible.

I want this because I want to generate ASP.NET Web API layer for my application's Service Layer. I proxied services (with inheriting from ApiController and additional IMyService interfaces), it works great but I want to add WebAPI specific attributes to this newly created Dynamic class, thus Web API framework can read them.

EDIT:

I want to explain detailed if someone want to know what I want actually.

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();
    }
}

Think that I have a IMyService which is implemented by MyServiceImpl. And I want to make a Api controller to be able to use this service from web. But as you see, api controller is just a proxy for real service. So, why I should write it? I can dynamically create it using castle windsor.

This is my idea and almost done it in my new project (https://github.com/hikalkan/aspnetboilerplate). But what if I need to add some attribute (such as Authorize) to GetAll method of the api controller. I cant directly add since there is no such a class, it's castle dynamic proxy.

So, beside this problem. I want to know if it's possible to add a attribute to a method of a synamic proxy class.

like image 327
hikalkan Avatar asked Sep 11 '13 07:09

hikalkan


1 Answers

See again that project https://github.com/aspnetboilerplate/aspnetboilerplate/issues/55 I also want to know how, so that I can define RoutePrefix on IService and Route on Action. Fortunately, I finally know how to define custom attributes for proxy.

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);
    }
}
like image 53
echofool Avatar answered Sep 21 '22 03:09

echofool