应用框架的设计与实现——.NET平台(4.3 SAF代码分析.源码1)

来源:互联网 发布:hcash挖矿软件 编辑:程序博客网 时间:2024/05/22 13:48
普通的实现方式和作者的实现方式:

1.普通实现方式。

业务代码:Business.cs

using System;

namespace Business
{
    
public class biz1 : MarshalByRefObject
    
{
        
public string hi()
        
{
            
return "hello world! I'm biz1.";
        }

    }

    
public class biz2 : MarshalByRefObject
    
{
        
public string hi()
        
{
            
return "hello world! I'm biz2.";
        }

    }

    
public class biz3 : MarshalByRefObject
    
{
        
public string hi()
        
{
            
return "hello world! I'm biz3.";
        }

    }


    
public class Demo1 : MarshalByRefObject
    
{
        
public string where()
        
{
            
return "Demo1: I'm here.";
        }

    }

    
public class Demo2 : MarshalByRefObject
    
{
        
public string where()
        
{
            
return "Demo2: I'm here.";
        }

    }

    
public class Demo3 : MarshalByRefObject
    
{
        
public string where()
        
{
            
return "Demo3: I'm here.";
        }

    }

}



远程对象服务:remotingserver.cs

using System;
using System.Runtime.Remoting;

namespace RemotingServer
{
    
public class Program
    
{
        
public static void Main()
        
{
            RemotingConfiguration.Configure(
"Server.config");
            Console.WriteLine(
"service started.");
            Console.WriteLine(
"press any key to exit application.");
            Console.ReadLine();
        }

    }

}



服务器端配置文件:Server.Config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  
<system.runtime.remoting>
    
<application name="remotingserver">
      
<service>
        
<wellknown mode="SingleCall" type="Business.biz1, Business"
           objectUri
="biz1.rem" />
     
<wellknown mode="SingleCall" type="Business.biz2, Business"
           objectUri
="biz2.rem" />
     
<wellknown mode="SingleCall" type="Business.biz3, Business"
           objectUri
="biz3.rem" />
     
<wellknown mode="SingleCall" type="Business.Demo1, Business"
           objectUri
="Demo1.rem" />
     
<wellknown mode="SingleCall" type="Business.Demo2, Business"
           objectUri
="Demo2.rem" />
     
<wellknown mode="SingleCall" type="Business.Demo3, Business"
           objectUri
="Demo3.rem" />
      
</service>
      
<channels>
        
<channel ref="http" port="8989"/>
      
</channels>
    
</application>
  
</system.runtime.remoting>
</configuration>



客户端代码:Client.cs

using System;
using System.Runtime.Remoting;

using Business;

namespace Client
{
    
public class Client
    
{
        
public static void Main()
        
{
            RemotingConfiguration.Configure(
"Client.config");
            biz1 biz1 
= new biz1();
            biz2 biz2 
= new biz2();
            biz3 biz3 
= new biz3();
            Demo1 demo1 
= new Demo1();
            Demo2 demo2 
= new Demo2();
            Demo3 demo3 
= new Demo3();

            Console.WriteLine(biz1.hi());
            Console.WriteLine(biz2.hi());
            Console.WriteLine(biz3.hi());
            Console.WriteLine(demo1.
where());
            Console.WriteLine(demo2.
where());
            Console.WriteLine(demo3.
where());

            Console.ReadLine();
        }

    }

}



客户端配置文件:Client.Config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  
<system.runtime.remoting>
    
<application name="Client">
      
<client>
        
<wellknown type="Business.biz1, Business"
                   url
="http://localhost:8989/remotingserver/biz1.rem"/>
        
<wellknown type="Business.biz2, Business"
                   url
="http://localhost:8989/remotingserver/biz2.rem"/>
        
<wellknown type="Business.biz3, Business"
                   url
="http://localhost:8989/remotingserver/biz3.rem"/>

        
<wellknown type="Business.Demo1, Business"
                   url
="http://localhost:8989/remotingserver/Demo1.rem"/>
        
<wellknown type="Business.Demo2, Business"
                   url
="http://localhost:8989/remotingserver/Demo2.rem"/>
        
<wellknown type="Business.Demo3, Business"
                   url
="http://localhost:8989/remotingserver/Demo3.rem"/>
      
</client>
    
</application>
  
</system.runtime.remoting>
</configuration>



2.作者的实现方式。

业务代码:Business.cs

using System;

