C# 在课堂上验证项目的良好实践。
事实上,我发现给这条线索一个标题很难。我目前正在学习C语言的一门点网编程课程。我们的任务是使用windows窗体和实体框架,使用存储库服务模式,构建一个简单的库 我有一个表单,其中我将向数据库添加一个图书实体,也就是向图书馆添加一本新书。我在这门课上做的事情是检查字段,以确保用户确实输入了一些文本,ISBN号的格式正确,这本书还不存在。。。。你明白了。我一直试图决定的是如何构建流程的执行方式。当我点击提交新书时,我最初在on_click方法中有一堆if语句来进行验证C# 在课堂上验证项目的良好实践。,c#,.net,coding-style,entity-framework-4.1,coding-efficiency,C#,.net,Coding Style,Entity Framework 4.1,Coding Efficiency,事实上,我发现给这条线索一个标题很难。我目前正在学习C语言的一门点网编程课程。我们的任务是使用windows窗体和实体框架,使用存储库服务模式,构建一个简单的库 我有一个表单,其中我将向数据库添加一个图书实体,也就是向图书馆添加一本新书。我在这门课上做的事情是检查字段,以确保用户确实输入了一些文本,ISBN号的格式正确,这本书还不存在。。。。你明白了。我一直试图决定的是如何构建流程的执行方式。当我点击提交新书时,我最初在on_click方法中有一堆if语句来进行验证 private void b
private void btn_bookSubmit_Click(object sender, EventArgs e)
{
string newBookName = this.tb_bookTitle.Text;
string newBookAuthor = this.tb_bookAuthor.Text;
string newBookISBN = this.tb_bookISBN.Text;
string description = this.tb_bookDesc.Text;
if (bookIsNotValid(newBookISBN, newBookName, newBookAuthor))
{
MessageBox.Show(this.validationError);
return;
}
if (bookService.BookTitleExists(newBookName))
{
MessageBox.Show("A book by this title already exists in the library");
return;
}
if (bookService.ISBNExists(newBookISBN))
{
MessageBox.Show("This ISBN belongs to another book in the library. Please double check the ISBN number and try again");
return;
}
if (this.authorService.doesAuthorExistByName(newBookAuthor))
{
DialogResult result = MessageBox.Show
("This author does not exist in the database. Do you want to add this author?",
"Author Does not Exist", MessageBoxButtons.YesNo);
if (result == DialogResult.Yes) this.authorService.addAuthor(newBookAuthor);
if (result == DialogResult.No)
{
MessageBox.Show("New book entry cancled. In order to enter a new book into the system a valid Author must be specified");
return ;
}
}
bookService.addBook(newBookISBN, newBookName, newBookAuthor, description);
MessageBox.Show(
string.Format("{0} succesfully added to the library", newBookName),
string.Format("{0} added Successfully", newBookName),
MessageBoxButtons.OK);
this.clearFields();
}
我心里想;对于一个方法来说,这是相当多的代码。因此,我在form类中将其拆分为更多的私有函数,并最终得到了一个如下所示的方法:
private void btn_bookSubmit_Click(object sender, EventArgs e)
{
string newBookName = this.tb_bookTitle.Text;
string newBookAuthor = this.tb_bookAuthor.Text;
string newBookISBN = this.tb_bookISBN.Text;
string description = this.tb_bookDesc.Text;
if (!isBookValid(newBookISBN, newBookName, newBookAuthor)) return;
if (!isTitleValid(newBookName)) return;
if (!isISBNvalid(newBookISBN)) return;
if (!isAuthorNew(newBookAuthor)) return;
bookService.addBook(newBookISBN, newBookName, newBookAuthor, description);
MessageBox.Show(
string.Format("{0} succesfully added to the library", newBookName),
string.Format("{0} added Successfully", newBookName),
MessageBoxButtons.OK);
this.clearFields();
}
现在我班上有很多方法。这是好的做法吗?对我来说,它看起来干净多了,但在浏览我的类时,可能比在一个函数中看到所有这些都发生更难筛选方法。我考虑的另一件事是将所有验证移到一个函数中,而不是移到多个函数中,但接下来我必须处理布尔返回,以及如何以不同的方式停止函数
我已经研究了我的程序2年了,已经尝试过,JavaScript,PHP,HTML5,C++,C,现在C试图找出我最喜欢的。我从所有编程中得到的最大好处是我喜欢漂亮高效的代码。我可能还不能做到这一点,但我正在尽我最大的努力去学习它。所以你可能注意到的任何其他糟糕的做法,请让我知道。到目前为止,课堂上一切都很顺利,我真正的问题是,我实施验证过程的方式是否可行?好的低劣的?慢 嗯,您的图书服务不接受图书对象吗 与其让表单验证什么是有效的书,不如让book类负责确定它是否有效
一些验证,如必填字段留空,可能是表单级验证的问题。。。但是很多这听起来像是book service的域和/或book类。我可以很容易地在book service中添加一个add方法,该方法将book作为参数,但我认为在service类中创建新的book实例更合适。我还认为任何消息框或反馈都应该包含在接口中,而不是服务类中。实际的验证逻辑本身发生在服务层,检查数据库是否存在重复项等等。但在我的界面中,我使用对服务层的访问来获得一个布尔值,作为是否存在重复的答案。是的,我知道你来自哪里,我最近也一直在努力解决这个问题。看起来您仍然可能会遇到这样的情况,尽管假设您在服务器上有一个DB后端,在某个地方,在您检查的时间和节省的时间之间可能会发生变化。我想你可以在那里处理一个异常并显示消息或其他东西。