有没有办法向温莎城堡明确注册开放的通用装饰 [英] Is there a way to explicitly register open generic decorators with castle windsor

查看:53
本文介绍了有没有办法向温莎城堡明确注册开放的通用装饰的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个正在进行的项目中经常使用温莎城堡,并使用一些装饰器,因此我的安装程序中可能会有类似的东西

I use castle windsor a lot in a project i'm working on and use decorators a little so I might have something like this in my installer

Component.For<IMyViewModelService>().ImplementedBy<MyViewModelServiceCacheDecorator>().LifestyleTransient()
Component.For<IMyViewModelService>().ImplementedBy<MyViewModelService>().LifestyleTransient()

因此,这样做很容易并且效果很好。我开始阅读有关简单注入器框架的文章,我真的很喜欢它们,您可以使用命令模式在打开的泛型上专门设置装饰器。

So doing this is easy enough and works well. I started reading around the simple injector framework and I really like they way you can specifically set the decorators on open generics when using the command pattern.

https://simpleinjector.readthedocs.org/en/latest/advanced.html#decorators

温莎城堡是否具有允许您以声明方式进行相同操作的功能?我正在使用温莎城堡3.3,并且始终保持最新状态。

Does castle windsor have any functionality that allows you to do the same thing in this declarative manner? I'm using castle windsor 3.3 and always stay with the latest.

我看到这个问题有点类似,但没有完整的结果
注册开放式通用装饰器,以在温莎堡的类型化实现中 a>

I see this question which is kind of similar but doesn't have a full outcome registering open generic decorators for typed implementations in castle windsor

推荐答案

也许我不了解您要做什么,但是Castle支持开放通用装饰器就可以了。给定这些对象:

Perhaps I'm not understanding what you're trying to do, but Castle supports open generic decorators just fine. Given these objects:

public interface IService<T>
{
    void Info();
}

public class Service<T> : IService<T>
{
    public void Info()
    {
        Console.WriteLine(GetType());
    }
}

public class ServiceDecorator<T> : IService<T>
{
    readonly IService<T> service;

    public ServiceDecorator(IService<T> service)
    {
        this.service = service;
    }

    public void Info()
    {
        Console.WriteLine(GetType());
        service.Info();
    }
}

此注册和解决方案:

container.Register(Component.For(typeof(IService<>)).ImplementedBy(typeof(ServiceDecorator<>)));
container.Register(Component.For(typeof(IService<>)).ImplementedBy(typeof(Service<>)));

然后解析服务并调用 Info

Then resolving the service and calling Info:

IService<int> service = container.Resolve<IService<int>>();
service.Info();

打印:

Sample.ServiceDecorator`1[System.Int32]
Sample.Service`1[System.Int32]

如果您要执行的操作不是,请使用更精确的示例更新您的问题。

If this is not what you're trying to do, please update your question with a more precise example.

这篇关于有没有办法向温莎城堡明确注册开放的通用装饰的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