简体   繁体   English

在使用IDataErrorInfo在MVVM中执行验证时,我应该在哪里执行检查以查看数据库中是否已存在值?

[英]Where should I perform a check to see if a value already exists in the database when performing validation in MVVM using IDataErrorInfo?

Getting started with all this MVVM stuff, I was following this post by Josh Smith that talks about an approach to validation while using MVVM. 开始使用所有这些MVVM的东西,我正在关注Josh Smith的这篇文章,文章讨论了使用MVVM时的验证方法。 The example is simple, and I began to wonder how I would use it in my own application. 这个例子很简单,我开始想知道如何在我自己的应用程序中使用它。

I have, in my BLL, a BookInfo class that implements IDataErrorInfo to report invalid values such as "published date can't be in the future" or "number of pages can't be negative". 在我的BLL中,我有一个BookInfo类,它实现IDataErrorInfo来报告无效值,例如“发布日期不能在将来”或“页数不能为负”。 Then my AddBookViewModel would check the state of the newly created BookInfo , see that errors exist, and the AddBookView will display a red blob next to the appropriate TextBox. 然后我AddBookViewModel将检查新创建的状态BookInfo ,发现存在错误,并AddBookView会显示下一个红色的斑点,以适当的文本框中。 That stuff is straightforward, just like in the example in the post. 那些东西很简单,就像帖子中的例子一样。

Now my BookInfo class also holds a list of author IDs. 现在我的BookInfo类还包含一个作者ID列表。 On addition of a new BookInfo to my database, I need to check if those author IDs already exist. 在我的数据库中添加新的BookInfo后,我需要检查这些作者ID是否已经存在。

Should this check be done in my BookInfo class? 这个检查应该在我的BookInfo类中完成吗? If so, then I would have to pass in my BLL's AuthorManager object to BookInfo 's constructor, since the former would contain methods such as CheckIfExists(int authorID) . 如果是这样,那么我会在我的BLL的传递AuthorManager对象BookInfo的构造,因为前者将包含方法,如CheckIfExists(int authorID)

Is this the recommended approach? 这是推荐的方法吗? What if there are a lot of records in the DB? 如果DB中有很多记录怎么办? Dynamically checking would affect performance? 动态检查会影响性能吗?

On the other hand, it would seem a bit messy to perform some checks in the BookInfo class and some elsewhere... especially when all those checks can be categorized into the same group... ie. 另一方面,在BookInfo类和其他地方执行某些检查似乎有点麻烦......特别是当所有这些检查可以归类到同一组时...即。 making sure a newly created BookInfo object is valid. 确保新创建的BookInfo对象有效。 Or maybe I'm wrong since I don't really have experience to make proper judgement. 或者也许我错了,因为我没有经验来做出正确的判断。

Some guidance? 一些指导?

I wouldn't do this. 我不会这样做。 I would keep the validations done 'inside' IDataErrorInfo simple and contextless. 我会在IDataErrorInfo内部完成验证,简单且无上下文。 Any validations that depend on context, such as cross-entity validations and validations that are dependent on a database, do that validation when you save the changes. 任何依赖于上下文的验证(例如跨实体验证和依赖于数据库的验证)在保存更改时都会执行该验证。

Trying these more complex context based validations in IDataErrorInfo will be error prone and often impossible. IDataErrorInfo尝试这些更复杂的基于上下文的验证将容易出错并且通常是不可能的。 It is often impossible to do this reliably without a context. 没有上下文,通常不可能可靠地完成这项工作。

I've written a blog post about this. 我写了一篇关于此的博客文章。 While it is written in the context (no pun intended) of the Validation Application Block, it talks about the general problem of context based validation. 虽然它是在验证应用程序块的上下文(没有双关语)中编写的,但它讨论了基于上下文的验证的一般问题。 It might be helpful. 它可能会有所帮助。 Here it is . 在这里

I agree with Steven that you should perform the server-side validations when attempting to save the data. 我同意Steven的观点,即您在尝试保存数据时应执行服务器端验证。

Another reason for this is network latency. 另一个原因是网络延迟。 Since WPF's support for IDataErrorInfo uses input events to determine when properties are validated, and results in a blocking/synchronous call to your VM object, the use of IDataErrorInfo has a direct impact on the responsiveness of the UI. 由于WPF对IDataErrorInfo的支持使用输入事件来确定何时验证属性,并导致对VM对象的阻塞/同步调用,因此使用IDataErrorInfo会直接影响UI的响应性。 You cannot begin an async call to your database to perform the validation and then dispatch validation errors to the UI thread when your network call completes. 您无法开始对数据库执行异步调用以执行验证,然后在网络调用完成时将验证错误分派给UI线程。 You would have to make a blocking call to your DB to get the result, which could wreak havoc on the UI thread while it waits for the call to return. 您必须对数据库进行阻塞调用才能获得结果,这可能会在等待调用返回时对UI线程造成严重破坏。

I hope someday WPF gets Silverlight's fancy new INotifyDataErrorInfo interface, which allows for the async validation scenario I described above. 我希望有一天WPF能够获得Silverlight新的INotifyDataErrorInfo接口,它允许我上面描述的异步验证方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM