question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ViewModel <-> View mapping feature

See original GitHub issue

I would like to be able to tell Stylet which View should be created for which ViewModel.

I have a specific use-case where I would like to create multiple VMs but have them all use a single View.

I understand this could be done by subclassing the ViewManager. However, having a simple dictionary that can tell Stylet to create MyView for both MyViewModel and MyOtherViewModel would be a nice feature.

EDIT: Someone pointed this from samples, which seems to be what I need, but I don’t know how I would use that?

EDIT 2: I see the Attribute in there but there’s a couple of issues with it

  1. You have to apply it to a View (rather than a ViewModel)
  2. AllowMultiple = false suggests that only 1 ViewModel can be mapped to a single View

Wouldn’t it be better if this attribute was applied to VMs, specifying the View that goes with them?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
canton7commented, Jul 20, 2020

Yes, you could do exactly that, although it’s mapping from ViewModel -> View of course, not ViewModel -> ViewModel (I’d use TryGetValue instead of doing two lookups, but the result is the same). You’ll need the fully-qualified names (including namespace).

NamespaceTransformations are only for transforming namespaces – they map e.g. SomeLibrary.SomeViewModel to SomeApplication.SomeView. You can see that they match the start of the ViewModel’s type name, and end at a .. This can be needed for the convention-based mapping that Stylet does out of the box.

1reaction
vgist1commented, Jul 20, 2020

@canton7 excuse my ignorance are you saying I could technically do something like this:

public class MyViewManager : ViewManager
{
    public Dictionary<string, string> Map { get; set; }
    
    protected override string ViewTypeNameForModelTypeName(string modelTypeName)
    {
        if (Map.ContainsKey(modelTypeName))
        {
            return base.ViewTypeNameForModelTypeName(Map[modelTypeName]);
        }
        return base.ViewTypeNameForModelTypeName(modelTypeName);
    }
}

Where map would be say

Map = new Dictionary<string, string>
{
    { "CustomerReportViewModel", "ReportViewModel" },
    { "CompanyReportViewModel", "ReportViewModel" },
    { "AccountReportViewModel", "ReportViewModel" },
};

Is that not what this technically does if I understood it right? https://github.com/canton7/Stylet/blob/master/Stylet/ViewManager.cs#L107

Read more comments on GitHub >

github_iconTop Results From Across the Web

Best way to project ViewModel back into Model
I believe best way to map from ViewModel to Entity is not to use AutoMapper for this. AutoMapper is a great tool to...
Read more >
ViewModel overview
ViewModel lets you manage your UI's data in a lifecycle-aware fashion.
Read more >
Model-View-ViewModel (MVVM)
The view model implements properties and commands to which the view can data bind to, and notifies the view of any state changes...
Read more >
FeatureViewModel | API Reference | ArcGIS Maps SDK for ...
A read-only property containing metadata regarding the last edit performed on a feature. more details, FeatureViewModel. map, Map. A reference to the view's...
Read more >
Why is it bad to map between a Model and a ViewModel ...
In a recent Pull Request (PR) of mine, a colleague suggested that it was a bad idea to map between a model and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found