views:

507

answers:

1

I'm having an issue with databinding to a service class in WPF. My service class has a static constructor that reads configuration information and pulls in some required information. The problem is that during design, I get the error the type initializer for Mytype threw an exception. I believe this is because the configuration information is not present in Visual Studio's config. When I run the app, everything works fine, but I cannot design anything because of this error.

I would presume that the design would revert to some default display if it can't get at my databinding object. Apparently it doesn't and kills the entire designer. Is there any workaround for this issue like turning off dynamic display of databound controls. This is supremently frustrating and disappointing. I am using Visual Studio 2008 SP1.

Here's a snippet of the databinding code in my grid.

<ObjectDataProvider ObjectType="{x:Type local:DataService}" x:Key="dataProvider" 
    MethodName="GetCollection" >
    <ObjectDataProvider.MethodParameters>
         <s:Int32>1</s:Int32>
    </ObjectDataProvider.MethodParameters>
</ObjectDataProvider>

Essentially the DataService class cannot instantiate itself unless it has configuration information. I was trying to create a workaround for my particular case where the static constructor instantiates a concrete implementation of a data repository. However, I cannot directly do that since that would create a circular reference in my project. I've tried inheriting the class and catching all exceptions in the parent class' static constructor. That still doesn't work. Any other ideas on workarounds or an error log that shows more detail about what exception was thrown.

I tried stripping everything out to find the lowest common denominator. I removed the static constructor and basically returned the bare essentials. What I found was that anything that retrieved data from the database failed to render. If I return a static list, everything is fine. I'm using the microsoft enterprise library data access block. Again, the application works fine when running it. I'm assuming it can't find the required database connection information, but why would this be an issue? Microsoft had to assume this would happen when they built VS2008. Am I doing something wrong?

A: 

You're right. The Visual Studio designer is unable to access your database connection strings. The common way of providing design-time data is to first check whether the application is in design mode and, if so, return some dummy-data without making any connections to the database.

Something like the following

public class DataService
{

   public IList<string> GetCollection()
   {

      if (!DesignerProperties.GetIsInDesignMode(new DependencyObject()))
      {

         // get data from database here
      }
      else
      {
         // return fake data for the designer
         return new List<string> {"my", "test", "data"};
      }
   }
}

The important thing is to never call upon anything that will need config if it will be used in design mode.

b-rad
My concern with this approach is that it would require my data service to have a reference to the UI in some shape or form. In this case I would need a reference to System.ComoonentModel which is okay. But I'd also need one to System.Windows which seems really weird and wrong. Any chance this behavior was changed in vs2010. Temporarily, I've opted to just databind in the load event by setting the datacontext and deal with the designer showing me something empty. Seems stupid, but I'd rather that than create this nasty conditional logic to satisfy VS' designer
Chris Cap