This weekend I once again sat down to work with my existing MVC application and see what I could find and how I could better improve the pattern and my overall understanding of the framework. I found a few gotchas and came up with some tips to help improve development.
- Unit Testing
- I was disappointed to find that as of right now, due to breaking changes, updated unit test project templates will be needed to use your favorite unit testing framework with MVC 2. This includes my favorite MbUnit v3. This does not mean that you cannot use the framework, you will just have to do the setup manually.
- One of the things that I am most excited for moving forward is the new way to define validations on the server side. Using the DataAnnotations library, located curiously in the System.ComponentModel parent namespace, you can easily define many of the most common validation routines, from Required to Range. This validation ties tightly in with the MVC framework
- In addition, you cannot validate complex types (objects, arrays, etc) via this method. I tried with an array that was assigned via ModelBinding, and the validation routine is not even called. However, based on what I read regarding .NET 4, this functionality will be available via the CustomValidator attribute. I really must comment on how it is strange that you can define your own custom validation attribute, but if it is attached to a complex type, you get nothing.
That all aside, I am really impressed with how well this ViewModel pattern works with a deferred execution system like Entity Framework. Since my EF entities never cross into the web layer, only their “dumb” proxy counterparts, the separation is really very clean and easy to maintain. At this point the main problem is the distinct Contexts that are open each session, but I plan to address that next week with Ninject.