Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从Castle Windsor文档运行提供的IOC(控制反转)演示?_C#_Inversion Of Control_Castle Windsor - Fatal编程技术网

C# 如何从Castle Windsor文档运行提供的IOC(控制反转)演示?

C# 如何从Castle Windsor文档运行提供的IOC(控制反转)演示?,c#,inversion-of-control,castle-windsor,C#,Inversion Of Control,Castle Windsor,在[编者按:写下这个问题后,wiki已经更新了更多的示例代码]上,有以下(不完整的)演示代码 如何完成此代码,使其在C#控制台应用程序中正常运行 //application starts... var container = new WindsorContainer(); // adds and configures all components using WindsorInstallers from executing assembly container.Install(FromAsse

在[编者按:写下这个问题后,wiki已经更新了更多的示例代码]上,有以下(不完整的)演示代码

如何完成此代码,使其在C#控制台应用程序中正常运行

//application starts...
var container = new WindsorContainer();

// adds and configures all components using WindsorInstallers from executing assembly
container.Install(FromAssembly.This());

// instantiate and configure root component and all its dependencies and their dependencies and...
var king = container.Resolve<IKing>();
king.RuleTheCastle();

// clean up, application exits
container.Dispose();
但是,当我运行它时,它抛出了一个运行时错误

更新:

我最终使用了,因为:

  • 该文档远远优于Castle Windsor(以及Spring.NET)
  • 与使用反射的温莎城堡相比,它的速度更快(8到50倍)
  • 要开始,请单击“访问Dojo”,然后按照以下步骤进行操作


    完成此操作后,您可以查看。视频确实很快地介绍了这些概念,因此最好完成第一个概念。

    通常,为了在C#console应用程序中应用IoC,您可以使用入口点(例如
    Main
    方法)来:

    • 实例化并配置IoC容器-示例代码中的前两行
    • 使用容器构建对象图(对象及其依赖项)-第三行代码
    • 通常通过调用解析对象上的方法(代码的第四行),将控制传递给解析对象
    • 应用程序完成后,告诉容器执行它需要执行的任何清理-第5行代码
    这就是代码演示所展示的,从这个角度来看,它是完整的

    但是,要使此应用程序正常工作,还有两个非常重要的先决条件:

    • 应用程序的设计必须考虑控制原理的反转(这实际上是最难的部分)
    • console应用程序的入口点程序集必须包含用于配置容器的元素,这些元素基本上定义了在需要接口时将使用的具体类型
    本例中安装程序类的代码如下所示:

    public class KingApplicationInstaller : IWindsorInstaller
    {
       public void Install(IWindsorContainer container, IConfigurationStore store)
       {
            container.Register(
                Component.For<IKing>().ImplementedBy<King>());
       }
    }
    
    公共类KingApplicationInstaller:IWindsorInstaller
    {
    public void安装(IWindsorContainer、IConfigurationStore)
    {
    集装箱。登记(
    Component.For().ImplementedBy());
    }
    }
    
    呃,为什么投票会被否决?如果Castle Windsor像它想象的那样简单,那么在Windsor wiki上完成示例演示代码不是很简单吗?除了示例之外,您还编写了一些代码吗?如果没有,您需要创建IKing接口,创建一个实现IKing接口的具体类,并创建一个配置来告诉Castle.Windsor将IKing解析为您的具体类。我已经创建了IKing接口,并创建了一个实现IKing接口的具体类。我还没有创建一个配置来告诉Castle.Windsor在哪里解析IKing作为混凝土类-我假设这会自动发生。你需要在程序集中添加Windsor安装程序,让Windsor知道IKing接口将在运行时由King混凝土类解析。太棒了!我现在就试试。首页上的代码应该只是告诉你如何使用
    WindsorContainer
    类,而不是一个完整的示例应用程序。因此,它不包括
    IKing
    King
    类型,也不包括它们的(隐含)安装程序。我确实意识到,对于新手来说,缺少安装程序可能太令人费解了,所以我添加了一个。@KrzysztofKoźmic:+1用于您添加的基于约定的安装程序示例。这让我想起了你在2011年国家数据中心(NDC)上发表的鼓舞人心的演讲。
    public class KingApplicationInstaller : IWindsorInstaller
    {
       public void Install(IWindsorContainer container, IConfigurationStore store)
       {
            container.Register(
                Component.For<IKing>().ImplementedBy<King>());
       }
    }