LINQ to SQL验证所有字段,而不仅仅是在第一个失败的字段处停止 [英] LINQ to SQL validate all fields, not just stop at first failed field

查看:92
本文介绍了LINQ to SQL验证所有字段,而不仅仅是在第一个失败的字段处停止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始使用LINQ to SQL类,并且非常喜欢它如何帮助我编写可读代码. 在文档中,典型示例指出要进行自定义验证,您应按如下方式创建部分类::

I just started using LINQ to SQL classes, and really like how this helps me write readable code. In the documentation, typical examples state that to do custom validation, you create a partial class as so::

partial class Customer 
{
    partial void OnCustomerIDChanging(string value)
    {
        if (value=="BADVALUE") throw new NotImplementedException("CustomerID Invalid");
    }
}

其他领域也是如此... 然后在后面的代码中,我放置了类似这样的内容以显示错误消息,并将用户保持在同一页面上,以便更正错误.

And similarly for other fields... And then in the codebehind, i put something like this to display the error message and keep the user on same page so to correct the mistake.

    public void CustomerListView_OnItemInserted(object sender, ListViewInsertedEventArgs e)
{
    string errorString = "";
    if (e.Exception != null)
    {
      e.KeepInInsertMode = true;
      errorString += e.Exception.Message;
      e.ExceptionHandled = true;
    }
    else errorString += "Successfully inserted Customer Data" + "\n";
    errorMessage.Text = errorString;
}

好的,很简单,但是一旦抛出第一个异常,它就会停止验证其余字段!意味着如果用户进行的模式多于一个错误,则只会通知她/他/他第一个错误. 还有另一种方法可以检查所有输入并显示每个输入中的错误吗? 任何建议表示赞赏,谢谢.

Okay, that's easy, but then it stops validating the rest of the fields as soon as the first Exception is thrown!! Mean if the user made mode than one mistake, she/he/it will only be notified of the first error. Is there another way to check all the input and show the errors in each ? Any suggestions appreciated, thanks.

推荐答案

我知道了.我没有在最初的验证失败时抛出异常,而是将错误消息存储在具有静态变量的类中.为此,我像这样扩展DataContext类:

I figured it out. Instead of throwing an exception at first failed validation, i store an error message in a class with static variable. to do this, i extend the DataContext class like this::

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for SalesClassesDataContext
/// </summary>
public partial class SalesClassesDataContext
{
    public class ErrorBox
    {
        private static List<string> Messages = new List<string>();
        public void addMessage(string message)
        {
            Messages.Add(message);
        }
        public List<string> getMessages() 
        {
            return Messages;
        }
    }
}

在与每个表相对应的类中,我将像这样继承新定义的类:

in the classes corresponding to each table, i would inherit the newly defined class like this::

public partial class Customer : SalesClassesDataContext.ErrorBox

仅在函数OnValidate中,如果错误数量不为0时,我将引发异常.因此,在不丢失输入数据的情况下,不会尝试插入并保持用户在同一输入页面上.

only in the function OnValidate i would throw an exception in case the number of errors is not 0. Hence not attempting to insert, and keeping the user on same input page, without loosing the data they entered.

这篇关于LINQ to SQL验证所有字段,而不仅仅是在第一个失败的字段处停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