Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6070323
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:55:13+00:00 2026-05-23T09:55:13+00:00

Sorry for the confusing title of the question. I am uncertain about how should

  • 0

Sorry for the confusing title of the question. I am uncertain about how should I implement ViewModels and Models which derive from a base class.

My ViewModel classes are based on a ViewModelBase, the ViewModelBase holds a ModelBase which serves as a base class for all other models.

Each ViewModel holds a “Model” Property, however, because a Model property was defined by the base ViewModel class as the ModelBase class, I always have to create another property which casts the Model – from ModelBase to the relevant Model class.

My question is – is there not a simpler solution? Is there a design pattern which is relevant for these issues?

Here’s a code sample:

public abstract class BasicViewModel : ViewModelBase
{
    public BasicViewModel()
    {
    }
    public ModelBase Model { get; set; }
}

public class ModelBase
{
}

public class ContainableViewModel : BasicViewModel
{
    public ContainableViewModel(ContainableModel model)
    {
        this.Model = model;
    }

    public ContainableModel MyModel { get { return (ContainableModel)Model; } }

    public int Children { get { return MyModel.Children; } set { MyModel.Children = value; } }
}

public class ContainableModel : ModelBase
{
    public ContainableModel()
    {
        Children = 2;
    }

    public int Children { get; set; }
}

As you can see, the “MyModel” property is the one which bugs me.

Thank you very much for your help and time!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T09:55:13+00:00Added an answer on May 23, 2026 at 9:55 am

    If each of your view model contains a Model property, you could use generics:

    public abstract class BasicViewModel<TModelType> : ViewModelBase
        where TModelType : ModelBase
    {
        public BasicViewModel(TModelType model)
        {
            Model = model;
        }
    
        public TModelType Model { get; set; }
    }
    
    public class ModelBase
    {
    }
    

    Your containable view model is now defined as follows:

    public class ContainableViewModel : BasicViewModel<ContainableModel>
    {
        public ContainableViewModel(ContainableModel model)
            : base(model)
        {
        }
    
        // you can now omit this method, it is defined on the abstract superclass
        //public ContainableModel Model { get { return ()Model; } }
    
        public int Children { get { return MyModel.Children; } set { MyModel.Children = value; } }
    }
    
    public class ContainableModel : ModelBase
    {
        public ContainableModel()
        {
            Children = 2;
        }
    
        public int Children { get; set; }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello everybody and sorry for the rather confusing question-title. I have a Hibernate Class
Sorry if the question title is confusing. Let me explain further. I am building
Sorry about the confusing title, I had a hard time describing the problem. I
Sorry about the confusing title. Are there any web IDEs available that can be
Sorry for the confusing title :P I'm sure I should know this but I
Sorry about the confusing title. I recently started doing this in my project, and
Hello and sorry for the confusing title of my question. My app assigns a
Sorry for the confusing title. Basically I made a BinaryReader class that reads in
Sorry for the confusing/vague question title. I couldn't come up with a better one.
I'm sorry for the little confusing title but say I have a class with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.