Skip to content

Latest commit

 

History

History
99 lines (77 loc) · 2.88 KB

v1.4_to_v2.md

File metadata and controls

99 lines (77 loc) · 2.88 KB

Migration notes from v1.4.0 to v2.0.0

This document describes the changes that need to be made to your code to migrate from version 1.4 to version 2.0 of the library.

Changes

Library Configuration

The configuration method AddMvvmNavigation has been deprecated. Update your code to use the new method as shown below:

- builder.Services.AddMvvmNavigation(options =>
+ builder.Services.AddMvvm(options =>
{ 
   options.HostingModel = BlazorHostingModel.Server
});

ViewModel Registration

View model registration has been improved in version 2.0 with support for auto registration and discovery. Use the new AddMvvm method and the ViewModelDefinition attribute to configure view model discovery and registration. Update your Program.cs and view model classes as shown below:

# Program.cs
- builder.Services.AddTransient<TransientViewModel>();
- builder.Services.AddScoped<ScopedViewModel>();
- builder.Services.AddSingleton<ISingletonViewModel, SingletonViewModel>();
- builder.Services.AddKeyedTransient<KeyedViewModel>("key");

+ builder.Services.AddMvvm(options =>
+ { 
+    options.HostingModel = BlazorHostingModel.Server,
+    options.RegisterViewModelsFromAssemblyContaining<TransientViewModel>();
+});

# ScopedViewModel.cs
+ [ViewModelDefinition(Lifetime = ServiceLifetime.Scoped)]
public class ScopedViewModel : ViewModelBase
{
}

# SingletonViewModel.cs
+ [ViewModelDefinition<ISingletonViewModel>(Lifetime = ServiceLifetime.Singleton)]
public class SingletonViewModel : ViewModelBase, ISingletonViewModel
{
}

# KeyedViewModel.cs
+ [ViewModelDefinition(Key = "key")]
public class KeyedViewModel : ViewModelBase
{
}

ViewModel Loaded

The Loaded method has been deprecated and replaced with the OnInitializedAsync method. Update your ViewModel classes as shown below:

public class MyViewModel : ViewModelBase
{
-   public override Task Loaded()
+   public override Task OnInitializedAsync()
    {
    }
{

Validation

The validation components MvvmValidationSummary have been deprecated. Update your code to use the new validation components as shown below:

@inherits MvvmComponentBase<EditContactViewModel>

<EditForm Model="ViewModel.Contact" OnValidSubmit="ViewModel.SaveCommand.Execute">
+    <MvvmObservableValidator />
+    <ValidationSummary />
-    <MvvmValidationSummary />

    <div class="row g-3">
        <div class="col-12">
            <label class="form-label">Name:</label>
            <InputText aria-label="name" @bind-Value="ViewModel.Contact.Name" class="form-control" placeholder="Some Name"/>
            <ValidationMessage For="() => ViewModel.Contact.Name" />
        </div>
    </div>

    <hr class="my-4">

    <div class="row">
        <button class="btn btn-primary btn-lg col"
                type="submit"
                disabled="@ViewModel.Contact.HasErrors">
        Save
        </button>
    </div>
</EditForm>