[tasque/viewmodel: 68/78] Implement Object Service feature



commit 530c8919f113b948af61ea22f38555834168ddc5
Author: Antonius Riha <antoniusriha gmail com>
Date:   Fri Aug 10 00:17:39 2012 +0200

    Implement Object Service feature
    
    Sometimes a situation occurs where objects which are totally alike
    in behavior and state need to be used by other objects and are
    therefore instantiated by those other classes. It then is somewhat
    cumbersome to instantiate these objects again and again although
    nobody actually needs that many instances. One instance would suffice.
    One approach to solve this would be to apply the Singleton pattern.
    Doing that has big implications (see GoF book) and possibly a disturbing impact on future development. The singleton pattern
    approach also doesn't fully match the problem: The objects in
    question may not be single to the whole application but only to a
    certain subtree of the UI tree structure.
    To provide a solution to this I introduce "Object Service" with this commit: Descendants of a UI subtree ask their ancestors, if they can
    provide them with an object of e specified type. Any object in the
    tree can provide this service by registering objects to the Object
    Service.

 src/libtasqueui/Legacy/MainWindowModel.cs |    3 ++
 src/libtasqueui/ViewModel.cs              |   31 +++++++++++++++++++++++++++++
 2 files changed, 34 insertions(+), 0 deletions(-)
---
diff --git a/src/libtasqueui/Legacy/MainWindowModel.cs b/src/libtasqueui/Legacy/MainWindowModel.cs
index 4e46f22..dccc02a 100644
--- a/src/libtasqueui/Legacy/MainWindowModel.cs
+++ b/src/libtasqueui/Legacy/MainWindowModel.cs
@@ -38,6 +38,9 @@ namespace Tasque.UIModel.Legacy
 			if (preferences == null)
 				throw new ArgumentNullException ("preferences");
 			
+			// register objects in object service
+			AddObjectToObjectService (new DueDateOptionsModel (this));
+			
 			UpdateCompletionDateRangeCompareDates ();
 			
 			Preferences = preferences;
diff --git a/src/libtasqueui/ViewModel.cs b/src/libtasqueui/ViewModel.cs
index 90c14e9..368dcca 100644
--- a/src/libtasqueui/ViewModel.cs
+++ b/src/libtasqueui/ViewModel.cs
@@ -24,6 +24,7 @@
 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 // THE SOFTWARE.
 using System;
+using System.Collections.Generic;
 using System.Collections.ObjectModel;
 using System.ComponentModel;
 using CrossCommand;
@@ -73,6 +74,35 @@ namespace Tasque.UIModel
 			Dispose (false);
 		}
 
+		
+		#region Object Service
+		//DOCS: Provides a service for descendants to retrieve an object instance
+		// of a class, which has a one-to-one relation with the MainWindow, though
+		// it is used multiple times by various descendants (better solution appreciated).
+		protected object GetObjectFromAncestor (Type objectType)
+		{
+			if (registeredObjects.ContainsKey (objectType))
+				return registeredObjects [objectType];
+			
+			return parent == null ? null : parent.GetObjectFromAncestor (objectType);
+		}
+		
+		protected void AddObjectToObjectService (object obj)
+		{
+			if (obj == null)
+				throw new ArgumentNullException ("obj");
+			
+			if (registeredObjects == null)
+				registeredObjects = new Dictionary<Type, object> ();
+			registeredObjects.Add (obj.GetType (), obj);
+		}
+		
+		protected bool RemoveObjectFromObjectService (object obj)
+		{
+			return obj == null ? false : registeredObjects.Remove (obj);
+		}
+		#endregion
+		
 		public event PropertyChangedEventHandler PropertyChanged;
 
 		protected virtual void OnPropertyChanged (string propertyName)
@@ -90,5 +120,6 @@ namespace Tasque.UIModel
 		Collection<ViewModel> children;
 		RelayCommand close;
 		ViewModel parent;
+		Dictionary<Type, object> registeredObjects;
 	}
 }



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]