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+ }
0 commit comments