bubbletea

 

C#对象级版本控制(2)(实现代码)

  1    class DiffInfo
  2    {
  3        private int _versionNum;
  4        public int VersionNum
  5        {
  6            get
  7            {
  8                return _versionNum;
  9            }

 10        }

 11        private string _comment;
 12        public string Comment
 13        {
 14            get
 15            {
 16                return _comment;
 17            }

 18        }

 19        public DiffInfo(int versionNum, string comment)
 20        {
 21            _versionNum = versionNum;
 22            _comment = comment;
 23        }

 24        public Hashtable DiffTable = new Hashtable();
 25    }

 26    class CommonVersion
 27    {
 28        protected Type _type;    
 29        protected FieldInfo[] _fi;    
 30        protected int _currVersionNum = 1;
 31
 32        protected object _originalVersion;            
 33        public object OriginalVersion
 34        {
 35            get
 36            {
 37                return _currVersion;
 38            }

 39        }

 40        protected object _currVersion;
 41        public object CurrVersion
 42        {
 43            get
 44            {
 45                return _currVersion;
 46            }

 47            set
 48            {
 49                _currVersion = value;
 50            }

 51        }

 52        public CommonVersion(object originalVersion)
 53        {
 54            _originalVersion = originalVersion;
 55            _currVersion = originalVersion;
 56            _type = originalVersion.GetType();
 57            _fi = _type.GetFields(BindingFlags.Static |BindingFlags.NonPublic
 58                | BindingFlags.Public | BindingFlags.Instance);
 59        }

 60        public void SetIgnoreFields(string[] ignoreList)
 61        {
 62            int deleteNum = 0;
 63            for (int i = 0; i < _fi.Length; i ++{
 64                for (int j = 0; j < ignoreList.Length; j ++{
 65                    if (_fi[i].Name == ignoreList[j]) {
 66                        _fi[i] = null;
 67                        deleteNum ++;
 68                        break;
 69                    }

 70                }

 71            }

 72            FieldInfo[] newFi = new FieldInfo[_fi.Length - deleteNum];
 73            int newNum = 0;
 74            for (int k = 0; k < _fi.Length; k ++{
 75                if (_fi[k] != null{
 76                    newFi[newNum++= _fi[k];
 77                }

 78            }

 79            _fi = newFi;
 80        }

 81        private ArrayList _diffList = new ArrayList();
 82        public void UpDateVersion(string comment)
 83        {
 84            DiffInfo newDiff = new DiffInfo(++_currVersionNum,comment);
 85            for (int i = 0; i < _fi.Length; i ++{
 86                if (_fi[i].GetValue(_originalVersion).Equals(_fi[i].GetValue(_currVersion)) == false{
 87                    newDiff.DiffTable.Add(_fi[i],_fi[i].GetValue(_currVersion));
 88                }

 89            }

 90            _diffList.Add(newDiff);
 91        }

 92        public object GetObjectByVersionNum(int versionNum)
 93        {
 94            if (versionNum == 1{
 95                return _originalVersion;
 96            }

 97            for (int i = 0; i < _diffList.Count; i ++{
 98                DiffInfo tempDiff = (DiffInfo)_diffList[i];
 99                if (tempDiff.VersionNum == versionNum) {
100                    IDictionaryEnumerator en = tempDiff.DiffTable.GetEnumerator();
101                    _currVersion = _originalVersion;
102                    while (en.MoveNext()) {
103                        FieldInfo f = (FieldInfo)en.Key;
104                        object o = en.Value;
105                        f.SetValue(_currVersion,o);
106                    }

107                    return _currVersion;
108                }

109            }

110            return null;
111        }

112        public Hashtable GetVersionInfo()
113        {            
114            Hashtable info = new Hashtable();
115            info.Add(1,"vendor");
116            IEnumerator en = _diffList.GetEnumerator();
117            while (en.MoveNext()) {
118                int versionNum = ((DiffInfo)en.Current).VersionNum;
119                string comment = ((DiffInfo)en.Current).Comment;
120                info.Add(versionNum,comment);
121            }

122            return info;
123        }

124        public int GetCurrVersion()
125        {
126            return _currVersionNum;
127        }

128    }

129    class HeadVersion:CommonVersion    
130    {
131        public HeadVersion(object o):base(o){}
132    }

133    class BranchVersion:CommonVersion
134    {
135        private string _name;
136        public string Name
137        {
138            get
139            {
140                return _name;
141            }

142        }

143        private int _fromVersionNum;
144        public int FromVersionNum
145        {
146            get
147            {
148                return _fromVersionNum;
149            }

150        }

151        public BranchVersion(string name,int fromVersionNum, object o):base(o)
152        {
153            _name = name;
154            _fromVersionNum = fromVersionNum;
155        }

156    }

157    public class VersionController
158    {
159        private HeadVersion _headVersion;
160        private ArrayList _branchList = new ArrayList();
161        private Hashtable _tagList = new Hashtable();
162        private string[] _ignoreList;
163
164        public VersionController(object head)
165        {
166            _headVersion = new HeadVersion(head);            
167        }

168        public void CreateBranch(string name)
169        {
170            BranchVersion newBranch = new BranchVersion(name,_headVersion.GetCurrVersion(),_headVersion.CurrVersion);
171            if (_ignoreList != null{
172                newBranch.SetIgnoreFields(_ignoreList);
173            }

174            _branchList.Add(newBranch);
175        }

176        public void DeleteBranch(string name)
177        {
178            IEnumerator en = _branchList.GetEnumerator();
179            while (en.MoveNext()) {
180                BranchVersion tempBranch = (BranchVersion)en.Current;
181                if (tempBranch.Name == name) {
182                    _branchList.Remove(tempBranch);
183                }

184            }

185        }

186        public object GetObjectFromHead()
187        {
188            return _headVersion.CurrVersion;
189        }

190        public object GetObjectFromBranch(string name)
191        {
192            IEnumerator en = _branchList.GetEnumerator();
193            while (en.MoveNext()) 
194            {
195                BranchVersion tempBranch = (BranchVersion)en.Current;
196                if (tempBranch.Name == name) 
197                {
198                    return tempBranch.CurrVersion;
199                }

200            }

201            return null;
202        }

203        public object GetObjectFromHeadIndicateVersionNum(int versionNum)
204        {
205            return _headVersion.GetObjectByVersionNum(versionNum);
206        }

207        public object GetObjectFromBranchIndicateVersionNum(string name, int versionNum)
208        {
209            IEnumerator en = _branchList.GetEnumerator();
210            while (en.MoveNext()) 
211            {
212                BranchVersion tempBranch = (BranchVersion)en.Current;
213                if (tempBranch.Name == name) 
214                {
215                    return tempBranch.GetObjectByVersionNum(versionNum);
216                }

217            }

218            return null;
219        }

220        public void StepHeadVersion(string comment)
221        {
222            _headVersion.UpDateVersion(comment);
223        }

224        public void StepBranchVersion(string name, string comment)
225        {
226            IEnumerator en = _branchList.GetEnumerator();
227            while (en.MoveNext()) 
228            {
229                BranchVersion tempBranch = (BranchVersion)en.Current;
230                if (tempBranch.Name == name) 
231                {
232                    tempBranch.UpDateVersion(comment);
233                    return;
234                }

235            }

236        }

237        public void SetHeadVal(object o)
238        {
239            _headVersion.CurrVersion = o;
240        }

241        public void SetBranchVal(string name, object o)
242        {
243            IEnumerator en = _branchList.GetEnumerator();
244            while (en.MoveNext()) 
245            {
246                BranchVersion tempBranch = (BranchVersion)en.Current;
247                if (tempBranch.Name == name)
248                {
249                    tempBranch.CurrVersion = o;
250                    return;
251                }

252            }

253        }

254        public void AddTagFromHead(string tagName)
255        {
256            _tagList.Add(tagName,_headVersion.CurrVersion);
257        }

258        public void AddTagFromBranch(string tagName,string branchName)
259        {
260            IEnumerator en = _branchList.GetEnumerator();
261            while (en.MoveNext()) 
262            {
263                BranchVersion tempBranch = (BranchVersion)en.Current;
264                if (tempBranch.Name == branchName) 
265                {
266                    _tagList.Add(tagName,tempBranch.CurrVersion);
267                    return;
268                }

269            }

270        }

271        public object GetTag(string tagName)
272        {
273            IDictionaryEnumerator en = _tagList.GetEnumerator();
274            while (en.MoveNext()) {
275                string name = (string)en.Key;
276                if (name == tagName) {
277                    return en.Value;
278                }

279            }

280            return null;
281        }

282        public void DeleteTag(string tagName)
283        {
284            IDictionaryEnumerator en = _tagList.GetEnumerator();
285            while (en.MoveNext()) {
286                string name = (string)en.Key;
287                if (name == tagName) {
288                    _tagList.Remove(en.Value);
289                }

290            }

291        }

292        public Hashtable GetHeadVersionInfo()
293        {
294            return _headVersion.GetVersionInfo();
295        }

296        public Hashtable GetBranchVersionInfo(string name,out int fromVersionNum)
297        {
298            IEnumerator en = _branchList.GetEnumerator();
299            while (en.MoveNext()) 
300            {
301                BranchVersion tempBranch = (BranchVersion)en.Current;
302                if (tempBranch.Name == name) 
303                {
304                    fromVersionNum = tempBranch.FromVersionNum;
305                    return tempBranch.GetVersionInfo();
306                }

307            }

308            fromVersionNum = -1;
309            return null;
310        }

311        public ArrayList GetTagInfo()
312        {
313            ArrayList info = new ArrayList();
314            IDictionaryEnumerator en = _tagList.GetEnumerator();
315            while (en.MoveNext()) {
316                string name = (string)en.Key;
317                info.Add(name);
318            }

319            return info;
320        }

321        public ArrayList GetTotalBranchNames()
322        {
323            ArrayList list = new ArrayList();
324            IEnumerator en = _branchList.GetEnumerator();
325            while (en.MoveNext()) {
326                BranchVersion branch = (BranchVersion)en.Current;
327                list.Add(branch.Name);
328            }

329            return list;
330        }

331        public void SetIngoreFields(string[] ignoreList)
332        {
333            _ignoreList = ignoreList;
334            _headVersion.SetIgnoreFields(ignoreList);
335            for (int i = 0; i < _branchList.Count; i ++{
336                ((BranchVersion)_branchList[i]).SetIgnoreFields(ignoreList);
337            }

338        }

339    }
测试代码如下:
  1    public class ToBeReflection
  2    {
  3        protected int i = 1;
  4        public string j = "test";    
  5        public double k = 3.4;
  6        public float u = 8.0F;
  7        public void SetI(int tempI)
  8        {
  9            i = tempI;
 10        }

 11        public int GetI()
 12        {
 13            return i;
 14        }

 15    }

 16    public class TestVersionController
 17    {
 18        public void Test()
 19        {
 20            ToBeReflection temp1 = new ToBeReflection();
 21            ToBeReflection temp2 = new ToBeReflection();
 22            VersionController vc = new VersionController(temp1);
 23            string[] ing = new string[1];
 24            ing[0= "i";
 25            vc.SetIngoreFields(ing);
 26            print("original : " + temp1.GetI().ToString());
 27            temp1 = (ToBeReflection)vc.GetObjectFromHead();
 28            print("first version: " + temp1.GetI().ToString());
 29            temp2.SetI(2);
 30            vc.SetHeadVal(temp2);
 31            temp1 = (ToBeReflection)vc.GetObjectFromHead();
 32            print("first version reset: " + temp1.GetI().ToString());
 33            vc.StepHeadVersion("step head to second version");
 34            temp1 = (ToBeReflection)vc.GetObjectFromHead();
 35            print("second version: " + temp1.GetI().ToString());
 36            temp1 = (ToBeReflection)vc.GetObjectFromHeadIndicateVersionNum(1);
 37            print("get first version from head:(second version) " + temp1.GetI().ToString());
 38            vc.StepHeadVersion("step second to third version(dump)");
 39            temp1 = (ToBeReflection)vc.GetObjectFromHead();
 40            print("get third version: " + temp1.GetI().ToString());
 41            printInfo(vc.GetHeadVersionInfo(),"Head");
 42
 43            vc.AddTagFromHead("BEFORE_BRANCH");
 44            temp1 = (ToBeReflection)vc.GetTag("BEFORE_BRANCH");
 45            print("get tag--before_branch: " + temp1.GetI().ToString());
 46            printTagInfo(vc.GetTagInfo());
 47
 48            vc.CreateBranch("branch1");
 49            temp1 = (ToBeReflection)vc.GetObjectFromBranch("branch1");
 50            print(temp1.GetI().ToString());
 51            temp2.SetI(3);
 52            vc.SetBranchVal("branch1",temp2);
 53            temp1 = (ToBeReflection)vc.GetObjectFromBranch("branch1");
 54            print("reset first branch1 " + temp1.GetI().ToString());
 55            vc.StepBranchVersion("branch1","step first branch1 to second");
 56            temp1 = (ToBeReflection)vc.GetObjectFromBranch("branch1");
 57            print("step first branch1 to second " + temp1.GetI().ToString());
 58
 59            temp2.SetI(6);
 60            vc.SetHeadVal(temp2);
 61            vc.StepHeadVersion("step third head to forth");
 62            temp1 = (ToBeReflection)vc.GetObjectFromHead();
 63            print("get forth head version: " + temp1.GetI().ToString());
 64
 65            vc.CreateBranch("branch2");
 66            vc.AddTagFromBranch("AFTER_BRANCH2","branch2");
 67
 68            temp1 = (ToBeReflection)vc.GetObjectFromHeadIndicateVersionNum(3);
 69            print("ignore i: " + temp1.GetI().ToString());
 70            printGraphInfo(vc);
 71        }

 72        private void print(string s)
 73        {
 74            Console.WriteLine(s);
 75        }

 76        private void printInfo(Hashtable t,string name)
 77        {
 78            Console.WriteLine("============= " + name + "info =============");
 79            IDictionaryEnumerator en = t.GetEnumerator();
 80            while (en.MoveNext()) {
 81                int versionNum = (int)en.Key;
 82                string comment = (string)en.Value;
 83                Console.WriteLine(versionNum.ToString() + " " + comment);
 84                Console.WriteLine("          ^");
 85                Console.WriteLine("          |");
 86                Console.WriteLine("          |");
 87            }

 88            Console.WriteLine("============= " + name + "info =============");
 89        }

 90        private void printTagInfo(ArrayList list)
 91        {
 92            IEnumerator en = list.GetEnumerator();
 93            while (en.MoveNext()) 
 94            {
 95                string tagName = (string)en.Current;
 96                Console.WriteLine(tagName);
 97            }

 98        }

 99        private void printGraphInfo(VersionController vc)
100        {
101            Console.WriteLine("@@@@@@@@@@@@@ print total version info: @@@@@@@@@@@@@@@@ \n");
102            Hashtable head = vc.GetHeadVersionInfo();
103            Console.WriteLine("============= " + "head info =============");
104            IDictionaryEnumerator en = head.GetEnumerator();
105            while (en.MoveNext()) 
106            {
107                int tempVersionNum = (int)en.Key;
108                string tempComment = (string)en.Value;
109                Console.WriteLine(tempVersionNum + ":" + tempComment);
110                Console.WriteLine("          ^");
111                Console.WriteLine("          |");
112                Console.WriteLine("          |");
113            }

114            Console.WriteLine("============= " + "head info =============\n");
115            Console.WriteLine("============= " + "branches info =============\n");
116            ArrayList branchNameList = vc.GetTotalBranchNames();
117            IEnumerator en2 = branchNameList.GetEnumerator();
118            while (en2.MoveNext()) {
119                string branchName = (string)en2.Current;
120                int fromVersionNum;
121                printInfo(vc.GetBranchVersionInfo(branchName,out fromVersionNum),branchName);
122                Console.WriteLine("---->from Head Version: " + fromVersionNum.ToString() + "\n");
123            }

124            Console.WriteLine("============= " + "branches info =============\n");
125            Console.WriteLine("============= " + "tags info =============\n");                
126            printTagInfo(vc.GetTagInfo());
127            Console.WriteLine("\n============= " + "tags info =============\n");    
128            Console.WriteLine("@@@@@@@@@@@@@ print total version info: @@@@@@@@@@@@@@@@");
129        }

130    }

posted on 2006-01-21 20:43 Roger的Blog 阅读(390) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航:
 

导航

统计

常用链接

留言簿(1)

随笔档案(13)

搜索

最新评论

阅读排行榜

评论排行榜