2012-03-14 48 views
5

這是一個更概念性的問題。這是我目前的困境;我正在編寫一個vb.net WPF應用程序並使用MVVM模式(愛它!可維護性只是非常棒)。目前所有代碼都是手工編寫的,並且沒有使用NHibernate或實體框架,因爲後端是一個訪問數據庫(由於策略我不能使用NH並且EF不支持JET數據庫,我們可能會在某些時候切換到MSSQL但從現在開始可能會有一段時間)。以最大效率更新MVVM模式記錄的正確方法

應用程序運行良好,想知道將更新發送回數據庫的最佳方式是什麼。

目前該方法是添加布爾到模型的設置部分的記錄「髒」,然後當更新被按下時,我們遍歷所有「髒」的記錄並使用oledbcommand(使用參數執行)要更新的sql語句。

這創建了一個很好的問題分離,但如果這是錯誤的方式,我想知道替代方法(請注意數據庫類型和相關的缺點,例如它不適用於EF)。

謝謝!

最終代碼在VB.NET評論等後:

Public Class Car 
Implements ICloneable 

Public Property Make() As String 
    Get 
     Return m_Make 
    End Get 
    Set(ByVal value As String) 
     m_Make = value 
    End Set 
End Property 
Private m_Make As String 

Public Property Model() As String 
    Get 
     Return m_Model 
    End Get 
    Set(ByVal value As String) 
     m_Model = value 
    End Set 
End Property 
Private m_Model As String 

Public Function Clone() As Object Implements System.ICloneable.Clone 
    Return New Car() With { _ 
    .Make = Me.Make, _ 
    .Model = Me.Model _ 
    } 
End Function 
End Class 



Public Class CarEqualityComparer 
Implements IEqualityComparer(Of Car) 

Public Overloads Function Equals(ByVal x As Car, ByVal y As Car) As Boolean Implements System.Collections.Generic.IEqualityComparer(Of Car).Equals 
    Return x.Make = y.Make AndAlso x.Model = y.Model 
End Function 

Public Overloads Function GetHashCode(ByVal obj As Car) As Integer Implements System.Collections.Generic.IEqualityComparer(Of Car).GetHashCode 
    Return 1 'http://blogs.msdn.com/b/jaredpar/archive/2008/06/03/making-equality-easier.aspx 
End Function 

End Class 

Public Class CarRepository 
    Private _carComparator As New CarEqualityComparer 

    Private _cars As New ChangeTracker(Of Car)(_carComparator) 

    Public Function GetCars() As IEnumerable(Of Car) 
     'TODO: JET/ADO code here, you would obviously do in a for/while loop 
     Dim dbId1 As Integer = 1 
     Dim make1 As String = "Ford" 
     Dim model1 As String = "Focus" 

     Dim dbId2 As Integer = 2 
     Dim make2 As String = "Hyundai" 
     Dim model2 As String = "Elantra" 

     'TODO: create or update car objects 
     Dim car1 As Car 
     If Not _cars.IsTracking(dbId1) Then 
      car1 = New Car() 
     Else 
      car1 = _cars.GetItem(dbId1) 
     End If 

     car1.Make = make1 
     car1.Model = model1 

     If Not _cars.IsTracking(dbId1) Then 
      _cars.StartTracking(dbId1, car1) 
     End If 


     Dim car2 As Car 
     If Not _cars.IsTracking(dbId2) Then 
      car2 = New Car() 
     Else 
      car2 = _cars.GetItem(dbId2) 
     End If 

     car2.Make = make2 
     car2.Model = model2 

     If Not _cars.IsTracking(dbId2) Then 
      _cars.StartTracking(dbId2, car2) 
     End If 

     Return _cars.GetTrackedItems() 
    End Function 

    Public Sub SaveCars(ByVal cars As IEnumerable(Of Car)) 

     'TODO: JET/ADO code here to update the item 
     Console.WriteLine("Distinct " & cars.Distinct.Count.ToString) 

     For Each changedItem As Car In _cars.GetChangedItems().Intersect(cars) 
      Console.Write("Saving: ") 
      Console.WriteLine(changedItem.Make) 
     Next 

     For Each newItem As Car In cars.Except(_cars.GetTrackedItems()) 
      Console.Write("Adding: ") 
      Console.WriteLine(newItem.Make) 
      Dim newId As Integer = CInt(Math.Ceiling(Rnd() * 5000)) 'Random right now but JET/ADO to get the id later.... 
      _cars.StartTracking(newId, newItem) 
     Next 

     Dim removalArray As New ArrayList 
     For Each deletedItem As Car In _cars.GetTrackedItems().Except(cars) 
      Console.Write("Removing: ") 
      Console.WriteLine(deletedItem.Make) 
      removalArray.Add(_cars.GetId(deletedItem)) 'Cannot remove right as iterating through array - clearly that would be problematic.... 
     Next 
     For Each dbId As Integer In removalArray 
      _cars.StopTracking(dbId) 
     Next 

     _cars.SetNewCheckpoint() 

    End Sub 
