轻量级的.NET对象查找服务和AOP开发框架Netop.Core源码解说(3)--类工厂/对象查找服务

来源:互联网 发布:人员调度 算法 编辑:程序博客网 时间:2024/05/21 08:03

上节谈了谈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

 

谢富鸿fhxie@sohu.com


转载自:http://www.cnblogs.com/fhxie/p/6884231.html

阅读全文
0 0