EnterpriceServices

业务层:

ContractedBlock.gif ExpandedBlockStart.gif 业务层
 1None.gifusing System;
 2None.gifusing System.EnterpriseServices;
 3None.gif
 4None.gifusing MyBusiness.Personnel;
 5None.gifusing MyBusiness.Orders;
 6None.gif
 7None.gif
 8None.gif[assembly: ApplicationName("MyBusiness.Administration")]
 9None.gif[assembly: ApplicationActivation(ActivationOption.Library)]
10None.gif
11None.gifnamespace MyBusiness.Administration
12ExpandedBlockStart.gifContractedBlock.gifdot.gif{
13ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
14InBlock.gif    /// Epmployee Administration Object
15ExpandedSubBlockEnd.gif    /// </summary>

16InBlock.gif     
17InBlock.gif    [ Transaction(TransactionOption.RequiresNew) ]
18InBlock.gif    [ ObjectPooling(true510) ]
19InBlock.gif    public class EmployeeMaintenance : ServicedComponent
20ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
21InBlock.gif        public EmployeeMaintenance()
22ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
23ExpandedSubBlockEnd.gif        }

24InBlock.gif
25InBlock.gif        [ AutoComplete(true) ]
26InBlock.gif        public void AddEmployee(string Name, string Address, int JobType, bool bMakePayrollFail, bool bMakeOrdersFail)
27ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
28InBlock.gif            // Create out tier 3 of 4 components that act as the data access layer.
29InBlock.gif            PayrollMaintenance payroll_maintenance = new PayrollMaintenance();
30InBlock.gif            OrdersMaintenance orders_maintenance = new OrdersMaintenance();
31InBlock.gif
32InBlock.gif            // Some business Logicdot.gifNames must always be stored in upcase!
33InBlock.gif            Name = Name.ToUpper();
34InBlock.gif
35InBlock.gif            // Let the tier 3 of 4 access the seperate databases and store 
36InBlock.gif            // our complex business information.
37InBlock.gif            payroll_maintenance.AddEmployee(Name, Address, JobType, bMakePayrollFail);
38InBlock.gif            orders_maintenance.SetupUser(Name, JobType, bMakeOrdersFail);
39ExpandedSubBlockEnd.gif        }

40ExpandedSubBlockEnd.gif    }

41ExpandedBlockEnd.gif}

42None.gif
DAL层:
ContractedBlock.gif ExpandedBlockStart.gif Orders类DAL
 1None.gifusing System;
 2None.gifusing System.Data;
 3None.gifusing System.Data.SqlClient;
 4None.gifusing System.EnterpriseServices;
 5None.gif
 6None.gif[assembly: ApplicationName("MyBusiness.Orders")]
 7None.gif[assembly: ApplicationActivation(ActivationOption.Library)]
 8None.gif
 9None.gifnamespace MyBusiness.Orders
10ExpandedBlockStart.gifContractedBlock.gifdot.gif{
11ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
12InBlock.gif    /// Orders Specific Mainenance Object
13ExpandedSubBlockEnd.gif    /// </summary>

14InBlock.gif    [ Transaction(TransactionOption.Required) ]
15InBlock.gif    [ ObjectPooling(true510) ]
16InBlock.gif    public class OrdersMaintenance : ServicedComponent
17ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
18InBlock.gif        public OrdersMaintenance()
19ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
20ExpandedSubBlockEnd.gif        }

21InBlock.gif
22InBlock.gif        [ AutoComplete(true) ]
23InBlock.gif        public void SetupUser(string Name, int JobType, bool MakeFail)
24ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
25InBlock.gif            string sConnection = "Persist Security Info=false;Data Source=192.168.0.98;Initial Catalog=MyOrdersDB;User ID=sa;Password=100200;";
26InBlock.gif            SqlConnection cnn= new SqlConnection(sConnection);
27InBlock.gif
28InBlock.gif            // Open the Database Connection
29InBlock.gif            cnn.Open();
30InBlock.gif
31InBlock.gif            DataSet ds = new DataSet();
32InBlock.gif            DataRow dr;
33InBlock.gif            SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM tblOrderUser", cnn);
34InBlock.gif            SqlCommandBuilder cb = new SqlCommandBuilder(da);
35InBlock.gif
36InBlock.gif            // Tell it we want Primary and index keys.
37InBlock.gif            da.MissingSchemaAction = MissingSchemaAction.AddWithKey;
38InBlock.gif
39InBlock.gif            // Load the table from the database.
40InBlock.gif            // This will become slow very quickly, so not a great design,
41InBlock.gif            // but it does demonstrate the reading and writing of data.
42InBlock.gif            da.Fill(ds, "tblOrderUser");
43InBlock.gif
44InBlock.gif            dr = ds.Tables["tblOrderUser"].NewRow();
45InBlock.gif
46InBlock.gif            dr["sName"= Name;
47InBlock.gif            dr["nJobType"= JobType;
48InBlock.gif            dr["sTransactionActivityID"= ContextUtil.ActivityId;
49InBlock.gif            dr["sTransactionContextID"= ContextUtil.ContextId;
50InBlock.gif
51InBlock.gif            ds.Tables["tblOrderUser"].Rows.Add(dr);
52InBlock.gif
53InBlock.gif            da.Update(ds, "tblOrderUser");
54InBlock.gif
55InBlock.gif            // Close the Database Connection
56InBlock.gif            cnn.Close();
57InBlock.gif
58InBlock.gif            if(MakeFail)
59ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
60InBlock.gif                // Oh no!!! Its all gone horibly wrong.
61InBlock.gif                throw new Exception("User requested Exception in PayrollMaintenance.AddEmployee");
62ExpandedSubBlockEnd.gif            }

63ExpandedSubBlockEnd.gif        }

64ExpandedSubBlockEnd.gif    }

65ExpandedBlockEnd.gif}

