Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

generating Viewmodel from derived model #16

Closed
khaledafifi opened this issue Aug 22, 2021 · 5 comments · Fixed by #25
Closed

generating Viewmodel from derived model #16

khaledafifi opened this issue Aug 22, 2021 · 5 comments · Fixed by #25
Labels
bug Something isn't working

Comments

@khaledafifi
Copy link

khaledafifi commented Aug 22, 2021

class A
{
string id{get; set;}
string Name{get; set;}
}

class B : A
{
string Email {get; set; }
}

the generated ViewModel from B must include props from A but it doesn't.

@thomasclaudiushuber
Copy link
Owner

thomasclaudiushuber commented Sep 4, 2021

Hi @khaledafifi , sorry, I was on holidays.

You're missing the public access modifier here. Private members are not visible in subclasses. Should be

class A : MvvmGen.ViewModel.ViewModelBase
{
public string id{get; set;}
public string Name{get; set;}
}

class B : A
{
public string Email {get; set; }
}

Please let me know if this works for you.

@khaledafifi
Copy link
Author

the same result .. the generated vm publish prosperities only from the derived class not the base.
please try yourself.
thanks to your great project.

@thomasclaudiushuber
Copy link
Owner

thomasclaudiushuber commented Oct 6, 2021

Hi @khaledafifi ,

I tried it. I defined these ViewModels

namespace MyViewModels
{
  [ViewModel]
  public partial class A : ViewModelBase
  {
    [Property]
    private string id;

    [Property]
    private string name;
  }

  [ViewModel]
  public partial class B : A
  {
    [Property]
    private string email;
  }
}

All properties from A are available on B through inheritance and this code works:

B b = new B();
b.Id = 5;
b.Name = "Thomas";
b.Email = "Testmail";

Note that the code editor in Visual Studio might show errors where no errors exist. They still need to catch up a bit with editor support for source generators.

@thomasclaudiushuber
Copy link
Owner

thomasclaudiushuber commented Feb 6, 2022

Ok, reproduced. You're using the ModelType property of the attribute I guess to generate a ViewModel from a Model.

So, for the code below, the MyViewModel should have the properties Name and Email, but it has only an Email property.

public class BaseType
{
    public string? Name { get; set; }
}

public class SubType : BaseType
{
    public string? Email { get; set; }
}

[ViewModel(ModelType = typeof(SubType))]
public partial class MyViewModel
{

}

The generated output in MvvmGen version 1.1.2 is this, the Name property is missing:

// <auto-generated>
//   This code was generated for you by
//   ⚡ MvvmGen, a tool created by Thomas Claudius Huber (https://www.thomasclaudiushuber.com)
//   Generator version: 1.1.2
// </auto-generated>
using MvvmGen.Commands;
using MvvmGen.Events;
using MvvmGen.ViewModels;

namespace WpfApp2
{
    partial class MyViewModel : global::MvvmGen.ViewModels.ViewModelBase
    {
        public MyViewModel()
        {
            this.OnInitialize();
        }

        partial void OnInitialize();

        public string? Email
        {
            get => Model.Email;
            set
            {
                if (Model.Email != value)
                {
                    Model.Email = value;
                    OnPropertyChanged("Email");
                }
            }
        }

        protected WpfApp2.SubType Model { get; set; }
    }
}

@thomasclaudiushuber
Copy link
Owner

Thank you @khaledafifi for the issue. It's fixed and it will be part of an upcoming release.

@thomasclaudiushuber thomasclaudiushuber added the bug Something isn't working label Feb 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants