DataAnnotations Validation only implemented in MVC? - wpf

I somehow missed the introduction of DataAnnotations. I’ve been looking at them off and on for the last couple of weeks. It seems like everything I’m finding ties back to MVC. Are there any full implementations for doing validation with WPF, WinForms, etc.?
If so, any pointers/links would be appreciated.
If not, are there plans to introduce integrated support in the future?
My current interests lie in WPF so that's really what I'm looking for.
TIA

Karl Shifflett from the Microsoft patterns and practices team has a detailed video on how to use the Data Annotations attributes in WPF. His source code is available via a link at the bottom of his blog post on his Stuff demo application.
Data Annotations is a nice middle ground between that and the ad-hoc validation one would normally see in the WPF books and tutorials and the more powerful Enterprise Validation Application Block that #Evan Larsen mentioned above. Plus, as you noticed, using Data Annotations allows for sharing of models with ASP.NET applications.

Regarding WPF, DataAnnotations support is not built in, but it shouldn't be too difficult to hook in to your own app.
The demo app of my open source library PDX shows one way to do it, but the documentation for 0.2 isn't quite finished yet :)
Here's another way to do it: http://babaandthepigman.wordpress.com/2010/02/14/wpf-commanding-and-data-annotations-validation/

Related

Cucumber.js testing without relying any web browsers(Headless or real web browsers)

I was a .net based windows application developer and being a web developer now.
For the last couple of years, I tried to follow principles of Specification by example in a project.
On the last project I was involved, our team used WPF with MVVM(Model-View-ViewModel) pattern and I tried SpecFlow for testing. (Well.. the coverage by SpecFlow was very low though cause it was not officially adopted from our team and I couldn't spend enough time on it.)
Now our team started to migrate the existing windows application to a web based application and decided to use cucumber.js as a tool for implementing 'Specification by example'.
When I was trying SpecFlow before, I made all the step definitions to test ViewModel layer instead of View and it was the only way actually. What I'm confused with cucumber.js is that most of examples demonstrate how to test features via web browsers(headless or real ones).
If the features are specifying specific ui behaviors then accessing a DOM element and evaluating its value make sense. However, as far as I learned about Specification by examples, testing under the UI layer is recommended instead of UI elements directly.
It seems like most of modern web applications have layers such as MVC, MVVM(knouckout.js) nowadays. Our web application will use Angularjs and this is my theory.
I guess it is possible to use feature files - which do not state any specific UI controls and behaviors - to test under the skin of UI layer, without relying browser support. That layer would be Model of angularjs, not ui elements.
Is this approach missing something or wrong with cucumber.js?
Please give me some insights and correct me if I'm wrong.
Thanks in advance!
Specification by example specifies requirements by giving examples from the point of view of the user. What's more representative of the user: clicking items on a web page, or directly calling code? A user won't be calling methods on your view model, so in my opinion a feature test is a much better way to test expected behaviour.

How to convert old project to WPF

We have lots of project developed using .net 2.0 version if I want to convert those project to WPF then what will be the approach. Please discuss in detail.
to let the migration to WPF easy, be sure that your existing code doesnt contains a high coupling between GUI and your Business layer, if it's the case the most important step is to refactor your existing code, so your GUI code will contains only what's related to GUI.
And after this step the part of your project to migrate will be not the big one, it will concerns only the GUI logic.
When I did this kind of migration from winform to WPF , we spent more time to remove coupling between GUI and other layers than to migrate the GUI layer, due to high coupling betwn GUI and other layers.
Having gone through a similar exercise I'll tell you that it can be done but there are some challenges.
Step1: Just for clarification when we say WPF some people mean Silvelright / web. WPF is not Silverlight so you need to clarify I want to migrate existing applicaitons to WPF thick client applications. Since you stated WPF I am assuming you mean thich client/windows based applications so we'll go that route. (I'll mention SL at the end)
Step 2: Evaluate your projects from the code level for the most part refactoring the code is fairly easy. Since you are migrating from .net 2.0 to .net 4.0 I would really look at how many of your classes are defined, and see what can be refactored or in some isntances completely redesigned. THis leads us directly into step 3.
Step 3: Pick a design pattern. One of the tricky parts is coming to terms with a new design patterns such as MVVM for WPF. More than lilkely you will want to strip out and redesign the entire UI. As a result you will also want to utilize a proper design pattern (MVVM) or some flavor of that. This will also tie back to step two as some of your code will need to change to support your design pattern.
There are a ton of resources online for MVVM as well as UI development. If you are looking for reading materials I would look to WPF 4 unleashed as well as WPF illustrated. Both are good reads to help you get up to speed on the UI components how they work and the proper approach to using them in a project.
Get familar with .net 4 and the use of collections, types and of course the numerous new classes for wpf.
Disclaimer: You can in many instances create a WPF application usign tradition winform layout and practices. Whether this is right or wrong is a matter of opion of the developer. My personal opinion is that yes you can forego learning new design patterns and use a winform approach but you will not gain the full benefit and capabilities of the framework using this approach.
And finally if you are planning to migrate these apps to the web (silverlight) your work will be a little more of a challenge. Silvelright does have a little higer learning curve and there are a few more rules (security, asynchronous calls, web services etc) that must be learned, and followed. I have foudn that in creating a migration that it is easier to migrate from Siverlight to WPF that vice versa. As Microsoft continues to improve Sivelright I think we will get to the point to where the code is interchangeable between the two patterns however right now it's not there.
Conduct a search online but here are a few links to help you get started:
Scott Guthrie (Silverlight) http://weblogs.asp.net/scottgu/default.aspx
Josh Smith (WPF MVVM) http://joshsmithonwpf.wordpress.com/
best of luck

