0

I have model class and interface:

public interface IEntity
{
    int ID { get; set; }
}

public class Entity : IEntity
{
    public int ID { get; set; }
}

I have ViewModel interface:

public interface IEntityListViewModel
{
    RangeObservableCollection<IEntity> Items { get; set; }
    IEntity SelectedItem { get; set; }

    void LoadItems();
}

I need a ViewModel base class:

public abstract class EntityListViewModel<T> : IEntityListViewModel where T :  IEntity
{
    public RangeObservableCollection<T> Items { get; set; }
    public T SelectedItem { get; set; }

    public EntityListViewModel()
    {
        Items = new RangeObservableCollection<T>();
    }

    protected abstract List<T> GetEntities();

    public void LoadItems()
    {
        var lst = GetEntities();
        Items.ReplaceRange(lst);
    }
}

Of course, the compiler requires the implementation of IEntityListViewModel.Items!

I could do:

public interface IEntityListViewModel<T> where T : IEntity
{
    RangeObservableCollection<T> Items { get; set; }
    T SelectedItem { get; set; }

    void LoadItems();
}

But I have another class:

public abstract class UserControlBase : UserControl
{
    public IEntityListViewModel VM { get; set; }        

    public virtual void UserControl_Loaded(object sender, RoutedEventArgs e)
    {
        VM.LoadItems();
    }        
}

I need UserControlBase because my ViewModels have many events, and in UserControlBase I want to subscribe to them. I could do:

public IEntityListViewModel<T> VM { get; set; }

but then I must have UserControlBase<T>. I do not like this.

How do I inherit EntityListViewModel<T> from IEntityListViewModel? Or is there some other solution?

2 Answers 2

1

You could do something like this

RangeObservableCollection<IEntity> IEntityListViewModel.Items
    {
        get
        {
            return new RangeObservableCollection<IEntity>(Items.Cast<IEntity>());
        }

        set
        {
            Items = new RangeObservableCollection<T>(value.Cast<T>());
        }
    }

    IEntity IEntityListViewModel.SelectedItem
    {
        get
        {
            return SelectedItem;
        }

        set
        {
            SelectedItem = (T)value;
        }
    }

To avoid having to create new RangeObservableCollection each time you would need to change to IEnumerable<T>. The other problem with this is that it might get expensive to do the cast to and from all the time. Also you did not mention which collection will be bound to on the front end (I am assuming that is what you are going to be doing), based on that some optimizations could be made.

Sign up to request clarification or add additional context in comments.

Comments

0

I'm a bit confuse of this question but I'll try to throw my answer.

How do I inherit EntityListViewModel from IEntityListViewModel

Based on the code I think this is what you want I guess

public abstract class EntityListViewModel<T> where T : IEntity, EntityListViewModel

This is the most simplest way to achieve your goal without forcing you to implement generic in UserControlBase class.

2 Comments

I haven't class EntityListViewModel. What is it?
That's the interface IEntityListViewModel, typo :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.