Article : COM + Services Part 4 ( .NET Implementation )
Reply
![]() |
|
|
From:
Nasha
|
Hey Group,
For using COM+ Services from .NET Assemblies is possible with help of System.EnterprisesServices Namespace.
Let us create a three tiered application containing a Business Manager classes, an Entity and a Data Layer class along with a client which uses the Buss layer object. We will start by creating Entity and DB layer classes.
The flow goes like this: Our Client creates an Employee object. This employee object has Employee data and data about employee's skills. Our client calls methods on the Employee Manager by passing the Employee object. Employee Manager calls the UpdateEmployee method on the EmpDataStore by passing the Employee object and then calls the UpdateEmpSkills method on EmpSkillsManager to update employee's skills data.
Creating the Employee and Skill Entities
1) Open a Class Library name it EmpEntity and add a class named Employee and add the below code to it.
using System; namespace EmpEntity { public class Employee { private string name; private ArrayList skills; // will hold an arraylist of Skill objects private string address; public Employee() {}
public string Name { get{return name;} set{ name = value;} } public string Address { get{return address;} set{address = value;} }
public ArrayList Skills { get{return skills;} set{skills = value;} } } }
using System; namespace EmpEntity { public class Skill { private string name; private int totexp; public Skill() {} public string Name { get{return name;} set{name = value;} } public int TotalExpWithSkill { get{return totexp;} set{totexp =value;} } } }
Creating the EmpDataStore and SkillDataStore
1) Open a new class library project name it EmpDataDLL and add the below two classes 2) Add Reference to EmpEntity
using System; using EmpEntity; using System.Data; using System.Data.SqlClient;
namespace EmpDataDLL { public class EmpDataStore { public EmpDataStore() {}
public void UpdateEmployee(Employee emp) { // Implementation using ADO.NET to update the // Emp details to the Employee Table } } }
using System;
namespace EmpDataDLL { public class SkillDataStore { public SkillDataStore() {}
public void UpdateSkill(ArrayList skills) { // Implementation using ADO.NET to update the Emp Skills //details by parsing through the arraylist to update the Employee Skills table } } }
Creating the Buss Managers or the Serviced Components
Any component that has to be COM+ component or as we call it a serviced component in .NET has to subclass from a class called Serviced Component.
1) Open a new class Library project named BussDLL. 2) Add Reference to System.Enterprises namespace 3) Create two new classes EmployeeManager and EmpSkillsManager and subclass it from ServicedComponent.
After we have created a serviced component let us see how we can implement transactions using Transaction and AutoComplete Attribute and ContextUtil class.
We have seen that there are various transaction types that COM+ components can have. You can set the components transaction type from the COM+ Services MMC interface but to implement the transaction type of the component you need to set the Transaction attribute.
To implement Transactions in .NET we need to first set the transaction option.
Transaction option can be set only at a class level using the Transaction Attribute. Transaction Attribute takes a parameter called as TransactionOption where you can mention the transaction type as follows :
[Transaction(TransactionOption.Required)] // you can mention any one of the five available types.
4) Add the Transaction attribute at the top of the classes.
MTS and COM+ both work on a two-phase commit protocol i.e. the transaction will either succeed or fail and all operations taking part in the transaction need to acknowledge its caller whether it succeeded or not. Hence when the transaction is through successfully we need to complete the transaction if we have to rollback we need to abort the transaction. When a transaction is aborted at any stage all the operations prior to the operation, which failed, are automatically roll-backed. To complete or abort a transaction we need to get its Context.... when I say context what I mean is a handle to the current transaction. There is a class in .NET called as ContextUtil that retrieves the current transaction context. All you need to do is just refer to the class in your code.
5) Add a method named UpdateEmployee to EmployeeManager class from where we will update employee's info. In this method we receive an employee object which we will send to EmpDataStore class to update employee data in the DB.
Any Transaction is completed using the SetComplete() method of ContextUtil class and aborted using SetAbort() method.
6) Finally our class should look something like this
using System.EnterpriseServices; using EmpEntity; using EmpDataDLL; using System;
namespace BussDLL { [Transaction(TransactionOption.Required)] public class EmployeeManager:ServicedComponent { public EmployeeManager() {} public void UpdateEmployee(Employee emp) { try { EmpDataDLL.EmpDataStore empDataStore= new EmpDataDLL.EmpDataStore(); empDataStore.UpdateEmployee(emp); EmpSkillsManager empSkillMgr = new EmpSkillsManager(); empSkillMgr.UpdateSkill(emp); ContextUtil.SetComplete(); } catch(Exception ex) { ContextUtil.SetAbort(); throw ex; } } } }
Note: - The call to the EmpDataStore is in a try catch block hence if any exception is thrown from the Database layer it will be caught and we will successfully rollback the transaction. If everything goes smooth we go ahead and complete the transaction.
Similarly we will create the EmpSkillsManager class
using System; using System.Collections; using System.EnterpriseServices; using EmpDataDLL; namespace BussDLL { public class EmpSkillsManager:ServicedComponent { public EmpSkillsManager() {} public void UpdateSkill(EmpEntity.Employee emp) { EmpDataDLL.SkillDataStore skillsDB = new SkillDataStore(); skillsDB.UpdateSkill(emp.Skills); ContextUtil.SetComplete(); } } }
Sometimes we want that if everything goes fine the transaction should be automatically completed only incase of any failure when an exception is raised the transaction should be aborted. In that case we can use the AutoComplete attribute i.e. [AutoComplete]
AutoComplete attribute is used at function level. When we use AutoComplete we need not call SetComplete but yes we need to call SetAbort.
Using the AutoComplete attribute
namespace BussDLL { [Transaction(TransactionOption.Required)] public class EmployeeManager { public EmployeeManager() {} [AutoComplete] public void UpdateEmployee(Employee emp) { try { EmpDataDLL.EmployeeDataStore empDataStore= new EmpDataDLL.EmployeeDataStore(); empDataStore.Update(emp); } catch(Exception ex) { ContextUtil.SetAbort(); throw ex; } } } }
We are through with the creation of our serviced components. Tomorrow we will create our client and check out app. Along with that we will check out the various types of COM+ Applications, Implement Object Pooling, JIT and Security in them. -- Please post your queries and comments for my articles in the user group for the benefit of all. I hope this step from my end is helpful to all of us. You can find all articles written by me at my personal blog http://spaces.msn.com/members/nasha.
Regards,
Namratha (Nasha)<o:p></o:p> |
|
View other groups in this category.
![]() |
To stop getting this e-mail, or change how often it arrives, go to your E-mail Settings.
Need help? If you've forgotten your password, please go to Passport Member Services.
For other questions or feedback, go to our Contact Us page.
If you do not want to receive future e-mail from this MSN group, or if you received this message by mistake, please click the "Remove" link below. On the pre-addressed e-mail message that opens, simply click "Send". Your e-mail address will be deleted from this group's mailing list.
Remove my e-mail address from dotNET User Group Hyd.
|
|