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 properties from a model which has a Model property fails #72

Closed
MartinRothschink opened this issue Aug 28, 2023 · 3 comments · Fixed by #81
Closed

Generating properties from a model which has a Model property fails #72

MartinRothschink opened this issue Aug 28, 2023 · 3 comments · Fixed by #81
Labels
bug Something isn't working

Comments

@MartinRothschink
Copy link

Hi Thomas,

I'm a big fan of MVVMGen and use it very often. Most of the time my ViewModels are based on an existing model class.

Today I encountered a showstopper because my model class already contains a Model property, causing a collision with the generated protected property for the Model class.

Possible solutions: the quickest would be to rename the generated internal protected property (e.g. _model instead of Model). Perfect would be to add another parameter to the ViewModelAttribute to set the internal name.

Thanks
Martin

@TopperDEL
Copy link

+1 on this

@thomasclaudiushuber
Copy link
Owner

Thank you @MartinRothschink. This sounds like a great addition and I'll look into it.

@thomasclaudiushuber
Copy link
Owner

Thank you @MartinRothschink and @TopperDEL

Will be fixed with next release (Planned for first week of December).

New usage:

namespace MyCode
{
    public class Employee
    {
        public Employee (int id)
        {
            Id = id;
        }
        public int Id { get; }
        public bool IsDeveloper { get; set; }
    }

    [ViewModel(typeof(Employee), ModelPropertyName="CustomPropName")]
    public partial class EmployeeViewModel
    {
    }
}

Will lead to this generated code:

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

        partial void OnInitialize();

        public int Id => CustomPropName.Id;

        public bool IsDeveloper
        {
            get => CustomPropName.IsDeveloper;
            set
            {
                if (CustomPropName.IsDeveloper != value)
                {
                    CustomPropName.IsDeveloper = value;
                    OnPropertyChanged("IsDeveloper");
                }
            }
        }

        protected MyCode.Employee CustomPropName { get; set; }
    }
}

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.

3 participants