End Class 

Public Class ChangeTracker(Of T As {ICloneable}) 
    'item "checkpoints" that are internal to this list 
    Private _originals As New Dictionary(Of Integer, T)() 
    Private _originalIndex As New Dictionary(Of T, Integer)() 

    'the current, live-edited objects 
    Private _copies As New Dictionary(Of Integer, T)() 
    Private _copyIndex As New Dictionary(Of T, Integer)() 

    Private _comparator As System.Collections.Generic.IEqualityComparer(Of T) 

    Public Sub New(ByVal comparator As System.Collections.Generic.IEqualityComparer(Of T)) 
     _comparator = comparator 
    End Sub 

    Public Function IsChanged(ByVal copy As T) As Boolean 
     Dim original = _originals(_copyIndex(copy)) 

     Return Not _comparator.Equals(copy, original) 

    End Function 

    Public Function GetChangedItems() As IEnumerable(Of T) 
     Dim items As IEnumerable(Of T) 
     items = _copies.Values.Where(Function(c) IsChanged(c)) 
     Return items 
    End Function 

    Public Function GetTrackedItems() As IEnumerable(Of T) 
     Return _copies.Values 
    End Function 

    Public Sub SetNewCheckpoint() 
     For Each copy In Me.GetChangedItems().ToList() 
      Dim dbId As Integer = _copyIndex(copy) 
      Dim oldOriginal = _originals(dbId) 
      Dim newOriginal = DirectCast(copy.Clone(), T) 

      _originals(dbId) = newOriginal 
      _originalIndex.Remove(oldOriginal) 
      _originalIndex.Add(newOriginal, dbId) 
     Next 
    End Sub 

    Public Sub StartTracking(ByVal dbId As Integer, ByVal item As T) 
     Dim newOriginal = DirectCast(item.Clone(), T) 
     _originals(dbId) = newOriginal 
     _originalIndex(newOriginal) = dbId 

     _copies(dbId) = item 
     _copyIndex(item) = dbId 
    End Sub 

    Public Sub StopTracking(ByVal dbId As Integer) 
     Dim original = _originals(dbId) 
     Dim copy = _copies(dbId) 

     _copies.Remove(dbId) 
     _originals.Remove(dbId) 
     _copyIndex.Remove(copy) 
     _originalIndex.Remove(original) 
    End Sub 

    Public Function IsTracking(ByVal dbId As Integer) As Boolean 
     Return _originals.ContainsKey(dbId) 
    End Function 

    Public Function IsTracking(ByVal item As T) As Boolean 
     Return _copyIndex.ContainsKey(item) 
    End Function 

    Public Function GetItem(ByVal dbId As Integer) As T 
     Return _copies(dbId) 
    End Function 

    Public Function GetId(ByVal item As T) As Integer 
     Dim dbId As Integer = (_copyIndex(item)) 
     Return dbId 
    End Function 

End Class 

回答

3

由於您使用的是更新/保存按鈕,提交您對數據庫的更改,我會建議使用一個倉庫狀的圖案,其中,只要執行保存操作,資源庫就會跟蹤更改。

這與實體框架如何實現自我跟蹤實體(STE)類似。在EF STE中,爲每個要跟蹤類似PropertyChanged的事件的實體創建一個跟蹤器對象,以確定對象是否「髒」。

這種方法的主要優點是您可以執行批量更新/刪除操作,而無需使用您的模型或視圖模型存儲任何持久狀態,或者必須始終保存您擁有的所有數據。這提供了更大的關注點(DAL vs M vs VM vs V)。我發現MVVM和Repository Pattern在一起很好。

這裏的總體思路:從數據庫

  1. 您加載的項目從一個存儲庫中。在加載項目時,將它們存儲在一個「跟蹤器」對象中,該對象保留最初存儲在數據庫中的對象的副本,以及與「實時」(可編輯)對象的關係。我們將此過程稱爲「創建檢查點」。
  2. 像往常一樣使用MVVM中的可編輯對象,允許用戶進行他們想要的任何更改。您無需跟蹤任何更改。
  3. 當用戶單擊'保存'按鈕時,您將屏幕上的所有對象都發送回存儲庫以進行保存。
  4. 存儲庫檢查每個對象與原始副本,並確定哪些項目是「髒」。
  5. 只有髒項目被保存到數據庫。
  6. 保存成功後,您將創建一個新的檢查點。

下面是一些示例代碼,我掀起了:

首先,這裏被稱爲Car樣品類,我們將在我們的倉庫使用。注意對象上沒有Dirty屬性。

public class Car : IEquatable<Car>, ICloneable 
{ 
    public string Make { get; set; } 
    public string Model { get; set; } 

    public bool Equals(Car other) 
    { 
     return other.Make == this.Make && 
       other.Model == this.Model; 
    } 

    public object Clone() 
    { 
     return new Car { Make = this.Make, Model = this.Model }; 
    } 
} 

接下來,這裏是一個CarRepository,你會用來初始化從數據庫對象:

