Search Unity

MVVM - is the best pattern ever!

Discussion in 'General Discussion' started by Art Of Bytes, May 25, 2012.

  1. Art Of Bytes

    Art Of Bytes

    Joined:
    Jun 8, 2011
    Posts:
    198
    Hi guys,

    As you probably know, we've created few MVVM frameworks for different UI libraries for Unity.
    And here I want to tell you about pattern itself. Believe me, I try to promote it not because we are selling this tools on AssetStore, vise versa, we created this tools because MVVM is so cool!

    We tried this pattern first when we worked with WPF, loved it very much, and when we started to implement our games with Unity and its UI libs, we cannot live without MVVM databindings and game logic separations from UI implementation. That's why we decided to develop our own MVVM implementation for EZGUI to use it in our project. It worked perfectly and we've decided that it could be useful for others and published EZData first, then NData and iData.

    Probably it sounds inconvenient to get familiar with some patterns if everything works as it is. But I'm sure, that everybody who has understood it and tried it, would use it for every project ever (until something even better appear :)

    We've created and tried to explain how it works on iGUI sliders examples, but it is applied to any other UI:
    http://tools.artofbytes.com/idata/idata-tutorials/multiple-sliders-bound-to-single-value

    If you've got questions, comments or stories of your MVVM usage, please, share them here. The main goal of this post is to show how powerful this pattern can be and how useful it in every project.
     
  2. HenryV

    HenryV

    Joined:
    Apr 25, 2012
    Posts:
    100
    I read about this briefly when I came across the NData framework for NGUI. Never really heard about it before though I am somewhat familiar with the Model-View-Controller pattern (mostly from web-frameworks).

    Is it for UI stuff only?
     
  3. Art Of Bytes

    Art Of Bytes

    Joined:
    Jun 8, 2011
    Posts:
    198
  4. wccrawford

    wccrawford

    Joined:
    Sep 30, 2011
    Posts:
    2,039
    Do you have an example of this that provides all the source code? I'd heard of MVVM before, but never really looked into it. All the description sounds good, but I'm not getting a clear picture in my head of how I'd implement it.
     
  5. nilton_felicio

    nilton_felicio

    Joined:
    May 17, 2012
    Posts:
    66
    +1.
    I also can not understand how your product works.
    I would like to see a tutorial with an implementation.
    It creates a database to save and load games?
    If yes, I am interested in NDATA
     
  6. Art Of Bytes

    Art Of Bytes

    Joined:
    Jun 8, 2011
    Posts:
    198
    @wccrawford, yes, sure, you can look at NData basic tutorial, NData Checkbox tutorial iData basic tutorial
    Every of them includes screenshot of MonoDevelop with ViewModel.cs class. This is the full implementation, and the only code you need - no event handling or other data processing - everything is done automatically!
    For sliders example from initial post the only code you need, and it is generated from snippets(see snippets usage in Basic Tutorials):
    Code (csharp):
    1. using UnityEngine;
    2. using System;
    3.  
    4. public class iDataExamplesUi : EZData.Context
    5. {
    6.    
    7.     #region Property SampleFloat
    8.     private readonly EZData.Property<float> _privateSampleFloatProperty = new EZData.Property<float>();
    9.     public EZData.Property<float> SampleFloatProperty { get { return _privateSampleFloatProperty; } }
    10.     public float SampleFloat
    11.     {
    12.     get    { return SampleFloatProperty.GetValue();    }
    13.     set    { SampleFloatProperty.SetValue(value); }
    14.     }
    15.     #endregion
    16. }
    17.  
    18. public class iDataExamplesViewModel : MonoBehaviour
    19. {
    20.     public iGuiRootContext View;
    21.     public iDataExamplesUi Context;
    22.    
    23.     void Awake()
    24.     {
    25.         Context = new iDataExamplesUi();
    26.         View.SetContext(Context);
    27.     }
    28. }
    29.  
    To EZData, NData and iData packages included scenes with working demo, that shows several bindings. Additionally, iData includes 14 simple examples scenes.
     
  7. Art Of Bytes

    Art Of Bytes

    Joined:
    Jun 8, 2011
    Posts:
    198
    We've got 2 NData tutorials at the moment - http://tools.artofbytes.com/ndata/tutorials

    They include step-by-step instructions how to use our framework. All code implementation is shown on screenshots and you don't even have to write it yourself (you can just use templates with snippets in MonoDevelop).

    That's exactly what we like MVVM for - no handling code is needed, thanks to DataBinding.

    I'm sorry, but I haven't understood this question, can you explain more concrete?

    Cheers and thanks for your interest, guys!
     
  8. nilton_felicio

    nilton_felicio

    Joined:
    May 17, 2012
    Posts:
    66
    Sorry for my English "google". hahahhh
    Sometimes it is difficult to explain.

    NDATA "manages", "creates" ..... Database as the game preferences.
    How to "Save Easy" from http://www.moodkie.com.
    Or "MonoSQLite" of http://www.monosapiens.com.br

    I hope I have explained achieved.
     
    Last edited: May 25, 2012
  9. Art Of Bytes

    Art Of Bytes

    Joined:
    Jun 8, 2011
    Posts:
    198
    NData is not about game preferences storage, no external databases are created, it is about internal data management and handling
     
  10. Art Of Bytes

    Art Of Bytes

    Joined:
    Jun 8, 2011
    Posts:
    198
    Not necessary, you can bind pretty much anything to dependency properties and collections. In general MVVM pattern is useful when you have some complex representation to user that is completely defined by some relatively simple internal state. That's quite a common task, usually when you have some text input it absolutely doesn't matter in your logic what's the size and the color of the font in the text input, or whether it's password field of regular text field.

    Here's a non-UI example - game character health value. When you apply some damage or healing to the character, you don't care whether health bar is displayed as an actual bar above the character or as a numbers in HUD or whether character has some animation when it's low on health. In the core of your game health would be represented by a single numeric value, and that's it, everything else (health bars, HUD, animations, etc.) is a view, no matter how fancy it is - it is completely defined by a single value. And that's the key idea - to find the minimal state that would from one side be enough to do your game logic, and from the other side be 100% defining for how the game is visible to the player. When such state is found, you can build all your game mechanics absolutely separately from creating a view.

    Benefits are obvious - less code coupling, cleaner game logic and UI code, better defined application behavior. Usual argument against it - is that more abstraction levels add more complexity, require more code and are harder to support, but with power of .NET it becomes possible to achieve this for free by automating data transfers between abstraction levels and make it even more compacts than usual ways to do that (especially when the view is much more complex than the underlying state).
     
  11. MonoSapiens

    MonoSapiens

    Joined:
    Apr 12, 2012
    Posts:
    79
    GREAT! If you want to know more about MonoSQLite, visit our BLOG and subscribe to our FORUM.

    Thanks,

    Eduardo Capanema
    Mono Sapiens