【问题标题】:Good practice for validating items in class.在课堂上验证项目的良好做法。
【发布时间】:2013-11-14 22:02:11
【问题描述】:

实际上,我发现给这个线程起一个标题非常困难。我目前正在学习 C# 中的点网编程课程。我们的任务是使用 Windows 窗体和使用存储库服务模式的实体框架构建一个简单的库。

我有一个表单,我在其中将 Book 实体添加到数据库中,也就是将新书添加到图书馆。我在这门课上做的事情是检查字段以确保用户实际输入了一些文本,ISBN 号是正确的格式,这本书还不存在......你明白了。我一直试图决定的是如何构建流程的执行方式。当我点击提交新书时,我最初在 on_click 方法中有一堆 if 语句来进行验证。

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(); 
    }

我心想;对于一种方法,那是相当多的代码。所以我将它拆分为表单类中的更多私有函数,并最终得到一个看起来像这样的方法:

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#,试图找出我最喜欢哪个。我从所有编程中获得最多的是我喜欢漂亮而高效的代码。我可能还不能做到这一点,但我尽我最大的努力去学习它。因此,您可能会注意到任何其他糟糕的做法,请告诉我。到目前为止,课堂上一切正常,我真正的问题是,我实施验证过程的方式好吗?好的?烂?慢的?

【问题讨论】:

    标签: c# .net coding-style entity-framework-4.1 coding-efficiency


    【解决方案1】:

    嗯,你们的图书服务不接受图书对象吗?

    而不是您的表单验证什么是有效的书,它似乎应该让Book 类负责确定它是否有效。

    某些验证,例如将必填字段留空可能是表单级别的验证问题...但其中很多听起来像是图书服务和/或图书类的领域。

    【讨论】:

    • 好吧,我可以轻松地在我的 Book Service 中添加一个 Add 方法,该方法将一本书作为参数,但我认为在服务类中创建新的 book 实例更合适。我还认为任何“消息框或反馈”都应该包含在界面中而不是服务类中。实际的验证逻辑本身发生在服务层,例如检查数据库是否有重复等。但是在我的界面中,我使用对服务层的访问来获取一个布尔值作为是否存在例如重复项的答案。
    • 是的,我明白你来自哪里,我自己最近一直在努力解决这个问题。不过,您似乎仍然可能遇到一种情况(假设您在某处的服务器上有一个数据库后端),在您检查的时间和保存的时间之间事情可能会发生变化。我想您可以在那里处理异常并显示消息或其他内容。
    猜你喜欢
    • 2015-10-07
    • 2012-11-02
    • 2011-09-01
    • 1970-01-01
    • 2019-07-03
    • 2022-11-22
    • 2020-08-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多