Adrian
Adrian

Reputation: 3438

Abstracting a class with generics

I have been learning Generics and have some pretty good understanding of it. Basically it seems that generics are a placeholder for a certain type to be determined at run time. Generally the letter T designates a generic but it is not limited to just that letter/name.

So re-inventing the wheel I can have a can have a method that can convert any type of list to an array like so

public static T[] ConvertListToArray<T>(List<T> list) {
    int count = list.Count;
    T[] array = new T[count];
    for (int i = 0; i < count; i++)
        array[i] = list[i];
    return array;
}

Now I am trying to put this to use inside a base class I want to use for all my viewmodels. Let's say I have an interface named IModelViewModel (a viewmodel for any EF model class)

public interface IModelViewModel<T>
{
    T Model { get; set; }

    bool IsNewRecord { get; set; }

    void ConvertModelToViewModel();

    void UpdateModelFromViewModel();

    void SaveToDb();

    void UpdateInDb();
}  

My logic is, any viewmodel that is created to represent a direct poco from EF must have a Model property to hold the original EF Poco, a boolean indicating if it is a new record to be added, a convert to ViewModel method (which basically fills this viewmodel's properties via the model, an UpdateModelFromViewModel method that update's the model from the view-bounded viewmodel, a save method and an update method. The T in this case is meant to abstract the interface so that it can hold any poco I put into it.

Now here is the base class.

public class ModelViewModelBase<T> : DependencyObject, Interfaces.IModelViewModel<T>
{
    public T Model { get; set; }
    public bool IsNewRecord { get; set; }

    public ModelViewModelBase(T model)
    {
        if (model == null || model.id == 0)
        {

            this.IsNewRecord = true;
            Model = new T();
        }
        else
        {
            this.IsNewRecord = false;
        }
    }


    public void ConvertModelToViewModel()
    {
        throw new NotImplementedException();
    }

    public void UpdateModelFromViewModel()
    {
        throw new NotImplementedException();
    }

    public void SaveToDb()
    {
        throw new NotImplementedException();
    }

    public void UpdateInDb()
    {
        throw new NotImplementedException();
    }
}  

The idea is: every ModelViewModel child class must have an EF poco object as it's model property. When the class is constructed, the boolean IsNewRecord must be set according to the state of the EF Poco. The class itself is not working due to the fact that I am not using generics 100% correct. Is this even possible what I am doing? If not, what would be the best way to get this functionality?

Upvotes: 1

Views: 94

Answers (1)

manojlds
manojlds

Reputation: 301137

You need to add contraints:

public class ModelViewModelBase<T> : DependencyObject, IModelViewModel<T> where T:Model, new()

(assuming Model is the base class for your models)

Upvotes: 2

Related Questions