Skip to content

Commit d61e833

Browse files
committed
Add 'src/Autofac.Extras.EnterpriseLibraryConfigurator/' from commit '70a89216d924960c9d9d9953baeaa76742cb1bff'
git-subtree-dir: src/Autofac.Extras.EnterpriseLibraryConfigurator git-subtree-mainline: 9236c66 git-subtree-split: 70a8921
2 parents 9236c66 + 70a8921 commit d61e833

File tree

8 files changed

+1119
-0
lines changed

8 files changed

+1119
-0
lines changed
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
5+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
6+
<ProductVersion>8.0.30703</ProductVersion>
7+
<SchemaVersion>2.0</SchemaVersion>
8+
<ProjectGuid>{97BC9D7B-A233-4109-AFB7-1F2F0C137CCF}</ProjectGuid>
9+
<OutputType>Library</OutputType>
10+
<AppDesignerFolder>Properties</AppDesignerFolder>
11+
<RootNamespace>Autofac.Extras.EnterpriseLibraryConfigurator</RootNamespace>
12+
<AssemblyName>Autofac.Extras.EnterpriseLibraryConfigurator</AssemblyName>
13+
<FileAlignment>512</FileAlignment>
14+
<SignAssembly>true</SignAssembly>
15+
<AssemblyOriginatorKeyFile>..\..\..\Build\SharedKey.snk</AssemblyOriginatorKeyFile>
16+
<TargetFrameworkVersion Condition="$(TargetFrameworkVersion)==''">v4.0</TargetFrameworkVersion>
17+
<TargetFrameworkProfile Condition="$(TargetFrameworkVersion)==''">
18+
</TargetFrameworkProfile>
19+
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\..\..\</SolutionDir>
20+
<RestorePackages>true</RestorePackages>
21+
</PropertyGroup>
22+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
23+
<DebugSymbols>true</DebugSymbols>
24+
<DebugType>full</DebugType>
25+
<Optimize>false</Optimize>
26+
<OutputPath>bin\Debug\</OutputPath>
27+
<DefineConstants>DEBUG;TRACE</DefineConstants>
28+
<ErrorReport>prompt</ErrorReport>
29+
<WarningLevel>4</WarningLevel>
30+
<CodeAnalysisRuleSet>..\..\..\Build\Full.ruleset</CodeAnalysisRuleSet>
31+
<DocumentationFile>bin\Debug\Autofac.Extras.EnterpriseLibraryConfigurator.xml</DocumentationFile>
32+
<RunCodeAnalysis>true</RunCodeAnalysis>
33+
</PropertyGroup>
34+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
35+
<DebugType>pdbonly</DebugType>
36+
<Optimize>true</Optimize>
37+
<OutputPath>bin\Release\</OutputPath>
38+
<DefineConstants>TRACE</DefineConstants>
39+
<ErrorReport>prompt</ErrorReport>
40+
<WarningLevel>4</WarningLevel>
41+
<CodeAnalysisRuleSet>..\..\..\Build\Full.ruleset</CodeAnalysisRuleSet>
42+
<DocumentationFile>bin\Release\Autofac.Extras.EnterpriseLibraryConfigurator.xml</DocumentationFile>
43+
<RunCodeAnalysis>true</RunCodeAnalysis>
44+
</PropertyGroup>
45+
<ItemGroup>
46+
<Reference Include="Autofac, Version=3.3.0.0, Culture=neutral, PublicKeyToken=17863af14b0044da, processorArchitecture=MSIL">
47+
<SpecificVersion>False</SpecificVersion>
48+
<HintPath>..\..\..\packages\Autofac.3.3.1\lib\net40\Autofac.dll</HintPath>
49+
</Reference>
50+
<Reference Include="Microsoft.Practices.EnterpriseLibrary.Common">
51+
<HintPath>..\..\..\packages\EnterpriseLibrary.Common.5.0.505.0\lib\NET35\Microsoft.Practices.EnterpriseLibrary.Common.dll</HintPath>
52+
</Reference>
53+
<Reference Include="Microsoft.Practices.ServiceLocation, Version=1.2.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
54+
<SpecificVersion>False</SpecificVersion>
55+
<HintPath>..\..\..\packages\CommonServiceLocator.1.2\lib\portable-windows8+net40+sl5+windowsphone8\Microsoft.Practices.ServiceLocation.dll</HintPath>
56+
</Reference>
57+
<Reference Include="Microsoft.Practices.Unity, Version=2.1.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
58+
<SpecificVersion>False</SpecificVersion>
59+
<HintPath>..\..\..\packages\Unity.2.1.505.2\lib\NET35\Microsoft.Practices.Unity.dll</HintPath>
60+
</Reference>
61+
<Reference Include="Microsoft.Practices.Unity.Configuration, Version=2.1.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
62+
<SpecificVersion>False</SpecificVersion>
63+
<HintPath>..\..\..\packages\Unity.2.1.505.2\lib\NET35\Microsoft.Practices.Unity.Configuration.dll</HintPath>
64+
</Reference>
65+
<Reference Include="Microsoft.Practices.Unity.Interception, Version=2.1.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
66+
<SpecificVersion>False</SpecificVersion>
67+
<HintPath>..\..\..\packages\Unity.Interception.2.1.505.2\lib\NET35\Microsoft.Practices.Unity.Interception.dll</HintPath>
68+
</Reference>
69+
<Reference Include="Microsoft.Practices.Unity.Interception.Configuration, Version=2.1.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
70+
<SpecificVersion>False</SpecificVersion>
71+
<HintPath>..\..\..\packages\Unity.Interception.2.1.505.2\lib\NET35\Microsoft.Practices.Unity.Interception.Configuration.dll</HintPath>
72+
</Reference>
73+
<Reference Include="System" />
74+
<Reference Include="System.Configuration" />
75+
<Reference Include="System.Core" />
76+
<Reference Include="System.Xml.Linq" />
77+
<Reference Include="System.Data.DataSetExtensions" />
78+
<Reference Include="Microsoft.CSharp" />
79+
<Reference Include="System.Data" />
80+
<Reference Include="System.Xml" />
81+
</ItemGroup>
82+
<ItemGroup>
83+
<Compile Include="..\..\..\GlobalAssemblyInfo.cs">
84+
<Link>Properties\GlobalAssemblyInfo.cs</Link>
85+
</Compile>
86+
<Compile Include="..\..\..\ProductAssemblyInfo.cs">
87+
<Link>Properties\ProductAssemblyInfo.cs</Link>
88+
</Compile>
89+
<CodeAnalysisDictionary Include="..\..\..\Build\CodeAnalysisDictionary.xml">
90+
<Link>CodeAnalysisDictionary.xml</Link>
91+
</CodeAnalysisDictionary>
92+
<Compile Include="AutofacContainerConfigurator.cs" />
93+
<Compile Include="AutofacParameterBuilderVisitor.cs" />
94+
<Compile Include="EnterpriseLibraryRegistrationExtensions.cs" />
95+
<Compile Include="Properties\AssemblyInfo.cs" />
96+
<Compile Include="Properties\VersionAssemblyInfo.cs" />
97+
</ItemGroup>
98+
<ItemGroup>
99+
<None Include="app.config" />
100+
<None Include="packages.config" />
101+
</ItemGroup>
102+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
103+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
104+
Other similar extension points exist, see Microsoft.Common.targets.
105+
<Target Name="BeforeBuild">
106+
</Target>
107+
<Target Name="AfterBuild">
108+
</Target>
109+
-->
110+
</Project>
Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
using System;
2+
using Autofac;
3+
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
4+
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ContainerModel;
5+
6+
namespace Autofac.Extras.EnterpriseLibraryConfigurator
7+
{
8+
/// <summary>
9+
/// <see cref="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ContainerModel.IContainerConfigurator"/>
10+
/// implementation for registering Enterprise Library dependencies into
11+
/// an Autofac container.
12+
/// </summary>
13+
/// <remarks>
14+
/// <para>
15+
/// Enterprise Library uses
16+
/// <see cref="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ContainerModel.IContainerConfigurator"/>
17+
/// implementations as a bridge between configuration sources
18+
/// (<see cref="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.IConfigurationSource"/>),
19+
/// type registration providers
20+
/// (<see cref="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ContainerModel.ITypeRegistrationsProvider"/>),
21+
/// and inversion of control containers. This implementation allows you to
22+
/// use Autofac as the container from which Enterprise Library services
23+
/// get resolved.
24+
/// </para>
25+
/// <para>
26+
/// Setup of a container via a configurator is done using the
27+
/// <see cref="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.EnterpriseLibraryContainer.ConfigureContainer(IContainerConfigurator,IConfigurationSource)"/>
28+
/// method. Using this configurator, registering Enterprise Library with
29+
/// Autofac looks like this:
30+
/// </para>
31+
/// <code lang="C#">
32+
/// // Create the ContainerBuilder and register EntLib.
33+
/// var builder = new ContainerBuilder();
34+
/// using (var configSource = ConfigurationSourceFactory.Create())
35+
/// {
36+
/// var configurator = new AutofacContainerConfigurator(builder);
37+
/// EnterpriseLibraryContainer.ConfigureContainer(configurator, configSource);
38+
/// }
39+
///
40+
/// // Register other services/dependencies, then set the
41+
/// // service locator.
42+
/// var container = builder.Build();
43+
/// var autofacLocator = new AutofacServiceLocator(container);
44+
/// EnterpriseLibraryContainer.Current = autofacLocator;
45+
/// </code>
46+
/// <para>
47+
/// Note the use of <see cref="T:Autofac.Extras.CommonServiceLocator.AutofacServiceLocator"/>
48+
/// to set the Enterprise Library service locator. You need to do that so
49+
/// Enterprise Library can do any internal resolutions it requires.
50+
/// </para>
51+
/// <para>
52+
/// Even though you can use this configurator directly, the simplest way
53+
/// to get Enterprise Library configured with Autofac is to use the
54+
/// <see cref="M:Autofac.Extras.EnterpriseLibraryConfigurator.EnterpriseLibraryRegistrationExtensions.RegisterEnterpriseLibrary"/>
55+
/// extensions. The above sample code becomes much simpler and more
56+
/// Autofac-styled with the extensions.
57+
/// </para>
58+
/// <code lang="C#">
59+
/// // Create the ContainerBuilder and register EntLib.
60+
/// var builder = new ContainerBuilder();
61+
/// builder.RegisterEnterpriseLibrary();
62+
///
63+
/// // Register other services/dependencies, then set the
64+
/// // service locator.
65+
/// var container = builder.Build();
66+
/// var autofacLocator = new AutofacServiceLocator(container);
67+
/// EnterpriseLibraryContainer.Current = autofacLocator;
68+
/// </code>
69+
/// <para>
70+
/// Note that using Autofac as the backing store for service resolution
71+
/// in Enterprise Library, changes in the dependency configuration source
72+
/// are not directly supported. That is, if the set of configured services
73+
/// changes, the container is not automatically rebuilt. This is a different
74+
/// behavior from Unity, which supports configuration change and container
75+
/// update during application execution.
76+
/// </para>
77+
/// <para>
78+
/// The reason this is important is that some internal Enterprise Library
79+
/// components make the assumption that the container has a configuration
80+
/// change manager (<see cref="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ContainerModel.ConfigurationChangeEventSource"/>)
81+
/// registered regardless of support. This configurator will add a placeholder
82+
/// implementation to the container to enable these components to function,
83+
/// but developers should be aware that no action will take place if configuration
84+
/// changes even though the component appears to be present in the container.
85+
/// </para>
86+
/// </remarks>
87+
/// <seealso cref="M:Autofac.Extras.EnterpriseLibraryConfigurator.EnterpriseLibraryRegistrationExtensions.RegisterEnterpriseLibrary"/>
88+
public class AutofacContainerConfigurator : IContainerConfigurator
89+
{
90+
/// <summary>
91+
/// The <see cref="Autofac.ContainerBuilder"/> to which Enterprise Library
92+
/// registrations should be added.
93+
/// </summary>
94+
private ContainerBuilder _builder = null;
95+
96+
/// <summary>
97+
/// Initializes a new instance of the <see cref="AutofacContainerConfigurator"/> class.
98+
/// </summary>
99+
/// <param name="builder">
100+
/// The <see cref="Autofac.ContainerBuilder"/> to which Enterprise Library
101+
/// registrations should be added.
102+
/// </param>
103+
/// <exception cref="System.ArgumentNullException">
104+
/// Thrown if <paramref name="builder" /> is <see langword="null" />.
105+
/// </exception>
106+
public AutofacContainerConfigurator(ContainerBuilder builder)
107+
{
108+
if (builder == null)
109+
{
110+
throw new ArgumentNullException("builder");
111+
}
112+
this._builder = builder;
113+
}
114+
115+
/// <summary>
116+
/// Consume the set of
117+
/// <see cref="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ContainerModel.TypeRegistration"/>
118+
/// objects and configure the associated container.
119+
/// </summary>
120+
/// <param name="configurationSource">
121+
/// Configuration source to read registrations from.
122+
/// </param>
123+
/// <param name="rootProvider">
124+
/// <see cref="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ContainerModel.ITypeRegistrationsProvider"/>
125+
/// that knows how to read the <paramref name="configurationSource"/>
126+
/// and return all relevant type registrations.
127+
/// </param>
128+
/// <remarks>
129+
/// <para>
130+
/// This method is used by <see cref="M:Microsoft.Practices.EnterpriseLibrary.Common.Configuration.EnterpriseLibraryContainer.ConfigureContainer"/>
131+
/// to add registrations from a provided <paramref name="configurationSource" />
132+
/// to the Autofac container.
133+
/// </para>
134+
/// <para>
135+
/// At the end of the registration process, a placeholder configuration
136+
/// change manager (<see cref="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ContainerModel.ConfigurationChangeEventSource"/>)
137+
/// will be added to the container if one hasn't already been added.
138+
/// This is required because some internal Enterprise Library
139+
/// components make the assumption that the container has a configuration
140+
/// change manager registered regardless of support. Developers should
141+
/// be aware that no action will take place if configuration
142+
/// changes even though the component appears to be present in the container.
143+
/// </para>
144+
/// </remarks>
145+
public void RegisterAll(IConfigurationSource configurationSource, ITypeRegistrationsProvider rootProvider)
146+
{
147+
if (configurationSource == null)
148+
{
149+
throw new ArgumentNullException("configurationSource");
150+
}
151+
if (rootProvider == null)
152+
{
153+
throw new ArgumentNullException("rootProvider");
154+
}
155+
foreach (TypeRegistration registration in rootProvider.GetRegistrations(configurationSource))
156+
{
157+
this._builder.RegisterTypeRegistration(registration);
158+
}
159+
160+
// For some reason you have to manually register an event source
161+
// with the container. Unity must somehow do this automatically in
162+
// the UnityContainerConfigurator but I can't find it. You need to
163+
// do this if you use the Logging application block since the
164+
// LoggingUpdateCoordinator requires this type. I'm not sure which
165+
// other components might require it.
166+
//
167+
// Autofac doesn't support changing the container after it's already
168+
// built, so rather than implement a ChangeTrackingContainerConfigurator,
169+
// we'll just stick a throwaway event source in here.
170+
this._builder.RegisterInstance(new ConfigurationChangeEventSourceImpl()).As<ConfigurationChangeEventSource>().PreserveExistingDefaults();
171+
}
172+
}
173+
}

0 commit comments

Comments
 (0)