Click here to Skip to main content
15,885,031 members
Articles / Operating Systems / Windows

Inversion of Control (IoC) in XAML

Rate me:
Please Sign up or sign in to vote.
4.78/5 (17 votes)
16 Jan 2007CPOL9 min read 63.9K   533   36  
An article about Inversion of Control in XAML
using System;
using System.Collections.Generic;
using System.Windows;

using XamlIoc;

using MyLib;

namespace MyConsoleApp
{
	/// <summary>Entry point of the application.</summary>
	public static class Program
	{
		/// <summary>Lets validate some persons!</summary>
		public static void Main()
		{
			TestPersonValidation();
			TestGenerics();
			TestShared();
		}

		private static void TestPersonValidation()
		{
			Console.WriteLine();
			Console.WriteLine("TestPersonValidation");
			Console.WriteLine();

			PersonValidator validator =
				ObjectFactory.GetObject<PersonValidator>(typeof(PersonValidator).FullName);

			validator.ValidatePersons(42, 12, 79, 14);
		}

		private static void TestGenerics()
		{
			Console.WriteLine();
			Console.WriteLine("TestGenerics");
			Console.WriteLine();

			IList<int> emptyList = ObjectFactory.GetObject<IList<int>>("myListOfInt");
			IList<int> populatedList = ObjectFactory.GetObject<IList<int>>("myListOfIntPopulated");

			Console.WriteLine("emptyList Capacity:  " + ((List<int>)emptyList).Capacity);

			Console.WriteLine("populatedList Content:");
			foreach (int i in populatedList)
			{
				Console.WriteLine(i);
			}
		}

		private static void TestShared()
		{
			Console.WriteLine();
			Console.WriteLine("TestShared");
			Console.WriteLine();

			Person objectFactoryPerson1 = ObjectFactory.GetObject<Person>("objectFactoryPerson");
			Person objectFactoryPerson2 = ObjectFactory.GetObject<Person>("objectFactoryPerson");
			Person staticResourcePerson1 = ObjectFactory.GetObject<Person>("staticResourcePerson");
			Person staticResourcePerson2 = ObjectFactory.GetObject<Person>("staticResourcePerson");

			Console.WriteLine(
				"object factory persons equal:  "
				+ object.ReferenceEquals(objectFactoryPerson1, objectFactoryPerson2));
			Console.WriteLine(
				"static resource persons equal:  "
				+ object.ReferenceEquals(staticResourcePerson1, staticResourcePerson2));
			Console.WriteLine(
				"object factory addresses equal:  "
				+ object.ReferenceEquals(objectFactoryPerson1.Address, objectFactoryPerson2.Address));
			Console.WriteLine(
				"static resource addresses equal:  "
				+ object.ReferenceEquals(staticResourcePerson1.Address, staticResourcePerson2.Address));
		}
	}
}

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 CGI
Canada Canada
Vincent-Philippe is a Senior Solution Architect working in Montreal (Quebec, Canada).

His main interests are Windows Azure, .NET Enterprise suite (e.g. SharePoint 2013, Biztalk Server 2010) & the new .NET 4.5 platforms.

Comments and Discussions