public class CarRepository 
{ 
    private ChangeTracker<Car> _cars = new ChangeTracker<Car>(); 

    public IEnumerable<Car> GetCars() 
    { 
     //TODO: JET/ADO code here, you would obviously do in a for/while loop 
     int dbId1 = 1; 
     string make1 = "Ford"; 
     string model1 = "Focus"; 

     //TODO: create or update car objects 
     Car car1; 
     if (!_cars.IsTracking(dbId1)) 
      car1 = new Car(); 
     else 
      car1 = _cars.GetItem(dbId1); 

     car1.Make = make1; 
     car1.Model = model1; 

     if (!_cars.IsTracking(dbId1)) 
      _cars.StartTracking(dbId1, car1); 

     return _cars.GetTrackedItems(); 
    } 

    public void SaveCars(IEnumerable<Car> cars) 
    { 
     foreach (var changedItem in _cars.GetChangedItems().Intersect(cars)) 
     { 
      //TODO: JET/ADO code here to update the item 
     } 

     foreach (var newItem in cars.Except(_cars.GetTrackedItems())) 
     { 
      //TODO: JET/ADO code here to add the item to the DB and get its new ID 
      int newId = 5; 
      _cars.StartTracking(newId, newItem); 
     }    

     _cars.SetNewCheckpoint(); 
    } 
} 

最後,還有就是倉庫用來跟蹤變化,並設置檢查點稱爲ChangeTracker一個輔助類。

public class ChangeTracker<T> where T : IEquatable<T>, ICloneable 
{ 
    //item "checkpoints" that are internal to this list 
    private Dictionary<int, T> _originals = new Dictionary<int, T>(); 
    private Dictionary<T, int> _originalIndex = new Dictionary<T, int>(); 

    //the current, live-edited objects 
    private Dictionary<int, T> _copies = new Dictionary<int, T>(); 
    private Dictionary<T, int> _copyIndex = new Dictionary<T, int>(); 

    public bool IsChanged(T copy) 
    { 
     var original = _originals[_copyIndex[copy]]; 
     return original.Equals(copy); 
    } 

    public IEnumerable<T> GetChangedItems() 
    { 
     return _copies.Values.Where(c => IsChanged(c)); 
    } 

    public IEnumerable<T> GetTrackedItems() 
    { 
     return _copies.Values; 
    } 

    public void SetNewCheckpoint() 
    { 
     foreach (var copy in this.GetChangedItems().ToList()) 
     { 
      int dbId = _copyIndex[copy]; 
      var oldOriginal = _originals[dbId]; 
      var newOriginal = (T)copy.Clone(); 

      _originals[dbId] = newOriginal; 
      _originalIndex.Remove(oldOriginal); 
      _originalIndex.Add(newOriginal, dbId); 
     } 
    } 

    public void StartTracking(int dbId, T item) 
    { 
     var newOriginal = (T)item.Clone(); 
     _originals[dbId] = newOriginal; 
     _originalIndex[newOriginal] = dbId; 

     _copies[dbId] = item; 
     _copyIndex[item] = dbId; 
    } 

    public void StopTracking(int dbId) 
    { 
     var original = _originals[dbId]; 
     var copy = _copies[dbId]; 

     _copies.Remove(dbId); 
     _originals.Remove(dbId); 
     _copyIndex.Remove(copy); 
     _originalIndex.Remove(original); 
    } 

    public bool IsTracking(int dbId) 
    { 
     return _originals.ContainsKey(dbId); 
    } 

    public bool IsTracking(T item) 
    { 
     return _copyIndex.ContainsKey(item); 
    } 

    public T GetItem(int dbId) 
    { 
     return _liveCopies[dbId]; 
    } 
} 

而且,這裏是你將如何使用您的信息庫中的程序:

static void Main(string[] args) 
{ 
    var repository = new CarRepository(); 

    var cars = repository.GetCars().ToArray(); 

    //make some arbitrary changes... 
    cars[0].Make = "Chevy"; 
    cars[1].Model = "Van"; 

    //when we call SaveCars, the repository will detect that 
    //both of these cars have changed, and write them to the database 
    repository.SaveCars(cars); 
} 

天真實施依賴於IEquatable和ICloneable,雖然這些肯定不是必要的,也有可能更好的方法做事情,或者你可能有更有效的方式來確定一個項目是否已經改變。 (例如,創建對象副本的想法不完全是內存友好的)。您還需要處理已刪除的項目,但這很容易添加到上面的示例中。

+0

嗨凱文,我只有一半瞭解這一點 - 你能解釋在代碼中實際發生了什麼嗎?有點像敘事? – 2012-03-14 19:50:37

+0

嗨,奧馬爾,我做了一些編輯,我希望這很有幫助。非常短的版本是我們仍然在進行髒跟蹤,除了我們只是在用戶單擊Save時計算哪些對象是髒的,而不是在用戶進行更改時管理髒標誌。 – 2012-03-14 20:09:39

+0

嘿凱文,怎麼樣ovveriding GetHashCode? – 2012-03-19 01:19:13

相關問題