輕量級的.NET對象尋找服務和AOP開發架構Netop.Core源碼解說(3)--類工廠/對象尋找服務

來源:互聯網
上載者:User

標籤:控制   property   類型   isp   sdn   get   文檔   void   down   

上節談了談Netop.Core的對於應用系統的配置資訊的處理,對於Netop.Core最核心的服務--類工廠/對象尋找服務當然要用到佈建服務,等下會說到。

  對於NET類工廠/對象尋找服務,名氣大的有Spring.Net(對應於Java的Spring--號稱輕量級中介軟體),為什麼還要再造一個輪子呢?如果說Spring是輕量級的,那Netop.Core就只

能是微量級的,夠用就好,學習曲線會大幅下降,學習研究代碼的時間也會大幅下降。
   夠用就好,何樂而不為?況且Netop.Core的類工廠/對象尋找服務除了可對本地服務進行執行個體化外,還對遠程服務(NET Remoting)和WCF服務進行統一的處理,就是說引用端程

序通過Netop.Core的類工廠/對象尋找服務執行個體化時,根本不知道也不用知道服務是本地的還是遠程服務(NET Remoting)或WCF服務。
  Netop.Core的類工廠/對象尋找服務是要對各應用服務進行配置的,就先從其配置說起:在設定檔(可查看測試程式的設定檔)Netop.Application節點下最少要配置:
    <Application.ObjectLocator>
      <DefaultAppObjectLocatorService>Netop.Core.LocatorService.DefaultAppObjectLocatorService,Netop.Core</DefaultAppObjectLocatorService>
      <ObjectServiceAgentName>AppObjectService</ObjectServiceAgentName>
      <AspectAgentName>AppAspect</AspectAgentName>
    </Application.ObjectLocator>
    <Application.Agent>
      <Agent name="AppObjectService" type="Netop.Core.Configuration.FileAgentConfiguration,Netop.Core">
        <File>Service.xml</File>
      </Agent>
      <Agent name="AppAspect" type="Netop.Core.Configuration.FileAgentConfiguration,Netop.Core">
        <File>Aspect.xml</File>
      </Agent>
    </Application.Agent>
</Application.ObjectLocator>

<AspectAgentName>AppAspect</AspectAgentName>是配置AOP服務的,下一節會說到,現暫不表述。
<ObjectServiceAgentName>AppObjectService</ObjectServiceAgentName>中的AppObjectService名對應於Application.Agent下節點Agent name="AppObjectService"的資訊:

      <Agent name="AppObjectService" type="Netop.Core.Configuration.FileAgentConfiguration,Netop.Core">
        <File>Service.xml</File>
      </Agent>

查看AppObjectLocatorConfiguration.cs檔案內容,ObjectServiceConfigurationManager類通過調用AppObjectLocatorConfigurationManager.Current獲得

Application.ObjectLocator資訊,從而獲得ObjectServiceAgentName對應的配置代理名稱,再通過調用配置代理服務得到Service.xml的內容。如測試程式Service.xml的內容為:
<Application.ObjectService>
  <Object name="A1" type="Test.Core.Service1,Test.Core">
    <Property name="Service2" ref ="A2"/>
  </Object>
  <Object name="A2" type="Test.Core.Service2,Test.Core">
  </Object>
  <Object name="A3" type="Test.Core.Service3,Test.Core" isAspect="1">
  </Object>
</Application.ObjectService>

  有了獲得Service.xml的內容的前提,實際的類工廠/對象尋找服務就是要實現這個介面:
    public interface IObjectLocator:IDisposable
    {
        object GetObject(string objectName);
        void FreeObject(string objectServiceName, object obj);

        Type GetObjectType(string objectName);
        bool IsSingleton(string objectName);
        bool IsRemotingObject(string objectName);
        bool IsCommunicationObject(string objectName);
    }
  具體實現這個介面的類正如