66None.gif
ContractedBlock.gif ExpandedBlockStart.gif Person类DAL
 1None.gifusing System;
 2None.gifusing System.Data;
 3None.gifusing System.Data.SqlClient;
 4None.gifusing System.EnterpriseServices;
 5None.gif
 6None.gif[assembly: ApplicationName("MyBusiness.Personnel")]
 7None.gif[assembly: ApplicationActivation(ActivationOption.Library)]
 8None.gif
 9None.gifnamespace MyBusiness.Personnel
10ExpandedBlockStart.gifContractedBlock.gifdot.gif{
11ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
12InBlock.gif    /// Payroll Specific Mainenance Object
13ExpandedSubBlockEnd.gif    /// </summary>

14InBlock.gif    [ Transaction(TransactionOption.Required) ]
15InBlock.gif    [ ObjectPooling(true510) ]
16InBlock.gif    public class PayrollMaintenance : ServicedComponent
17ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
18InBlock.gif        public PayrollMaintenance ()
19ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
20ExpandedSubBlockEnd.gif        }

21InBlock.gif
22InBlock.gif        public void AddEmployee(string Name, string Address, int JobType, bool MakeFail)
23ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
24InBlock.gif            string sConnection = "Persist Security Info=false;Data Source=192.168.0.98;Initial Catalog=MyPersonnelDB;User ID=sa;Password=100200;";
25InBlock.gif            SqlConnection cnn= new SqlConnection(sConnection);
26InBlock.gif
27InBlock.gif            // Open the Database Connection
28InBlock.gif            cnn.Open();
29InBlock.gif
30InBlock.gif            DataSet ds = new DataSet();
31InBlock.gif            DataRow dr;
32InBlock.gif            SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM tblEmployees", cnn);
33InBlock.gif            SqlCommandBuilder cb = new SqlCommandBuilder(da);
34InBlock.gif
35InBlock.gif            // Tell it we want Primary and index keys.
36InBlock.gif            da.MissingSchemaAction = MissingSchemaAction.AddWithKey;
37InBlock.gif
38InBlock.gif            // Load the table from the database.
39InBlock.gif            // This will become slow very quickly, so not a great design,
40InBlock.gif            // but it does demonstrate the reading and writing of data.
41InBlock.gif            da.Fill(ds, "tblEmployees");
42InBlock.gif
43InBlock.gif            dr = ds.Tables["tblEmployees"].NewRow();
44InBlock.gif
45InBlock.gif            dr["sName"= Name; ;
46InBlock.gif            dr["sAddress"= Address;
47InBlock.gif            dr["nJobType"= JobType;
48InBlock.gif            dr["sTransactionActivityID"= ContextUtil.ActivityId;
49InBlock.gif            dr["sTransactionContextID"= ContextUtil.ContextId;
50InBlock.gif
51InBlock.gif            ds.Tables["tblEmployees"].Rows.Add(dr);
52InBlock.gif
53InBlock.gif            da.Update(ds, "tblEmployees");
54InBlock.gif
55InBlock.gif            // Close the Database Connection
56InBlock.gif            cnn.Close();
57InBlock.gif
58InBlock.gif            if(MakeFail)
59ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
60InBlock.gif                // Oh no!!! Its all gone horibly wrong.
61InBlock.gif                throw new Exception("User requested Exception in PayrollMaintenance.AddEmployee");
62ExpandedSubBlockEnd.gif            }

63ExpandedSubBlockEnd.gif        }

64ExpandedSubBlockEnd.gif    }

65ExpandedBlockEnd.gif}

66None.gif
客户:
ContractedBlock.gif ExpandedBlockStart.gif 客户代码
 1None.gifEmployeeMaintenance employee_maintenance = new EmployeeMaintenance();
 2None.gif
 3None.gif            // Its a one function wonder, but we could call a few. The
 4None.gif            // transaction would succeed as long as they all voted yes.
 5None.gif            // Remeber the transaction state lived with the life of the
 6None.gif            // object and the transaction is commited or rolled back 
 7None.gif            // when it goes out of scope.
 8None.gif
 9None.gif            try
10ExpandedBlockStart.gifContractedBlock.gif            dot.gif{
11InBlock.gif
12InBlock.gif                employee_maintenance.AddEmployee
13InBlock.gif                    (
14InBlock.gif                    txtEmployee.Text, 
15InBlock.gif                    txtAddress.Text, 
16InBlock.gif                    Convert.ToInt32(txtJobType.Text), 
17InBlock.gif                    cbExceptionPersonnel.Checked, 
18InBlock.gif                    cbExceptionOrders.Checked
19InBlock.gif                    );
20InBlock.gif
21ExpandedBlockEnd.gif            }

22None.gif            catch(Exception ex)
23ExpandedBlockStart.gifContractedBlock.gif            dot.gif{
24InBlock.gif                string sMessage = "The transaction threw the follwing Exception:\n\n";
25InBlock.gif                
26InBlock.gif                sMessage += ex.Message + "\n";
27InBlock.gif                sMessage += ex.Source + "\n";
28InBlock.gif                sMessage += "\nThe transaction will be rolled back.";
29InBlock.gif
30InBlock.gif                MessageBox.Show(sMessage, "Unhandled Exeption");
31InBlock.gif
32InBlock.gif                throw ex;
33ExpandedBlockEnd.gif            }

转载于:https://www.cnblogs.com/nanshouyong326/archive/2006/11/27/574120.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值