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

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 { public: void AddHandler(MyModelHandlerFunc_t handler); void Notify() { clientHandler(); } private: MyModelHandlerFunc_t clientHandler; } public ref class myViewModelClient : public //some winRT class{ internal: void myHandler(); void SetUpModelHandler(myModel *model) { MyModelHandlerFunc_t handler = std::bind(&myHandler , this); model->AddHandler(handler); } }
so that the Model just sees
MyModelHandlerFunc_t
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 ?
Raj
- Edited by naiveCoder Monday, December 2, 2013 12:44 PM
Monday, December 2, 2013 12:42 PM
Answers
-
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); }
Raj
- Marked as answer by naiveCoder Thursday, December 5, 2013 1:16 PM
Thursday, December 5, 2013 1:16 PM
All replies
-
I am not following what you are trying to do really but perhaps this will help. Your object should support INotifyPropertyChanged.
This is an overview of the architecture: http://msdn.microsoft.com/en-us/library/hh821028.aspx
and this should help too: http://www.bing.com/search?q=model+view+viewmodel
Jeff Sanders (MSFT)
@jsandersrocks - Windows Store Developer Solutions @WSDevSol
Getting Started With Windows Azure Mobile Services development? Click here
Getting Started With Windows Phone or Store app development? Click here
My Team Blog: Windows Store & Phone Developer Solutions
My Blog: Http Client Protocol Issues (and other fun stuff I support)Tuesday, December 3, 2013 8:41 PMModerator -
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); }
Raj
- Marked as answer by naiveCoder Thursday, December 5, 2013 1:16 PM
Thursday, December 5, 2013 1:16 PM