Quartet Lounge Team 2006
A dedicated team on the .Net open source frameworking.
 

        关于Sloth架构,前面已经做过两篇介绍了。接下来,随着Sloth代码的发布,结合实例,介绍一下Sloth的使用(看看我们怎么偷懒...:))

业务组件

咱先按着Demo的思路来,首先编写个简单的业务组件

SayHelloDemo:

 

       .
        
private string propertyStr = string.Empty;

        
public string PropertyStr
        
{
            
get
            
{
                
return propertyStr;
            }

            
set
            
{
                propertyStr 
= value;
            }

        }


        
public string SayHello(string name)
        
{
            
this.propertyStr = name;
            
return name + " 欢迎您!";
        }

        
public string SayHello(string name, string sex)
        
{
            
this.propertyStr = name;
            
return name + sex + "士,欢迎您!";
        }

 

       可以看到这个业务组件并没有什么特别之处,换句话说,我们手上现有的业务组件其实大都可以复用(省心省时啊:)),对已有的成熟业务组件的复用是我们目的之一。

客户端

接下来,我们设计客户端程序。借助Sloth,我只需要知道业务组件的类名、方法名及参数即可调用业务组件。客户端与业务组件之间已完全解耦,他们之间的联系由Sloth通过反射调用来实现。对于ASP.NET同样可行。

       WinFormClient:

         

 

          
                private NetRisc.Sloth.InfoChannels slothIC = new NetRisc.Sloth.InfoChannels();
                .......
                //调用SayHello方法
            object[] parameters = new object[1];
            parameters[
0= this.textBox1.Text;
            
this.label3.Text = slothIC.Invoke<string>("SayHelloDemo","SayHello", parameters);

            
//调用SayHello重载方法           
                parameters = new object[2];

            parameters[0= this.textBox1.Text;
            parameters[
1= this.textBox2.Text;
            
this.label4.Text = slothIC.Invoke<string>("SayHelloDemo""SayHello", parameters);

            
//获取PropertyStr属性值         

               this.label11.Text = slothIC.GetProperty<string>("SayHelloDemo", "PropertyStr");

 

       WebClient:

 

NetRisc.Sloth.InfoChannels slothIC = new NetRisc.Sloth.InfoChannels();

        
//调用本地组件
        object[] parameters = new object[1];
        parameters[
0= this.txtName.Value;
        
this.txtHello.Value = slothIC.Invoke<string>("SayHelloDemo""SayHello", parameters);

        
        parameters 
= new object[2];
        parameters[
0= this.txtName.Value;
        parameters[
1= this.txtSex.Value;
        
this.txtHello1.Value = slothIC.Invoke<string>("SayHelloDemo""SayHello", parameters);

        
//获取对象属性
        this.txtProperty.Value = slothIC.GetProperty<string>("SayHelloDemo""PropertyStr");

 

       业务组件及客户端准备好后只需要生成相应配置文件即可(此次由于时间关系,暂未发布配置工具,但是有相关说明文件Readme_deploy.txt,麻烦各位参照说明手工修改一下)

       运行效果:

      

 

       以上,Sloth带来的好处其实不言自明,业务的重构或展现层的修改之间,几乎不会相互影响。

远程调用代理(SlothAgent)

       目前分布式系统凭借其易用性安全性等特点仍有很大市场,但是分布式系统的开发量及难度也是很大的。SlothAgent可以部分简化此问题。

       Sloth提供了远程调用方法,通过SlothAgent调用远程组件方法,甚至直接获得远程对象,以上的业务组件很容易就可实现远程调用。

       只需要改写很少的代码:

      

       WinFormClient:

          

          //调用SayHello方法(远程调用)
            object[] parameters = new object[1];
            parameters[
0= this.textBox1.Text;
            
this.label3.Text = slothIC.RemoteService<string>("SayHelloDemo","SayHello", parameters);

            
//调用SayHello重载方法(远程调用)
            parameters = new object[2];
            parameters[
0= this.textBox1.Text;
            parameters[
1= this.textBox2.Text;
            
this.label4.Text = slothIC.RemoteService<string>("SayHelloDemo""SayHello", parameters);

            
//获取PropertyStr属性值(远程调用)
            this.label11.Text = slothIC.Property<string>("SayHelloDemo""PropertyStr");

 

       WebClient:(同样,只需要更改为远程调用方法)

 

       然后配置SlothSlothAgent的通信方式即可(Sloth.dll.xml文件),SlothAgent为了客户端的需求,支持TCP/IPHTTP两种协议通讯,所以可以在服务器上同时设置TCP/IPHTTP的监听端口,方便和客户端通讯;同时,Sloth的配置文件中设置的TCP/IPHTTP端口必须和服务器上设置的一致

    运行客户端程序,与本地调用完全一致,图略。

       以上,大家可以发现,整个架构其实是建立在Sloth+SlothAgent+Xml配置文件之上。

虚拟业务

        通过虚拟业务配置工具(此次由于时间关系,暂未发布配置工具,配置文件为ControlBus.xmlSloth即根据此配置文件自动完成调用),我们可以很容易的在图形界面上实现多个业务方法的组合,形成新的复杂的业务逻辑方法。

       我们首先创建几个独立的业务方法:

       BusinessDemo

 

业务逻辑方法(目前外部只可调用Public方法)

       假设业务逻辑如下:(数据库为sqlserver2000NorthWind

Ø         需要完成录入产品表Products一条记录,但是只录入 产品名称 ProductName 产品类型名称 CategoryName,而Products表只有CategoryID列。

Ø         首先由SearID方法从Categories表获取对应CategoryName CategoriesID

Ø         SaveProduct方法由ProductNameCategoriesID 产生插入sql

Ø         最后由Execute方法实现数据库操作。

       我们已为此虚拟业务逻辑生成了正确的配置文件,同时将此虚拟业务方法命名为 SavePersonnelInfo(虽然暂时没提供配置工具,但是大家可以发现可以手工修改配置文件ControlBus.xml以达到改名或改业务的目的)

<?xml version="1.0"?>
<InformationBus>
  
<SavePersonnelInfo Virtual="1" Transaction="0">
    
<Component>
      
<Methods UseCode="3">
        
<searchID Action="0" CompareOrign="">
          
<Parameter ParameterCount="1">
            
<cName Origin="" Point="" ParameterType="String">
            
</cName>
          
</Parameter>
          
<Object>BusinessDemo</Object>
          
<NextNode>SaveProduct</NextNode>
        
</searchID>
        
<SaveProduct Action="0" CompareOrign="">
          
<Parameter ParameterCount="2">
            
<categoryID Origin="searchID" Point="Return" ParameterType="Int32">
            
</categoryID>
            
<productName Origin="" Point="" ParameterType="String">
            
</productName>
          
</Parameter>
          
<Object>BusinessDemo</Object>
          
<NextNode>Execute</NextNode>
        
</SaveProduct>
        
<Execute Action="0" CompareOrign="">
          
<Parameter ParameterCount="1">
            
<sqlstr Origin="SaveProduct" Point="Return" ParameterType="String">
            
</sqlstr>
          
</Parameter>
          
<Object>BusinessDemo</Object>
          
<NextNode>
          
</NextNode>
        
</Execute>
      
</Methods>
    
</Component>
    
<CustomParameter>
      
<searchID>
        
<Return>Return</Return>
      
</searchID>
      
<SaveProduct>
        
<Return>Return</Return>
      
</SaveProduct>
    
</CustomParameter>
  
</SavePersonnelInfo>
</InformationBus>


        客户端调用虚拟业务:

 

          //调用虚拟组合业务(远程调用)
            object[] parameters = new object[2];
            parameters[
0= this.textBox3.Text;
            parameters[
1= this.textBox4.Text;
            
this.label8.Text = slothIC.RemoteService<bool>("SavePersonnelInfo", parameters).ToString();


       利用Sloth这个功能,进一步减少了因为需求变更引起的业务重构工作量,如果再算上节省的测试工作量,这开发成本可少了一大块,更为重要的是时间....

快速开发工具集

       在这个demo中,大家可以发现我的数据操作层是直接引用了一个组件NetRisc.DataBase.DBAccess.dll及其他几个组件,其实这些都属于我们的快速开发工具集里面的成员,接下来我们将逐步整理、公布这些工具集(OpenSource)。这就是我们目前开发中复用的组件:)俗话说好马配好鞍,Sloth架构+开发工具集才是我们真正实现资源复用,快速开发的利器:)。

       例如这个数据库操作组件(NetRisc.DataBase.DBAccess)目前支持SQLServer Oracle数据库的各类操作,需要的只是在配置文件里面提供数据库连接字符串,使用时指定数据库类型即可。

       最后,代码整理、发布工作一直在我们紧张忙碌的工作之余顺利进行ing,希望各位持续关注我们Quartet Lounge Team 2006 ,我们更希望大家参与进来,贡献您的想法,您的组件。让我们享受Sloth开发之快....

      Sloth及演示程序源代码下载发布页:
      http://www.cnblogs.com/duochunyu/archive/2006/10/26/540896.html 

ps:26日下午代码发布时选错了分类。向版大道歉:不好意思,希望您看到...

 

                                                                                                                      

      

 

      

 

posted on 2006-10-27 00:40  QuartetLounge  阅读(644)  评论(10编辑  收藏  举报