Skip to content

Commit 8d6a94c

Browse files
committed
Releasing v1.0.0
1 parent bb2dbd4 commit 8d6a94c

14 files changed

+121
-271
lines changed

package.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<Version>1.0.0</Version>
5-
<PackageReleaseNotes>This package is distributed as .NET Standard 1.0 and .NET Framework 4.7 package.</PackageReleaseNotes>
5+
<PackageReleaseNotes>This package is distributed as .NET Standard 1.0 package.</PackageReleaseNotes>
66
</PropertyGroup>
77

88
<PropertyGroup>

src/Aggregate.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22
using System;
33
using System.Collections.Generic;
44
using System.Linq;
5-
using Unity;
5+
using System.Reflection;
66
using Unity.Injection;
7-
using Unity.Lifetime;
87

98
namespace Unity.Microsoft.DependencyInjection
109
{
@@ -39,28 +38,29 @@ public void Register()
3938
Container.RegisterType(Type, Last.GetLifetime(Container),
4039
new InjectionFactory((c, t, s) =>
4140
{
42-
if (Last.ServiceType.IsGenericTypeDefinition)
41+
if (Last.ServiceType.GetTypeInfo().IsGenericTypeDefinition)
4342
return c.Resolve(t, Last.GetImplementationType().FullName);
4443
var instance = Resolve(c);
4544
return instance;
4645
}));
4746

4847
var enumType = typeof(IEnumerable<>).MakeGenericType(Type);
49-
Container.RegisterType(enumType, new HierarchicalTransientLifetimeManager(Container),
48+
Container.RegisterType(enumType, new HierarchicalTransientLifetimeManager(),
5049
new InjectionFactory(c =>
5150
{
5251
List<object> instances = new List<object>();
5352
foreach (var serv in Services)
5453
{
55-
if (!serv.ServiceType.IsGenericTypeDefinition)
54+
if (!serv.ServiceType.GetTypeInfo().IsGenericTypeDefinition)
5655
{
5756
var qualifier = serv.GetImplementationType().FullName;
5857
var instance = Container.Resolve(serv.ServiceType, qualifier);
5958
instances.Add(instance);
6059
}
6160
}
6261
return typeof(Enumerable)
63-
.GetMethod("Cast")
62+
.GetTypeInfo()
63+
.GetDeclaredMethod("Cast")
6464
.MakeGenericMethod(Type)
6565
.Invoke(null, new[] { instances });
6666
}));

src/Aggregates.cs

Lines changed: 0 additions & 29 deletions
This file was deleted.

src/Configuration.cs

Lines changed: 23 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,33 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4-
4+
using System.Reflection;
55
using Microsoft.Extensions.DependencyInjection;
6-
76
using Unity.Injection;
8-
using Unity.Lifetime;
97

108
namespace Unity.Microsoft.DependencyInjection
119
{
12-
public static class Configuration
10+
internal static class Configuration
1311
{
14-
static Aggregates _aggregates;
12+
static List<Aggregate> _aggregates;
1513

16-
public static IServiceProvider Configure(this IUnityContainer container, IServiceCollection services)
14+
internal static void Configure(this IUnityContainer container, IServiceCollection services)
1715
{
18-
container.AddNewExtension<MDIExtension>();
19-
20-
var provider = new ServiceProvider(container);
21-
2216
var aggregateTypes = GetAggregateTypes(services);
2317

24-
var aggregateList = aggregateTypes.Select(t => new Aggregate(t, container)).ToList();
25-
_aggregates = new Aggregates(aggregateList);
18+
_aggregates = aggregateTypes.Select(t => new Aggregate(t, container)).ToList();
2619
container.RegisterInstance(_aggregates);
2720

2821
// Configure all registrations into Unity
2922
foreach (var serviceDescriptor in services)
3023
{
3124
container.RegisterType(serviceDescriptor, _aggregates);
3225
}
33-
_aggregates.Register();
3426

35-
container.RegisterInstance<IServiceScopeFactory>(provider);
36-
container.RegisterType<TransientObjectPool>(new HierarchicalLifetimeManager());
37-
38-
return provider;
27+
foreach (var type in _aggregates)
28+
{
29+
type.Register();
30+
}
3931
}
4032

4133
internal static void Register(this IUnityContainer container,
@@ -61,26 +53,20 @@ internal static void Register(this IUnityContainer container,
6153

6254
private static HashSet<Type> GetAggregateTypes(IServiceCollection services)
6355
{
64-
var aggregateTypes = new HashSet<Type>
65-
(
66-
services.
67-
GroupBy
68-
(
69-
serviceDescriptor => serviceDescriptor.ServiceType,
70-
serviceDescriptor => serviceDescriptor
71-
).
72-
Where(typeGrouping => typeGrouping.Count() > 1).
73-
Select(type => type.Key)
74-
);
75-
return aggregateTypes;
56+
var enumerable = services.GroupBy(serviceDescriptor => serviceDescriptor.ServiceType,
57+
serviceDescriptor => serviceDescriptor)
58+
.Where(typeGrouping => typeGrouping.Count() > 1)
59+
.Select(type => type.Key);
60+
61+
return new HashSet<Type>(enumerable);
7662
}
7763

7864

7965

8066
private static void RegisterType(this IUnityContainer container,
81-
ServiceDescriptor serviceDescriptor, Aggregates aggregates)
67+
ServiceDescriptor serviceDescriptor, List<Aggregate> aggregates)
8268
{
83-
var aggregate = aggregates.Get(serviceDescriptor.ServiceType);
69+
var aggregate = aggregates.FirstOrDefault(a => a.Type == serviceDescriptor.ServiceType);
8470
if (aggregate != null)
8571
aggregate.AddService(serviceDescriptor);
8672
else
@@ -121,17 +107,19 @@ private static void RegisterSingleton(this IUnityContainer container,
121107

122108
internal static bool CanResolve(this IUnityContainer container, Type type)
123109
{
124-
if (type.IsClass && !type.IsAbstract)
110+
var info = type.GetTypeInfo();
111+
112+
if (info.IsClass && !info.IsAbstract)
125113
{
126-
if (typeof(Delegate).IsAssignableFrom(type) || typeof(string) == type || type.IsEnum
127-
|| type.IsArray || type.IsPrimitive)
114+
if (typeof(Delegate).GetTypeInfo().IsAssignableFrom(info) || typeof(string) == type || info.IsEnum
115+
|| type.IsArray || info.IsPrimitive)
128116
{
129117
return container.IsRegistered(type);
130118
}
131119
return true;
132120
}
133121

134-
if (type.IsGenericType)
122+
if (info.IsGenericType)
135123
{
136124
var gerericType = type.GetGenericTypeDefinition();
137125
if ((gerericType == typeof(IEnumerable<>)) ||

src/ConstructorSelectorPolicy.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using Unity;
2-
using System;
1+
using System;
32
using System.Collections.Generic;
43
using System.Linq;
54
using System.Reflection;
@@ -39,12 +38,13 @@ private ConstructorInfo FindDependencyConstructor<T>(IBuilderContext context)
3938
IEnumerable<ConstructorInfo> constructors = context.BuildKey.Type.GetTypeInfo()
4039
.DeclaredConstructors.Where(c => (!c.IsStatic) && c.IsPublic);
4140

42-
ConstructorInfo[] injectionConstructors = constructors
41+
var constructorInfos = constructors as ConstructorInfo[] ?? constructors.ToArray();
42+
ConstructorInfo[] injectionConstructors = constructorInfos
4343
.Where(ctor => ctor.IsDefined(typeOfAttribute, true))
4444
.ToArray();
4545
switch (injectionConstructors.Length)
4646
{
47-
case 0: return FindSingleConstructor(constructors) ?? Other(constructors.ToArray(), context);
47+
case 0: return FindSingleConstructor(constructorInfos) ?? Other(constructorInfos.ToArray(), context);
4848
case 1: return injectionConstructors[0];
4949
default:
5050
throw new InvalidOperationException(
@@ -77,8 +77,8 @@ private ConstructorInfo Other(ConstructorInfo[] constructors, IBuilderContext co
7777
var qtd = b.GetParameters().Length.CompareTo(a.GetParameters().Length);
7878
if (qtd == 0)
7979
{
80-
return b.GetParameters().Sum(p => p.ParameterType.IsInterface ? 1 : 0)
81-
.CompareTo(a.GetParameters().Sum(p => p.ParameterType.IsInterface ? 1 : 0));
80+
return b.GetParameters().Sum(p => p.ParameterType.GetTypeInfo().IsInterface ? 1 : 0)
81+
.CompareTo(a.GetParameters().Sum(p => p.ParameterType.GetTypeInfo().IsInterface ? 1 : 0));
8282
}
8383
return qtd;
8484
});
@@ -110,8 +110,8 @@ private ConstructorInfo Other(ConstructorInfo[] constructors, IBuilderContext co
110110

111111
if (!bestConstructorParameterTypes.IsSupersetOf(parameters.Select(p => p.ParameterType)))
112112
{
113-
if (bestConstructorParameterTypes.All(p => p.IsInterface)
114-
&& !parameters.All(p => p.ParameterType.IsInterface))
113+
if (bestConstructorParameterTypes.All(p => p.GetTypeInfo().IsInterface)
114+
&& !parameters.All(p => p.ParameterType.GetTypeInfo().IsInterface))
115115
return bestConstructor;
116116

117117
var msg = $"Falha ao procurar um construtor para {context.BuildKey.Type.FullName}\n" +
Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
1-
using Unity.Lifetime;
1+
using System;
2+
using Unity.Lifetime;
23

34
namespace Unity.Microsoft.DependencyInjection
45
{
56
/// <summary>
67
/// A special lifetime manager which works like <see cref="TransienLifetimeManager"/>,
7-
/// except that in the presence of child containers, each child gets it's own instance
8-
/// of the object, instead of sharing one in the common parent.
8+
/// except it makes container remember all Disposable objects it created. Once container
9+
/// is disposed all these objects are disposed as well.
910
/// </summary>
10-
internal class HierarchicalTransientLifetimeManager : HierarchicalLifetimeManager
11+
internal class HierarchicalTransientLifetimeManager : LifetimeManager
1112
{
12-
private IUnityContainer _container;
13-
14-
public HierarchicalTransientLifetimeManager(IUnityContainer container)
15-
{
16-
_container = container;
17-
}
18-
1913
public override void SetValue(object newValue, ILifetimeContainer container = null)
2014
{
21-
_container.Resolve<TransientObjectPool>().Add(newValue);
15+
if (newValue is IDisposable disposable)
16+
container?.Add(disposable);
2217
}
2318

2419
public override object GetValue(ILifetimeContainer container = null)
2520
{
2621
return null;
2722
}
2823

29-
protected override void Dispose(bool disposing)
24+
public override void RemoveValue(ILifetimeContainer container = null)
3025
{
31-
_container = null;
32-
base.Dispose(disposing);
3326
}
27+
28+
protected override LifetimeManager OnCreateLifetimeManager()
29+
{
30+
return this;
31+
}
32+
33+
public override bool InUse { get => false; set => base.InUse = false; }
3434
}
3535
}

src/ServiceDescriptorExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ internal static LifetimeManager GetLifetime(this ServiceDescriptor serviceDescri
3333
case ServiceLifetime.Singleton:
3434
return new ContainerControlledLifetimeManager();
3535
case ServiceLifetime.Transient:
36-
return new HierarchicalTransientLifetimeManager(container);
36+
return new HierarchicalTransientLifetimeManager();
3737
default:
3838
throw new NotImplementedException(
3939
$"Unsupported lifetime manager type '{serviceDescriptor.Lifetime}'");

0 commit comments

Comments
 (0)