Skip to content

Commit 6f6823c

Browse files
author
Jake Ginnivan
committed
Added autofac package
1 parent a834921 commit 6f6823c

20 files changed

+13769
-45
lines changed

ConventionTests.proj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,6 @@
5353
</PropertyGroup>
5454
<MakeDir Directories="$(Root)build\Artifacts" />
5555
<Exec Command="@(NuGet) pack $(Root)TestStack.ConventionTests\TestStack.ConventionTests.nuspec -Version $(Version) -OutputDir $(Root)build\Artifacts -BasePath $(Root)TestStack.ConventionTests\bin\$(Configuration)" />
56+
<Exec Command="@(NuGet) pack $(Root)TestStack.ConventionTests.Autofac\TestStack.ConventionTests.Autofac.nuspec -Version $(Version) -OutputDir $(Root)build\Artifacts -BasePath $(Root)TestStack.ConventionTests.Autofac\bin\$(Configuration)" />
5657
</Target>
5758
</Project>

ConventionTests.sln

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio 2013
4-
VisualStudioVersion = 12.0.20623.1 VSUPREVIEW
4+
VisualStudioVersion = 12.0.20827.3
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConventionTests", "ConventionTests\ConventionTests.csproj", "{1E12EA0C-9182-4029-991A-B0B9D38F5783}"
77
EndProject
@@ -29,6 +29,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SampleApp.Tests", "SampleAp
2929
EndProject
3030
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SampleApp", "SampleApp\SampleApp.csproj", "{56467A5A-7DD6-45B3-A84C-144A3C5D0C7A}"
3131
EndProject
32+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestStack.ConventionTests.Autofac", "TestStack.ConventionTests.Autofac\TestStack.ConventionTests.Autofac.csproj", "{A747FD64-5338-4572-879D-A9DEB00EBD56}"
33+
EndProject
3234
Global
3335
GlobalSection(SolutionConfigurationPlatforms) = preSolution
3436
Debug|Any CPU = Debug|Any CPU
@@ -59,6 +61,10 @@ Global
5961
{56467A5A-7DD6-45B3-A84C-144A3C5D0C7A}.Debug|Any CPU.Build.0 = Debug|Any CPU
6062
{56467A5A-7DD6-45B3-A84C-144A3C5D0C7A}.Release|Any CPU.ActiveCfg = Release|Any CPU
6163
{56467A5A-7DD6-45B3-A84C-144A3C5D0C7A}.Release|Any CPU.Build.0 = Release|Any CPU
64+
{A747FD64-5338-4572-879D-A9DEB00EBD56}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
65+
{A747FD64-5338-4572-879D-A9DEB00EBD56}.Debug|Any CPU.Build.0 = Debug|Any CPU
66+
{A747FD64-5338-4572-879D-A9DEB00EBD56}.Release|Any CPU.ActiveCfg = Release|Any CPU
67+
{A747FD64-5338-4572-879D-A9DEB00EBD56}.Release|Any CPU.Build.0 = Release|Any CPU
6268
EndGlobalSection
6369
GlobalSection(SolutionProperties) = preSolution
6470
HideSolutionNode = FALSE
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
namespace TestStack.ConventionTests.Autofac
2+
{
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Reflection;
7+
using global::Autofac.Core;
8+
using global::Autofac.Core.Activators.Delegate;
9+
using global::Autofac.Core.Activators.ProvidedInstance;
10+
using global::Autofac.Core.Activators.Reflection;
11+
using global::Autofac.Core.Lifetime;
12+
13+
public class AutofacRegistrations : IConventionData
14+
{
15+
private readonly IComponentRegistry componentRegistry;
16+
17+
public AutofacRegistrations(IComponentRegistry componentRegistry)
18+
{
19+
this.componentRegistry = componentRegistry;
20+
}
21+
22+
public string Description
23+
{
24+
get { return "All AutofacContainer Registrations"; }
25+
}
26+
27+
public bool HasData
28+
{
29+
get { return true; }
30+
}
31+
32+
public IComponentRegistry ComponentRegistry
33+
{
34+
get { return componentRegistry; }
35+
}
36+
37+
public Type GetConcreteType(IComponentRegistration r)
38+
{
39+
var reflectionActivator = r.Activator as ReflectionActivator;
40+
if (reflectionActivator != null) return reflectionActivator.LimitType;
41+
42+
var delegateActivator = r.Activator as DelegateActivator;
43+
if (delegateActivator != null) return delegateActivator.LimitType;
44+
45+
var providedInstanceActivator = r.Activator as ProvidedInstanceActivator;
46+
if (providedInstanceActivator != null) return providedInstanceActivator.LimitType;
47+
48+
throw new InvalidOperationException(r.Activator.GetType() + " is not a known component registration type");
49+
}
50+
51+
public Lifetime GetLifetime(IComponentRegistration componentRegistration)
52+
{
53+
if (componentRegistration.Ownership == InstanceOwnership.OwnedByLifetimeScope && componentRegistration.Sharing == InstanceSharing.Shared &&
54+
componentRegistration.Lifetime is RootScopeLifetime)
55+
return Lifetime.SingleInstance;
56+
if (componentRegistration.Ownership == InstanceOwnership.OwnedByLifetimeScope && componentRegistration.Sharing == InstanceSharing.Shared &&
57+
componentRegistration.Lifetime is CurrentScopeLifetime)
58+
return Lifetime.InstancePerLifetimeScope;
59+
if (componentRegistration.Ownership == InstanceOwnership.OwnedByLifetimeScope && componentRegistration.Sharing == InstanceSharing.None &&
60+
componentRegistration.Lifetime is CurrentScopeLifetime)
61+
return Lifetime.Transient;
62+
if (componentRegistration.Ownership == InstanceOwnership.ExternallyOwned && componentRegistration.Sharing == InstanceSharing.None &&
63+
componentRegistration.Lifetime is CurrentScopeLifetime)
64+
return Lifetime.ExternallyOwned;
65+
if (componentRegistration.Ownership == InstanceOwnership.ExternallyOwned && componentRegistration.Sharing == InstanceSharing.Shared &&
66+
componentRegistration.Lifetime is CurrentScopeLifetime)
67+
return Lifetime.SingleInstanceExternallyOwned;
68+
69+
throw new InvalidOperationException(string.Format("Unknown registration type for {3} Ownership: {0}, Sharing: {1}, Lifetime type: {2}", componentRegistration.Ownership, componentRegistration.Sharing,
70+
componentRegistration.Lifetime.GetType().Name, GetConcreteType(componentRegistration)));
71+
}
72+
73+
public IEnumerable<ParameterInfo> GetRegistrationCtorParameters(IComponentRegistration componentRegistration)
74+
{
75+
var activator = componentRegistration.Activator as ReflectionActivator;
76+
if (activator == null)
77+
return Enumerable.Empty<ParameterInfo>();
78+
79+
var limitType = activator.LimitType;
80+
return activator.ConstructorFinder.FindConstructors(limitType).SelectMany(ctor => ctor.GetParameters());
81+
}
82+
}
83+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
namespace TestStack.ConventionTests.Autofac
2+
{
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using global::Autofac;
7+
using global::Autofac.Core;
8+
9+
public class CanResolveAllRegisteredServices : IConvention<AutofacRegistrations>
10+
{
11+
private readonly IContainer container;
12+
13+
public CanResolveAllRegisteredServices(IContainer container)
14+
{
15+
this.container = container;
16+
}
17+
18+
public void Execute(AutofacRegistrations data, IConventionResultContext result)
19+
{
20+
var distinctTypes = data.ComponentRegistry.Registrations
21+
.SelectMany(r => r.Services.OfType<TypedService>().Select(s => s.ServiceType).Union(GetGenericFactoryTypes(data, r)))
22+
.Distinct();
23+
24+
var failingTypes = new List<Type>();
25+
foreach (var distinctType in distinctTypes)
26+
{
27+
object resolvedInstance;
28+
if (!container.TryResolve(distinctType, out resolvedInstance))
29+
failingTypes.Add(distinctType);
30+
}
31+
32+
result.Is("Can resolve all types registered with Autofac", failingTypes);
33+
}
34+
35+
private IEnumerable<Type> GetGenericFactoryTypes(AutofacRegistrations data, IComponentRegistration componentRegistration)
36+
{
37+
return from ctorParameter in data.GetRegistrationCtorParameters(componentRegistration)
38+
where ctorParameter.ParameterType.FullName.StartsWith("System.Func")
39+
select ctorParameter.ParameterType.GetGenericArguments()[0];
40+
}
41+
}
42+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
namespace TestStack.ConventionTests.Autofac
2+
{
3+
/// <summary>
4+
/// Ordered by logical dependency precendence. Higher values should not reference lower
5+
/// </summary>
6+
public enum Lifetime
7+
{
8+
Transient = 0,
9+
InstancePerLifetimeScope = 1,
10+
SingleInstance = 2,
11+
ExternallyOwned = 3,
12+
SingleInstanceExternallyOwned = 4
13+
}
14+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("TestStack.ConventionTests.Autofac")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("TestStack.ConventionTests.Autofac")]
13+
[assembly: AssemblyCopyright("Copyright © 2013")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// The following GUID is for the ID of the typelib if this project is exposed to COM
23+
[assembly: Guid("97020754-a2a1-4ea8-87d4-964795d79d3f")]
24+
25+
// Version information for an assembly consists of the following four values:
26+
//
27+
// Major Version
28+
// Minor Version
29+
// Build Number
30+
// Revision
31+
//
32+
// You can specify all the values or you can default the Build and Revision Numbers
33+
// by using the '*' as shown below:
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
namespace TestStack.ConventionTests.Autofac
2+
{
3+
using System.Collections.Generic;
4+
using global::Autofac.Core;
5+
using TestStack.ConventionTests.ConventionData;
6+
7+
public class ServicesShouldOnlyHaveDependenciesWithLesserLifetime : IConvention<AutofacRegistrations>
8+
{
9+
public void Execute(AutofacRegistrations data, IConventionResultContext result)
10+
{
11+
var exceptions = new List<string>();
12+
foreach (var registration in data.ComponentRegistry.Registrations)
13+
{
14+
var registrationLifetime = data.GetLifetime(registration);
15+
16+
foreach (var ctorParameter in data.GetRegistrationCtorParameters(registration))
17+
{
18+
IComponentRegistration parameterRegistration;
19+
var typedService = new TypedService(ctorParameter.ParameterType);
20+
21+
// If the parameter is not registered with autofac, ignore
22+
if (!data.ComponentRegistry.TryGetRegistration(typedService, out parameterRegistration)) continue;
23+
24+
var parameterLifetime = data.GetLifetime(parameterRegistration);
25+
26+
if (parameterLifetime >= registrationLifetime) continue;
27+
28+
var typeName = data.GetConcreteType(registration).ToTypeNameString();
29+
var parameterType = ctorParameter.ParameterType.ToTypeNameString();
30+
31+
var error = string.Format("{0} ({1}) => {2} ({3})",
32+
typeName, registrationLifetime,
33+
parameterType, parameterLifetime);
34+
exceptions.Add(error);
35+
}
36+
}
37+
38+
result.Is("Components should not depend on with greater lifetimes", exceptions);
39+
}
40+
}
41+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{A747FD64-5338-4572-879D-A9DEB00EBD56}</ProjectGuid>
8+
<OutputType>Library</OutputType>
9+
<AppDesignerFolder>Properties</AppDesignerFolder>
10+
<RootNamespace>TestStack.ConventionTests.Autofac</RootNamespace>
11+
<AssemblyName>TestStack.ConventionTests.Autofac</AssemblyName>
12+
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
13+
<FileAlignment>512</FileAlignment>
14+
</PropertyGroup>
15+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
16+
<DebugSymbols>true</DebugSymbols>
17+
<DebugType>full</DebugType>
18+
<Optimize>false</Optimize>
19+
<OutputPath>bin\Debug\</OutputPath>
20+
<DefineConstants>DEBUG;TRACE</DefineConstants>
21+
<ErrorReport>prompt</ErrorReport>
22+
<WarningLevel>4</WarningLevel>
23+
</PropertyGroup>
24+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
25+
<DebugType>pdbonly</DebugType>
26+
<Optimize>true</Optimize>
27+
<OutputPath>bin\Release\</OutputPath>
28+
<DefineConstants>TRACE</DefineConstants>
29+
<ErrorReport>prompt</ErrorReport>
30+
<WarningLevel>4</WarningLevel>
31+
</PropertyGroup>
32+
<ItemGroup>
33+
<Reference Include="Autofac">
34+
<HintPath>..\packages\Autofac.3.1.1\lib\net40\Autofac.dll</HintPath>
35+
</Reference>
36+
<Reference Include="System" />
37+
<Reference Include="System.Core" />
38+
<Reference Include="System.Xml.Linq" />
39+
<Reference Include="System.Data.DataSetExtensions" />
40+
<Reference Include="Microsoft.CSharp" />
41+
<Reference Include="System.Data" />
42+
<Reference Include="System.Xml" />
43+
</ItemGroup>
44+
<ItemGroup>
45+
<Compile Include="AutofacRegistrations.cs" />
46+
<Compile Include="CanResolveAllRegisteredServices.cs" />
47+
<Compile Include="Lifetime.cs" />
48+
<Compile Include="Properties\AssemblyInfo.cs" />
49+
<Compile Include="ServicesShouldOnlyHaveDependenciesWithLesserLifetime.cs" />
50+
</ItemGroup>
51+
<ItemGroup>
52+
<None Include="packages.config" />
53+
<None Include="TestStack.ConventionTests.Autofac.nuspec" />
54+
</ItemGroup>
55+
<ItemGroup>
56+
<ProjectReference Include="..\TestStack.ConventionTests\TestStack.ConventionTests.csproj">
57+
<Project>{955B0236-089F-434D-BA02-63A1E24C2B7C}</Project>
58+
<Name>TestStack.ConventionTests</Name>
59+
</ProjectReference>
60+
</ItemGroup>
61+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
62+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
63+
Other similar extension points exist, see Microsoft.Common.targets.
64+
<Target Name="BeforeBuild">
65+
</Target>
66+
<Target Name="AfterBuild">
67+
</Target>
68+
-->
69+
</Project>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<ProjectConfiguration>
2+
<CopyReferencedAssembliesToWorkspace>false</CopyReferencedAssembliesToWorkspace>
3+
<ConsiderInconclusiveTestsAsPassing>false</ConsiderInconclusiveTestsAsPassing>
4+
<PreloadReferencedAssemblies>false</PreloadReferencedAssemblies>
5+
<AllowDynamicCodeContractChecking>true</AllowDynamicCodeContractChecking>
6+
<AllowStaticCodeContractChecking>false</AllowStaticCodeContractChecking>
7+
<IgnoreThisComponentCompletely>false</IgnoreThisComponentCompletely>
8+
<RunPreBuildEvents>false</RunPreBuildEvents>
9+
<RunPostBuildEvents>false</RunPostBuildEvents>
10+
<PreviouslyBuiltSuccessfully>true</PreviouslyBuiltSuccessfully>
11+
<InstrumentAssembly>true</InstrumentAssembly>
12+
<PreventSigningOfAssembly>false</PreventSigningOfAssembly>
13+
<AnalyseExecutionTimes>true</AnalyseExecutionTimes>
14+
<IncludeStaticReferencesInWorkspace>true</IncludeStaticReferencesInWorkspace>
15+
<DefaultTestTimeout>60000</DefaultTestTimeout>
16+
<UseBuildConfiguration />
17+
<UseBuildPlatform />
18+
<ProxyProcessPath />
19+
<UseCPUArchitecture>AutoDetect</UseCPUArchitecture>
20+
<MSTestThreadApartmentState>STA</MSTestThreadApartmentState>
21+
<BuildProcessArchitecture>x86</BuildProcessArchitecture>
22+
</ProjectConfiguration>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0"?>
2+
<package >
3+
<metadata>
4+
<version>$version$</version>
5+
<authors>Krzysztof Kozmic, Jake Ginnivan</authors>
6+
<owners>Krzysztof Kozmic, Jake Ginnivan</owners>
7+
<id>TestStack.ConventionTests.Autofac</id>
8+
<title>TestStack.ConventionTests.Autofac</title>
9+
<tags>xunit nunit convention testing documentation autofac</tags>
10+
<requireLicenseAcceptance>false</requireLicenseAcceptance>
11+
<description>Simple convention-tester</description>
12+
<summary>A selection of pre-packaged conventions to validate autofac</summary>
13+
<projectUrl>https://github.com/TestStack/ConventionTests</projectUrl>
14+
<licenseUrl>https://github.com/TestStack/ConventionTests/blob/master/license.txt</licenseUrl>
15+
<dependencies>
16+
<dependency id="TestStack.ConventionTests" />
17+
<dependency id="Autofac" />
18+
</dependencies>
19+
</metadata>
20+
<files>
21+
<file src="TestStack.ConventionTests.Autofac.dll" target="lib\net40" />
22+
<file src="TestStack.ConventionTests.Autofac.pdb" target="lib\net40" />
23+
</files>
24+
</package>

0 commit comments

Comments
 (0)