Pet Shop 4.0 什么是 System.Transactions ? 泛型
System.Transactions 是 .NET 2.0 框架中新增的事务控件命名空间。它是一种处理分布式事务的新方式,没有 COM+ 注册和 COM+ 目录的开销。请注意,Microsoft 分布式事务协调器用于初始化事务。
运行情况
同步定单处理中的 Order.Insert() 方法使用 System.Transactions 插入一个定单并更新库存。通过添加对 System.Transaction 命名空间的引用,并将定单插入方法和库存减少方法包装在 TransactionScope 内,我们实现了 Order.Insert() 方法,如代码清单 1 所示。
清单 1. 运行中的 System.Transactions
using System; using System.Transactions; using PetShop.IBLLStrategy; namespace PetShop.BLL { /// <summary> /// This is a synchronous implementation of IOrderStrategy /// By implementing IOrderStrategy interface, the developer can /// add a new order insert strategy without re-compiling the whole /// BLL. /// </summary> public class OrderSynchronous : IOrderStrategy { ... /// <summary> /// Inserts the order and updates the inventory stock within /// a transaction. /// </summary> /// <param name="order">All information about the order</param> public void Insert(PetShop.Model.OrderInfo order) { using (TransactionScope ts = new TransactionScope(TransactionScopeOption.Required)) { dal.Insert(order); // Update the inventory to reflect the current inventory // after the order submission. Inventory inventory = new Inventory(); inventory.TakeStock(order.LineItems); // Calling Complete commits the transaction. // Excluding this call by the end of TransactionScope's // scope will rollback the transaction. ts.Complete(); } } } }
在 .NET Pet Shop 3 中,分布式事务由企业服务处理,需要 COM+ 注册。OrderInsert 类从服务组件派生,事务由 COM+ 处理。然后,服务组件使用 regsvr32 命令进行注册。
清单 2. Pet Shop 3 的定单插入
using System; using System.Collections; using System.EnterpriseServices; using System.Runtime.InteropServices; ... namespace PetShop.BLL { /// <summary> /// A business component to manage the creation of orders /// Creation of an order requires a distributed transaction /// so the Order class derives from ServicedComponents /// </summary> [Transaction(System.EnterpriseServices.TransactionOption.Required)] [ClassInterface(ClassInterfaceType.AutoDispatch)] [ObjectPooling(MinPoolSize=4, MaxPoolSize=4)] [Guid("14E3573D-78C8-4220-9649-BA490DB7B78D")] public class OrderInsert : ServicedComponent { ... /// <summary> /// A method to insert a new order into the system /// The orderId will be generated within the method and should not /// be supplied as part of the order creation the inventory will be /// reduced by the quantity ordered. /// </summary> /// <param name="order">All the information about the order</param> /// <returns> /// The new orderId is returned in the order object /// </returns> [AutoComplete] public int Insert(OrderInfo order) { // Get an instance of the Order DAL using the DALFactory IOrder dal = PetShop.DALFactory.Order.Create(); // Call the insert method in the DAL to insert the header int orderId = dal.Insert(order); // Get an instance of the Inventory business component Inventory inventory = new Inventory(); inventory.TakeStock( order.LineItems); ... // Set the orderId so that it can be returned to the caller return orderId; } } }
System.Transactions 的优点
从企业服务移动到 System.Transactions 可以简化部署,因为后者不需要使用 COM+ 目录。使用 COM+ 目录时,我们忽略了其他一些额外的功能,只保留了分布式事务支持。System.Transaction 使得在 ASP.NET 2.0 应用程序中编程和部署分布式应用程序变得十分简单。System.Transactions 在运行时的性能提高了 50%,因为它避免了对象实例化的 COM+ 目录查找所产生的开销。最后一个优点是,针对 SQL Server 2005 运行时,System.Transactions 能够检测到某个分布式事务何时针对宿主在一个 SQL Server 2005 实例上的两个不同数据库运行。在这种情况下,它能够将该分布式事务提升为一个本地事务,这样就可避免与分布式事务登录/两阶段提交相关的全部开销,从而极大地提高性能。
泛型
什么是泛型?
每次返回一个 Pet Shop 模型对象集合时,我们都针对该对象使用泛型类型的一个集合列表。这是 C# 2.0 的一个新增功能,称之为泛型。
运行情况
我们可以从清单 3 所示的 GetProductsByCategory 方法中看到泛型的运行情况。
清单 3. Product.cs (Pet Shop 4.0)
/// <summary> /// A method to retrieve products by category name /// </summary> /// <param name="order">The category name to search by</param> /// <returns>A Generic List of ProductInfo</returns> public IList<ProductInfo>GetProductsByCategory(string category) { // Return new if the string is empty if (string.IsNullOrEmpty(category)) return new List<ProductInfo>(); // Run a search against the data store return dal.GetProductsByCategory(category); }
以下是 Pet Shop 3 中的等效代码,它返回一个 IList:
清单 4. Product.cs (Pet Shop 3)
/// <summary> /// A method to retrieve products by category name /// </summary> /// <param name="order">The category name to search by</param> /// <returns> /// An interface to an arraylist of the search results /// </returns> public IList GetProductsByCategory(string category) { // Return null if the string is empty if (category.Trim() == string.Empty) return null; // Get an instance of the Product DAL using the DALFactory IProduct dal = PetShop.DALFactory.Product.Create(); // Run a search against the data store return dal.GetProductsByCategory(category); }
泛型的优点
泛型允许我们返回对象的强类型集合,而不是 .NET Pet Shop 3 中的 IList 集合。泛型强类型集合提供类型安全,其性能优于普通的集合。另外,泛型强类型集合将在 Visual Studio 2005 Intellisense 中出现,这可以提高开发人员工作效率。