namespace Business
{
    
// bizFactory
    public class bizFactory : MarshalByRefObject
    
{
        
public Biz getbiz1()
        
{
            
return new Biz1();
        }

        
public Biz getbiz2()
        
{
            
return new Biz2();
        }

        
public Biz getbiz3()
        
{
            
return new Biz3();
        }

    }

    
// abstruct class Biz
    public abstract class Biz : MarshalByRefObject
    
{
        
public Biz()
        
{
            Console.WriteLine(
"biz construct.");
        }

        
~Biz()
        
{
            Console.WriteLine(
"biz destory.");
        }

        
public abstract string hi();
    }

    
public class Biz1 : Biz
    
{
        
public override string hi()
        
{
            
return "hello world! I'm biz1.";
        }

    }

    
public class Biz2 : Biz
    
{
        
public override string hi()
        
{
            
return "hello world! I'm biz2.";
        }

    }

    
public class Biz3 : Biz
    
{
        
public override string hi()
        
{
            
return "hello world! I'm biz3.";
        }

    }

    
// DemoFactory
    public class DemoFactory : MarshalByRefObject
    
{
        
public Demo getDemo1()
        
{
            
return new Demo1();
        }

        
public Demo getDemo2()
        
{
            
return new Demo2();
        }

        
public Demo getDemo3()
        
{
            
return new Demo3();
        }

    }

    
// abstruct class Demo
    public abstract class Demo : MarshalByRefObject
    
{
        
public Demo()
        
{
            Console.WriteLine(
"Demo construct.");
        }

        
~Demo()
        
{
            Console.WriteLine(
"Demo destory.");
        }

        
public abstract string where();
    }

    
public class Demo1 : Demo
    
{
        
public override string where()
        
{
            
return "Demo1: I'm here.";
        }

    }

    
public class Demo2 : Demo
    
{
        
public override string where()
        
{
            
return "Demo2: I'm here.";
        }

    }

    
public class Demo3 : Demo
    
{
        
public override string where()
        
{
            
return "Demo3: I'm here.";
        }

    }

}



远程对象服务: remotingserver.cs

using System;
using System.Runtime.Remoting;

namespace RemotingServer
{
    
public class Program
    
{
        
public static void Main()
        
{
            RemotingConfiguration.Configure(
"Server.config");
            Console.WriteLine(
"service started.");
            Console.WriteLine(
"press any key to exit application.");
            Console.ReadLine();
        }

    }

}



服务端配置文件:Server.Config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  
<system.runtime.remoting>
    
<application name="remotingserver">
      
<service>
        
<wellknown mode="SingleCall" type="Business.bizFactory, Business"
           objectUri
="bizFactory.rem" />

     
<wellknown mode="SingleCall" type="Business.DemoFactory, Business"
           objectUri
="DemoFactory.rem" />
      
</service>
      
<channels>
        
<channel ref="http" port="8989"/>
      
</channels>
    
</application>
  
</system.runtime.remoting>
</configuration>



客户端类工厂:ClassFactory.cs
using System;
using System.Xml;
using System.Configuration;

using SAF.Configuration;

namespace ClassService
{
    
public class ClassFactory
    
{
        
public static object GetFactory(string factoryName)
        
{
            
object factory = null;
            SAF.Configuration.ConfigurationManager cm 
= (SAF.Configuration.ConfigurationManager)ConfigurationSettings.GetConfig("Framework");
            ClassFactoryConfiguration cf 
= cm.ClassFactoryConfig;
            XmlNode classFactoryData 
= cf.GetFactoryData(factoryName);

            
//obtain the type information
            string type = classFactoryData.Attributes["type"].Value;
            Type t 
= System.Type.GetType(type);
            
string location = classFactoryData.Attributes["location"].Value;
            factory 
= Activator.GetObject(t,location);
            
return factory;
        }

    }

}




客户端代码:Client.cs
using System;
using System.Runtime.Remoting;

using ClassService;
using Business;

namespace Client
{
    
public class Client
    
{
        
public static void Main()
        
{
            bizFactory bizFactory 
= (bizFactory)ClassFactory.GetFactory("bizFactory");
            DemoFactory demoFactory
= (DemoFactory)ClassFactory.GetFactory("DemoFactory");

            Biz biz1 
= bizFactory.getbiz1();
            Biz biz2 
= bizFactory.getbiz2();
            Biz biz3 
= bizFactory.getbiz3();
            Demo demo1 
= demoFactory.getDemo1();
            Demo demo2 
= demoFactory.getDemo2();
            Demo demo3 
= demoFactory.getDemo3();

            Console.WriteLine(biz1.hi());
            Console.WriteLine(biz2.hi());
            Console.WriteLine(biz3.hi());
            Console.WriteLine(demo1.
where());
            Console.WriteLine(demo2.
where());
            Console.WriteLine(demo3.
where());

            Console.ReadLine();
        }

    }

}




客户端配置文件:Client.exe.Config
调试了一天,发现配置文件名竟然错了。应该是(配置文件名=应用程序名+.config)

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    
<configSections>
        
<section name="Framework" type="SAF.Configuration.ConfigurationHandler,SAF.Configuration" />
    
</configSections>
    
<Framework type="SAF.Configuration.ConfigurationManager,SAF.Configuration">
        
<SAF.ClassFactory>
            
<Class name="bizFactory" location="http://localhost:8989/remotingserver/bizFactory.rem" type="Business.bizFactory, Business" />
            
<Class name="DemoFactory" location="http://localhost:8989/remotingserver/DemoFactory.rem" type="Business.DemoFactory, Business" />
        
</SAF.ClassFactory>
    
</Framework>
</configuration>