Click here to Skip to main content
15,881,172 members
Articles / Programming Languages / C#

Romeo and Juliet

Rate me:
Please Sign up or sign in to vote.
4.87/5 (34 votes)
3 Dec 2011CPOL11 min read 58.1K   413   67  
Making relationships first class citizens.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

using Clifton.Tools.Strings.Extensions;
using Clifton.Windows.Forms;
using Clifton.Windows.Forms.XmlTree;

using XTreeInterfaces;

namespace XTreeDemo
{
	// Used in the tree definition XML to provide a class at the terminal point of a hierarchy.
	public class NullInstance : IHasCollection
	{
		public string Name { get; set; }
		public Dictionary<string, dynamic> Collection { get { return null; } }
	}

	// Other examples:
	// SchemaDef has a collection of TableDef objects.
	// The parent controller of a TableDef object is the GenericController<SchemaDef, TableDef>

	/// <summary>
	/// Generic controller for a backing class that implements a single collection to which child nodes can be added.
	/// </summary>
	/// <typeparam name="T">Backing class that implements the instance.  For example, TableDef</typeparam>
	/// <typeparam name="U">The collection that this controller manages, for example, TableFieldDef</typeparam>
	public class GenericController<T> : 
		XtreeNodeController, IGenericController
		where T : IHasCollection, new()
	{
		public T Instance { get; set; }

		public override string Name { get; set; }

		public override object Item
		{
			get { return Instance; }
			set { Instance = (T)value; }
		}

		public Dictionary<string, dynamic> Collection { get { return Instance.Collection; } }

		public string GenericTypeName
		{
			get
			{
				// Example: XTreeDemo.GenericController`1[[ROPLib.Entity, ROPLib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]
				string fulltype = this.GetType().FullName;
				string typeName = fulltype.RightOf('.').Between('.', ',');

				return typeName;
			}
		}

		public GenericController()
		{
			Instance = new T();
		}

		public GenericController(bool createInstance)
		{
			if (createInstance)
			{
				Instance = new T();
			}
		}

		public override int Index(object item)
		{
			return Instance.Collection[GenericTypeName].IndexOf((T)item);
		}

		public override bool AddNode(IXtreeNode parentInstance, string tag)
		{
			IGenericController ctrl = (IGenericController)parentInstance;
			ctrl.Collection[GenericTypeName].Add(Instance);

			return true;
		}

		public override bool DeleteNode(IXtreeNode parentInstance)
		{
			// TODO: Inject the ability to confirm the delete operation.

			IGenericController ctrl = (IGenericController)parentInstance;
			ctrl.Collection[GenericTypeName].Remove(Instance);

			return true;
		}

		public override bool AutoDeleteNode(IXtreeNode parentInstance)
		{
			IGenericController ctrl = (IGenericController)parentInstance;
			ctrl.Collection[GenericTypeName].Remove(Instance);

			return true;
		}

		public override void InsertNode(IXtreeNode parentInstance, int idx)
		{
			throw new NotImplementedException();
		}

		public override void Select(TreeNode tn)
		{
			Program.Properties.SelectedObject = Instance;
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect Interacx
United States United States
Blog: https://marcclifton.wordpress.com/
Home Page: http://www.marcclifton.com
Research: http://www.higherorderprogramming.com/
GitHub: https://github.com/cliftonm

All my life I have been passionate about architecture / software design, as this is the cornerstone to a maintainable and extensible application. As such, I have enjoyed exploring some crazy ideas and discovering that they are not so crazy after all. I also love writing about my ideas and seeing the community response. As a consultant, I've enjoyed working in a wide range of industries such as aerospace, boatyard management, remote sensing, emergency services / data management, and casino operations. I've done a variety of pro-bono work non-profit organizations related to nature conservancy, drug recovery and women's health.

Comments and Discussions