posts - 32, comments - 118, trackbacks - 0, articles - 0

2006年6月18日

Active Record Pattern 因为 Ruby on Rails 而出名。ASP.NET 中广泛流的使用 Typed DataSet 的办法,其实也就是 Active Record Pattern。

因为 Typed DataSet 有很多弊端,又不是 POJO Entity,所以被一开始就被我否定的了,但是不幸的是,同时我也否定了 Active Record Pattern。

事实上这个模式在“数据库驱动的 Web 系统”这个前提下是相当成功的。因为这种系统中的 Entity Life Cycle 可以非常短。

  • 一个 Life Cycle 从数据库中生来出来,到绑定到界面上结束。
  • 另一个 Life Cycle 从收集用户输入开始,到保存入数据库结束。

在“数据库驱动的 Web 系统”中,用轻量级 POJO Entity 加上 Active Record Pattern 是非常合适的。

用 Domain Driven Design 概念并没有错,但是不要遗忘进一步细化到应用前提,比如在 Web 下这个模型应该是个什么样子。系统模型的 Entity 不一定能直接用到 Web 系统中。GoF 提倡的 OOP 原则是 "favor interface over base class"。核心含义就是只关心系统中必须知道的那一部分。同样应用到设计中,非常有必要为不同的应用场景设计轻量级的 Entity。

这就产生对轻量级 ORM 的需求。把数据库的数据载入 Entity,再把 Entity  中数据存回数据库。轻量级就是仅此功能而已,不要企图区管理 Entity Life Cycle,不要追求 Container Level Transaction,更不要弄出个 Application Server 出来。

实际上 MS 已经意识在这点,在下一代的 ADO.NET 中将包含 Entity Data Model (EDM),见 The ADO.NET Entity Framework Overview。几度取消 Object Space 的发布,并不是它不重要,而是太重要了,以至于必须为它安置个更加合适的位置,比如.NET 的核心部位,ADO.NET。使得 Typed DataSet 的粉丝没有话说。

posted @ 2006-06-17 11:21 yysun 阅读(2474) 评论(5) 编辑

2006年6月16日

现在主要做 .NET,但是每过一段时间总是要回头看看 Java 世界。看到了新的 EJB 3.0 spec。联想到最近这里有不少 ORM 的讨论,就顺便推荐大家看看这个 Java Persistence API in 60 Minutes

Java 中的 Annotation 就是 .NET 中的 Attribute。虽然 Java 中有这个东东比 .NET 晚,但是通过 EJB 3.0,Java 世界把 Annotation 发挥得更加淋漓尽致,ORM, Dependency Injection, AOP ... 。这正是士别三日,当刮目相看。

长久以来,一直觉得 .NET 缺乏的是 Container 这个概念。 MTS 实在 Sucks。EJB 以前当然也 Sucks。人们甚至觉得 EJB 带来的问题比解决的问题多。这两个因素掩盖了 container 的重要性。加上 SOA 这个 Buzz word 又转移了一部分注意力。

现在的 EJB 3.0 借助 Annotation 实现了瘦身,完全回到了 POJOs 的路线上来。顿时感到一阵清风。

很希望看到 .NET 世界也出个类似的 Container 。NHibernate?
没有 Container 的情况下, ORM 也没有多少意义。

[Update] 谢谢下面很多好的评论。联想到了 Annotation (注记) 与 Attribute (属性) 的用法。

一是静态的 MetaData。告诉使用者我是什么,比如 [Serializable] 。二是动态的,在 Container 配合下,请它帮我做点什么。例如 [ServiceDependecy] 就是告诉 ObjectBuilder 这个 Container 用 Dependcy Injection 帮我初始化。@Remote 就是告诉 EJB Container,有人远程叫我得时候,你想办法把我送出去 ...。通过 Container 进行功能扩充后,我自己集中做我得事情,不必顾及其他方面的细节(AOP的精髓)。所以,这就可以用 POJO 来解决主要的业务,加个注记,扔给 EJB Contain,让它变成 Stateless Session Bean。加个注记,扔给 EntityManager,让它保存到什么地方去,Table, XML ...。 

Dependcy Injection 相对简单些,在 Container 的什么 Collection 中找到想要的东西。 Interception 和 Dynamic Proxy 则相对复杂些,一般需要 Emit 新的类出来。Deployment (发布) 因此有了新的含义。在发布的时候动态生成程序 Emit dynamic proxy during deployment 是个很有意思的方向。

posted @ 2006-06-15 15:38 yysun 阅读(2226) 评论(6) 编辑

2006年6月8日

Smart Client Software Factory team is recommending the "Model View Presenter" pattern. They have created Guidance Package for using this pattern.

From my experience, the "Presentation Model" pattern is better. When I asked if SC-SF can also include Guidance Package for PM, it looks like people may think MVP is more suitable for unit tests than PM. Read this thread.

To me, PM is even easier for unit testing, because it is less coupled.

MVP.pngPM.png

I have refactored the CAB Smart Part Quick Start project to use MVP and PM patterns and have also created unit test projects for comparison.

Source Code:

They can be added to the CAB Smart Part Quick Start solution.

 

posted @ 2006-06-07 15:38 yysun 阅读(1607) 评论(0) 编辑

2006年5月16日

Composite UI Application Block (CAB) is really a powerful framework for smart client applications. It comes with samples implementing the Model View Control (MVC) pattern and the Model View Presenter (MVP) pattern. There is actually a third pattern called Presentation Model. See the pictures from http://jgoodies.com/articles/patterns-and-binding.pdf.

