Cross-platform inheritance of view models












0















So I have run to a problem while trying to follow the MVVM pattern for my WPF project and I spent hours searching for a solution with no luck. The problem is the following: Let's assume that I have a .net Standard library where I have all my view models for my application, for example let's assume that there are the following view models :




BaseDropDownMenuViewModel that inherits from the BaseControlViewModel



AdvancedDropDownMenuViewModel that inherits from the BaseDropDownMenuViewModel



MultiDropDownViewModel that inherits from the AdvancedDropDownViewModel




The view models contain both the functionality of the controls and variables used for their appear for example a BackColor and a ForeColor property that are placed in the BaseControlViewModel. Now ,even though this is sufficient enough for me, I want to add some properties that apply only for desktop platforms, for example a MouseOverBackColor and a MouseOverForeColor property and I also need to be able to override the existing ones ex BackColor. These properties of course would only make sense on desktop platform and not a mobile platform.



Now I have found two solutions for my problem but both of them are not sufficient enough...




  • Create a new .net standard library with an interface that forces the implementation of properties available only for desktop applications and create a class for every view model of the base library and implement that interface. In this scenario I will have a huge amount of duplicate code but I will be able to use these classes for my Windows version of my app and the mobile version will not have unused variables for every single one of its controls for example a MouseOverBackColor property that it can't take advantage of.


  • Which leads us to my second option which is to implement those properties (MouseOverBackColor,MouseOverForeColor,...) on the BaseControlViewModel on the base .net standard library and have all the versions of my app have those variables even though some might not be able to utilize them. That way I want have duplicate code...



I believe that this problem would be easy to solve if C# supported multi inheritance of classes but since it doesn't I am trying to find another "magic" way of solving this issue. Note: I have also searched about tricks to achieve "multi class inheritance" but those that I was interested in were storing the classes as variables in the main class, which none of this is interests me at the moment because the application would require a massive modification to take advantage of this trick...










share|improve this question























  • Why not make two Interfaces that the ViewModel both implements. They can have overlapping properties, no problem. So your Destop Interface will have one set of props visible and the Mobile one another but you won't have duped code ...

    – Fildor
    Nov 21 '18 at 12:00











  • I don't quite get what you are suggesting, could you elaborate a bit more please?

    – PapLabros
    Nov 21 '18 at 14:00











  • There is no multi inheritance, but you can implement more than one interface. So you can make IDesktopVM and IMobileVM and have MultiDropDownViewModel implement both. Then your respective clients will see the appropriate API but you do not have duplicate code, because it's actually the same class.

    – Fildor
    Nov 21 '18 at 16:46
















0















So I have run to a problem while trying to follow the MVVM pattern for my WPF project and I spent hours searching for a solution with no luck. The problem is the following: Let's assume that I have a .net Standard library where I have all my view models for my application, for example let's assume that there are the following view models :




BaseDropDownMenuViewModel that inherits from the BaseControlViewModel



AdvancedDropDownMenuViewModel that inherits from the BaseDropDownMenuViewModel



MultiDropDownViewModel that inherits from the AdvancedDropDownViewModel




The view models contain both the functionality of the controls and variables used for their appear for example a BackColor and a ForeColor property that are placed in the BaseControlViewModel. Now ,even though this is sufficient enough for me, I want to add some properties that apply only for desktop platforms, for example a MouseOverBackColor and a MouseOverForeColor property and I also need to be able to override the existing ones ex BackColor. These properties of course would only make sense on desktop platform and not a mobile platform.



Now I have found two solutions for my problem but both of them are not sufficient enough...




  • Create a new .net standard library with an interface that forces the implementation of properties available only for desktop applications and create a class for every view model of the base library and implement that interface. In this scenario I will have a huge amount of duplicate code but I will be able to use these classes for my Windows version of my app and the mobile version will not have unused variables for every single one of its controls for example a MouseOverBackColor property that it can't take advantage of.


  • Which leads us to my second option which is to implement those properties (MouseOverBackColor,MouseOverForeColor,...) on the BaseControlViewModel on the base .net standard library and have all the versions of my app have those variables even though some might not be able to utilize them. That way I want have duplicate code...



I believe that this problem would be easy to solve if C# supported multi inheritance of classes but since it doesn't I am trying to find another "magic" way of solving this issue. Note: I have also searched about tricks to achieve "multi class inheritance" but those that I was interested in were storing the classes as variables in the main class, which none of this is interests me at the moment because the application would require a massive modification to take advantage of this trick...










