C# 如何在模型/适配器层的长过程中将信息传递到视图层
我试图找到在MVC模型中视图层和模型/适配器层之间来回通信的正确方法。以下是我发行的一些psuedo代码,以及我希望它做什么。有关说明,请参阅MyAdapter.ImportStuff方法中的注释C# 如何在模型/适配器层的长过程中将信息传递到视图层,c#,C#,我试图找到在MVC模型中视图层和模型/适配器层之间来回通信的正确方法。以下是我发行的一些psuedo代码,以及我希望它做什么。有关说明,请参阅MyAdapter.ImportStuff方法中的注释 public class MyView : System.Windows.Forms.UserControl { private MyContoller Ctrl = new MyContoller(); public MyView() { } private
public class MyView : System.Windows.Forms.UserControl
{
private MyContoller Ctrl = new MyContoller();
public MyView()
{
}
private void OnButtonClick()
{
Ctrl.DoSqlStuffNow();
TellUserImportFinished();
}
public void TellUserThingsHaveBeenDeleted()
{
this.label1.Text = "Stuff deleted...";
}
public void TellUserHowManyRowsHaveBeenInserted(int rowCount)
{
this.label2.Text = $"Number of rows inserted: {rowCount}...";
}
public void TellUserComplexQueriesAreDone()
{
this.label3.Text = "Complex queries are done...";
}
public void TellUserImportFinished()
{
System.Windows.Forms.MessageBox.Show("Success!");
}
}
internal class MyContoller
{
MyAdapter Adpt = new MyAdapter();
internal void DoSqlStuffNow()
{
object someParameter = new object();
Adpt.ImportStuff(someParameter);
}
}
internal class MyAdapter
{
internal void ImportStuff(object someParameter)
{
SqlConnection sqlConnection = new SqlConnection("connection");
// All of this has to happen in one connection/transaction.
// It should not be broken into multiple methods
DeleteFromTable1();
DeleteFromTable2();
// Now I want to tell the user delete is complete!
MakeTempTable();
InsertIntoTempTable(someParameter);
int rowCount = ImportIntoTable1();
// Now I want to tell the user how many rows were just inserted!
DoComplexQuery();
DoAnotherComplexQuery();
// Now I want to tell the user complex queries are done!
ImportIntoTable2();
DropTempTable();
// Presumably, I can say "success" when this function returns, which is fine
}
}
我的特殊情况更糟,因为控制器/适配器实际上是静态类,所以处理事件并不有趣,但我甚至不确定在这个简单的情况下如何最好地处理它
任何建议都将不胜感激 我不会把事情复杂化。查看您的设计,我将使用适配器类中的事件和
MyView
中的绑定
根据要通知的内容创建4个事件。由于所有事情都发生在一个方法中,所以您还可以有1个事件和4个不同的状态。尽管如此,我不喜欢这种方法,因为它在事件处理程序中需要一堆if
当然,您可以编写自己的委托,但为了简单起见,我确实使用了EventHandler类
public event EventHandler DeleteCompleted;
//int: the number of rows inserted
public event EventHandler<int> RowsInserted;
public event EventHandler ComplexQuerycCompleted;
//bool: if the operation completed successfully.
public event EventHandler<bool> OperationCompleted;
稍后在视图中,您将MyController
中的属性绑定到用于反映更改的UI控件
阅读有关数据绑定的内容
我不清楚您何时说过“控制器/适配器实际上是静态类”,但我认为上面的内容确实可以帮助您
当然,另一个选择是使用,但这一个需要您以反应式的方式学习Rx.Net和编程
internal class MyAdapter
{
public event EventHandler DeleteCompleted;
public event EventHandler<int> RowsInserted;
public event EventHandler ComplexQuerycCompleted;
public event EventHandler<bool> OperationCompleted;
internal void ImportStuff(object someParameter)
{
SqlConnection sqlConnection = new SqlConnection("connection");
// All of this has to happen in one connection/transaction.
// It should not be broken into multiple methods
DeleteFromTable1();
DeleteFromTable2();
// Now I want to tell the user delete is complete!
this.DeleteCompleted?.Invoke(this, new EventArgs());
MakeTempTable();
InsertIntoTempTable(someParameter);
int rowCount = ImportIntoTable1();
// Now I want to tell the user how many rows were just inserted!
this.RowsInserted?.Invoke(this, rowCount);
DoComplexQuery();
DoAnotherComplexQuery();
// Now I want to tell the user complex queries are done!
this.ComplexQuerycCompleted.Invoke(this, new EventArgs());
ImportIntoTable2();
DropTempTable();
// Presumably, I can say "success" when this function returns, which is fine
this.OperationCompleted?.Invoke(this, true);
}
}
internal class MyContoller : INotifyPropertyChanged
{
MyAdapter Adpt = new MyAdapter();
public event PropertyChangedEventHandler PropertyChanged;
private int rowsInserted;
public int RowsInserted
{
get { return rowsInserted;}
set
{
if (rowsInserted == value)
return;
rowsInserted = value;
RaisedPropertyChanged(nameof(RowsInserted));
}
}
internal MyContoller()
{
//Remember to remote the event subscriptions calling:
// Adpt.RowsInserted -= OnRowsInserted
Adpt.RowsInserted += OnRowsInserted;
}
internal void DoSqlStuffNow()
{
object someParameter = new object();
Adpt.ImportStuff(someParameter);
}
/// <summary>
/// Raiseds the property changed.
/// </summary>
/// <param name="propertyName">Property name.</param>
protected void RaisedPropertyChanged([CallerMemberNameAttribute] string propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
/// <summary>
/// Called when the Rows has been inserted in the <see cref="MyAdapter"/>
/// </summary>
/// <param name="sender">Sender</param>
/// <param name="numberOfRows">The Number of rows in inserted</param>
void OnRowsInserted(object sender, int numberOfRows)
{
//Notify the UI about objects inserted
RowsInserted = numberOfRows;
}
}