Last active
December 12, 2015 01:08
-
-
Save shiftkey/4688529 to your computer and use it in GitHub Desktop.
Musing on supplying sample data in a non-sucky way (with ReactiveUI)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using ReactiveUI; | |
namespace WpfApplication1 | |
{ | |
public class DesignMainViewModel : MainViewModel | |
{ | |
public DesignMainViewModel() | |
{ | |
UserName = "shiftkey"; | |
} | |
} | |
public class MainViewModel : ReactiveObject | |
{ | |
[DesignData("shiftkey")] | |
public string UserName { get; set; } | |
} | |
public partial class MainWindow : IViewFor<MainViewModel> | |
{ | |
public MainWindow() | |
{ | |
InitializeComponent(); | |
// 1. keep sample data on the viewmodel | |
// PRO: keeps view unclutted | |
// CON: makes viewmodel cluttered | |
// CON: attributes take object parameter - not as type-safe | |
// CON: this sucks for things like collections or anything complex | |
this.Bind(ViewModel, vm => vm.UserName); | |
// 2. extension methods off Bind/BindOneWay | |
// PRO: type-checking | |
// PRO: keeps design data close to UI | |
// CON: this still sucks for things like collections | |
this.Bind(ViewModel, vm => vm.UserName) | |
.WithDesignTime("shiftkey"); | |
// 2a. slightly different syntax perhaps? | |
this.Bind(ViewModel, vm => vm.UserName, "shiftkey"); | |
// 3. specify design data once after all Bind/BindOneWay | |
// PRO: keep data separate from ViewModel | |
// PRO: no stuffing around in the the awful d: space | |
// PRO: more friendly for complex properties like collections | |
// CON: yay over-abstraction | |
// CON: needing to keep your design VM and runtime VM in sync (I use interfaces but it's not great) | |
// Q: perhaps this could lean on conventions (use DesignFooViewModel for FooViewModel) | |
// and not even require an explicit call | |
this.BindDesignData<DesignMainViewModel>(); | |
} | |
// and the rest of the code ... | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment