Example .NET Config File with Drill Configuration

Please consider the minimal Drill configuration in a .NET config file example below:

<?xml version="1.0"?>
<configuration>
	<configSections>
		<sectionGroup name="drill">
			<section name="dependencyResolvers" type="Drill.Configuration.ConfigFile.DependencyResolversConfigurationSection, Drill" />
		</sectionGroup>
	</configSections>
	<drill>
		<dependencyResolvers>
			<dependencyResolver name="MyResolver">
				<drillBits>
					<drillBit name="MyUnityContainer" type="Drill.DrillBits.Unity.UnityDrillBit, Drill.DrillBits.Unity">
						<cfgSources>
							<cfgSource key="UnityConfigSectionAppConfig" method="ConfigFile" />
						</cfgSources>
					</drillBit>
				</drillBits>
			</dependencyResolver>
		</dependencyResolvers>
	</drill>
</configuration>

The example is a minimal App.config file with only the drill/dependencyResolvers config section defined. The DependencyResolversConfigurationSection class from the Drill assembly handles the drill/dependencyResolvers config section, adapting it to the proper Drill configuration interfaces. (This same Drill config can be applied to Web.config files as well.)

The rest of the Drill configuration XML defines a minimal dependency resolver named "MyResolver" which is using a single DrillBit, the Unity DrillBit, as the single underlying DI container. The Unity Drill bit has a single configuration source defined (the cfgSource element) which will use the default .NET config file to find its configuration as defined by the ConfigFile method attribute value.

NOTE: The example above omits the Unity configuration in order to allow you to focus on the Drill configuration. A working minimal App.config file that contains both Drill and Unity configuration is shown below for completeness.

<?xml version="1.0"?>
<configuration>
	<configSections>
		<sectionGroup name="drill">
			<section name="dependencyResolvers" type="Drill.Configuration.ConfigFile.DependencyResolversConfigurationSection, Drill" />
		</sectionGroup>
		<section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration" />
	</configSections>
	<drill>
		<dependencyResolvers>
			<dependencyResolver name="MyResolver">
				<drillBits>
					<drillBit name="MyUnityContainer" type="Drill.DrillBits.Unity.UnityDrillBit, Drill.DrillBits.Unity">
						<cfgSources>
							<cfgSource key="UnityConfigSectionAppConfig" method="ConfigFile" />
						</cfgSources>
					</drillBit>
				</drillBits>
			</dependencyResolver>
		</dependencyResolvers>
	</drill>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
    </startup>
	<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
		<alias alias="IColor" type="Drill.Samples.SampleAppCommon.IColor, Drill.Samples.SampleAppCommon"/>
		<alias alias="BlueColor" type="Drill.Samples.SampleAppCommon.BlueColor, Drill.Samples.SampleAppCommon"/>
		<container name="MyUnityContainer">
			<register type="IColor" mapTo="BlueColor">
				<lifetime type="transient" />
			</register>
		</container>
	</unity>
</configuration>

Drill configuration has much more to offer, but the example above is the bare minimum. Optional parts of the configuration are not shown here and when they are not defined, reasonable defaults are used.


Last edited Nov 24, 2012 at 3:36 PM by wreynolds, version 3

Comments

No comments yet.