EnterpriceServices
业务层:
1
using System;2
using System.EnterpriseServices;3

4
using MyBusiness.Personnel;5
using MyBusiness.Orders;6

7

8
[assembly: ApplicationName("MyBusiness.Administration")]9
[assembly: ApplicationActivation(ActivationOption.Library)]10

11
namespace MyBusiness.Administration12


{13

/**//// <summary>14
/// Epmployee Administration Object15
/// </summary>16
17
[ Transaction(TransactionOption.RequiresNew) ]18
[ ObjectPooling(true, 5, 10) ]19
public class EmployeeMaintenance : ServicedComponent20

{21
public EmployeeMaintenance()22

{23
}24

25
[ AutoComplete(true) ]26
public void 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 Logic
Names 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

1
using System;2
using System.Data;3
using System.Data.SqlClient;4
using System.EnterpriseServices;5

6
[assembly: ApplicationName("MyBusiness.Orders")]7
[assembly: ApplicationActivation(ActivationOption.Library)]8

9
namespace MyBusiness.Orders10


{11

/**//// <summary>12
/// Orders Specific Mainenance Object13
/// </summary>14
[ Transaction(TransactionOption.Required) ]15
[ ObjectPooling(true, 5, 10) ]16
public class OrdersMaintenance : ServicedComponent17

{18
public OrdersMaintenance()19

{20
}21

22
[ AutoComplete(true) ]23
public void SetupUser(string Name, int JobType, bool MakeFail)24

{25
string 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 Connection29
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 Connection56
cnn.Close();57

58
if(MakeFail)59

{60
// Oh no!!! Its all gone horibly wrong.61
throw new Exception("User requested Exception in PayrollMaintenance.AddEmployee");62
}63
}64
}65
}66

1
using System;2
using System.Data;3
using System.Data.SqlClient;4
using System.EnterpriseServices;5

6
[assembly: ApplicationName("MyBusiness.Personnel")]7
[assembly: ApplicationActivation(ActivationOption.Library)]8

9
namespace MyBusiness.Personnel10


{11

/**//// <summary>12
/// Payroll Specific Mainenance Object13
/// </summary>14
[ Transaction(TransactionOption.Required) ]15
[ ObjectPooling(true, 5, 10) ]16
public class PayrollMaintenance : ServicedComponent17

{18
public PayrollMaintenance ()19

{20
}21

22
public void AddEmployee(string Name, string Address, int JobType, bool MakeFail)23

{24
string 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 Connection28
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 Connection56
cnn.Close();57

58
if(MakeFail)59

{60
// Oh no!!! Its all gone horibly wrong.61
throw new Exception("User requested Exception in PayrollMaintenance.AddEmployee");62
}63
}64
}65
}66

1
EmployeeMaintenance employee_maintenance = new EmployeeMaintenance();2

3
// Its a one function wonder, but we could call a few. The4
// transaction would succeed as long as they all voted yes.5
// Remeber the transaction state lived with the life of the6
// object and the transaction is commited or rolled back 7
// when it goes out of scope.8

9
try10

{11

12
employee_maintenance.AddEmployee13
(14
txtEmployee.Text, 15
txtAddress.Text, 16
Convert.ToInt32(txtJobType.Text), 17
cbExceptionPersonnel.Checked, 18
cbExceptionOrders.Checked19
);20

21
}22
catch(Exception ex)23

{24
string 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

32
throw ex;33
}

浙公网安备 33010602011771号