Factory

The Codoxide.Common.Patterns.Factory bring 2 indispensable concepts for class library developers : The Abstract Factory Pattern) and Dependency Injection.

E.g.

C#

	class Program
	{
		static void Main(string[] args)
		{
			using (IDatabase db = Factory.Build<IDatabase>())
			{
				Console.WriteLine("Your Database Class has been loaded:");
				Console.WriteLine("\tType: {0}", db.GetType().Name);
				Console.WriteLine("\tConnection String: {0}",
                                                            db.ConnectionString);
			}
		}
	}

App.Config

<configuration>
	<configSections>
		<section name="FactorySettings" 
                         type="Codoxide.Common.Patterns.FactorySettings, Codoxide.Common" />
	</configSections>
	<connectionStrings>
		<clear />
		<add name="LocalSqlServer"
                     connectionString="Data Source=(local);Integrated Security=SSPI;" 
                     providerName="System.Data.SqlClient"/>
	</connectionStrings>
	<FactorySettings>
		<Settings>
			<MapOfString>
				<value name="Codoxide.Common.Data.IDatabase">
					Codoxide.Common.Data.Database`3[
						[System.Data.SqlClient.SqlConnection,
                                                   System.Data, Version=2.0.0.0, 
                                                   Culture=neutral,
                                                   PublicKeyToken=b77a5c561934e089],
						[System.Data.SqlClient.SqlCommand, 
                                                   System.Data, Version=2.0.0.0, 
                                                   Culture=neutral, 
                                                   PublicKeyToken=b77a5c561934e089],
						[System.Data.SqlClient.SqlDataAdapter, 
                                                   System.Data, Version=2.0.0.0, 
                                                   Culture=neutral, 
                                                   PublicKeyToken=b77a5c561934e089]
					],Codoxide.Common
				</value>
			</MapOfString>
		</Settings>
	</FactorySettings>
</configuration>

Factory.Build<T>() generic method can be used to request the construction of any of the following types of objects:

1. Interface/Abstract Classes

Most often, you'd use the Factory.Build<T>() method to construct instances of Interfaces or Abstract Classes. This allows you to differ the specification of actual concrete type* to instantiate to the user.

2. Normal .NET Objects

Though unnecessary, the Factory.Build<T>() method can be used to initialize any .NET class*. You may use this technique if you anticipate that the end user may want to your code to use a more specialized version of a base type.

E.g. Your code

// ...
InterfaceLayout employee = Factory.Build<InterfaceLayout>();
// ...

Say you develop a super easy user interface layout afterwards and want to make that available to the user. You can put something like the following in the config section

 
<FactorySettings>
		<Settings>
			<MapOfString>
				<value name="Codoxide.Sample.InterfaceLayout">
                                       SuperEasyLayout, Codoxide.Sample
                                </value>
			</MapOfString>
		</Settings>
</FactorySettings>

* Note that currently, Factory.Build<T>() requires that the type

Factory.Build<T>() also supports ISupportLazyInitialization.

Back to top
factory.txt · Last modified: 2008/12/13 15:07 by sameeraperera