0

In my site I use a View Model for each page. Each View Model consists of properties only, nothing else. Several of the pages use the same group of properties. What I would like to do is create a single class for each group of properties and then use the relevant groups on each pages View Model.

example property groups:

public class GroupCar
{
    public string CarName { get; set; }
    public string CarColour { get; set; }
    public string CarLink { get; set; }
}

public class GroupSport
{
    public string SportName { get; set; }
    public string SportLocation { get; set; }
    public string SportLink { get; set; }
}

public class GroupFood
{
    public string FoodName { get; set; }
    public string FoodPrice { get; set; }
    public string FoodLink { get; set; }
}

Now in my View Model I would have several properties for this page and I would also like to use some properties from these groups.

I can Inherit one of the groups easily

public class VMMyPage : GroupCar
{
    //My Bespoke Properties
}

How do I inherit multiple groups though...something like:

public class VMMyPage : GroupCar, GroupSport, GroupFood
{
    //My Bespoke Properties
}

I know you can't do this in C# but is there a workaround? I've read several articles about using an interface class but there are no examples of exactly what I'm trying to achieve?

0

2 Answers 2

1

This is how you achieve it with interfaces. But with inheritance from an interface you have to implement the properties in your viewmodel class.

    public interface IGroupCar
    {
        string CarName { get; set; }
        string CarColour { get; set; }
        string CarLink { get; set; }
    }

    public interface IGroupSport
    {
        string SportName { get; set; }
        string SportLocation { get; set; }
        string SportLink { get; set; }
    }

    public interface IGroupFood
    {
        string FoodName { get; set; }
        string FoodPrice { get; set; }
        string FoodLink { get; set; }
    }

    public class VMMyPage : IGroupCar, IGroupSport, IGroupFood
    {
        public string CarName { get; set; }
        public string CarColour { get; set; }
        public string CarLink { get; set; }
        public string SportName { get; set; }
        public string SportLocation { get; set; }
        public string SportLink { get; set; }
        public string FoodName { get; set; }
        public string FoodPrice { get; set; }
        public string FoodLink { get; set; }
        // Your custom view model properties          
    }
4
  • Ok so if you use an interface you still have to write out all the properties again in the model?
    – Dan88
    Mar 11, 2016 at 10:50
  • Yes you do, the interface is just a signature class that you have to inherit all of it's properties.
    – James Dev
    Mar 11, 2016 at 10:53
  • Ok Thanks for the answer. Not sure interfaces really help me in this situation then. The idea was I only had to write the variables out once and then I can add, remove, update and use them all from one central location.
    – Dan88
    Mar 11, 2016 at 11:02
  • @HenkHolterman is right you have to do this with class inheritance.
    – James Dev
    Mar 11, 2016 at 11:04
0

In C#, the classes are only allowed to inherit from a single parent class.But you can use interfaces or a combination of one class and interface(s).

So,here you can store the properties in Interfaces and you can accomplish your logic.

To know more about Inheritance read

2
  • From the question of OP I don't think he really cares about having a contract whether or not the properties are available in them though, I think he just wants access to them without having to type/copy them all over again. Mar 11, 2016 at 10:32
  • Yes I don't want to write them out in every View Model. I want one central location where I can easily manage them across all the models at the same time.
    – Dan88
    Mar 11, 2016 at 10:48

Not the answer you're looking for? Browse other questions tagged or ask your own question.