Skip to content
This repository was archived by the owner on Apr 18, 2020. It is now read-only.

Commit a378b75

Browse files
authored
Merge pull request #27 from osoykan/dev
OnRegistering & OnConventionalRegistering events added #26
2 parents 4cebfe3 + c19f84c commit a378b75

File tree

8 files changed

+1073
-871
lines changed

8 files changed

+1073
-871
lines changed

common.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<Project>
22
<PropertyGroup>
3-
<VersionPrefix>3.0.2</VersionPrefix>
3+
<VersionPrefix>3.1.0</VersionPrefix>
44
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
55
<NoWarn>$(NoWarn);CS1591</NoWarn>
66
<PackageIconUrl>https://raw.githubusercontent.com/osoykan/Stove/master/stove.png</PackageIconUrl>
Lines changed: 16 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,21 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
1+
using System.Collections.Generic;
42
using System.Reflection;
53

64
namespace Autofac.Extras.IocManager
75
{
8-
public static class AutofacExtensions
9-
{
10-
/// <summary>
11-
/// Helper for anonymouse resolvings <see cref="IocManager.Resolve{T}(object)" />
12-
/// </summary>
13-
/// <param name="this">The this.</param>
14-
/// <returns></returns>
15-
internal static IEnumerable<TypedParameter> GetTypedResolvingParameters(this object @this)
16-
{
17-
foreach (PropertyInfo propertyInfo in @this.GetType().GetTypeInfo().GetProperties())
18-
{
19-
yield return new TypedParameter(propertyInfo.PropertyType, propertyInfo.GetValue(@this, null));
20-
}
21-
}
22-
23-
/// <summary>
24-
/// Finds all types based <see cref="TLifetime" /> in given <see cref="Assembly" />
25-
/// </summary>
26-
/// <typeparam name="TLifetime">Lifetime of dependencies</typeparam>
27-
/// <param name="builder">Autofac's <see cref="ContainerBuilder" /></param>
28-
/// <param name="assembly">Assemby to search</param>
29-
internal static void RegisterDependenciesByAssembly<TLifetime>(this ContainerBuilder builder, Assembly assembly) where TLifetime : ILifetime
30-
{
31-
typeof(TLifetime)
32-
.AssignedTypesInAssembly(assembly)
33-
.ForEach(builder.RegisterApplyingLifetime<TLifetime>);
34-
}
35-
36-
/// <summary>
37-
/// Registers given type according to it's lifetime. Type can be generic or not.
38-
/// </summary>
39-
/// <typeparam name="TLifetime">Lifetime of dependency</typeparam>
40-
/// <param name="builder">Autofac's <see cref="ContainerBuilder" /></param>
41-
/// <param name="typeToRegister">Type to register Autofac Container</param>
42-
internal static void RegisterApplyingLifetime<TLifetime>(this ContainerBuilder builder, Type typeToRegister) where TLifetime : ILifetime
43-
{
44-
List<Type> defaultInterfaces = typeToRegister.GetDefaultInterfaces().ToList();
45-
46-
if (typeToRegister.GetTypeInfo().IsGenericTypeDefinition)
47-
{
48-
List<Type> defaultGenerics = defaultInterfaces.Where(t => t.GetTypeInfo().IsGenericType).ToList();
49-
AddStartableIfPossible(typeToRegister, defaultGenerics);
50-
builder.RegisterGeneric(typeToRegister)
51-
.As(defaultGenerics.ToArray())
52-
.AsSelf()
53-
.WithPropertyInjection()
54-
.ApplyLifeStyle(typeof(TLifetime));
55-
}
56-
else
57-
{
58-
List<Type> defaults = defaultInterfaces.Where(t => !t.GetTypeInfo().IsGenericType).ToList();
59-
AddStartableIfPossible(typeToRegister, defaults);
60-
builder.RegisterType(typeToRegister)
61-
.As(defaults.ToArray())
62-
.AsSelf()
63-
.WithPropertyInjection()
64-
.ApplyLifeStyle(typeof(TLifetime));
65-
}
66-
}
67-
68-
private static void AddStartableIfPossible(Type typeToRegister, ICollection<Type> defaultInterfaces)
69-
{
70-
if (typeToRegister.IsAssignableTo<IStartable>())
71-
{
72-
defaultInterfaces.Add(typeof(IStartable));
73-
}
74-
}
75-
}
6+
public static class AutofacExtensions
7+
{
8+
/// <summary>
9+
/// Helper for anonymouse resolvings <see cref="IocManager.Resolve{T}(object)" />
10+
/// </summary>
11+
/// <param name="this">The this.</param>
12+
/// <returns></returns>
13+
internal static IEnumerable<TypedParameter> GetTypedResolvingParameters(this object @this)
14+
{
15+
foreach (PropertyInfo propertyInfo in @this.GetType().GetTypeInfo().GetProperties())
16+
{
17+
yield return new TypedParameter(propertyInfo.PropertyType, propertyInfo.GetValue(@this, null));
18+
}
19+
}
20+
}
7621
}

0 commit comments

Comments
 (0)