Unity Example

The following example shows, how Unity can be used to inject functionality into a console application.

using System;
using System.Linq;
using CommandlineHandling;
using Microsoft.Practices.Unity;

namespace UnitySample
{
	class SampleOption : Option
	{
		public SampleOption() 
		{
			this.Execute += DoSomething;
		}

		private object DoSomething(ICommandLineOption caller, ICommandLineOption current)
		{
		//	TODO: Add some code here.

			return null;
		}

		public int Value { get { return this.GetValue<int>(); } }
	}

	class Program
	{
		static void Main(string[] args)
		{
			var container = new UnityContainer();
			container.RegisterType<ICommandLineOption, SampleOption>("x");

			var handler = new CommandLineHandler();
			handler.ResolveOption += (x) => {
				
				return container.Resolve<ICommandLineOption>(x);
			};

			var arg = handler.Parse(args).FindByName("x").First();
			
			arg.InvokeExecute();

			Console.ReadKey();
		}
	}
}

Last edited Aug 1, 2011 at 6:30 PM by mfriedrich, version 3

Comments

No comments yet.