• NBear IOC 重构笔记


    按 NBearV3 中文用户手册的做法, 历经坎坷。 愿本文给相似经历的朋友一些帮助。

    1。整理好所需要的 Dll 和 MQService (Hosting 的调度中心),Hosting 程序。这个结构很好。 Hosting 可分布,但是对于客户端程序来说,只认得 MQService 。
        Dll版本的不一致会造成许多莫名其妙的问题。
        Hosting 可能起动不了。说 类实现没有实现接口。
        客户端可能报,Dll 不一致。

    用 sn -T webpart.dll (注意 -T  须是大写) , 可查看 Dll 的 公钥 。

    2。MQService 和 Hosting , 例子程序是正确的。贴一下配置文件吧。
    MQService.config

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      
    <configSections>
        
    <section name="serviceFactory" type="NBear.IoC.Service.Configuration.ServiceFactoryConfigurationSection, NBear.IoC" />
      
    </configSections>
      
    <serviceFactory type="Remoting" name="testServiceFactory" protocol="TCP" server="192.168.1.97" port="8888" debug="true" maxTry="30" />
    </configuration>


    Hosting.config

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
        
    <configSections>
            
    <section name="serviceFactory" type="NBear.IoC.Service.Configuration.ServiceFactoryConfigurationSection, NBear.IoC" />
            
    <section name="castle"   type="Castle.Windsor.Configuration.AppDomain.CastleSectionHandler, Castle.Windsor" />

        
    </configSections>
        
    <castle>
            
    <components>
                
    <!--You can use standard castle component decleration schema to define service interface impls here-->
                
    <component id="categoryservice" service="Bus.Ent.IDictProc,Bus" type="Bus.Ent.DictProc,BusImp"/>
            
    </components>
        
    </castle>
        
    <serviceFactory type="Remoting" name="testServiceFactory" protocol="TCP" server="192.168.1.97" port="8888" debug="true" maxTry="30" />
    </configuration>


    3。 Console客户端和Web客户端的调用方式,配置方式,是一样的, 但例子给出的, 怎么调也不对。应该像下面一样:

    <?xml version="1.0"?>
    <configuration>
        
    <configSections>
            
    <section name="serviceFactory" type="NBear.IoC.Service.Configuration.ServiceFactoryConfigurationSection, NBear.IoC" />
            
    <section name="entityConfig" type="NBear.Common.EntityConfigurationSection, NBear.Common"/>
            
    <section name="castle" type="Castle.Windsor.Configuration.AppDomain.CastleSectionHandler, Castle.Windsor"/>
        
    </configSections>
        
    <entityConfig>
            
    <includes>
                
    <add key="Sample Entity Config" value="~/EntCfg.xml"/>
            
    </includes>
        
    </entityConfig>
        
    <castle>
            
    <components>
                
    <!--You can use standard castle component decleration schema to define service interface impls here-->
                
    <component id="categoryservice" service="Bus.Ent.IDictProc,BusIfc" type=""/>
            
    </components>
        
    </castle>
        
    <serviceFactory type="Remoting" name="testServiceFactory" protocol="TCP" server="192.168.1.97" port="8888" debug="true" maxTry="30" />
        
    <appSettings/>
        
    <connectionStrings>
            
    <!--<add name="Northwind" connectionString="Server=(local);Database=Northwind;Uid=sa;Pwd=sa" providerName="NBear.Data.SqlServer.SqlDbProvider"/>-->
        
    </connectionStrings>
        
    </configuration>

    component 节中, 不要写 type , 如果写了实现的 Type ,它就不会远程调用了。 想想也是, 客户端只留一个接口就OK了。不必要留实现。

    4。接口要继承自 NBear.IoC.Service.IServiceInterface  , 而不是 IService 。这一点例子也写错了。
    5。其它的很简单,照例子做就没有问题。如果出现问题,多检查环境。

    再说说, 什么是 IOC 。

    IOC 是 Inversion of Control , 控制反转, 直接理解,不太形象,这里有著名的好莱坞理论:如果你想做什么事,你告诉我,但是你呆着别动,到时我会找你。后被Martin Fowler改名为 Dependency Injection 依赖注射,也就是将类之间的关系通过第三方进行注射,不需要类自己去解决调用关系。

    它是一种实现将类解耦的方式,也算是一种设计原则 ,形成了一个设计模式(DI依赖倒置),也是分布式设计的具体应用。

    在分布式中这样说,客户端说,我要创建 A 对象, 调用 A.B 方法 。但是 , 客户端只有 A 接口 和 A.B 空方法 。 具体实现会在服务器端来做。 这就是 IOC 。 实现了调用与实现的分离。

    在百科上看到了 IOC 的定义,让人感觉更明白,更容易理解:

    IoC  (Inversion of Control)

      中文译为控制反转  又称为“依赖注入”(DI =Dependence Injection)

      IOC的基本概念是:不创建对象,但是描述创建它们的方式。在代码中不直接与对象和服务连接,但在配置文件中描述哪一个组件需要哪一项服务。容器负责将这些联系在一起。

      其原理是基于OO设计原则的The Hollywood Principle:Don't call us, we'll call you(别找我,我会来找你的)。也就是说,所有的组件都是被动的(Passive),所有的组件初始化和调用都由容器负责。组件处在一个容器当中,由容器负责管理。

      简单的来讲,就是由容器控制程序之间的关系,而非传统实现中,由程序代码直接操控。这也就是所谓“控制反转”的概念所在:控制权由应用代码中转到了外部容器,控制权的转移,是所谓反转。

    最后一句,结合代码,点睛之笔。

    alarm   作者:NewSea     出处:http://newsea.cnblogs.com/    QQ,MSN:iamnewsea@hotmail.com

      如无特别标记说明,均为NewSea原创,版权私有,翻载必纠。欢迎交流,转载,但要在页面明显位置给出原文连接。谢谢。
  • 相关阅读:
    spark-2.0.0与hive-1.2.1整合
    kafka_2.11-0.10.0.0安装步骤
    hive-1.2.1安装步骤
    linux时间编程
    gdb与coredump
    linux下静态库、动态库的创建与使用
    linux常用工具
    js数组、内置对象、自定义对象
    JS学习五(js中的事件)
    JS学习四(BOM DOM)
  • 原文地址:https://www.cnblogs.com/newsea/p/1367341.html
Copyright © 2020-2023  润新知