C# 基于Ninject约定的绑定&x2B;装饰师

C# 基于Ninject约定的绑定&x2B;装饰师,c#,.net,ninject,C#,.net,Ninject,我们使用基于Ninject约定的绑定将一组命令和查询自动绑定到它们的处理程序。到目前为止,我们有一个装饰工作使用以下 绑定所有不带属性的对象: Kernel.Bind(x => x.FromThisAssembly() .SelectAllClasses() .InheritedFrom(typeof(ICommandHandler<>)) .WithoutAttribute<DoCheckAttribute&

我们使用基于Ninject约定的绑定将一组命令和查询自动绑定到它们的处理程序。到目前为止,我们有一个装饰工作使用以下

绑定所有不带属性的对象:

    Kernel.Bind(x =>
      x.FromThisAssembly()
       .SelectAllClasses()
       .InheritedFrom(typeof(ICommandHandler<>))
       .WithoutAttribute<DoCheckAttribute>()
       .BindAllInterfaces());
    Kernel.Bind(x =>
      x.FromThisAssembly()
       .SelectAllClasses()
       .InheritedFrom(typeof(ICommandHandler<>))
       .WithAttribute<DoCheckAttribute>()
       .BindAllInterfaces()
       .Configure(c => c.WhenInjectedInto(typeof(DoCheckDecorator<>))));
    Kernel.Bind(x =>
      x.FromThisAssembly()
       .SelectAllClasses()
       .InheritedFrom(typeof(ICommandHandler<>))
       .WithoutAttribute<DoCheckAttribute>()
       .WithoutAttribute<DoOtherCheckAttribute>()
       .BindAllInterfaces());
    Kernel.Bind(x =>
      x.FromThisAssembly()
       .SelectAllClasses()
       .InheritedFrom(typeof(ICommandHandler<>))
       .WithAttribute<DoCheckAttribute>()
       .WithoutAttribute<DoOtherCheckAttribute>()
       .BindAllInterfaces()
       .Configure(c => c.WhenInjectedInto(typeof(DoCheckDecorator<>))));

    Kernel.Bind(x =>
      x.FromThisAssembly()
       .SelectAllClasses()
       .InheritedFrom(typeof(ICommandHandler<>))
       .WithoutAttribute<DoCheckAttribute>()
       .WithAttribute<DoOtherCheckAttribute>()
       .BindAllInterfaces()
       .Configure(c => c.WhenInjectedInto(typeof(DoOtherCheckDecorator<>))));
Kernel.Bind(x=>
x、 来自此程序集()
.SelectAllClasses()
.InheritedFrom(typeof(ICommandHandler))
.without属性()
.BindAllInterfaces());
使用属性绑定所有对象:

    Kernel.Bind(x =>
      x.FromThisAssembly()
       .SelectAllClasses()
       .InheritedFrom(typeof(ICommandHandler<>))
       .WithoutAttribute<DoCheckAttribute>()
       .BindAllInterfaces());
    Kernel.Bind(x =>
      x.FromThisAssembly()
       .SelectAllClasses()
       .InheritedFrom(typeof(ICommandHandler<>))
       .WithAttribute<DoCheckAttribute>()
       .BindAllInterfaces()
       .Configure(c => c.WhenInjectedInto(typeof(DoCheckDecorator<>))));
    Kernel.Bind(x =>
      x.FromThisAssembly()
       .SelectAllClasses()
       .InheritedFrom(typeof(ICommandHandler<>))
       .WithoutAttribute<DoCheckAttribute>()
       .WithoutAttribute<DoOtherCheckAttribute>()
       .BindAllInterfaces());
    Kernel.Bind(x =>
      x.FromThisAssembly()
       .SelectAllClasses()
       .InheritedFrom(typeof(ICommandHandler<>))
       .WithAttribute<DoCheckAttribute>()
       .WithoutAttribute<DoOtherCheckAttribute>()
       .BindAllInterfaces()
       .Configure(c => c.WhenInjectedInto(typeof(DoCheckDecorator<>))));

    Kernel.Bind(x =>
      x.FromThisAssembly()
       .SelectAllClasses()
       .InheritedFrom(typeof(ICommandHandler<>))
       .WithoutAttribute<DoCheckAttribute>()
       .WithAttribute<DoOtherCheckAttribute>()
       .BindAllInterfaces()
       .Configure(c => c.WhenInjectedInto(typeof(DoOtherCheckDecorator<>))));
Kernel.Bind(x=>
x、 来自此程序集()
.SelectAllClasses()
.InheritedFrom(typeof(ICommandHandler))
.WithAttribute()
.BindAllInterfaces()
.Configure(c=>c.whenInjectedTo(typeof(DoCheckDecorator)));
我们尝试了以下方法来添加另一个decorator,但是失败了

绑定所有不带属性的对象:

    Kernel.Bind(x =>
      x.FromThisAssembly()
       .SelectAllClasses()
       .InheritedFrom(typeof(ICommandHandler<>))
       .WithoutAttribute<DoCheckAttribute>()
       .BindAllInterfaces());
    Kernel.Bind(x =>
      x.FromThisAssembly()
       .SelectAllClasses()
       .InheritedFrom(typeof(ICommandHandler<>))
       .WithAttribute<DoCheckAttribute>()
       .BindAllInterfaces()
       .Configure(c => c.WhenInjectedInto(typeof(DoCheckDecorator<>))));
    Kernel.Bind(x =>
      x.FromThisAssembly()
       .SelectAllClasses()
       .InheritedFrom(typeof(ICommandHandler<>))
       .WithoutAttribute<DoCheckAttribute>()
       .WithoutAttribute<DoOtherCheckAttribute>()
       .BindAllInterfaces());
    Kernel.Bind(x =>
      x.FromThisAssembly()
       .SelectAllClasses()
       .InheritedFrom(typeof(ICommandHandler<>))
       .WithAttribute<DoCheckAttribute>()
       .WithoutAttribute<DoOtherCheckAttribute>()
       .BindAllInterfaces()
       .Configure(c => c.WhenInjectedInto(typeof(DoCheckDecorator<>))));

    Kernel.Bind(x =>
      x.FromThisAssembly()
       .SelectAllClasses()
       .InheritedFrom(typeof(ICommandHandler<>))
       .WithoutAttribute<DoCheckAttribute>()
       .WithAttribute<DoOtherCheckAttribute>()
       .BindAllInterfaces()
       .Configure(c => c.WhenInjectedInto(typeof(DoOtherCheckDecorator<>))));
Kernel.Bind(x=>
x、 来自此程序集()
.SelectAllClasses()
.InheritedFrom(typeof(ICommandHandler))
.without属性()
.without属性()
.BindAllInterfaces());
使用属性绑定所有对象:

    Kernel.Bind(x =>
      x.FromThisAssembly()
       .SelectAllClasses()
       .InheritedFrom(typeof(ICommandHandler<>))
       .WithoutAttribute<DoCheckAttribute>()
       .BindAllInterfaces());
    Kernel.Bind(x =>
      x.FromThisAssembly()
       .SelectAllClasses()
       .InheritedFrom(typeof(ICommandHandler<>))
       .WithAttribute<DoCheckAttribute>()
       .BindAllInterfaces()
       .Configure(c => c.WhenInjectedInto(typeof(DoCheckDecorator<>))));
    Kernel.Bind(x =>
      x.FromThisAssembly()
       .SelectAllClasses()
       .InheritedFrom(typeof(ICommandHandler<>))
       .WithoutAttribute<DoCheckAttribute>()
       .WithoutAttribute<DoOtherCheckAttribute>()
       .BindAllInterfaces());
    Kernel.Bind(x =>
      x.FromThisAssembly()
       .SelectAllClasses()
       .InheritedFrom(typeof(ICommandHandler<>))
       .WithAttribute<DoCheckAttribute>()
       .WithoutAttribute<DoOtherCheckAttribute>()
       .BindAllInterfaces()
       .Configure(c => c.WhenInjectedInto(typeof(DoCheckDecorator<>))));

    Kernel.Bind(x =>
      x.FromThisAssembly()
       .SelectAllClasses()
       .InheritedFrom(typeof(ICommandHandler<>))
       .WithoutAttribute<DoCheckAttribute>()
       .WithAttribute<DoOtherCheckAttribute>()
       .BindAllInterfaces()
       .Configure(c => c.WhenInjectedInto(typeof(DoOtherCheckDecorator<>))));
Kernel.Bind(x=>
x、 来自此程序集()
.SelectAllClasses()
.InheritedFrom(typeof(ICommandHandler))
.WithAttribute()
.without属性()
.BindAllInterfaces()
.Configure(c=>c.whenInjectedTo(typeof(DoCheckDecorator)));
Bind(x=>
x、 来自此程序集()
.SelectAllClasses()
.InheritedFrom(typeof(ICommandHandler))
.without属性()
.WithAttribute()
.BindAllInterfaces()
.Configure(c=>c.whenInjectedTo(typeof(DoOtherCheckDecorator)));
使用Ninject可以通过这种方式实现这一点吗?我们是否必须回滚到手动定义每个绑定,即

    Bind<X>.To<Y>.WhenInjectedInto(?)
Bind.To.whenInjectedTo(?)
理想情况下,我们将使用以下语法:

    Bind<X>.To<Y>.WithDecorator<Z>.When(a => a.HasAttribute<DoCheckAttribute>)
Bind.To.WithDecorator.When(a=>a.HasAttribute)

所以Ninject似乎已经有了一个可以解决这个问题的扩展。使用拦截器扩展,可以将属性写入:

    [AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
    public sealed class DoCheckAttribute : InterceptAttribute
    {
        public override IInterceptor CreateInterceptor(IProxyRequest request)
        {
            return request.Context.Kernel.Get<DoCheckInterceptor>();
        }
    }
这意味着绑定变得非常简单:

    Kernel.Bind(x =>
       x.FromThisAssembly()
        .SelectAllClasses()
        .InheritedFrom(typeof(ICommandHandler<>))
        .BindAllInterfaces());

你喜欢Ninject吗?如果您愿意,我可以向您展示如何使用简单的注入器:使用装饰器。使用简单的喷油器,实际上很容易。