share|improve this question























  • Why not make two Interfaces that the ViewModel both implements. They can have overlapping properties, no problem. So your Destop Interface will have one set of props visible and the Mobile one another but you won't have duped code ...

    – Fildor
    Nov 21 '18 at 12:00











  • I don't quite get what you are suggesting, could you elaborate a bit more please?

    – PapLabros
    Nov 21 '18 at 14:00











  • There is no multi inheritance, but you can implement more than one interface. So you can make IDesktopVM and IMobileVM and have MultiDropDownViewModel implement both. Then your respective clients will see the appropriate API but you do not have duplicate code, because it's actually the same class.

    – Fildor
    Nov 21 '18 at 16:46














0












0








0








So I have run to a problem while trying to follow the MVVM pattern for my WPF project and I spent hours searching for a solution with no luck. The problem is the following: Let's assume that I have a .net Standard library where I have all my view models for my application, for example let's assume that there are the following view models :




BaseDropDownMenuViewModel that inherits from the BaseControlViewModel



AdvancedDropDownMenuViewModel that inherits from the BaseDropDownMenuViewModel



MultiDropDownViewModel that inherits from the AdvancedDropDownViewModel




The view models contain both the functionality of the controls and variables used for their appear for example a BackColor and a ForeColor property that are placed in the BaseControlViewModel. Now ,even though this is sufficient enough for me, I want to add some properties that apply only for desktop platforms, for example a MouseOverBackColor and a MouseOverForeColor property and I also need to be able to override the existing ones ex BackColor. These properties of course would only make sense on desktop platform and not a mobile platform.



Now I have found two solutions for my problem but both of them are not sufficient enough...




  • Create a new .net standard library with an interface that forces the implementation of properties available only for desktop applications and create a class for every view model of the base library and implement that interface. In this scenario I will have a huge amount of duplicate code but I will be able to use these classes for my Windows version of my app and the mobile version will not have unused variables for every single one of its controls for example a MouseOverBackColor property that it can't take advantage of.


  • Which leads us to my second option which is to implement those properties (MouseOverBackColor,MouseOverForeColor,...) on the BaseControlViewModel on the base .net standard library and have all the versions of my app have those variables even though some might not be able to utilize them. That way I want have duplicate code...



I believe that this problem would be easy to solve if C# supported multi inheritance of classes but since it doesn't I am trying to find another "magic" way of solving this issue. Note: I have also searched about tricks to achieve "multi class inheritance" but those that I was interested in were storing the classes as variables in the main class, which none of this is interests me at the moment because the application would require a massive modification to take advantage of this trick...










share|improve this question














So I have run to a problem while trying to follow the MVVM pattern for my WPF project and I spent hours searching for a solution with no luck. The problem is the following: Let's assume that I have a .net Standard library where I have all my view models for my application, for example let's assume that there are the following view models :




BaseDropDownMenuViewModel that inherits from the BaseControlViewModel



AdvancedDropDownMenuViewModel that inherits from the BaseDropDownMenuViewModel



MultiDropDownViewModel that inherits from the AdvancedDropDownViewModel




The view models contain both the functionality of the controls and variables used for their appear for example a BackColor and a ForeColor property that are placed in the BaseControlViewModel. Now ,even though this is sufficient enough for me, I want to add some properties that apply only for desktop platforms, for example a MouseOverBackColor and a MouseOverForeColor property and I also need to be able to override the existing ones ex BackColor. These properties of course would only make sense on desktop platform and not a mobile platform.



Now I have found two solutions for my problem but both of them are not sufficient enough...




  • Create a new .net standard library with an interface that forces the implementation of properties available only for desktop applications and create a class for every view model of the base library and implement that interface. In this scenario I will have a huge amount of duplicate code but I will be able to use these classes for my Windows version of my app and the mobile version will not have unused variables for every single one of its controls for example a MouseOverBackColor property that it can't take advantage of.


  • Which leads us to my second option which is to implement those properties (MouseOverBackColor,MouseOverForeColor,...) on the BaseControlViewModel on the base .net standard library and have all the versions of my app have those variables even though some might not be able to utilize them. That way I want have duplicate code...



I believe that this problem would be easy to solve if C# supported multi inheritance of classes but since it doesn't I am trying to find another "magic" way of solving this issue. Note: I have also searched about tricks to achieve "multi class inheritance" but those that I was interested in were storing the classes as variables in the main class, which none of this is interests me at the moment because the application would require a massive modification to take advantage of this trick...







c# inheritance mvvm cross-platform






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 '18 at 11:06









PapLabrosPapLabros

312




312













  • Why not make two Interfaces that the ViewModel both implements. They can have overlapping properties, no problem. So your Destop Interface will have one set of props visible and the Mobile one another but you won't have duped code ...

    – Fildor
    Nov 21 '18 at 12:00











  • I don't quite get what you are suggesting, could you elaborate a bit more please?

    – PapLabros
    Nov 21 '18 at 14:00











  • There is no multi inheritance, but you can implement more than one interface. So you can make IDesktopVM and IMobileVM and have MultiDropDownViewModel implement both. Then your respective clients will see the appropriate API but you do not have duplicate code, because it's actually the same class.

    – Fildor
    Nov 21 '18 at 16:46



















  • Why not make two Interfaces that the ViewModel both implements. They can have overlapping properties, no problem. So your Destop Interface will have one set of props visible and the Mobile one another but you won't have duped code ...

    – Fildor
    Nov 21 '18 at 12:00











  • I don't quite get what you are suggesting, could you elaborate a bit more please?

    – PapLabros
    Nov 21 '18 at 14:00











  • There is no multi inheritance, but you can implement more than one interface. So you can make IDesktopVM and IMobileVM and have MultiDropDownViewModel implement both. Then your respective clients will see the appropriate API but you do not have duplicate code, because it's actually the same class.

    – Fildor
    Nov 21 '18 at 16:46

















Why not make two Interfaces that the ViewModel both implements. They can have overlapping properties, no problem. So your Destop Interface will have one set of props visible and the Mobile one another but you won't have duped code ...

– Fildor
Nov 21 '18 at 12:00





Why not make two Interfaces that the ViewModel both implements. They can have overlapping properties, no problem. So your Destop Interface will have one set of props visible and the Mobile one another but you won't have duped code ...

– Fildor
Nov 21 '18 at 12:00













I don't quite get what you are suggesting, could you elaborate a bit more please?

– PapLabros
Nov 21 '18 at 14:00





I don't quite get what you are suggesting, could you elaborate a bit more please?

– PapLabros
Nov 21 '18 at 14:00













There is no multi inheritance, but you can implement more than one interface. So you can make IDesktopVM and IMobileVM and have MultiDropDownViewModel implement both. Then your respective clients will see the appropriate API but you do not have duplicate code, because it's actually the same class.

– Fildor
Nov 21 '18 at 16:46





There is no multi inheritance, but you can implement more than one interface. So you can make IDesktopVM and IMobileVM and have MultiDropDownViewModel implement both. Then your respective clients will see the appropriate API but you do not have duplicate code, because it's actually the same class.

– Fildor
Nov 21 '18 at 16:46












1 Answer
1






active

oldest

votes


















0














You can do this with interfaces:



Example - One class, two interfaces



public interface IDesktopProperties
{
Color BackColor {get; set;}
Color MouseOverBackColor {get; set;}
}

public interface IMobileProperties
{
Color BackColor {get; set;}
Orientation ScreenOrientation {get; set;}
}

public class MySuperFancyViewModel : IDesktopProperties, IMobileProperties
{
public Color BackColor {get; set;}
public Color MouseOverBackColor {get; set;}
public Orientation ScreenOrientation {get; set;}
}


Now in your Desktop App, you'll have something like



// Will only see properties BackColor and MouseOverBackColor
private IDesktopProperties myProps;
public MyDesktopUI( IDesktopProperties properties ) // <= injected: MySuperFancyViewModel
{
myProps = properties;
}


And accordingly for Mobile



