在.NET中使用域对象持续模式
下面的示例演示了客户端如何使用这段代码。
public class NorthwindApp { static void Main (string[] args) { Customer cust = new Customer(); CustomerFactory custFactory = new CustomerFactory(); //从Northwind数据库载入客户 cust.CustomerId = "ALFKI"; custFactory.load(cust); // 传递 Customer 对象 FooBar(cust); // custList是Customer对象列表 ArrayList custList = custFactory.FindCustomersByState("CA"); } }
在上面代码中,load方法根据CustomerID(应用程序可以把这个值传递到任何子系统中而不需要暴露持续性代码)从数据库中载入Customer对象。同样,如果你载入Customer对象的数组列表,你随后也可以传递数组列表,也没有持续性代码依赖。
使用域对象持续模式分离持续性代码和Customer对象,使得Customer对象更加面向对象,更易于理解,因为它的对象模型更加接近于数据库中的数据模型。此外,这种分离使你能够把Customer传递到应用程序的不同部分(甚至于通过.net Remoting传递到分布式应用程序),而不需要暴露持续性代码。
0
相关文章