A versioned domain model framework.
vdm的Python项目详细描述
关于
版本化域模型(VDM)是一个包,它允许您“版本化”您的 与源代码版本控制系统(如 subversion允许您对代码进行版本转换。特别是,版本化的域模型 版本完整的模型,而不仅仅是单个域对象(有关 这一区别见下文)。
目前,该包是作为sqlalchemy的扩展提供的(已测试 针对v0.4-v0.8)。
这个库相当稳定,作者在生产中使用过 自V0.2(2008年5月)起的系统。
完全版本化的域模型
允许同时涉及多个对象的“原子”更改,以及 有必要引入一个显式的 “revision”对象,表示域模型的单个变更集。
我们还需要引入“国家”的概念。这让我们 (有些)域对象是有状态的,特别是那些要进行版本控制的对象。 (状态对于支持删除/撤消删除功能以及 实现版本化的多对多关系)。
对于每个原始的域对象,我们最终得到2个域 对象:
- The ‘continuity’: the original domain object.
- The ‘version/revision’: the versions/revisions of that domain object.
通常用户永远不需要(显式地)关注 版本/修订对象,因为它们只与原始域交互 对象,该对象将在必要时向 “版本/修订”。
下面是一个伪代码示例:
# We need a session of some kind to track which objects have been changed # In SQLAlchemy can use its Session object session = get_session_in_some_way() # Our Revision object rev1 = Revision(author='me') # Associate revision with session # Any given session will have a single associated revision session.revision = rev1 # Book and Author are domain objects which has been made versioned using this library # Note the typo! b1 = Book(name='warandpeace', title='War and Peacee') b2 = Book(name='annakarenina', title='Anna') # Note the duplicate! b3 = Book(name='warandpeace') a1 = Author(name='tolstoy') # this is just shorthand for ending this revision and saving all changes # this may vary depending on the implementation rev1.commit() timestamp1 = rev1.timestamp # some time later rev2 = Revision(author='me') session.revision = rev2 b1 = Book.get(name='warandpeace') # correct typo b1.title = 'War and Peace' # add the author a1 = Author.get(name='tolstoy') b1.authors.append(a1) # duplicate item so delete b3.delete() rev2.commit() # some time even later rev1 = Revision.get(timestamp=timestamp1) b1 = Book.get(name='warandpeace') b1 = b1.get_as_of(rev1) assert b1.title == 'War and Peacee' assert b1.authors == [] # etc
实际代码
要查看一些实际的代码,请查看sqlalchemy:
vdm/sqlalchemy/demo.py vdm/sqlalchemy/demo_test.py
一般概念文档
一个很好的起点是fowler的模式,它适用于随时间变化的事物:
http://martinfowler.com/eaaDev/timeNarrative.html
特别是时间对象:
http://martinfowler.com/eaaDev/TemporalObject.html
两种可能的方法:
- (simpler) Versioned domain objects are versioned independently (like a wiki). This is less of a versioned ‘domain model’ and more of plain versioned domain objects.
- (more complex) Have explicit ‘Revision’ object and multiple objects can be changed simultaneously in each revision (atomicity). This is a proper versioned domain model.
备注:第一种方法是:
- Impossible to support versioning of many-to-many links between versioned domain objects.
- Impossible to change multiple objects ‘at once’ – that is as part of one atomic change
- Difficult to support domain model traversal, that is the ability to navigate around the domain model at a particular ‘revision’/point-in-time.
- More discussions of limitations can be found in this thread [1].
[1]:<;http://groups.google.com/group/sqlelixir/browse_thread/thread/50aee902ce3555fb/>;
版本化域模型(VDM)包主要支持第二种情况 (这显然包括第一个作为子类的子类)–因此得名。
用例
sa=在sqlalchemy中实现
- (sa)简单版本化对象的crud(除hasa外没有其他引用)
2.(sa)多-2-多和多-2-一关系的版本控制,其中 两个相关对象都有版本控制。
- (sa)取消删除上述内容。
- (sa)上述清洗。
- (SA)支持在一次提交中更改多个对象。
- (sa)头部和“过去”的一致对象遍历
7号。(SA)对版本控制对象的差异支持和 给出修改。
- 并发检查:
Simultaneous edits of different parts of the domain model
Simultaneous edits of same parts of domain model (conflict resolution or locking)
- Alice and Bob both get object X
- Bob updates object X and commits (A’s X is now out of date)
- Alice updates object X and commits
- Conflict!!
This can be resolved in the following ways:
- Locking
- Merging
Rather than summarize all situations just see Fowler on concurrency
- 支持挂起的更新(因此更新必须在可见之前获得批准)
- A non-approved user makes a change
- This change is marked as pending
- This change is notified to a moderator
- A moderator either allows or disallows the change