Skip to content

Commit 3e9ec3e

Browse files
committed
Code cleanup
1 parent a60072e commit 3e9ec3e

6 files changed

Lines changed: 45 additions & 34 deletions

File tree

src/UnityContainerAttributeRegistration/Attribute/RegisterInstanceAttribute.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace UnityContainerAttributeRegistration.Attribute
99
/// Mark a property to be registered to an <see cref="Unity.IUnityContainer" />
1010
/// </summary>
1111
[AttributeUsage(AttributeTargets.Property)]
12-
public class RegisterInstanceAttribute: System.Attribute
12+
public class RegisterInstanceAttribute : System.Attribute
1313
{
1414
/// <summary>
1515
/// Candidate for registration to <see cref="Unity" />.

src/UnityContainerAttributeRegistration/Attribute/RegisterInstanceProviderAttribute.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
using System;
22

3+
34
namespace UnityContainerAttributeRegistration.Attribute
45
{
56
/// <summary>
6-
/// Mark a type to be a provider for <see cref="RegisterInstanceAttribute"/>
7+
/// Mark a type to be a provider for <see cref="RegisterInstanceAttribute" />
78
/// </summary>
89
[AttributeUsage(AttributeTargets.Class)]
910
public class RegisterInstanceProviderAttribute : System.Attribute

src/UnityContainerAttributeRegistration/Populator/InstancePopulator.cs

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,10 @@ public override IUnityContainer Populate(IUnityContainer container)
5151
}
5252

5353
/// <summary>
54-
/// Create a list of <see cref="InstanceToRegister"/> depending on class marked with <see cref="RegisterInstanceProviderAttribute"/>
54+
/// Create a list of <see cref="InstanceToRegister" /> depending on class marked with <see cref="RegisterInstanceProviderAttribute" />
5555
/// </summary>
56-
/// <param name="container"><see cref="IUnityContainer"/> to resolve <paramref name="providerClassType"/></param>
57-
/// <param name="providerClassType">Class type used to search for <see cref="RegisterInstanceAttribute"/></param>
56+
/// <param name="container"><see cref="IUnityContainer" /> to resolve <paramref name="providerClassType" /></param>
57+
/// <param name="providerClassType">Class type used to search for <see cref="RegisterInstanceAttribute" /></param>
5858
/// <returns>List of instances to register with all needed parameters</returns>
5959
/// <exception cref="InvalidOperationException"><paramref name="providerClassType" /> type must not be static or abstract.</exception>
6060
private IEnumerable<InstanceToRegister> GetInstancesToRegisterFor(IUnityContainer container, Type providerClassType)
@@ -65,24 +65,28 @@ private IEnumerable<InstanceToRegister> GetInstancesToRegisterFor(IUnityContaine
6565
$"Class type must not be static or abstract to be used with RegisterTypeAttribute: {providerClassType.FullName}");
6666
}
6767

68-
object providerClassInstance = container.Resolve(providerClassType);
69-
PropertyInfo[] properties = providerClassType.GetProperties();
70-
71-
return properties.Where(info => info.CustomAttributes.Any(data => data.AttributeType == typeof(RegisterInstanceAttribute)))
72-
.Select(info =>
73-
{
74-
object instance = info.GetValue(providerClassInstance);
75-
RegisterInstanceAttribute attribute = info.GetCustomAttribute<RegisterInstanceAttribute>();
76-
Type from = attribute.From;
77-
IInstanceLifetimeManager lifetimeManager = attribute.LifetimeManager == null ? null : GetInstanceByType<IInstanceLifetimeManager>(attribute.LifetimeManager);
78-
79-
return new InstanceToRegister(instance, from, lifetimeManager);
80-
})
81-
.ToList();
68+
object providerClassInstance = container.Resolve(providerClassType);
69+
PropertyInfo[] properties = providerClassType.GetProperties();
70+
71+
return properties
72+
.Where(info => info.CustomAttributes.Any(data => data.AttributeType == typeof(RegisterInstanceAttribute)))
73+
.Select(info =>
74+
{
75+
object instance = info.GetValue(providerClassInstance);
76+
RegisterInstanceAttribute attribute = info.GetCustomAttribute<RegisterInstanceAttribute>();
77+
Type from = attribute.From;
78+
IInstanceLifetimeManager lifetimeManager =
79+
attribute.LifetimeManager == null
80+
? null
81+
: GetInstanceByType<IInstanceLifetimeManager>(attribute.LifetimeManager);
82+
83+
return new InstanceToRegister(instance, from, lifetimeManager);
84+
})
85+
.ToList();
8286
}
8387

