There are numerous ways of handling design-time data in view-models. While some people use complex dependency injection frameworks or inversion of control containers, the simplest method is to just check the DesignerProperties.IsInDesignTool flag. Unfortunately this has the side effect of embedding the design-time data into one’s application. Jeremy Likness offers a couple ways of working around this.
His first proposal is the use of partial methods and compiler constants. If the partial method that loads the data, named _WireDesignerData in his example, is excluded from the release build then any calls to it will also be removed.
Another option is the Conditional attribute. This has the same net effect, but doesn’t require the use of partial classes and #if blocks to remove the design-time code.
InfoQ Asks: What is your favorite strategy for handling design-time data?
Community comments
No Favorite
by Rob Eisenberg,
No Favorite
by Rob Eisenberg,
Your message is awaiting moderation. Thank you for participating in the discussion.
I can't say that I have a favorite strategy. There is no really good way to handle this problem. Design-time data *should* be handled transparently by the tooling and/or by your MVVM framework of choice, with no need to write any additional code. This is important for maintenance *and* extensibility reasons. If you take Jeremy's approach, you end up with a ton of code you have to maintain, which exists soley to support the strategy of a tool, which may or may not work the same in the next version. This problem exists solely because the Expression Blend team is a very closed group and has, at least in my case, blatantly refused to discuss necessary improvements in tooling around this issue. I'm a Silverlight MVP and a Silverlight Insider, and if I can't even get them to have a conversation with me on this subject (I've been trying for two years), that's a real problem. Changes to their tooling to improve the situation are relatively simple, but they aren't interested.
As a side note, the issue of IoC is (or should be) completely orthogonal to design-time data issues. IoC is critical for the decomposition of a complex UI and for the long-term maintenance and extensibililty plan. You should not have to trade off building a good app, just so you can get a tool to show decent values in a text box at design-time.
Sorry if this sounded like a rant, but this question highlights a problem that which has nothing to do with building good software. It's a problem that was created by Microsoft and thrust on the developer; and it's a problem they refuse to engage experienced members of the community on. None of this should be an issue.