gpt4 book ai didi

c# - 以 MVVM 模式更新记录以获得最大效率的正确方法

转载 作者:太空狗 更新时间:2023-10-29 23:33:31 25 4
gpt4 key购买 nike

这更像是一个概念性问题。这是我目前的困境;我正在编写一个 vb.net WPF 应用程序并使用 MVVM 模式(喜欢它!可维护性非常棒)。目前所有代码都是手工编写的,没有使用 NHibernate 或 Entity Framework,因为后端是一个访问数据库(由于政策我不能使用 NH 并且 EF 不支持 JET 数据库,我们可能会在某个时候切换到 MSSQL但这可能需要一段时间)。

应用程序运行良好,想知道将更新发送回数据库的最佳方式是什么。

目前的方法是将 bool 值添加到模型设置部分的记录为“脏”,然后当按下更新时,我们遍历所有“脏”的记录并使用 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

最佳答案

由于您使用更新/保存按钮将更改提交到数据库,我建议使用类似于存储库的模式,其中存储库会在执行保存操作时跟踪更改。

这类似于 Entity Framework 实现 self 跟踪实体 (STE) 的方式。在 EF STE 中,为您要跟踪的每个实体创建一个跟踪器对象,该对象监听类似于 PropertyChanged 的事件以确定对象是否“脏”。

这种方法的主要好处是您可以执行批量更新/删除,而无需存储模型或 View 模型的任何持久性状态,也不必始终将您拥有的所有内容保存到数据库中。这提供了更大的关注点分离(DAL vs M vs VM vs V)。我发现 MVVM 和存储库模式配合得很好。

这是整体方法:

  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,尽管这些当然不是必需的,并且可能有更好的做事方式,或者您可能有更有效的方法来确定项目是否已更改。 (例如,创建对象副本的想法并不完全是内存友好的)。您还需要处理已删除的项目,但这很容易添加到上面的示例中。

关于c# - 以 MVVM 模式更新记录以获得最大效率的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9707109/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com