VS2010's designer throws a ConfigurationErrorsException with above message when I put a call to CustomerList.Get() in the ctor of my ViewModel<T> derived class, though executing the program runs fine.
A bit of background:
I have an OrderViewModel derived from ViewModel<Order>. In the Windows' xaml, I declare a resource of type OrderViewModel.
On the OrderViewModel, I added a DependencyProperty for my CustomerList, and it's when trying to fill the list in the ctor that the designer pukes. Again, running the code doesn't give any exception and does retrieve the CustomerList.
CustomerList derives from ReadOnlyListBase<CustomerList, Customer>
Any ideas ?
The designer creates an instance of the view (XAML). If your view directly references the viewmodel like this:
<my:ReallyCoolViewModel x:Key="ViewModel" />
then as the XAML is loaded by the designer, the designer will create an instance of your type. Of course that triggers the constructor, which calls the data portal. And all this is running inside of Visual Studio, so it uses the app.config file for Visual Studio - which obviously doesn't have your database connection string.
There are a few other scenarios where this can happen - but it is a recurring theme - as the designer instantiates the XAML, that triggers some of your code that uses the data portal - which fails for perfectly understandable reasons.
There are a couple solutions.
One is to use a CollectionViewSource as a resource, instead of directly using your viewmodel:
<CollectionViewSource x:Key="ViewModelViewSource" />
This is what the designer will do if you use drag-and-drop data binding. This doesn't automatically create an instance of your type, since some other code must run to hook your viewmodel to the Source property of the CollectionViewSource.
Another option is to put an "is in design time" check into your viewmodel's constructor. That's this line of code:
I had intuitively used the "design time" check, and it did solve the problem, though I don't think using this kind of check is clean when doing something other than custom controls. Maybe I'm wrong.
As for the CollectionViewSource, is there an example of using it in the CSLA samples ?
The C# SL sample called CslaMvvmSl was created using the VS10 designer features, so it uses CVS resources. It also uses a version of Bxf (before I put it in CodePlex) to manage the view <-> viewmodel wire-ups, etc.
This article may help, though I haven't tried it yet with CSLA. It shows you how to setup sample data rather than calling the object.
Copyright (c) Marimer LLC