|
XmlConfiguration
XML configuration is useful for components that depend on the deployment environment.
IntroductionMost 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. SyntaxAutofac 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' AttributesThe following can be used as attributes on the component element (defaults are the same as for the programmatic API):
Valid 'component' Nested Elements
There are features missing from the XML configuration syntax that are available through the programmatic API - these will be added as needed. ModulesConfiguring 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 FilesYou can include additional config files using: <files> <file name="Controllers.config" section="controllers" /> Configuring the ContainerTo 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 SectionsYou 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. |
Sign in to add a comment
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.
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?
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
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?
Is there a way to inject a bean created using xml into another bean? <component name="a" />
<component name="b">
</component>I understand you can inject dependencies within code, but i'd like to do this via xml.
I meant to say <property name="A" ref="a" />