My favorites | Sign in
Logo
                
Search
for
Updated Aug 05, 2009 by nicholas.blumhardt
XmlConfiguration  
XML configuration is useful for components that depend on the deployment environment.

Introduction

Most IoC containers provide a programmatic interface as well as XML configuration support, and Autofac is no exception.

Autofac encourages programmatic configuration through the ContainerBuilder class. Using the programmatic interface is central to the design of the container. XML is recommended only when concrete classes cannot be chosen or configured at compile-time.

Syntax

Autofac can read standard .NET application config files. These are the ones called AppName.config.

You need to declare a section handler somewhere near the top of your config file - you can give the container configuration section its own unique name ("mycomponents" in this example.)

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
	<configSections>
		<section name="mycomponents" type="Autofac.Configuration.SectionHandler, Autofac"/>
	</configSections>

Then, provide a section describing your components:

	<mycomponents defaultAssembly="Autofac.Example.Calculator.Api">
		<components>
			<component
				type="Autofac.Example.Calculator.Addition.Add, Autofac.Example.Calculator.Addition"
				service="Autofac.Example.Calculator.Api.IOperation" />

			<component
				type="Autofac.Example.Calculator.Division.Divide, Autofac.Example.Calculator.Division"
				service="Autofac.Example.Calculator.Api.IOperation" >
				<parameters>
					<parameter name="places" value="4" />
				</parameters>
			</component>

Valid 'component' Attributes

The following can be used as attributes on the component element (defaults are the same as for the programmatic API):

Attribute Name Description Valid Values
type The only required attribute. The concrete class of the component (assembly-qualified if in an assembly other than the default.) Any .NET type name that can be created through reflection.
service A service exposed by the component. For more than one service, use the nested services element. As for type.
scope Instance scope - see the InstanceScope enumeration. singleton, factory or container
ownership Container's ownership over the instances - see the InstanceOwnership enumeration. container or external
name A string name for the component. Any non-empty string value.
member-of Use to create multivalued components (see RegisterCollection() The name of another component that implements ICompositeComponent.
inject-properties Enable property (setter) injection for the component. never, all, unset.

Valid 'component' Nested Elements

Element Description
services A list of service elements, whose element content contains the names of types exposed as services by the component (see the service attribute.)
parameters A list of explicit constructor parameters to set on the instances (see example above.)
properties A list of explicit property values to set (syntax as for parameters.)

There are features missing from the XML configuration syntax that are available through the programmatic API - these will be added as needed.

Modules

Configuring the container using components is very fine-grained and can get verbose quickly. Autofac has support for packaging components into Modules in order to encapsulate implementation while providing flexible configuration.

Modules are registered by type:

<modules>
  <module type="MyModule" />

You can add nested parameters and properties to a module registration in the same manner as for components above.

Additional Config Files

You can include additional config files using:

<files>
  <file name="Controllers.config" section="controllers" />

Configuring the Container

To configure the container use a ConfigurationSettingsReader initialised with the name you gave to your XML configuration section.

var builder = new ContainerBuilder();
builder.RegisterModule(new ConfigurationSettingsReader("mycomponents"));
// Register other components and call Build() to create the container...

The container settings reader will overwrite the registrations of any components already registered, thus you can write your application so that it will run with sensible defaults and then override only those component registrations necessary for a particular deployment.

Multiple Files or Sections

You can use multiple settings readers in the same container, to read different sections or even different config files if the filename is supplied to the ConfigurationSettingsReader constructor.


Comment by spurrymoses, Dec 30, 2008

The doco above doesn't explain to me why the 'type' is a comma delimited string. Is it necessary to comma delimit 2 'names', what exactly are they - given that the doco talks about only 1 thing, and there's actually 2.

Comment by spurrymoses, Dec 30, 2008

Is the one line of code in 'Configuring the Container' sufficient? It assumes the reader already knows how to write the "rest of the code" to get this to work. ie where does the 'builder' variable come from? Why not just include the whole code snippet?

Comment by tornak47, Feb 10, 2009

Do you mean type="Autofac.Example.Calculator.Addition.Add, Autofac.Example.Calculator.Addition"? If so, this is a single type but has been entered using an assembly-qualified name. You will find this a lot in XML configuration files - it needs to know what assembly the type is located in. In this case, the 'Autofac.Example.Calculator.Addition.Add' type is located in the Autofac.Example.Calculator.Addition assembly

Comment by venetiabird, Aug 02, 2009

Is there a way to do constructor injection by specifying the type as opposed to a value? In the above example, the parameter for the constructor has a name, and then a value - how can I specify that the parameter being passed in is a custom type?

Comment by seungykim, Aug 05, 2009

Is there a way to inject a bean created using xml into another bean? <component name="a" />

<component name="b">

<property name="B" ref="b" />
</component>

I understand you can inject dependencies within code, but i'd like to do this via xml.

Comment by seungykim, Aug 05, 2009

I meant to say <property name="A" ref="a" />


Sign in to add a comment
Hosted by Google Code