Getting Started

Step 1: Define the view model

The view model is the contract between the controller and the view. This should have the properties that may be
bound to from the view, including any commands (ICommand).

Example

public interface IDemoViewModel
{
    string Title { get; set; }
    string Text { get; set; }
    long Counter { get; set; }
    IList<string> List { get; set; }
    ICommand Command { get; set; }
}

Step 2: Define the controller service

The controller service hosts the command behavior for the view model.

Example

public interface IViewModelController
{
    void Command(IDemoViewModel demoViewModel);
    void Initialize(IDemoViewModel demoViewModel);
}

Step 3: Configure the view model

The view model configuration controls how the view model behaves at runtime.

This includes:
  • Binding to a validator
  • Binding to a controller
  • Whether singleton or transient instances are returned from the container
  • Identifying any properties that should have INotifyPropertyChanged behavior
  • Identifying commands that should be bound to the controller
  • Initializing specific properties after construction

Example

public class DemoViewModelConfiguration : ConfigureViewModel<IDemoViewModel>
{
    public DemoViewModelConfiguration()
    {
        Validator<DemoViewModelValidator>();
        Controller<IViewModelController>();
        Not.Transient();
        Notify(x => x.Title);
        Notify(x => x.Text);
        Notify(x => x.Counter);
        Command(x => x.Command);
        Initialize(x => x.List, () => new ObservableCollection<string>());
    }
}

Step 4: Configure the validator

This step is optional. However you can configure the view model to be validated using
FluentValidation (http://fluentvalidation.codeplex.com/) as the validator.

Example

public class DemoViewModelValidator : ValidationConfiguration<IDemoViewModel>
{
    public DemoViewModelValidator()
    {
        RuleFor(x => x.Text)
            .NotNull()
            .NotEmpty();
    }
}

Step 5: Build the controller service

The controller is responsible for making the whole thing run. It hosts the commands that are
used by view models, and optionally an initialization method, one for each view model bound
to the controller, that is called when a new view model is constructed.

For commands to be wired up, the view model must identify a command to be connected,
and the controller must have a method that takes a single paramter for the view model and
must have the same name as the ICommand property on the view model. Any interaction
with the command from the view model will be dispatched to the controller.

Example

public class ViewModelController : IViewModelController
{
    public void Command(IDemoViewModel demoViewModel)
    {
        ++demoViewModel.Counter;
    }

    public void Initialize(IDemoViewModel demoViewModel)
    {
        demoViewModel.Title = "View Model Demo!";
        demoViewModel.Text = "Welcome, Fluency!";
    }
}

Step 6: Configure the controller service

The controller configuration controls how the controller behaves at runtime.

This includes:
  • Providing an implementation
  • Whether singleton or transient instances are returned from the container


public class ViewModelControllerConfiguration : ConfigureController<IViewModelController>
{
    public ViewModelControllerConfiguration()
    {
        ImplementedBy<ViewModelController>();
        Not.Transient();
    }
}

Step 7: Build the configuration

Building the configuration prepares the environment and wires everything up.

Example

protected override void OnStartup(StartupEventArgs e)
{
    Configuration
        .Begin()
            .Add<ViewModelControllerConfiguration>()
            .Add<DemoViewModelConfiguration>()
        .End();
}

Step 8: Bind to the view model

Binding to a view model kicks the whole thing off, and causes any related controllers and view models to be built and presented.

Example

<Window.DataContext>
    <fvm:ViewModelActivator Type="{x:Type demo:IDemoViewModel}"/>
</Window.DataContext>
<StackPanel Orientation="Vertical">
    <StackPanel.Resources>
        <DataTemplate DataType="{x:Type ValidationError}">
            <TextBlock FontStyle="Italic"
                        Foreground="Red"
                        HorizontalAlignment="Right"
                        Margin="0,1"
                        Text="{Binding Path=ErrorContent}" />
        </DataTemplate>
    </StackPanel.Resources>
    <TextBox x:Name="Text"
                Height="30"
                Text="{Binding Path=Text, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}"
                Validation.ErrorTemplate="{x:Null}"/>
    <ContentPresenter Content="{Binding ElementName=Text, Path=(Validation.Errors).CurrentItem}" />
    <Button Height="30"
            Command="{Binding Path=Command}" />
    <TextBlock Height="30"
                Text="{Binding Path=Counter}" />
</StackPanel>

Step 9: Relax

This example is based on the TestApplication that is provided in the source code.

Last edited Apr 28, 2010 at 2:38 AM by bobble79, version 14

Comments

No comments yet.