2

我是Castle Windsor的新手,我想知道是否有人可以用通用方式注册以下组件,以便我的所有命令都可以自动注册。还有如何添加装饰器。如何在Castle Windsor中自动注册(按惯例)通用装饰器

编辑:以下似乎工作,但我坚持如何配置装饰。

 WindsorContainer container = new WindsorContainer(); 

     container 
      .Register(
       Classes.FromAssemblyInThisApplication() 
        .BasedOn(typeof(ICommand)) 
        .LifestyleTransient()); 

public interface ICommand 
{ 
} 

public interface ICommandArgs 
{ 
} 

public interface ICommand<TArgs> : ICommand where TArgs : ICommandArgs 
{ 
    void Execute(TArgs args); 
} 

public class ChangePasswordArgs : ICommandArgs 
{ 
    public ChangePasswordArgs() 
    { 
    } 
} 

public class ChangePasswordCommand : ICommand<ChangePasswordArgs> 
{ 
    public void Execute(ChangePasswordArgs args) 
    { 
     Console.WriteLine("[CHANGING PASSWORD]"); 
    } 
} 
public class TransactionCommand<TArgs> where TArgs : ICommandArgs 
{ 
    private readonly ICommand<TArgs> innerCommand; 

    public TransactionCommand(ICommand<TArgs> innerCommand) 
    { 
     this.innerCommand = innerCommand; 
    } 

    public void Execute(TArgs args) 
    { 
     this.innerCommand.Execute(args); 
    } 
} 

编辑:这是我目前在但装饰没有被正确解析:

 container.Register(
      Component.For(typeof(ICommand<>)) 
       .ImplementedBy(typeof(TransactionalCommand<>)) 
        .LifestyleTransient(), 
      Classes.FromAssemblyInThisApplication() 
       .BasedOn(typeof(ICommand<>)) 
        .WithService.AllInterfaces() 
         .LifestyleTransient() 
     ); 
+0

你有什么试过的?你有没有读过http://mikehadlow.blogspot.in/2010/01/10-advanced-windsor-tricks-4-how-to.html? –

+0

是的,我做了,但它不包括自动注册 – Marco

回答

2

Herehere是关于城堡,自动注册(根据约定,所有的主题列表)和装饰。 Thisthis就是很好的例子。如果您先尝试搜索问题并阅读,因为很久以前就已经回答了,那么对您而言,这将会更加高效。 关于.NET与Castle中的IoC的好书是Mark Seeman的this one。这本书展示了许多与城堡的例子。你也可以看看他的博客。

+2

我知道这是一个旧的答案,但请考虑在你的答案中创建一些例子。强烈建议不要链接回答,因为链接可能会更改或删除。 –

相关问题