Tuesday, April 28, 2015

Crosstalk between modules : Query providers and Event Managers

*Pub-sub mechanism in an LOB application  
 //Account class  
 AccountModule.AccountSearchVM  
 {  
 //property  
      public AccountViewModel SelectedAccount  
      {  
           get...  
           set  
           {  
                //other code here  
                //raise the event when account is selected  
                SelectedAccountEventArgs accountSelectedEvent = new SelectedAccountEventArgs();  
                EventManager.Current.PublishEvent<SelectedAccountEventArgs>(this, accountSelectedEvent);  
           }  
      }  
 }  
 //Positions class (show positions in right pane for selected account in the left pane [in AccountModule]  
 PositionsModule.PositionsVM  
 {  
      //ctor  
      PositionsVM()  
      {  
           ApplicationContext.Current.SubscribeToEvent<SelectedAccountEventArgs>(this, SelectedAccountChanged);  
           //Do something with SelectedAccountChanged method then, make use of SelectedAccountEventArgs  
      }  
 }  
--------------------------------------------------------
//QueryProviders  
 //This code is in Framework module  
 public void RegisterQueryProvider<QueryType>(QueryType query) where QueryType : IQueryProvider  
 {  
      Type type = typeof(QueryType);  
      //QueryManager is a singleton class which manages a dictionary of Dictionary<Type, IQueryProvider>  
      //instance is then added  
      //internal bool AddQuery<QueryType>(QueryType query) where QueryType : IQueryProvider  
   /* {  
           bool ret = false;  
           Type type = typeof(QueryType);  
           if (!m_queries.ContainsKey(type))  
           {  
                m_queries.Add(type, query);  
                ret = true;  
           }  
           return ret;  
      } */  
      if (!QueryManager.Current.AddQuery<QueryType>(query))  
      {  
           throw new Exception("Query is already registered!!.");  
      }  
 }  
 //inside Plugin initialze method, pass the instance of the view model  
 apphost.RegisterQueryProvider<ISelectedAccountQuery>(m_viewModel);