MVC MVP PM

As described in Martin Fowler's article (MVP, PM), the presenter in MVP has no state and the views need to provide interface allow presenter to manipulate them. The Presentation Model holds and synchronizes the state, but no need to create view interfaces.

Choice between MVP and PM, as per Martin Fowler's article:

This decision would be strongly influenced by a decent framework that supports such synchronization. If you have such a thing then Presentation Model seems the better choice.

The difference is that in MVP the presenter uses the interfaces to push data into the views. That presenter refers to views. In PM, views pull data from the presentation model.  Views refer to pm via interface. The pm implements the interfaces. Personally,

What esstentially MVP and PM do is to take the state and behavior of the presentation out of view. The views are dumb. They do nothing other than data binding and bypass user inputs.

But in MVP, although it splits one "autonomous View" into two parts, actually presenter and view are logically one thing, physically two class. When adding a new view, a new presenter is required.

In PM, it is a decoupled structure. Presentation model has no idea about view(s). It just holds the state and raise events. Adding new views needs no changes to the PM. View can also choose to connect different presentation model.

Compare to MVC or MVP, I prefer using the Presentation Model, because it has a centralized place to store state/data and centralized event source to notify views to update themselves.

The CAB's Event Broker is perfect for notification. .NET 2.0's object binding is perfect to synchronize state between PM and Views.

Tags:

posted @ 2006-05-15 17:33 yysun 阅读(1812) 评论(1) 编辑

DUMP TRANSACTION irp WITH NO_LOG
BACKUP LOG irp WITH NO_LOG
DBCC SHRINKDATABASE(irp)

posted @ 2006-05-15 13:23 yysun 阅读(856) 评论(0) 编辑

2006年5月13日

继续 VSLive Toronto 的后续故事。 上次记录了 SQL/e,这次是关于 Daniel Cazzulino 的发言 《Microsoft p&p ObjectBuilder》。听完这个演讲发现最近周围的事情还都发生了些关联。

首先是日前买了本书 《Head Up Design Patterns》。它的第一章就以一个引人入胜的故事介绍了设计模式的 Strategy Pattern。这个模式的好处多多,但是负作用是会产生了很多相互关联的类。如何按照合理地创建这些类,并把它们组和起来。这就引出了另外一个话题 Inversion of Control / Dependency Injection

这几天帮女儿复习准备乐理考试,还碰到了 Interval Inversion / Triad Inversion,搞得我就很头昏。Dependency Injection 好像还好理解些。

Daniel Cazzulino 讲的 ObjectBuilder 就是一个具有 dependency injection 功能的轻量级对象容器*。 ObjectBuilder 是 MS patterns & practices Composite UI Application Block 以及 Enterprise Library 的核心部分。

这就又引到了 CAB - Composite UI Application Block。也是不久前 WB Editor 3.0 的设计已经开始了。鉴于 2.0 版本中 RAD 流毒太深**,决定应该采用个好的系统结构,正在犹豫,是否用这篇 Implement a Microsoft Word-like Object Model for Your .NET Framework Application 文章介绍的方法呢,还是用 CAB

经过聆听 Daniel Cazzulino 的讲解,不但了解了 OB 的功能,还了解了其内部的一些机制。因此决定进一步学习了一下 CAB Hands on Lab。于是得到结论就是 WB Editor 3.0 将采用 CAB。

Daniel Cazzulino 还讲了个有用的内容,Guidance Automation Toolkit / GAT,值得再用一篇笔记介绍。
* 轻量级对象容器和 ** RAD 流毒也可以讲些故事。

Tags:

posted @ 2006-05-12 12:59 yysun 阅读(1789) 评论(0) 编辑

2006年5月9日

摘要: 在VSLive Toronto 上见到两个人物,Steve Lasker 和 Daniel CazzulinoSteve Lasker 介绍了使用 SQL Server Everywhere 的 Occasionally Connected Systems。 SQL Server Everywhere(SQL/e) 何须人也?原来2-4年前做 Pocket PC 程序就相识,那时它叫 SQL Se...阅读全文

posted @ 2006-05-08 15:37 yysun 阅读(1843) 评论(4) 编辑

2006年5月8日

摘要: Changes in WB Editor 2.5 Added upload image from clipboard Added image/file upload through FTP Added "select blog server" menu for cross posting Added "open new editor" menu Added new plugin: clipboar...阅读全文

posted @ 2006-05-08 08:36 yysun 阅读(878) 评论(0) 编辑

2005年12月10日

摘要:   Flickr 是免费的图片发布和分享网站。最近被收购到 yahoo 旗下。 把图片存在 Flickr 然后连接到自己的 blog 中 是个很不错的做法。 下一版的 WB Editor 2 将支持直接上传图片到 Flickr 了。另外还会有一个插件,来管理和检索图片。  阅读全文

posted @ 2005-12-09 20:15 yysun 阅读(1080) 评论(1) 编辑

2005年12月4日

摘要: 开发个 Biztalk 系统真是颇费周折。记下一些经验备用。 1. Error about enlisting DTC. Solution: 1) Turn off the RPC security on your Windows Server 2003 computers. http://support.microsoft.com/default.aspx?scid=kb;en-us;83918...阅读全文

posted @ 2005-12-04 07:57 yysun 阅读(976) 评论(1) 编辑