Skip to content

Commit 9944d0e

Browse files
committed
Code cleanup
1 parent f1bc32b commit 9944d0e

7 files changed

Lines changed: 49 additions & 41 deletions

File tree

src/UnityContainerAttributeRegistration/Attribute/RegisterFactoryAttribute.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class RegisterFactoryAttribute : System.Attribute
1414
/// <summary>
1515
/// Candidate for registration to <see cref="Unity" />.
1616
/// </summary>
17-
public RegisterFactoryAttribute([CanBeNull] Type from = null,
17+
public RegisterFactoryAttribute([CanBeNull] Type from = null,
1818
[CanBeNull] Type lifetimeManager = null)
1919
{
2020
From = from;

src/UnityContainerAttributeRegistration/Populator/FactoryPopulator.cs

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ public override IUnityContainer Populate(IUnityContainer container, IList<Type>
2222

2323
foreach(FactoryToRegister factoryToRegister in factoriesToRegister)
2424
{
25-
Type returnType = factoryToRegister.ReturnType;
26-
Func<IUnityContainer, Type, string, object> factory = factoryToRegister.Factory;
27-
IFactoryLifetimeManager lifetimeManager = factoryToRegister.LifetimeManager;
25+
Type returnType = factoryToRegister.ReturnType;
26+
Func<IUnityContainer, Type, string, object> factory = factoryToRegister.Factory;
27+
IFactoryLifetimeManager lifetimeManager = factoryToRegister.LifetimeManager;
2828

2929
container.RegisterFactory(returnType, factory, lifetimeManager);
3030
}
@@ -35,7 +35,7 @@ public override IUnityContainer Populate(IUnityContainer container, IList<Type>
3535
private IEnumerable<FactoryToRegister> GetInstancesToRegisterFor(IUnityContainer container, Type providerClassType)
3636
{
3737
object providerClassInstance = container.Resolve(providerClassType);
38-
MethodInfo[] methodInfos = providerClassType.GetMethods();
38+
MethodInfo[] methodInfos = providerClassType.GetMethods();
3939

4040
return methodInfos
4141
.Where(info => info.CustomAttributes.Any(data => data.AttributeType == typeof(RegisterFactoryAttribute)))
@@ -63,18 +63,21 @@ private FactoryToRegister CreateFactoryToRegisterFrom(MethodInfo info, object in
6363
? null
6464
: GetInstanceByType<IFactoryLifetimeManager>(attribute.LifetimeManager);
6565

66-
6766
return new FactoryToRegister(returnType, GetFactoryMethodFor(info, instance), lifetimeManager);
6867
}
6968

7069
private bool IsUnityFactorySignature(MethodInfo methodInfo)
7170
{
72-
var parameters = methodInfo.GetParameters();
71+
ParameterInfo[] parameters = methodInfo.GetParameters();
7372

7473
switch(parameters.Length)
7574
{
76-
case 1 when parameters[0].ParameterType == typeof(IUnityContainer):
77-
case 3 when parameters[0].ParameterType == typeof(IUnityContainer) && parameters[1].ParameterType == typeof(Type) && parameters[2].ParameterType == typeof(string):
75+
case 1 when parameters[0]
76+
.ParameterType == typeof(IUnityContainer):
77+
case 3 when parameters[0]
78+
.ParameterType == typeof(IUnityContainer) && parameters[1]
79+
.ParameterType == typeof(Type) && parameters[2]
80+
.ParameterType == typeof(string):
7881
{
7982
return true;
8083
}
@@ -87,10 +90,12 @@ private bool IsUnityFactorySignature(MethodInfo methodInfo)
8790

8891
private Func<IUnityContainer, Type, string, object> GetFactoryMethodFor(MethodInfo methodInfo, object instance)
8992
{
90-
return (container, typeValue, stringValue) => {
91-
IList<object> invokeParams = new List<object>{container};
93+
return (container, typeValue, stringValue) =>
94+
{
95+
IList<object> invokeParams = new List<object> {container};
9296

93-
if(methodInfo.GetParameters().Length == 3)
97+
if(methodInfo.GetParameters()
98+
.Length == 3)
9499
{
95100
invokeParams.Add(typeValue);
96101
invokeParams.Add(stringValue);
@@ -102,10 +107,12 @@ private Func<IUnityContainer, Type, string, object> GetFactoryMethodFor(MethodIn
102107

103108
private sealed class FactoryToRegister
104109
{
105-
public FactoryToRegister([NotNull] Type returnType, [NotNull] Func<IUnityContainer, Type, string, object> factory, [CanBeNull] IFactoryLifetimeManager lifetimeManager)
110+
public FactoryToRegister([NotNull] Type returnType,
111+
[NotNull] Func<IUnityContainer, Type, string, object> factory,
112+
[CanBeNull] IFactoryLifetimeManager lifetimeManager)
106113
{
107-
ReturnType = returnType;
108-
Factory = factory;
114+
ReturnType = returnType;
115+
Factory = factory;
109116
LifetimeManager = lifetimeManager;
110117
}
111118

src/UnityContainerAttributeRegistration/Populator/InstancePopulator.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
using Unity;
99
using Unity.Lifetime;
1010

11-
using UnityContainerAttributeRegistration.Provider;
1211
using UnityContainerAttributeRegistration.Attribute;
1312

1413

@@ -19,7 +18,7 @@ namespace UnityContainerAttributeRegistration.Populator
1918
/// </summary>
2019
internal class InstancePopulator : Populator
2120
{
22-
/// <inheritdoc cref="Populator"/>
21+
/// <inheritdoc cref="Populator" />
2322
/// <exception cref="InvalidOperationException">Class type must not be static or abstract.</exception>
2423
public override IUnityContainer Populate(IUnityContainer container, IList<Type> typesWithAttribute)
2524
{

src/UnityContainerAttributeRegistration/Populator/TypePopulator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace UnityContainerAttributeRegistration.Populator
1515
/// </summary>
1616
internal class TypePopulator : Populator
1717
{
18-
/// <inheritdoc cref="Populator"/>
18+
/// <inheritdoc cref="Populator" />
1919
/// <exception cref="InvalidOperationException">Class type must not be static or abstract.</exception>
2020
public override IUnityContainer Populate(IUnityContainer container, IList<Type> typesWithAttribute)
2121
{

src/UnityContainerAttributeRegistration/UnityContainerPopulator.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ public sealed class UnityContainerPopulator
2323
private readonly IAssemblyProvider appDomain;
2424

2525
[NotNull]
26-
private readonly IPopulator typePopulator;
26+
private readonly IPopulator typePopulator;
2727

2828
[NotNull]
29-
private readonly IPopulator instancePopulator;
29+
private readonly IPopulator instancePopulator;
3030

3131
[NotNull]
32-
private readonly IPopulator factoryPopulator;
32+
private readonly IPopulator factoryPopulator;
3333

3434
/// <summary>
3535
/// Use <see cref="System.AppDomain.CurrentDomain" /> to populate an <see cref="Unity.IUnityContainer" />
@@ -82,11 +82,11 @@ private IDictionary<Type, IList<Type>> FindAnnotatedTypes(TypeDefined typeDefine
8282
IEnumerable<Type> types = appDomain.GetAssemblies()
8383
.SelectMany(assembly => assembly.GetTypes());
8484

85-
IDictionary<Type, IList<Type>> typesPerAttribute = new Dictionary<Type, IList<Type>>()
86-
{
87-
{typeof(RegisterTypeAttribute), new List<Type>()},
88-
{typeof(RegisterProviderAttribute), new List<Type>()}
89-
};
85+
IDictionary<Type, IList<Type>> typesPerAttribute = new Dictionary<Type, IList<Type>>
86+
{
87+
{typeof(RegisterTypeAttribute), new List<Type>()},
88+
{typeof(RegisterProviderAttribute), new List<Type>()}
89+
};
9090

9191
foreach(Type classType in types)
9292
{
@@ -98,7 +98,7 @@ private IDictionary<Type, IList<Type>> FindAnnotatedTypes(TypeDefined typeDefine
9898
Type attributeType = kv.Key;
9999
IList<Type> typesWithAttribute = kv.Value;
100100

101-
if (classType.IsDefined(attributeType, typeDefined == TypeDefined.Inherit))
101+
if(classType.IsDefined(attributeType, typeDefined == TypeDefined.Inherit))
102102
{
103103
if(classType.IsStatic() || classType.IsAbstract)
104104
{

src/UnityContainerAttributeRegistrationTest/Assets/RegisterFactoryTestClasses/TestClasses.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,6 @@ public class ProviderWithInvalidReturnValueWithOneArgument
125125
public void Factory(IUnityContainer container)
126126
{
127127
}
128-
129128
}
130129

131130
[RegisterProvider]
@@ -135,7 +134,6 @@ public class ProviderWithInvalidReturnValueWithThreeArguments
135134
public void Factory(IUnityContainer container, Type typeValue, string stringValue)
136135
{
137136
}
138-
139137
}
140138

141139
[RegisterProvider]
@@ -146,7 +144,6 @@ public MyClass Factory(IUnityContainer container, string stringValue)
146144
{
147145
return new MyClass();
148146
}
149-
150147
}
151148

152149
[RegisterProvider]
@@ -157,7 +154,6 @@ public MyClass Factory(IUnityContainer container, Type typeValue)
157154
{
158155
return new MyClass();
159156
}
160-
161157
}
162158

163159
[RegisterProvider]
@@ -168,7 +164,6 @@ public MyClass Factory(IUnityContainer container, string stringValue, Type typeV
168164
{
169165
return new MyClass();
170166
}
171-
172167
}
173168

174169
[RegisterProvider]
@@ -179,7 +174,6 @@ public MyClass Factory(IUnityContainer container, Type typeValue, string stringV
179174
{
180175
return new MyClass();
181176
}
182-
183177
}
184178

185179
[RegisterProvider]

src/UnityContainerAttributeRegistrationTest/Attribute/RegisterFactoryAttributeTest.cs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,22 @@ public class RegisterFactoryAttributeTest : TestBase
2323
[TestCase(typeof(ProviderWithOneArgument), typeof(MyClass), typeof(MyClass), typeof(TransientLifetimeManager))]
2424
[TestCase(typeof(ProviderWithThreeArguments), typeof(MyClass), typeof(MyClass), typeof(TransientLifetimeManager))]
2525
[TestCase(typeof(ProviderUsingInterface), typeof(IMyInterface), typeof(IMyInterface), typeof(TransientLifetimeManager))]
26-
[TestCase(typeof(ProviderWithHierarchicalLifetimeManager), typeof(MyClass), typeof(MyClass), typeof(HierarchicalLifetimeManager))]
27-
[TestCase(typeof(ProviderWithSingletonLifetimeManager), typeof(MyClass), typeof(MyClass), typeof(SingletonLifetimeManager))]
28-
[TestCase(typeof(ProviderWithTransientLifetimeManager), typeof(MyClass), typeof(MyClass), typeof(TransientLifetimeManager))]
29-
[TestCase(typeof(ProviderWithContainerControlledLifetimeManager), typeof(MyClass), typeof(MyClass), typeof(ContainerControlledLifetimeManager))]
30-
[TestCase(typeof(ProviderWithContainerControlledTransientManager), typeof(MyClass), typeof(MyClass), typeof(ContainerControlledTransientManager))]
31-
[TestCase(typeof(ProviderWithExternallyControlledLifetimeManager), typeof(MyClass), typeof(MyClass), typeof(ExternallyControlledLifetimeManager))]
32-
[TestCase(typeof(ProviderWithPerResolveLifetimeManager), typeof(MyClass), typeof(MyClass), typeof(PerResolveLifetimeManager))]
33-
[TestCase(typeof(ProviderWithPerThreadLifetimeManager), typeof(MyClass), typeof(MyClass), typeof(PerThreadLifetimeManager))]
26+
[TestCase(typeof(ProviderWithHierarchicalLifetimeManager), typeof(MyClass), typeof(MyClass),
27+
typeof(HierarchicalLifetimeManager))]
28+
[TestCase(typeof(ProviderWithSingletonLifetimeManager), typeof(MyClass), typeof(MyClass),
29+
typeof(SingletonLifetimeManager))]
30+
[TestCase(typeof(ProviderWithTransientLifetimeManager), typeof(MyClass), typeof(MyClass),
31+
typeof(TransientLifetimeManager))]
32+
[TestCase(typeof(ProviderWithContainerControlledLifetimeManager), typeof(MyClass), typeof(MyClass),
33+
typeof(ContainerControlledLifetimeManager))]
34+
[TestCase(typeof(ProviderWithContainerControlledTransientManager), typeof(MyClass), typeof(MyClass),
35+
typeof(ContainerControlledTransientManager))]
36+
[TestCase(typeof(ProviderWithExternallyControlledLifetimeManager), typeof(MyClass), typeof(MyClass),
37+
typeof(ExternallyControlledLifetimeManager))]
38+
[TestCase(typeof(ProviderWithPerResolveLifetimeManager), typeof(MyClass), typeof(MyClass),
39+
typeof(PerResolveLifetimeManager))]
40+
[TestCase(typeof(ProviderWithPerThreadLifetimeManager), typeof(MyClass), typeof(MyClass),
41+
typeof(PerThreadLifetimeManager))]
3442
public void TestPopulate(Type providerType, Type expectedFrom, Type expectedTo, Type expectedFactoryLifetimeManagerType)
3543
{
3644
Scope scope = new Scope();

0 commit comments

Comments
 (0)