Is there a tutorial or a book doing MVVM + DI and using Data from DataBase all in one?

It's possible to find tutorials on MVVM and DI, somewhat harder about getting Data from database properly into GUI.
Finding something that talks about all these things and how they integrate is near impossible.
I guess some people will say that it doesn't matter and all stands independently but I don't believe it. Especially considering MVVM and Data as I find keeping a tight grip on data managing while doing MVVM to be the hardest part.
So is there anything I could use, and I don't wan't some small simple problem solved but something near complete, at least steps from DB to GUI dynamically done. I'm willing to pay for such a book.
You could look at Prism guidance provided by Microsoft Patterns & Practice group. Prism is actually complete architectural approach to build enterprise level solutions. But it contains StockTrader Reference Implementation application that is built using DI, MVVM and includes interaction with data storage (they use XML files as data storage to provide easy distribution, but it really doesn't matter what data storage is used). Also, the guidance contains several small quickstart projects which emphasize particular part of the architecture. There is comprehensive documentation and active community around Prism, so you can find answers to any question. And it is completely free.
Among many other things my book Dependency Injection in .NET discusses all of those things. However, the subjects is DI first and foremost, with MVVM and data access only incidentals. However, you may find some of the code examples accompanying the book helpful.
In addition to Prism guidance already mentioned, I highly recommend Karl Shifflett's MVVM In-the-Box tutorial. It has great content and also encourages you to experiment with code as you progress through the topics (it is integrated with Visual Studio).
You might be interested in the BookLibrary sample application of the WPF Application Framework (WAF). The BookLibrary shows how to use MVVM, DI with MEF and data management with Entity Framework and SQL Compact Edition.

Do you know any real-world, useful WPF/Silverlight applications using MVVM?

I'm looking for code of full applications that (1) use the MVVM pattern and (2) are approachable for WPF/Silverlight/MVVM beginners. So far I have only found:
Crack.NET - A runtime debugging and scripting tool (useful but quite complicated)
CipherText - WPF Password Manager (perfect but written in VB.NET)
What are some others?
preferably smaller helper apps or tools that use MVVM but that have a completed feel, perhaps with WPF/Silverlight graphic/animation goodness, etc. which show not only the MVVM concepts being used but in a context of a real-world, completed application
My Contribution
Thanks for all the tips, I collected together a list of 15+ MVVM code examples and posted the here:
Overview of WPF/Silverlight code examples that help you learn MVVM.
I too class myself as a beginner in MVVM developing a large scale application for about a month now. I'm over most of the main hurdles, but keep getting confused up by a few things.
Be careful with the suggestions from Kent. They'll get you started, but the subtle differences in Silverlight and WPF will trip you up occasionally (triggers are one).
I don't know of any full sites, but I started with the Composite Application Guidance and PRISM libraries. These a little too cumbersome for a 'beginner' and just MVVM, but some of the concepts are very useful (event aggregator for example).
I suggest that you read blogs by Josh Twist, Josh Smith, Shaun Wildermuth and Nikhil Kothari. These guys are very knowledgeable. They are also very open to answering questions too.
What is your development background? Some of the concepts surrounding method calls, responses and delegation are a real break from "traditional sequential" coding techniques.
Given the question, you may be interested in the current effort going on to create a "Pet Shop" type reference implementation for M-V-VM. You can join the conversation or follow progress here.
I've been using MVVM for the last couple of months on a project and it's been fantastic. I've been doing UI work for over 12 years and it's the best thing I've come across.
I have been using MVVM for about 2 years now, and I can say that it is a resounding success. I have shipped one large-scale medical imaging solution using MVVM in Silverlight. I am also about to ship a complicated WPF application using Prism and MVVM extensively. In fact, I don't know how I'd achieve some of my problems elegantly without MVVM in this more recent application.
+1 on MVVM. It works. It is testable. It is extensible. It facilitates developer-designer workflows. It ALSO doesn't have to be messy and plumbing-heavy.
In addition, I created a medium-sized Silverlight application using MVVM for the CodeMash conference in January. You can see it and get the source code here.
Try this one. Also, Family.Show may be useful.
The WPF Disciples group has decided to create a real-world MVVM + Mediator reference application similar to the Pet Shop refence application for Web Apps. Your can follow the development on codeplex here

Good Silverlight-MVVM Practice Example

I've read a number of good articles about the Model-View-ViewModel pattern and my team intends to implement this pattern in the latest version of our app. I still don't quite get ALL the bits that go together to make this work. I'd like to find a good example of this pattern that I can work through. Something on a small scale, much like Jason Dolinger does in his video here, but I'd like something that I can work through on my own at a slower pace.
Thanks in advance for your help.
Basically a ViewModel is a wrapper around model. Now that might not be very helpful yet :-) Think of the model as the data your application works with, say a person. Now a person has a birthday and you might want to have a form to enter the person's birthday. Suppose we are just using a simple TextBox the date shows up something like 01-02-2009 12:00:00AM. For starters we don't want the time part and we also might not be to happy about the 01-02-2009 part as this depends on your locale settings.
So here the ViewModel comes im. It wraps the Person class and exposed the date as three integer values, year, month and day. In the property set it tries to build a date from the different values and displays any errors that might occur.
So simply said a ViewModel is a Model wrapper specifically geared towards a particular view (display). It eliminates most IValueConvertors at the same time.
Josh Smith has a nice explanation here: http://joshsmithonwpf.wordpress.com/2008/11/14/using-a-viewmodel-to-provide-meaningful-validation-error-messages and a big discussion here: http://groups.google.com/group/wpf-disciples/browse_thread/thread/3fe270cd107f184f?pli=1
Prism 4.0 now includes documentation for creating MVVM applications. This is targeted for using the Prism libraries but the concepts work for the MVVM pattern in general.
http://compositewpf.codeplex.com/releases
Here's one more to add to the list: Simple MVVM Toolkit by Tony Sneed (yours truly): http://simplemvvmtoolkit.codeplex.com.
The toolkit consists of helper classes, Visual Studio item templates, and code snippets. In addition to the actual toolkit, there is a sample app with step-by-step instructions on how to build a basic Silverlight app using the MVVM pattern.
Nikhil Kothari has a post about how to use that pattern with his Silverlight.FX library.
Check it out here: http://www.nikhilk.net/ViewModel-Pattern-DLR.aspx
There is also:
Silverlight MVVM: An (Overly) Simplified Explanation
I have gone through many recently, the two that stand out are:
Dan Wahlin's (LIDNUG):
http://www.lidnug.org/Archives.aspx
John Papa's (PDC 2010):
http://player.microsoftpdc.com/Session/76864d75-b4da-4858-aac7-786c5f28e344
The best explanation of a ViewModel was given by Scott Hanselman during his MVC talk in Europe.
A one-liner response is -- It is a model (class) that is created for the sole purpose of sending all the necessary data that a particular View needs and also for receiving modified data from the View in case of two-way bindings.
Take a look at my article about real life MVVM in Silverlight. Though it is not for newcomers, and targeting mostly expert developers working on real life projects.
http://alexburtsev.wordpress.com/2011/03/05/mvvm-pattern-in-silverlight-and-wpf/

Resources