业务层 1using System; 2using System.EnterpriseServices; 3 4using MyBusiness.Personnel; 5using MyBusiness.Orders; 6 7 8[assembly: ApplicationName("MyBusiness.Administration")] 9[assembly: ApplicationActivation(ActivationOption.Library)] 10 11namespace MyBusiness.Administration 12{ 13/**////<summary> 14/// Epmployee Administration Object 15///</summary> 16 17 [ Transaction(TransactionOption.RequiresNew) ] 18 [ ObjectPooling(true, 5, 10) ] 19publicclass EmployeeMaintenance : ServicedComponent 20{ 21public EmployeeMaintenance() 22{ 23 } 24 25 [ AutoComplete(true) ] 26publicvoid AddEmployee(string Name, string Address, int JobType, bool bMakePayrollFail, bool bMakeOrdersFail) 27{ 28// Create out tier 3 of 4 components that act as the data access layer. 29 PayrollMaintenance payroll_maintenance =new PayrollMaintenance(); 30 OrdersMaintenance orders_maintenance =new OrdersMaintenance(); 31 32// Some business LogicNames must always be stored in upcase! 33 Name = Name.ToUpper(); 34 35// Let the tier 3 of 4 access the seperate databases and store 36// our complex business information. 37 payroll_maintenance.AddEmployee(Name, Address, JobType, bMakePayrollFail); 38 orders_maintenance.SetupUser(Name, JobType, bMakeOrdersFail); 39 } 40 } 41} 42
DAL层:
Orders类DAL 1using System; 2using System.Data; 3using System.Data.SqlClient; 4using System.EnterpriseServices; 5 6[assembly: ApplicationName("MyBusiness.Orders")] 7[assembly: ApplicationActivation(ActivationOption.Library)] 8 9namespace MyBusiness.Orders 10{ 11/**////<summary> 12/// Orders Specific Mainenance Object 13///</summary> 14 [ Transaction(TransactionOption.Required) ] 15 [ ObjectPooling(true, 5, 10) ] 16publicclass OrdersMaintenance : ServicedComponent 17{ 18public OrdersMaintenance() 19{ 20 } 21 22 [ AutoComplete(true) ] 23publicvoid SetupUser(string Name, int JobType, bool MakeFail) 24{ 25string sConnection ="Persist Security Info=false;Data Source=192.168.0.98;Initial Catalog=MyOrdersDB;User ID=sa;Password=100200;"; 26 SqlConnection cnn=new SqlConnection(sConnection); 27 28// Open the Database Connection 29 cnn.Open(); 30 31 DataSet ds =new DataSet(); 32 DataRow dr; 33 SqlDataAdapter da =new SqlDataAdapter("SELECT * FROM tblOrderUser", cnn); 34 SqlCommandBuilder cb =new SqlCommandBuilder(da); 35 36// Tell it we want Primary and index keys. 37 da.MissingSchemaAction = MissingSchemaAction.AddWithKey; 38 39// Load the table from the database. 40// This will become slow very quickly, so not a great design, 41// but it does demonstrate the reading and writing of data. 42 da.Fill(ds, "tblOrderUser"); 43 44 dr = ds.Tables["tblOrderUser"].NewRow(); 45 46 dr["sName"] = Name; 47 dr["nJobType"] = JobType; 48 dr["sTransactionActivityID"] = ContextUtil.ActivityId; 49 dr["sTransactionContextID"] = ContextUtil.ContextId; 50 51 ds.Tables["tblOrderUser"].Rows.Add(dr); 52 53 da.Update(ds, "tblOrderUser"); 54 55// Close the Database Connection 56 cnn.Close(); 57 58if(MakeFail) 59{ 60// Oh no!!! Its all gone horibly wrong. 61thrownew Exception("User requested Exception in PayrollMaintenance.AddEmployee"); 62 } 63 } 64 } 65} 66
Person类DAL 1using System; 2using System.Data; 3using System.Data.SqlClient; 4using System.EnterpriseServices; 5 6[assembly: ApplicationName("MyBusiness.Personnel")] 7[assembly: ApplicationActivation(ActivationOption.Library)] 8 9namespace MyBusiness.Personnel 10{ 11/**////<summary> 12/// Payroll Specific Mainenance Object 13///</summary> 14 [ Transaction(TransactionOption.Required) ] 15 [ ObjectPooling(true, 5, 10) ] 16publicclass PayrollMaintenance : ServicedComponent 17{ 18public PayrollMaintenance () 19{ 20 } 21 22publicvoid AddEmployee(string Name, string Address, int JobType, bool MakeFail) 23{ 24string sConnection ="Persist Security Info=false;Data Source=192.168.0.98;Initial Catalog=MyPersonnelDB;User ID=sa;Password=100200;"; 25 SqlConnection cnn=new SqlConnection(sConnection); 26 27// Open the Database Connection 28 cnn.Open(); 29 30 DataSet ds =new DataSet(); 31 DataRow dr; 32 SqlDataAdapter da =new SqlDataAdapter("SELECT * FROM tblEmployees", cnn); 33 SqlCommandBuilder cb =new SqlCommandBuilder(da); 34 35// Tell it we want Primary and index keys. 36 da.MissingSchemaAction = MissingSchemaAction.AddWithKey; 37 38// Load the table from the database. 39// This will become slow very quickly, so not a great design, 40// but it does demonstrate the reading and writing of data. 41 da.Fill(ds, "tblEmployees"); 42 43 dr = ds.Tables["tblEmployees"].NewRow(); 44 45 dr["sName"] = Name; ; 46 dr["sAddress"] = Address; 47 dr["nJobType"] = JobType; 48 dr["sTransactionActivityID"] = ContextUtil.ActivityId; 49 dr["sTransactionContextID"] = ContextUtil.ContextId; 50 51 ds.Tables["tblEmployees"].Rows.Add(dr); 52 53 da.Update(ds, "tblEmployees"); 54 55// Close the Database Connection 56 cnn.Close(); 57 58if(MakeFail) 59{ 60// Oh no!!! Its all gone horibly wrong. 61thrownew Exception("User requested Exception in PayrollMaintenance.AddEmployee"); 62 } 63 } 64 } 65} 66
客户:
客户代码 1EmployeeMaintenance employee_maintenance =new EmployeeMaintenance(); 2 3// Its a one function wonder, but we could call a few. The 4// transaction would succeed as long as they all voted yes. 5// Remeber the transaction state lived with the life of the 6// object and the transaction is commited or rolled back 7// when it goes out of scope. 8 9try 10{ 11 12 employee_maintenance.AddEmployee 13 ( 14 txtEmployee.Text, 15 txtAddress.Text, 16 Convert.ToInt32(txtJobType.Text), 17 cbExceptionPersonnel.Checked, 18 cbExceptionOrders.Checked 19 ); 20 21 } 22catch(Exception ex) 23{ 24string sMessage ="The transaction threw the follwing Exception:\n\n"; 25 26 sMessage += ex.Message +"\n"; 27 sMessage += ex.Source +"\n"; 28 sMessage +="\nThe transaction will be rolled back."; 29 30 MessageBox.Show(sMessage, "Unhandled Exeption"); 31 32throw ex; 33 }