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. 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". 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. That stuff is straightforward, just like in the example in the post.
Now my BookInfo
class also holds a list of author IDs. On addition of a new BookInfo to my database, I need to check if those author IDs already exist.
Should this check be done in my BookInfo
class? 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)
.
Is this the recommended approach? What if there are a lot of records in the 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. making sure a newly created BookInfo
object is valid. Or maybe I'm wrong since I don't really have experience to make proper judgement.
Some guidance?