// Will only see properties BackColor and ScreenOrientation
private IMobileProperties myProps;
public MyMobileUI( IMobileProperties properties ) // <= injected: MySuperFancyViewModel
{
myProps = properties;
}





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53410779%2fcross-platform-inheritance-of-view-models%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    You can do this with interfaces:



    Example - One class, two interfaces



    public interface IDesktopProperties
    {
    Color BackColor {get; set;}
    Color MouseOverBackColor {get; set;}
    }

    public interface IMobileProperties
    {
    Color BackColor {get; set;}
    Orientation ScreenOrientation {get; set;}
    }

    public class MySuperFancyViewModel : IDesktopProperties, IMobileProperties
    {
    public Color BackColor {get; set;}
    public Color MouseOverBackColor {get; set;}
    public Orientation ScreenOrientation {get; set;}
    }


    Now in your Desktop App, you'll have something like



    // Will only see properties BackColor and MouseOverBackColor
    private IDesktopProperties myProps;
    public MyDesktopUI( IDesktopProperties properties ) // <= injected: MySuperFancyViewModel
    {
    myProps = properties;
    }


    And accordingly for Mobile



    // Will only see properties BackColor and ScreenOrientation
    private IMobileProperties myProps;
    public MyMobileUI( IMobileProperties properties ) // <= injected: MySuperFancyViewModel
    {
    myProps = properties;
    }





    share|improve this answer




























      0














      You can do this with interfaces:



      Example - One class, two interfaces



      public interface IDesktopProperties
      {
      Color BackColor {get; set;}
      Color MouseOverBackColor {get; set;}
      }

      public interface IMobileProperties
      {
      Color BackColor {get; set;}
      Orientation ScreenOrientation {get; set;}
      }

      public class MySuperFancyViewModel : IDesktopProperties, IMobileProperties
      {
      public Color BackColor {get; set;}
      public Color MouseOverBackColor {get; set;}
      public Orientation ScreenOrientation {get; set;}
      }


      Now in your Desktop App, you'll have something like



      // Will only see properties BackColor and MouseOverBackColor
      private IDesktopProperties myProps;
      public MyDesktopUI( IDesktopProperties properties ) // <= injected: MySuperFancyViewModel
      {
      myProps = properties;
      }


      And accordingly for Mobile



      // Will only see properties BackColor and ScreenOrientation
      private IMobileProperties myProps;
      public MyMobileUI( IMobileProperties properties ) // <= injected: MySuperFancyViewModel
      {
      myProps = properties;
      }





      share|improve this answer


























        0












        0








        0







        You can do this with interfaces:



        Example - One class, two interfaces



        public interface IDesktopProperties
        {
        Color BackColor {get; set;}
        Color MouseOverBackColor {get; set;}
        }

        public interface IMobileProperties
        {
        Color BackColor {get; set;}
        Orientation ScreenOrientation {get; set;}
        }

        public class MySuperFancyViewModel : IDesktopProperties, IMobileProperties
        {
        public Color BackColor {get; set;}
        public Color MouseOverBackColor {get; set;}
        public Orientation ScreenOrientation {get; set;}
        }


        Now in your Desktop App, you'll have something like



        // Will only see properties BackColor and MouseOverBackColor
        private IDesktopProperties myProps;
        public MyDesktopUI( IDesktopProperties properties ) // <= injected: MySuperFancyViewModel
        {
        myProps = properties;
        }


        And accordingly for Mobile



        // Will only see properties BackColor and ScreenOrientation
        private IMobileProperties myProps;
        public MyMobileUI( IMobileProperties properties ) // <= injected: MySuperFancyViewModel
        {
        myProps = properties;
        }





        share|improve this answer













        You can do this with interfaces:



        Example - One class, two interfaces



        public interface IDesktopProperties
        {
        Color BackColor {get; set;}
        Color MouseOverBackColor {get; set;}
        }

        public interface IMobileProperties
        {
        Color BackColor {get; set;}
        Orientation ScreenOrientation {get; set;}
        }

        public class MySuperFancyViewModel : IDesktopProperties, IMobileProperties
        {
        public Color BackColor {get; set;}
        public Color MouseOverBackColor {get; set;}
        public Orientation ScreenOrientation {get; set;}
        }


        Now in your Desktop App, you'll have something like



        // Will only see properties BackColor and MouseOverBackColor
        private IDesktopProperties myProps;
        public MyDesktopUI( IDesktopProperties properties ) // <= injected: MySuperFancyViewModel
        {
        myProps = properties;
        }


        And accordingly for Mobile



        // Will only see properties BackColor and ScreenOrientation
        private IMobileProperties myProps;
        public MyMobileUI( IMobileProperties properties ) // <= injected: MySuperFancyViewModel
        {
        myProps = properties;
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 21 '18 at 16:58









        FildorFildor

        7,19232247




        7,19232247
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53410779%2fcross-platform-inheritance-of-view-models%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            How to send String Array data to Server using php in android

            Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

            Is anime1.com a legal site for watching anime?