在正式展开之前,有一些概念要先做一个界定。首先: 领域 模型是指系统应对的 领域 中所有逻辑的一个抽象,本质上它是 领域 中各种对象和概念以及它们之间关系的 集合 。你可以用自然语言描述它,也可以用UML来描述,或者是代码去描述。特别地,当我们使用面
在正式展开之前,有一些概念要先做一个界定。首先:领域模型是指系统应对的领域中所有逻辑的一个抽象,本质上它是领域中各种对象和概念以及它们之间关系的集合。你可以用自然语言描述它,也可以用UML来描述,或者是代码去描述。特别地,当我们使用面向对象建模技术来实现这个领域模型时,我们可以把这个实现出来的模型称之为对象模型。我们可以认为领域模型是一个概念模型,是分析阶段的产物。让精心构建的对象模型高效地工作有很多底层的技术问题需要解决,其中如何满足领域对象的业务方法在计算过程中对数据的需求是一个普遍存在的问题(实际上,在实际应用中,我们会遇到更为复杂的情况,不只是有数据的需求,还可能出现对应用层面发生依赖)。对于这一问题,目前有两种模型可供借鉴,那就是基于数据访问的集合类和领域事件模式。
基于数据访问的集合类(Data Access Based Collection)
package oobbs.domainmodel; import java.io.Serializable; import java.util.List; /** * The collection interface represents a set of objects, it's like the * java.util.Collection, however, there no real objects in this collection, it * only looks like a collection, its method's implementation is database access * operation! see <code>oobbs.infrastructure.persistence.AbstractHibernateCollection</code> * @author laurence.geng */ public interface Collection<entity pk extends serializable owner> { void setOwner(Owner owner); void setOwnerName(String ownerName); /** * Adds an object. This method will persist entity to database directly! * @param e an entity instance. * @return the pK the generated primary key * after insert into database. */ PK add(Entity e); void addAll(java.util.Collection<entity> c); /** * Removes the entity. This method will remove this entity from database * directly. */ void remove(Entity e); void removeAll(java.util.Collection<entity> c); boolean contains(Entity o); boolean isEmpty(); int size(); /** * The most important method. It returns a subset of the whole collection. * the returned subset is fetched from database by sql, hql or other data * access way, The Collection itself never load all elements once time! */ List<entity> toList(int startIndex, int offset); void flush(); }</entity></entity></entity></entity>
下面则是基本于hibernate的集合接口实现类。它实现了所有的基本的操作。在Forum类中就会这样一个字段以及相应的getter和setter:
@Transient private Collection<thread long forum> forumThreads; @Autowired /** * Sets ForumThreadCollection. * ForumThreadCollection is injected by this setter. When a collection instance injected, set this forum to its forum! */ public void setForumThreads(@Qualifier("forumThreads") Collection<thread long forum> forumThreads) { this.forumThreads = forumThreads; this.forumThreads.setOwner(this); this.forumThreads.setOwnerName("forum"); } /** * Gets this forum's thread collection. */ public Collection<thread long forum> getForumThreads() { return forumThreads; }</thread></thread></thread>
List<thread> threads = forum.getForumThreads().toList(startThreadIndex,threadTotal);</thread>
领域事件(Domain Event)
Domain Event模式最初由udi dahan提出,发表在自己的博客上:http://www.udidahan.com/2009/06/14/domain-events-salvation/这一模式得到广泛的认可。它所要应对的正是将领域对象从对repository或service的依赖中解脱出来,避免让领域对象对这些设施产生直接依赖。它的做法就是当领域对象的业务方法需要依赖到这些对象时就发出一个事件,这个事件会被相应的对象监听到并做出处理。在我的oobbs系统中,我对这一模式做了一些改进,主要是消除静态方法和规范事件模型。在我的方案中,这一机制由这样几个角色:DomainEventDispatcher,DomainEvent和DomainEventListener.
DomainEventDispatcher会以字段的形式存在于领域对象中,负责在业务方法中dispatch领域事件。下面是所有Dispatcher的基类:
package oobbs.domainmodel; import java.util.HashMap; import java.util.Map; /** * The DomainEventDispatcher take charge of listener registration and dispatch * domain event to corresponding listener to handle. Usually, one dispatch per * domain object. */ public class DomainEventDispatcher { /** The listener map. all registered listeners are stored in this map. */ protected Map<string domainobejctlistener> listeners = new HashMap<string domainobejctlistener>(); /** * Adds a listener. * * @param listener the listener */ public void addListener(DomainObejctListener listener) { listeners.put(listener.getName(), listener); } /** * Removes all listeners. */ public void removeAllListeners() { listeners.clear(); } }</string></string>
一般来说一个领域对象会有一个对应的event dispatcher,这个dispatcher会有一组重载的dispatch方法,用于分发不同的领域事件。下面是oobbs中Forum对象的event dispatcher.
package oobbs.domainmodel.forum; import oobbs.Constants; import oobbs.domainmodel.DomainEventDispatcher; import oobbs.domainmodel.ResultCollector; /** * * The ForumEventDispatcher dispatch all events which about Forum object. * @author * laurence.geng */ public class ForumEventDispatcher extends DomainEventDispatcher { public void dispatch(GetForumThreadEvent event, ResultCollector result) { ForumListener forumListener = (ForumListener) listeners.get(Constants.FORUM_REPO_AS_FORUM_LISTENER); forumListener.handleGetForumThreadEvent(event, result); } public void dispatch(GetForumPostCountEvent event, ResultCollector result) { ForumListener forumListener = (ForumListener) listeners.get(Constants.FORUM_REPO_AS_FORUM_LISTENER); forumListener.handleGetFroumPostCountEvent(event, result); } public void dispatch(GetForumThreadCountEvent event, ResultCollector result) { ForumListener forumListener = (ForumListener) listeners.get(Constants.FORUM_REPO_AS_FORUM_LISTENER); forumListener.handleGetForumThreadCountEvent(event, result); } }
package oobbs.domainmodel; /** * The supper class of all domain events. all events should provide event * source, the domain object which fired this event. */ public class DomainEvent { /** The event source, the domain object which fired this event. */ protected Object source; public DomainEvent(Object source) { super(); this.source = source; } /** * Gets the event source. * * @return the event source */ public Object getSource() { return source; } }
package oobbs.domainmodel.forum; import oobbs.domainmodel.DomainEvent; /** * The Event that forum request to get its threads. * @author laurence.geng */ public class GetForumThreadEvent extends DomainEvent { /** The start index of request thread. */ private int startIndex; /** The count of request thread. */ private int count; public GetForumThreadEvent(Forum source, int startIndex, int count) { super(source); this.startIndex = startIndex; this.count = count; } public int getStartIndex() { return startIndex; } public int getCount() { return count; } }
package oobbs.domainmodel; /** * The super class of all domain object listeners. it handles events from * domain objects. Each listener has to provide a name as key for registering * itself to dispatcher. * @see DomainObejctEvent * @author laurence.geng */ public interface DomainObejctListener { /** * Gets the name. * * @return the name */ public String getName(); }
package oobbs.domainmodel.forum; import oobbs.domainmodel.DomainObejctListener; import oobbs.domainmodel.ResultCollector; /** * * The forum listener. It handles all events from Forum object. * * @see * ForumEvent * @author laurence.geng */ public interface ForumListener extends DomainObejctListener { /** * * Handle the event that a forum requests to get its threads. * * @param * event the event * @param result the result */ public void handleGetForumThreadEvent(GetForumThreadEvent event,ResultCollector result); }
/** * * The Forum's repository with hibernate implementation, besides, it's a forum * listener which handle * all events come from forum object. */ public class ForumHibernateRepository extends AbstractHibernateRepository<forum long> implements ForumRepository { /* * (non-Javadoc) * * @see * oobbs.domainmodel.forum.ForumListener#handleGetForumThreadEvent(oobbs * .domainmodel.forum.GetForumThreadEvent, * oobbs.domainmodel.ResultCollector) */ @Overridepublic void handleGetForumThreadEvent(final GetForumThreadEvent event, ResultCollector result) { List<thread> threads = (List<thread>) getHibernateTemplate().executeWithNativeSession(new HibernateCallback() { @SuppressWarnings("unchecked") public Object doInHibernate(Session session) throws HibernateException, SQLException { logger.info("Start to load threads of forum."); // Get forum. String getForuumThreadHql = "from Thread as thread where thread.forum=:forum"; List<thread> threads = (List<thread>) session .createQuery(getForuumThreadHql) .setCacheable(true) .setParameter("forum", event.getSource()) .setFirstResult(event.getStartIndex()) .setMaxResults(event.getCount()).list(); return threads; } }); result.add(threads); } } </thread></thread></thread></thread></forum>
public List<thread> getThreads(int startIndex, int count) { GetForumThreadEvent event = new GetForumThreadEvent(this, startIndex, count); ResultCollector result = new ResultCollector(); forumEventDispatcher.dispatch(event, result); return (List<thread>) result.getUniqueResult(); }</thread></thread>
当然,在上面讲述的整个机制中,我们漏掉了一环没有讲,那就是dispatcher是如何被实例化并完成注册listener工作的。由于oobbs使用了spring的IOC管理对象, dispatcher是由IOC创建并完成注册listener等初始化工作的。下面的类完成了这一系列工作:
package oobbs.infrastructure.appcontext; import oobbs.domainmodel.DomainObejctListener; import oobbs.domainmodel.forum.ForumEventDispatcher; import oobbs.domainmodel.forum.ThreadEventDispatcher; import org.apache.log4j.Logger; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanPostProcessor; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; /** * * The ApplicationBeanPostProcessor will do some initialization work when bean * is created by spring ioc container, * such as: Adding listeners for domain * event dispatchers and so on. * @author laurence.geng */ public class ApplicationBeanPostProcessor implements BeanPostProcessor, ApplicationContextAware { /** The Constant logger. */ private static final Logger logger = Logger .getLogger(ApplicationBeanPostProcessor.class); /** The application context. */ private ApplicationContext applicationContext; /* * (non-Javadoc) * @see org.springframework .beans.factory * .config.BeanPostProcessor #postProcessBeforeInitialization * (java.lang.Object, java.lang.String) */ public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { return bean; // we could potentially return any object reference here... } /* * (non-Javadoc) * @see * org.springframework.beans.factory.config.BeanPostProcessor * #postProcessAfterInitialization(java.lang.Object, java.lang.String) */ public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { logger.debug("Bean '" + beanName + "' created : " + bean.toString()); // Adding listeners for domain event dispatchers. if ("forumEventDispatcher".equals(beanName)) { ForumEventDispatcher forumEventDispatcher = (ForumEventDispatcher) bean; forumEventDispatcher.addListener((DomainObejctListener) applicationContext.getBean("forumRepository")); } if ("threadEventDispatcher".equals(beanName)) { ThreadEventDispatcher threadEventDispatcher = (ThreadEventDispatcher) bean; threadEventDispatcher.addListener((DomainObejctListener) applicationContext.getBean("threadRepository")); } return bean; } /* * (non-Javadoc) * @see * org.springframework.context.ApplicationContextAware#setApplicationContext * (org.springframework.context.ApplicationContext) */ @Override public void setApplicationContext(ApplicationContext arg0) throws BeansException { this.applicationContext = arg0; } }
小结