Design : best practice to expose a CX ref class to Model ( which is in standard C++ ) ?? RRS feed

  • Question

  • Hi,

    i have a view class and view-model class associated with it ( i am following the standard MVVM pattern)

    the view and view-model are connected through the know "binding" practice.

    the problem i have is how to connect the Model to the view-model ? 

    i have my app-logic entirely in C++ ( so that i can reuse it in another platform) and i need some way to connect the

    view-model and its corresponding Model object so that they could communicate with each other (through function calls and notifications ) 

    i have built my own notification framework say , myNotificationClass, and the model uses this framework to publish the changes happening in it.

    my question is what is the best practice followed in the such common situation by the view-model to subscribe to the model changes ? so as to keep the model independent from the C++/CX environment.

    i tried the following - 

    typedef std::function<void()> MyModelHandlerFunc_t;
    class myModel
       void AddHandler(MyModelHandlerFunc_t handler);
       void Notify()
        MyModelHandlerFunc_t clientHandler;
    public ref class myViewModelClient : public //some winRT class{
            void myHandler();
            void SetUpModelHandler(myModel *model)
             MyModelHandlerFunc_t handler = std::bind(&myHandler , this);

    so that the Model just sees 


    from its side.

    but the above code is not compiling ( i expected this ) as we can't bind the C++/cx ref-class member function like 

    MyModelHandlerFunc_t handler = std::bind(&myHandler , this);

    so, my question is what is practice followed for such scenarios ?

    how to "hide" the view-model details (meaning, try expose the ref class to the Model as normal C++ class object ) from the Model and make things work transparently b/w  Model and view-model ? 


    • Edited by naiveCoder Monday, December 2, 2013 12:44 PM
    Monday, December 2, 2013 12:42 PM


  • i seem to have figured out one solution.

    in the view model - we can do the following 

    void ViewModel::RegisterForModelNotification() {     WeakReference wr(this);    auto notifHandler = [wr](){           ViewModel^ vm = wr.Resolve<ViewModel>();         

    vm->HandleModelNotification();        }  Model->RegisterObserver(notifHandler); } 


    • Marked as answer by naiveCoder Thursday, December 5, 2013 1:16 PM
    Thursday, December 5, 2013 1:16 PM

All replies