<DefaultAppObjectLocatorService>Netop.Core.LocatorService.DefaultAppObjectLocatorService,Netop.Core</DefaultAppObjectLocatorService>所配置的為

DefaultAppObjectLocatorService這個類,DefaultAppObjectLocatorService最重要的方法就是GetObject,這是獲得對象的方法,另一個FreeObject是釋放對象的方法。
  做完了這些,統一由程式AppObjectLocatorManager提供給外部程式調用。
  下面分別從本地服務、遠程服務(NET Remoting)和WCF服務三個方面解說:

  一、本地服務
  本地服務還部分實現依賴注入(Dependency Injection)/控制反轉(Inversion of Control)的設值注入(value值注入和引用注入),為了微量化沒有做構造注入(理論上構造注

入的事情也可以通過設值注入的方式相同的應用功能).
  普通的本地服務配置為:
  <Object name="A2" type="Test.Core.Service2,Test.Core">
  </Object>
  所有的配置都有name各type的設定。
  上面配置為:名稱叫A2的Test.Core.Service2,Test.Core的類型服務,調用為:
  IService2 s2 = Netop.Core.LocatorService.AppObjectLocatorManager.GetObject("A2")  as  IService2 ;
  不使用時調用:Netop.Core.LocatorService.AppObjectLocatorManager.FreeObject("A2", s2);
  配置還可增加服務是否單例(isSingleton),如 isSingleton的值為"true"或"1"或"yes",則為單例服務,否則為非單例服務。預設為非單例服務。
  <Object name="A2" type="Test.Core.Service2,Test.Core" isSingleton="true">
  </Object>
  1.含有value值注入的本地服務
  配置為:
  <Object name="A" type="Netop.Test.A,Netop.Test" isSingleton = "true" >
      <Property name="Code" value="2222"></Property>
  </Object>
  屬性Code在執行個體化時將被注入“2222”值.調用及isSingleton同普通的本地服務類似。
  2.含有引用注入的本地服務
  配置為:
  <Object name="A1" type="Test.Core.Service1,Test.Core">
    <Property name="Service2" ref ="A2"/>
  </Object>
  <Object name="A2" type="Test.Core.Service2,Test.Core">
  </Object>
  Service1屬性Service2是參照"A2"服務,執行個體化時將被注入. 調用及isSingleton同普通的本地服務一樣。
   
  只有本地服務才可進行設值注入和isSingleton的設定。

  二、遠程服務(NET Remoting)
  NET Remoting是在WCF服務出現之前的極好的遠程服務,但在WCF服務出來之後便屬於淘汰技術,現留著主要是為了相容,而且為了那些原有NET Remoting程式而不願改寫為WCF服

務的懶人們(當做是老闆考慮成本不願花時間花金錢呀)。
  NET Remoting服務端的發布和配置不提了,感興趣的朋友可以去查看相關資料。對於引用端(用戶端),使用者不用作其它什麼配置,除了要在Service.xml檔案中配置:

   <Object name="A1" type="Test2.Service1,Test2" location="http://127.0.0.1:8989/Test2" isClientActivated="true"></Object>
   <Object name="A2" type="Test2.Service2,Test2" location="http://127.0.0.1:8989/Service2.rem" ></Object>
   <Object name="A1IIS" type="Test2.Service1,Test2" location="http://127.0.0.1/ROS" isClientActivated="true"></Object>
   <Object name="A2IIS" type="Test2.Service2,Test2" location="http://127.0.0.1/ROS /Service2.rem" ></Object>
   即遠程服務要設定location,可選設定isClientActivated。location為伺服器端的位置(非IIS發布還要含連接埠),isClientActivated="true"表示用戶端啟用,否則為伺服器

