如何实现“类版本控制"(使用同一类的不同版本) [英] How to implement "class versioning" (using different version of the same class)
问题描述
问题是:我们的主类(比如:Contract)每年都在变化.添加了一些属性,删除了其他属性.我们不知道明年会怎样.它可能会发生很大变化,也可能根本不会发生变化.
Here is the problem : our main class (say : Contract) change every year. Some properties are added, other are removed. We don't know how it will look next year. It may change a lot or not at all.
另一方面,我们现在(新要求...)必须保留每个合同的历史记录.每次用户更新合约时,整个对象都必须存储在备份中(例如 - 在表中序列化).
On the other hand, we now (new requirement...) have to keep an historic of every contracts. Each time the user update a contract, the whole object must be stored in a backup (e.g. - serialized in a table).
当然,我们必须能够读回它......一种选择(残酷的)是每年都有一个新的合同类(Contract2008,Contract2009,......).
Of course, we must be able to read it back... One option (brutal) is to have a new Contract class every year (Contract2008, Contract2009, ...).
但这会非常麻烦(而且丑陋),因为很多类都依赖于 Contract - 事实上,我们每年都必须创建一堆新类.
But it would be very cumbersome (and ugly) since a lot of classes depend on Contract - in facts, we would have to create a bunch of new classes every year.
遇到过这样的问题吗?有什么建议吗?
Ever have this kind of problem ? Any suggestion ?
提前致谢!
(我们使用的是 C# 2.0.)
(We're using C# 2.0.)
添加:感谢您的回答.我们现在问自己如何使用字典/XML 文件来实现版本控制而不破坏所有代码.在这种情况下,字典似乎很性感 :o)
ADDED : Thank for your answers. We're now asking ourself how we could use a dictionary / an XML file to implement versioning without breaking all the code. Dictionary seems very sexy in this context :o)
推荐答案
停止序列化对象.以关系方式存储数据.序列化适用于短期,比如通过网络传输;不是永久存储.
Stop serializing the object. Store the data relationally. Serialization is intended for short durations, say like streaming across the wire; not permanent storage.
而不是将字段编码到类中;听起来您可能需要更多字典类型的结构,该结构可以根据按年份存储的数据库配置值动态提取字段.然后根据这些值构建动态 UI.每年都必须创建一个新类并测试代码,维护成本似乎是不可能的.
Instead of coding the fields into the class; it sounds like you might need more of dictionary sort of structure that can dynamically pull fields based on database configuration values stored by year. Then build a dynamic UI based on those values. Having to create a new class each year and test the code seems impossibly expensive to maintain.
这篇关于如何实现“类版本控制"(使用同一类的不同版本)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!