8488
/// <summary>
85-
/// Wrapper to regsiter isntances
89+
/// Wrapper to regsiter isntances
8690
/// </summary>
8791
private sealed class InstanceToRegister
8892
{
@@ -96,19 +100,19 @@ public InstanceToRegister([CanBeNull] object instance,
96100
}
97101

98102
/// <summary>
99-
/// Concrete instance to register
103+
/// Concrete instance to register
100104
/// </summary>
101105
[CanBeNull]
102106
public object Instance { get; }
103107

104108
/// <summary>
105-
/// Requested type
109+
/// Requested type
106110
/// </summary>
107111
[CanBeNull]
108112
public Type Type { get; }
109113

110114
/// <summary>
111-
/// Used lifetime manager
115+
/// Used lifetime manager
112116
/// </summary>
113117
[CanBeNull]
114118
public IInstanceLifetimeManager LifetimeManager { get; }

src/UnityContainerAttributeRegistration/UnityContainerPopulator.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ namespace UnityContainerAttributeRegistration
1313
/// </summary>
1414
public sealed class UnityContainerPopulator
1515
{
16-
private readonly IPopulator typePopulator;
17-
private readonly IPopulator instancePopulator;
16+
private readonly IPopulator typePopulator;
17+
private readonly IPopulator instancePopulator;
1818

1919
/// <summary>
2020
/// Use <see cref="System.AppDomain.CurrentDomain" /> to populate an <see cref="Unity.IUnityContainer" />
@@ -29,7 +29,7 @@ public UnityContainerPopulator() : this(new AssemblyProvider())
2929
/// <param name="appDomain">Custom <see cref="IAssemblyProvider" /></param>
3030
public UnityContainerPopulator([NotNull] IAssemblyProvider appDomain)
3131
{
32-
typePopulator = new TypePopulator(appDomain);
32+
typePopulator = new TypePopulator(appDomain);
3333
instancePopulator = new InstancePopulator(appDomain);
3434
}
3535

src/UnityContainerAttributeRegistrationTest/Attribute/RegisterInstanceAttributeTest.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,23 @@
1414

1515
using static NUnit.Framework.Assert;
1616

17+
1718
namespace UnityContainerAttributeRegistrationTest.Attribute
1819
{
1920
public class RegisterInstanceAttributeTest : TestBase
2021
{
2122
[Test]
2223
[TestCase(typeof(DefaultProvider), typeof(AnyClass), typeof(AnyClass), typeof(ContainerControlledLifetimeManager))]
23-
[TestCase(typeof(ProviderUsingFromWithoutLifetimeManager), typeof(IAnyInterface), typeof(AnyClass), typeof(ContainerControlledLifetimeManager))]
24-
[TestCase(typeof(ProviderUsingFromWithSingletonLifetimeManager), typeof(IAnyInterface), typeof(AnyClass), typeof(SingletonLifetimeManager))]
25-
[TestCase(typeof(ProviderUsingFromWithContainerControlledLifetimeManager), typeof(IAnyInterface), typeof(AnyClass), typeof(ContainerControlledLifetimeManager))]
26-
[TestCase(typeof(ProviderUsingFromWithExternallyControlledLifetimeManager), typeof(IAnyInterface), typeof(AnyClass), typeof(ExternallyControlledLifetimeManager))]
27-
[TestCase(typeof(ProviderWithExternallyControlledLifetimeManager), typeof(AnyClass), typeof(AnyClass), typeof(ExternallyControlledLifetimeManager))]
24+
[TestCase(typeof(ProviderUsingFromWithoutLifetimeManager), typeof(IAnyInterface), typeof(AnyClass),
25+
typeof(ContainerControlledLifetimeManager))]
26+
[TestCase(typeof(ProviderUsingFromWithSingletonLifetimeManager), typeof(IAnyInterface), typeof(AnyClass),
27+
typeof(SingletonLifetimeManager))]
28+
[TestCase(typeof(ProviderUsingFromWithContainerControlledLifetimeManager), typeof(IAnyInterface), typeof(AnyClass),
29+
typeof(ContainerControlledLifetimeManager))]
30+
[TestCase(typeof(ProviderUsingFromWithExternallyControlledLifetimeManager), typeof(IAnyInterface), typeof(AnyClass),
31+
typeof(ExternallyControlledLifetimeManager))]
32+
[TestCase(typeof(ProviderWithExternallyControlledLifetimeManager), typeof(AnyClass), typeof(AnyClass),
33+
typeof(ExternallyControlledLifetimeManager))]
2834
public void TestPopulate(Type providerType, Type expectedFrom, Type expectedTo, Type expectedInstanceLifetimeManagerType)
2935
{
3036
Scope scope = new Scope();

src/UnityContainerAttributeRegistrationTest/Helper/TestBase.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ protected bool IsUnityContainerRegistration(IContainerRegistration registration)
1616
}
1717

1818
protected bool IsExpectedRegisteredContainer(IContainerRegistration registration,
19-
Type expectedFrom,
20-
Type expectedTo,
21-
Type expectedTypeLifetimeManagerType)
19+
Type expectedFrom,
20+
Type expectedTo,
21+
Type expectedTypeLifetimeManagerType)
2222
{
2323
bool registeredType = registration.RegisteredType == expectedFrom;
2424
bool mappedToType = registration.MappedToType == expectedTo;

0 commit comments

Comments
 (0)