啟用.
  調用同普通的本地服務一樣.
  遠程服務不可進行設值注入和isSingleton的設定,因為其執行個體化的本質就只是一代理對象而矣。

  三、WCF服務
  WCF服務的使用用較複雜一些,下面舉例說明。對於WCF服務:

  契約為
  namespace Contracts{
    [ServiceContract(Name = "TestService", Namespace="http://www.Netop.com")]
        public interface ITestService
        {
        [OperationContract]
        string MyOperation1(string myValue);       
        }
   }    
  實現為:
    public class TestService : ITestService
    {
        public string MyOperation1(string myValue)
        {   return "Hello: " + myValue;    }
    }

  然後要做的是:
  1、首先要實現一個繼承System.ServiceModel.ClientBase<T>的用戶端代理類
  繼承System.ServiceModel.ClientBase<T>的用戶端代理類為:

  namespace Test{
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
    public partial class TestServiceClient : System.ServiceModel.ClientBase<ITestService>, ITestService
    {
        public TestServiceClient(){  }
        public TestServiceClient(string endpointConfigurationName): base(endpointConfigurationName) { }        
        public TestServiceClient(string endpointConfigurationName, string remoteAddress): base(endpointConfigurationName, remoteAddress) { }
        public TestServiceClient(string endpointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress) : base(endpointConfigurationName,

remoteAddress) { }        
        public TestServiceClient(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress): base(binding,

remoteAddress) { }
       
        public string MyOperation1(string myValue)
        {
            return base.Channel.MyOperation1(myValue);
        }
    }
  }
  2、伺服器和用戶端該怎麼配置就還是怎麼配置(請參考有關WCF資料,在此不細說)
  如在用戶端配置的是:
  <system.serviceModel>
      <client>
        <endpoint address="http://127.0.0.1/Test/TestService.svc" binding="wsHttpBinding"
          contract="Contracts.ITestService" name="TestService" />
      </client>
  </system.serviceModel>

  3、在Service.xml的配置為:
  <Object name="TestService1" type="Test.TestServiceClient,Test" communicationObjectEndpointName="TestService ">
  </Object>

  WCF服務要多設定communicationObjectEndpointName屬性。
  即:type的值為繼承System.ServiceModel.ClientBase<T>的用戶端代理對象的全名,communicationObjectEndpointName的值為用戶端配置的WCF服務名。

  調用同普通的本地服務一樣.
  Contracts.ITestService testService1 = Netop.Core.LocatorService.AppObjectLocatorManager.GetObject("TestService1")  as  Contracts.ITestService ;
  不使用時一定要記得調用:Netop.Core.LocatorService.AppObjectLocatorManager.FreeObject("TestService1", testService1);

  WCF服務不可進行設值注入和isSingleton的設定,因為其執行個體化的本質就只是一代理對象而矣。

  總結一下:DefaultAppObjectLocatorService類實現了IObjectLocator介面,GetObject實現了本地服務、遠程服務(NET Remoting)和WCF服務的引用。本地服務還實現了依賴

注入中的設值注入,當然還實現了AOP--下節將要談到的內容。不管上面所有的實現,最後的調用僅為GetObject和FreeObject,如下所示:

    IService2 s2 = Netop.Core.LocatorService.AppObjectLocatorManager.GetObject("A2")  as  IService2 ;
    ...... //其它代碼

    Netop.Core.LocatorService.AppObjectLocatorManager.FreeObject("A2", s2);

就是這麼簡單!

    輕量級的.NET對象尋找服務和AOP開發架構源碼Netop.Core3.5:http://download.csdn.NET/detail/tom_cat_xie_jxdy/9837303

    輕量級的.NET對象尋找服務和AOP開發架構測試源碼 :http://download.csdn.Net/detail/tom_cat_xie_jxdy/9837278

   Netop.Core--輕量級的.NET對象尋找服務和AOP開發架構文檔:http://download.csdn.net/detail/tom_cat_xie_jxdy/9838212

 

輕量級的.NET對象尋找服務和AOP開發架構Netop.Core源碼解說(3)--類工廠/對象尋找服務

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.