SubSonic中的字段付值--MakeOld Update
? 根據(jù)設(shè)計(jì)當(dāng)MakeOld后(在讀取數(shù)據(jù)庫后,或者手動(dòng)調(diào)用),對記錄(SubSonic生成的類)屬性附值時(shí),Sonic會(huì)檢測這個(gè)Value是否與原來的不同,只有值不同時(shí)才會(huì)附值成功,并將該列添加到DirtyColumns,而DirtyColumns中的列才會(huì)被Update采用,一般情況下 只要所有列中有一個(gè)列的是Dirty==true(被更改過),那么在Save時(shí)就會(huì)采用Update,
注意:SubSonic中判斷是否采用Update判斷“全部字段集合”中是否存在一個(gè)字段被更改,而生成Update命令時(shí)使用的集合是DirtyColumns集合,而不是直接從全部字段集合中查找那些被更改的字段,在多數(shù)情況下不會(huì)出問題,但有些時(shí)候可能帶來意想不到的問題,參考下面的場境3。
//=============參考代碼1================
??????????? public bool IsDirty
??????????? {
??????????????? get
??????????????? {
??????????????????? foreach(TableColumnSetting setting in this)
??????????????????? {
??????????????????????? if(setting.IsDirty)
??????????????????????????? return true;
??????????????????? }
??????????????????? return false;
??????????????? }
??????????????? //set
??????????????? //{
??????????????? //??? foreach (TableColumnSetting setting in this)
??????????????? //??????? setting.IsDirty = value;
??????????????? //}
??????????? }
//============參考代碼2============
???? public QueryCommand GetSaveCommand(string userName)
??????? {
??????????? if(IsNew)
??????????????? return GetInsertCommand(userName);
??????????? if(IsDirty)
??????????????? return GetUpdateCommand(userName);
??????????? return null;
??????? }
如果您對記錄未做任何更改而直接調(diào)用?.Save()時(shí)會(huì)根據(jù)IsNew與IsDirty的取值來決定采用Insert或Update。
如果兩個(gè)多是false,那么就返回null.那么Save操作將什么都不做。
//============參考代碼3============
?? QueryCommand cmd = GetSaveCommand(userName);
??????????????? if(cmd == null)
??????????????????? return;
場境1:
Employe emp=new Employe(1);//加載一個(gè)員工數(shù)據(jù)
//這個(gè)時(shí)候 IsNew=false,IsDirty=false
emp.Name=emp.Name; //沒有改變
emp.Save();//這里的Save方法將不做任何處理
?
場境2:
Employe emp=new Employe(1);
emp.Name=txtName.txt;//假設(shè)您在文本框中調(diào)整了Name取值
//這個(gè)時(shí)候 isNew=false;IsDirty=true;
emp.Save();將使用Update,并且只更新Name字段。
?
場境3:(報(bào)錯(cuò))
Employe emp=new Employe(1);
Employe backEmp=new Employe();
backEmp.CopyFrom(backEmp);
//copyFrom后會(huì)backEmp.IsDirty全部是true
//參考代碼7
backEmp.Name="xxxx";
backEmp.MackOld();//將IsNew設(shè)置成False
backEmp.Save(); //報(bào)錯(cuò),IsDirty=True,而DirtyColumns為空,
//生成 Update Employe Set Where EmpoyeId=1 這樣的錯(cuò)誤TSQL
?
場境4:(報(bào)錯(cuò))
Employe emp=new Employe(1);
Employe backEmp=new Employe();
backEmp.CopyFrom(backEmp);
backEmp.MackOld();
backEmp.Name=backEmp.Name;
backEmp.Save();//這時(shí)IsDirty是True,而DirtyColumns為空
?
正確作法,應(yīng)該在backEmp.MackOld();后再調(diào)用backEmp.MackClear();
//=============參考代碼8=================
????? /// <summary>
??????? /// Called after any property is set. Sets IsDirty to <c>true</c>.
??????? /// </summary>
??????? public void MarkClean()
??????? {
??????????? foreach(TableSchema.TableColumnSetting setting in columnSettings)
??????????????? setting.IsDirty = false;
??????????? DirtyColumns.Clear();
??????? }
//========參考代碼4==========
??????? /// <summary>
??????? /// Called after Update() invokation. Sets IsNew to <c>false</c>.
??????? /// </summary>
??????? public void MarkOld()
??????? {
??????????? IsLoaded = true;
??????????? _isNew = false;
??????? }
//========參考代碼5======================
??????? /// <summary>
??????? /// Copies the passed-in instance settings to this instance
??????? /// </summary>
??????? /// <param name="copyInstance">The copy instance.</param>
??????? public void CopyFrom(T copyInstance)
??????? {
??????????? if(copyInstance == null)
??????????????? throw new ArgumentNullException("copyInstance");
??????????? foreach(TableSchema.TableColumnSetting setting in copyInstance.columnSettings)
??????????????? SetColumnValue(setting.ColumnName, setting.CurrentValue);
??????? }
//========參考代碼6======================
??????? /// <summary>
??????? /// Sets a value for a particular column in the record
??????? /// </summary>
??????? /// <param name="columnName">Name of the column, as defined in the database</param>
??????? /// <param name="oValue">The value to set the type to</param>
??????? public void SetColumnValue(string columnName, object oValue)
??????? {
??????????? columnSettings = columnSettings ?? new TableSchema.TableColumnSettingCollection();
??????????? // add the column to the DirtyColumns
??????????? // if this instance has already been loaded
??????????? // and this is a change to existing values
??????????? if(IsLoaded && !IsNew)
??????????? {
??????????????? TableSchema.Table schema = GetSchema();
??????????????? object oldValue = null;
??????????????? string oldValueMsg = "NULL";
??????????????? string newValueMsg = "NULL";
??????????????? bool areEqualOrBothNull = false;
??????????????? try
??????????????? {
??????????????????? oldValue = columnSettings.GetValue(columnName);
??????????????? }
??????????????? catch {}
??????????????? if(oldValue == null && oValue == null)
??????????????????? areEqualOrBothNull = true;
??????????????? else
??????????????? {
??????????????????? if(oldValue != null)
??????????????????? {
??????????????????????? oldValueMsg = oldValue.ToString();
??????????????????????? areEqualOrBothNull = oldValue.Equals(oValue);
??????????????????? }
??????????????????? if(oValue != null)
??????????????????????? newValueMsg = oValue.ToString();
??????????????? }
??????????????? TableSchema.TableColumn dirtyCol = schema.GetColumn(columnName);
??????????????? if(dirtyCol != null && !areEqualOrBothNull)
??????????????? {
??????????????????? string auditMessage = String.Format("Value changed from {0} to {1}{2}", oldValueMsg, newValueMsg, Environment.NewLine);
??????????????????? TableSchema.TableColumn dirtyEntry = DirtyColumns.GetColumn(columnName);
??????????????????? if(dirtyEntry != null)
??????????????????? {
??????????????????????? DirtyColumns.Remove(dirtyEntry);
??????????????????????? auditMessage = String.Concat(dirtyCol.AuditMessage, auditMessage);
??????????????????? }
??????????????????? dirtyCol.AuditMessage = auditMessage;
??????????????????? DirtyColumns.Add(dirtyCol);
??????????????? }
??????????? }
??????????? columnSettings.SetValue(columnName, oValue);//這里最終調(diào)用參考代碼7
??????? }
//============參考代碼7============
??????????? /// <summary>
??????????? /// Gets or sets the current value.
??????????? /// </summary>
??????????? /// <value>The current value.</value>
??????????? public object CurrentValue
??????????? {
??????????????? get { return _currentValue; }
??????????????? set
??????????????? {
??????????????????? if(value == null && _currentValue == null)
??????????????????????? return;
??????????????????? if(value != null)
??????????????????? {
??????????????????????? if(value.Equals(_currentValue))
??????????????????????????? return;
??????????????????? }
??????????????????? _currentValue = value;
??????????????????? _isDirty = true;
??????????????? }
??????????? }
轉(zhuǎn)載于:https://www.cnblogs.com/wdfrog/archive/2010/06/30/1768159.html
總結(jié)
以上是生活随笔為你收集整理的SubSonic中的字段付值--MakeOld Update的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: VS code编译C或C++
- 下一篇: [转]详细介绍如何做关联