我想使用MVC模式winforms将记录添加到数据库中。将记录添加到数据库中的功能正在运行,但我想通过使用对象方法来实现。
有人请问我在这里使用MVC模式的错误是什么?
查看:
using System;
using System.Data.SqlClient;
using System.Globalization;
using System.Windows.Forms;
using MVCwithDBFormReg.Controller;
using MVCwithDBFormReg.Model;
namespace MVCwithDBFormReg.View
{
public partial class Form1 : Form, IUserView
{
private readonly User _model;
public Form1(User model)
{
_model = model;
InitializeComponent();
}
public event CreateUserHandler Create;
private void button3_Click(object sender, System.EventArgs e)
{
_model.FirstName = FirstName;
_model.LastName = LastName;
_model.Language = Language;
_model.Country = Country;
_model.State = State;
_model.Zipcode = Zipcode;
_model.TimeZone = TimeZone;
_model.Sex = Sex;
_model.Month = Month;
_model.Day = Day;
_model.Year = Year;
_model.Occupation = Occupation;
Create(_model);
}
}
}
型号:
namespace MVCwithDBFormReg.Model
{
public class User
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Language{ get; set; }
public string Country{ get; set; }
public string State{ get; set; }
public int Zipcode { get; set; }
public string TimeZone{ get; set; }
public Gender Sex { get; set; }
public enum Gender
{
Male = 1,
Female = 2
}
public string Month { get; set; }
public string Day { get; set; }
public int Year { get; set; }
public string Occupation { get; set; }
}
控制器:
namespace MVCwithDBFormReg.Controller
{
public class UserController
{
private SqlConnection sqlConnection1;
private void ViewOnSave(User model)
{
if (model.FirstName == "" || model.LastName == "")
MessageBox.Show("Please enter your name");
else
{
sqlConnection1.Open();
string insert = "INSERT INTO REGISTER(FNAME, LNAME, LANG, COUNTRY, STATE, ZIPCODE, TIMEZONE, GENDER, BDAY, BMONTH, BYEAR, OCCUPATION) " +
"VALUES ('" + model.FirstName + "','" + model.LastName + "','"
+ model.Language + "','" + model.Country + "','"
+ model.State + "','" + model.Zipcode
+ "','" + model.TimeZone + "','"
+ model.Sex + "','" + model.Day + "','"
+ model.Month + "','" + model.Year + "','"
+ model.Occupation + "')";
SqlCommand cmd = new SqlCommand(insert, this.sqlConnection1);
cmd.ExecuteNonQuery();
sqlConnection1.Close();
MessageBox.Show("You have been successfully registered to our database.");
}
}
}
}
答案 0 :(得分:4)
事情在技术上并不完全是MVC(我怀疑在WinForms项目中对MVC有多大意义,MVVM模式已经证明自己更有用。虽然我可能错了,因为有特殊情况下MVC有意义。)< / p>
首先,您的模型类不是UserRegister,而是User。其次,你不一定需要UserRegister类,因为你可以保存在控制器中(这不是一个很好的练习,但很简单,足以用于训练目的)。或者如果你真的想要将数据库操作与控制器代码分开,你可以像Repository模式一样方便(就像一个Repository类中的UserRegister,UserLoad,UserFind类)。我建议阅读一些关于建筑和设计的Martin Fowler书籍(或者只是网络教程)。
现在我们已经了解了您的模型(纯数据) - 用户。您的视图不应该有任何逻辑(例如将数据保存到数据库或计算泰迪熊的折扣) - 仅查看。视图绑定到模型,这意味着它显示了模型具有的内容,并通过控件设置模型的属性。就像 - 你有一个用户名的文本框,它显示来自Model.Name的数据,并在它被改变时设置Model.Name。
每当点击Save
按钮时,您的视图会向您的控制器发送已更改模型,以便它可以对其进行操作。控制器然后将事物拼凑在一起 - 比如要求UserRegister保存从View获得的用户。顺便说一句,虽然你可以使用它们,但不需要在这里使用它们。
我会尝试以支持上述所有内容的方式显示您的代码:
查看:
private readonly User _model;
public Form1(User model)
{
_model = model;
InitializeComponent();
}
public event CreateUserHandler Create; // should be changed to use User
//Function to create a new record into the DB
private void btnSave_click(object sender, System.EventArgs e)
{
// if model is not bound via binding, it needs update before Create.
Create(_model); // needs changes to current EventHandler
}
模型 - 没有变化; UserRegister - 死了。
某处(如Delegates.cs单独文件中):
public delegate void CreateUserHandler(User model);
控制器:
public UserController(IUserView view)
{
_view = view;
_view.Create += ViewOnSave;
}
private void ViewOnSave(User model)
{
SqlConnection connection = new SqlConnection(connString);
connection.Open();
//... save query goes here
}
注意,这里不使用存储库。无论如何,这个MVC仍然是不完整和错误的,我建议阅读一些关于ASP.NET MVC的教程来掌握.NET环境中的MVC模式。