.NET 领域对象持久化模式

原模式说明请见: http://www.theserverside.net/articles/showarticle.tss?id=DomainObjectsPersistencePattern

领域(业务)对象(Domain Objects)是应用中的核心数据结构,也包含了商业规则,怎么持久化领域对象,做到灵活、易复用、好维护,是这个Pattern想解决的问题。要解决这个问题就要将持久化代码(Persistence Code)和领域对象代码分开,中间使用了Factory来加载、存储领域对象,此外通过定义抽象IFactory接口来实现调用代码和不同具体存取工厂实例来的松耦合。下面是它的示例代码:

 
 
public class Customer { // Private data members String _customerId; String _companyName; String _contactName; String _contactTitle; public Customer() {} // Properties for Customer object public String CustomerId { get { return _customerId; } set { _customerId = value;} } public String CompanyName { get { return _companyName; } set { _companyName = value;} } public String ContactName { get { return _contactName; } set { _contactName = value;} } public String ContactTitle { get { return _contactTitle; } set { _contactTitle = value;} } } public interface ICustomerFactory { // Standard transactional methods for single-row operations void Load(Customer cust); void Insert(Customer cust); void Update(Customer cust); void Delete(Customer cust); // Query method to return a collection ArrayList FindCustomersByState(String state); } public class CustomerFactory : ICustomerFactory { // Standard transactional methods for single-row operations void Load(Customer cust) { /* Implement here */ } void Insert(Customer cust) { /* Implement here */ } void Update(Customer cust) { /* Implement here */ } void Delete(Customer cust) { /* Implement here */ } // Query method to return a collection ArrayList FindCustomersByState(String state) { /* Implement here */ } }

这个呢是客户端调用代码:

 
 
public class NorthwindApp { static void Main ( string [] args) { Customer cust = new Customer(); CustomerFactory custFactory = new CustomerFactory(); // Let's load a customer from Northwind database. cust.CustomerId = " ALFKI " ; custFactory.load(cust); // Pass on the Customer object FooBar(cust); // custList is a collection of Customer objects ArrayList custList = custFactory.FindCustomersByState( " CA " ); } }

 

想法是不是很简单?而且越看越和微软早期的ECC Pattern(就是我们常见的Ctrl +Info类的处理方式)很像:

应该说二者有相同处,不过看问题的角度和试图解决的问题有一些区别。

其实这里边还是有一些问题没有解决的:

1)这不是非常的OO,哈,至少在几年前的Java代码里边这么写会被人骂死的;

2)对于对象关系的处理没有说明。是对所有的关系再由相应的Factory来处理,还是怎么作?不过就算不用这个Pattern,也存在同样的问题。关系的维护要么自己维护,要么交给很牛的对象持久层服务来自动完成,比如我们的NOD。不过NOD又必须将操作和对象绑定,这样对于现在的多层架构又不是很好处理...扯远了。

无论如何,这样作还是有比较多的好处的。具体怎么用,就要看具体的解决方案框架了。这个就是一个很大也很头疼的话题了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值