Click here to Skip to main content
15,885,216 members
Articles / Desktop Programming / WPF

Templates, Inversion of Control, Factories, and so on

Rate me:
Please Sign up or sign in to vote.
4.75/5 (5 votes)
8 Jul 2011CPOL11 min read 22.9K   270   18  
This article gives a little presentation of Control Templates, Data Templates, Inversion of Control, and Factories, explaining why they are all related and how to better use them.
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Pfz.Databasing.Managers;
using System.Reflection;
using System.Linq;

namespace Pfz.Databasing.Filtering
{
	/// <summary>
	/// Represents a group of filters, that work together by AND or OR clauses.
	/// </summary>
	[Serializable]
	public sealed class Filter:
		ICloneable<Filter>,
		IEquatable<Filter>
	{
		/// <summary>
		/// Creates the new group using the AND grouping operator.
		/// </summary>
		public Filter()
		{
		}
		
		/// <summary>
		/// Creates the new group using the given grouping operator.
		/// </summary>
		/// <param name="groupMode"></param>
		public Filter(FilterMode groupMode)
		{
			GroupMode = groupMode;
		}
		
		/// <summary>
		/// Gets the grouping operator used by this group.
		/// </summary>
		public FilterMode GroupMode { get; private set; }
		
		internal readonly List<FilterItem> _filters = new List<FilterItem>();
		internal readonly List<Filter> _subGroups = new List<Filter>();
		
		/// <summary>
		/// Gets the sub-groups.
		/// </summary>
		public ReadOnlyCollection<Filter> SubGroups
		{
			get
			{
				return _subGroups.AsReadOnly();
			}
		}
		
		/// <summary>
		/// Gets the filters.
		/// </summary>
		public ReadOnlyCollection<FilterItem> Filters
		{
			get
			{
				return _filters.AsReadOnly();
			}
		}
		
		/// <summary>
		/// Adds a new filter using the given path and value. The operator used 
		/// is EqualTo.
		/// </summary>
		public void Add(ExtendedPropertyInfoPath path, object value)
		{
			FilterItem filter = new FilterItem(path, FilterOperation.EqualTo, value);
			_filters.Add(filter);
		}

		/// <summary>
		/// Adds a new filter using the given property and value. The operator
		/// used is EqualTo.
		/// </summary>
		public void Add(PropertyInfo property, object value)
		{
			var extendedProperty = new ExtendedPropertyInfo(property.DeclaringType, property);
			var path = new ExtendedPropertyInfoPath(extendedProperty);
			Add(path, value);
		}

		
		/// <summary>
		/// Adds a new filter using the given path, operation and value.
		/// </summary>
		public void Add(ExtendedPropertyInfoPath path, FilterOperation operation, object value)
		{
			FilterItem filter = new FilterItem(path, operation, value);
			_filters.Add(filter);
		}
		
		/// <summary>
		/// Adds an existing group as a sub-group.
		/// </summary>
		public void AddSubGroup(Filter subGroup)
		{
			_subGroups.Add(subGroup);
		}
		
		/// <summary>
		/// Adds a new sub-group, of type AND.
		/// </summary>
		public Filter AddSubGroup()
		{
			Filter result = new Filter();
			_subGroups.Add(result);
			return result;
		}
		
		/// <summary>
		/// Adds a new sub-group, with the given groupMode.
		/// </summary>
		public Filter AddSubGroup(FilterMode groupMode)
		{
			Filter result = new Filter(groupMode);
			_subGroups.Add(result);
			return result;
		}
		
		/// <summary>
		/// Clones the actual filter group.
		/// </summary>
		public Filter Clone()
		{
			Filter result = new Filter(GroupMode);
			
			foreach(var subGroup in _subGroups)
				result._subGroups.Add(subGroup.Clone());
				
			foreach(var filter in _filters)
				result._filters.Add(filter.Clone());
			
			return result;
		}

		/// <summary>
		/// Compares this group with another object.
		/// </summary>
		public override bool Equals(object obj)
		{
			Filter other = obj as Filter;
			if (other == null)
				return false;

			return Equals(other);
		}

		/// <summary>
		/// Compares this group with another group.
		/// The values are ignored, as this is used to refill prepared queries.
		/// </summary>
		public bool Equals(Filter other)
		{
			if (other == null)
				return false;

			return _filters.SequenceEqual(other._filters) && _subGroups.SequenceEqual(other._subGroups);
		}

		/// <summary>
		/// Gets the hashcode for this filtergroup.
		/// </summary>
		public override int GetHashCode()
		{
			unchecked
			{
				int result = 0;

				foreach(var filter in _filters)
					result ^= filter.GetHashCode();

				foreach(var subGroup in _subGroups)
					result ^= subGroup.GetHashCode();

				return result;
			}
		}
		
		object ICloneable.Clone()
		{
			return Clone();
		}
	}
}

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
Software Developer (Senior) Microsoft
United States United States
I started to program computers when I was 11 years old, as a hobbyist, programming in AMOS Basic and Blitz Basic for Amiga.
At 12 I had my first try with assembler, but it was too difficult at the time. Then, in the same year, I learned C and, after learning C, I was finally able to learn assembler (for Motorola 680x0).
Not sure, but probably between 12 and 13, I started to learn C++. I always programmed "in an object oriented way", but using function pointers instead of virtual methods.

At 15 I started to learn Pascal at school and to use Delphi. At 16 I started my first internship (using Delphi). At 18 I started to work professionally using C++ and since then I've developed my programming skills as a professional developer in C++ and C#, generally creating libraries that help other developers do their work easier, faster and with less errors.

Want more info or simply want to contact me?
Take a look at: http://paulozemek.azurewebsites.net/
Or e-mail me at: paulozemek@outlook.com

Codeproject MVP 2012, 2015 & 2016
Microsoft MVP 2013-2014 (in October 2014 I started working at Microsoft, so I can't be a Microsoft MVP anymore).

Comments and Discussions