【第一章 Design】 3 运行时组件

来源:互联网 发布:做美工的软件 编辑:程序博客网 时间:2024/04/29 17:42

        Cayenne框架 是Java应用程序和关系数据库的中介,下图展示了框架的主要组件。

 

        

 

(实现原理部分 , 就不详细翻译了)

 

 垂直结构 - Cayenne 持久化栈

 

Vertically this structure reflects the runtime organization of the Cayenne persistence stack. Cayenne instantiates and configures the stack (shown in yellow above) using the XML descriptors provided by the user (usually these are the XML files created by CayenneModeler). Alternatively persistence stack can be assembled via the API and/or using Spring or any similar approach. Moreover Cayenne stack is fully dynamic and can be altered at runtime, since currently (as of version 1.2) Cayenne does not rely on bytecode enhancement.

 

 

 

 水平结构 - Cayenne 层

 

The top Cayenne layer (ObjectContext, Query API, Expression API) is what the application needs to work with persistent objects. This layer implements Cayenne part of the persistence contract between the application and the framework, while underlying layers deal with details of the object-to-relational translation and data source interaction. Access to the mapping metadata and the stack objects residing in the lower layers is possible via the API.

 

 

 

 主要组成 

    框架中的一些独立组件
  1. ObjectContext
  2. Mapping Metadata
  3. DataChannel
  4. DataNode
  5. DbAdapter
  6. org.apache.cayenne.event - 事件支持包.
  7. org.apache.cayenne.conn - 数据源包.

 

1  ObjectContext  --  对象上下文

org.apache.cayenne.ObjectContext is an interface that defines API for the application interaction with Cayenne. Two most notable implementors are DataContext, used in most Cayenne applications, and CayenneContext used on the remote client.

ObjectContext accesses the underlying persistence stack via a parent DataChannel. ObjectContexts that support multiple levels of nesting (such as DataContext) themselves implement a org.apache.cayenne.DataChannel interface.

 

2 Mapping Metadata --  映射元数据

In runtime the mapping metadata is accessed via a DataChannel in a form of org.apache.cayenne.map.EntityResolver (EntityResolver available via ObjectContext is usually obtained from the channel behind the scenes). EntityResolver provides a single namespace for multiple DataMaps.

Another important function of the EntityResolver is providing access to the org.apache.cayenne.property.ClassDescriptor instances for each persistent entity. Those a compiled by Cayenne on the fly using EntityResolver's org.apache.cayenne.property.ClassDescriptorFactory.

 

 3 DataChannel --  数据通道

 "org.apache.cayenne.DataChannel" is an abstraction of Cayenne persistence stack. It is very simple - it defines only 4 methods, two for actually performing persistent operations, one to access metadata, and one - to access EventManager: 

   QueryResponse onQuery(ObjectContext originatingContext, Query query);

    GraphDiff onSync(ObjectContext originatingContext, GraphDiff changes, int syncType);

    EventManager getEventManager();

    EntityResolver getEntityResolver();

 

DataChannel is rarely used by the application directly. Instead ObjectContexts use it internally as an abstract persistent store. There are few kinds of DataChannels in Cayenne:

  • DataDomain (the most commonly used DataChannel) connects to one or more databases via DataNodes.
  • DataContext can serve as a DataChannel for other DataContexts
  • ClientChannel is a DataChannel that connects to a remote Cayenne service.

4 DataNode  -- 数据源节点 (这个可以支持一个项目使用多个数据库)

DataNode is a Cayenne "driver" for a single physical data source, such as a relational database. In fact currently DataNode only handles JDBC sources (so maybe it should be renamed to JdbcDataNode?).

DataNode doesn't do much work on its own, instead delegating most tasks to "org.apache.cayenne.action.SQLAction" instances returned by DbAdapter.

 

5 DBAdapter -- 数据库适配器(数据节点的实际实现)

 

The JDBC specification provides a great abstraction for relational database work. Still in practice some cross-database portability 。org.apache.cayenne.dba.DbAdapter interface is used in Cayenne for an additional portability layer sitting on top of JDBC.

Usually Cayenne detects the type of the database dynamically and installs the right adapter in runtime. However a user can enforce a specific adapter class by entering its name using "Adapter" tab in the DataNode Editor panel in CayenneModeler. Alternatively an adapter can be set on a DataNode via a method call.

 

提供强大的关系数据库抽象,并且实现跨数据库的功能。能够在运行时自动检测数据库类型,自动加载适应的适配器。还可自己通过dba.DbAdapter定义和扩展。

 

 

6 org.apache.cayenne.event 事件支持包

 

Cayenne includes a full-featured events mechanism. It allows creation of local and distributed event queues. It is very powerful and generic; it is not tied to Cayenne persistence features in any way and can be used in any application.

 

 

 7 org.apache.cayenne.conn 数据源包

 

DataNode obtains connections via a javax.sql.DataSource. If an application is deployed in a J2EE container, DataSource is normally provided by container via JNDI. For standalone applications (or if you don't want to use JNDI) Cayenne provides its own DataSource that includes connection pooling features. Whenever you select org.apache.cayenne.conf.DriverDataSourceFactory in the Modeler, you are choosing to use Cayenne DataSource.

Cayenne DataSource implementation is located in the org.apache.cayenne.conn package and is fully independent from the rest of the framework. Since Cayenne DataSource is chosen automatically, users rarely need to know more details. One notable exception are applications requiring users to interactively enter database login information. In this case you can write your own DataSource wrapper, and internally instantiate Cayenne DataSource (org.apache.cayenne.conn.PoolManager) after the user provides login and password.



 

 原文:

 

http://cayenne.apache.org/doc/runtime-components.html