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

  • Home
  • SEARCH
  • 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 766535
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T17:03:46+00:00 2026-05-14T17:03:46+00:00

My Model is a generic class that contains a (for example) Value property which

  • 0

My Model is a generic class that contains a (for example) Value property which can be int, float, string, bool, etc. So naturally this class is represented something like Model<T>. For the sake of collections Model<T> implements the interface IModel, although IModel is itself empty of any content.

My ViewModel contains and instance of Model<T> and it is passed in through ViewModel‘s constructor. I still want to know what T is in ViewModel, so when I expose Model to the View I know the datatype of Model‘s buried Value property. The class for ViewModel ends up looking like the following:

class ViewModel<T>
{
   private Model<T> _model;

   public ViewModel(Model<T> model) { ....blah.... }

   public T ModelsValue {get; set; }

}

This works fine, but is limited. So now I need to expose a collection of IModels with varying Ts to my View, so I’m trying to set up an ObservableCollection of new ViewModel<T>s to a changing list of IModels. The problem is, I can’t figure out how to get T from Model<T> from IModel to construct ViewModel<T>(Model<T>) at runtime.

In the VS2010 debugger I can mouseover any IModel object and see its full Model<int> for example at runtime so I know the data is in there.

Any ideas?

  • 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-14T17:03:47+00:00Added an answer on May 14, 2026 at 5:03 pm

    Here’s what I’m using for view model collections:

    Preface:

    Your view model objects can be weakly typed. Give IModel a property object Value {get;} and expose that in a ModelViewModel : ViewModel<IModel> that you use for all IModel objects (see my ViewModel<T> implementation below). If you have various combinations of ObservableCollection<IModel>, ICollection<Model<T>>, etc., the framework shown here is a lifesaver. If you still need generic view model, you can derive a ModelViewModel<T> : ModelViewModel that takes a Model<T> in its constructor. The logic to create the appropriate type would go in the converter passed to ViewModelCollection.Create below. Do be warned that this design will impose a performance penalty.

    ModelViewModel CreateModelViewModel(IModel model)
    {
        Type viewModelType = typeof(ModelViewModel<>).MakeGenericType(model.Type);
        ModelViewModel viewModel = Activator.CreateInstance(viewModelType, model);
        return viewModel;
    }
    

    Example usage:

    public class CatalogViewModel : ViewModel<ICatalog>
    {
        public CatalogViewModel(ICatalog catalog)
            : base(catalog)
        {
            Func<ICatalogProduct, ProductViewModel> viewModelFactory = CreateProductViewModel;
    
            this.Products = ViewModelCollection.Create(catalog.Products, viewModelFactory);
        }
    
        public ICollection<ProductViewModel> Products
        {
            get;
            private set;
        }
    
        private ProductViewModel CreateProductViewModel(ICatalogProduct product)
        {
            return new ProductViewModel(product, this);
        }
    }
    

    Benefits:

    • Uses lazy implementations to allow for efficient and even recursive bindings in trees.
    • The view model collections only implement INotifyCollectionChanged if the underlying model collection implements INotifyCollectionChanged.

    Overview of the classes (full implementations linked to github):

    • ViewModel<TModel>: Base class for my view model classes. Exposes a Model property that I use in the view model’s backing code.

    • ObservableViewModelCollection<TViewModel, TModel>: Lazy (actually not currently, but definitely should be), observable mapping from a model to a view model. Implements INotifyCollectionChanged.

    • ViewModelCollection<TViewModel, TModel>: Lazy mapping from a collection of TModel to a collection of TViewModel.

    • ViewModelCollection: Static helper – returns an ICollection<TViewModel>, using ObservableViewModelCollection<TViewModel, TModel> when the source collection implements INotifyCollectionChanged, otherwise using ViewModelCollection<TViewModel, TModel>.

    A few extra types that might be useful for your view model collections:

    ConcatCollection: Like ViewModelCollection, this includes a static helper to automatically choose an appropriate implementation. The ConcatCollection concatenates collections by binding directly to the source collection(s).

    • ConcatCollection
    • ConcatCollection<T>
    • ObservableConcatCollection<T>

    Here is an example of how I used this type to expose a Children property to the view while maintaining my observable collections all the way to back to the original source.

    public class ProductViewModel : ViewModel<IProduct>
    {
        public ProductViewModel(IProduct product)
            : base(product)
        {
            Func<IProduct, ProductViewModel> productViewModelFactory = CreateProductViewModel;
            Func<IRelease, ReleaseViewModel> releaseViewModelFactory = CreateReleaseViewModel;
    
            this.Products = ViewModelCollection.Create(product.Products, productViewModelFactory);
            this.Releases = ViewModelCollection.Create(product.Releases, releaseViewModelFactory);
            this.Children = ConcatCollection.Create<object>((ICollection)this.Products, (ICollection)this.Releases);
        }
    
        public IList<ProductViewModel> Products
        {
            get;
            private set;
        }
    
        public IList<ReleaseViewModel> Releases
        {
            get;
            private set;
        }
    
        public IEnumerable<object> Children
        {
            get;
            private set;
        }
    
        private ProductViewModel CreateProductViewModel(IProduct product)
        {
            return new ProductViewModel(product);
        }
    
        private ReleaseViewModel CreateReleaseViewModel(IRelease release)
        {
            return new ReleaseViewModel(release);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 420k
  • Answers 420k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Autocommit probably is on by default. With DataSourceTransactionManager, autocommit is… May 15, 2026 at 10:46 am
  • Editorial Team
    Editorial Team added an answer What a HORRENDOUS debug session.. well there's good news.. I… May 15, 2026 at 10:46 am
  • Editorial Team
    Editorial Team added an answer You will have problems with permissions if the image is… May 15, 2026 at 10:46 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.