This repository has been archived by the owner on Apr 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from osoykan/dev
OnRegistering & OnConventionalRegistering events added #26
- Loading branch information
Showing
8 changed files
with
1,073 additions
and
871 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,76 +1,21 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Collections.Generic; | ||
using System.Reflection; | ||
|
||
namespace Autofac.Extras.IocManager | ||
{ | ||
public static class AutofacExtensions | ||
{ | ||
/// <summary> | ||
/// Helper for anonymouse resolvings <see cref="IocManager.Resolve{T}(object)" /> | ||
/// </summary> | ||
/// <param name="this">The this.</param> | ||
/// <returns></returns> | ||
internal static IEnumerable<TypedParameter> GetTypedResolvingParameters(this object @this) | ||
{ | ||
foreach (PropertyInfo propertyInfo in @this.GetType().GetTypeInfo().GetProperties()) | ||
{ | ||
yield return new TypedParameter(propertyInfo.PropertyType, propertyInfo.GetValue(@this, null)); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Finds all types based <see cref="TLifetime" /> in given <see cref="Assembly" /> | ||
/// </summary> | ||
/// <typeparam name="TLifetime">Lifetime of dependencies</typeparam> | ||
/// <param name="builder">Autofac's <see cref="ContainerBuilder" /></param> | ||
/// <param name="assembly">Assemby to search</param> | ||
internal static void RegisterDependenciesByAssembly<TLifetime>(this ContainerBuilder builder, Assembly assembly) where TLifetime : ILifetime | ||
{ | ||
typeof(TLifetime) | ||
.AssignedTypesInAssembly(assembly) | ||
.ForEach(builder.RegisterApplyingLifetime<TLifetime>); | ||
} | ||
|
||
/// <summary> | ||
/// Registers given type according to it's lifetime. Type can be generic or not. | ||
/// </summary> | ||
/// <typeparam name="TLifetime">Lifetime of dependency</typeparam> | ||
/// <param name="builder">Autofac's <see cref="ContainerBuilder" /></param> | ||
/// <param name="typeToRegister">Type to register Autofac Container</param> | ||
internal static void RegisterApplyingLifetime<TLifetime>(this ContainerBuilder builder, Type typeToRegister) where TLifetime : ILifetime | ||
{ | ||
List<Type> defaultInterfaces = typeToRegister.GetDefaultInterfaces().ToList(); | ||
|
||
if (typeToRegister.GetTypeInfo().IsGenericTypeDefinition) | ||
{ | ||
List<Type> defaultGenerics = defaultInterfaces.Where(t => t.GetTypeInfo().IsGenericType).ToList(); | ||
AddStartableIfPossible(typeToRegister, defaultGenerics); | ||
builder.RegisterGeneric(typeToRegister) | ||
.As(defaultGenerics.ToArray()) | ||
.AsSelf() | ||
.WithPropertyInjection() | ||
.ApplyLifeStyle(typeof(TLifetime)); | ||
} | ||
else | ||
{ | ||
List<Type> defaults = defaultInterfaces.Where(t => !t.GetTypeInfo().IsGenericType).ToList(); | ||
AddStartableIfPossible(typeToRegister, defaults); | ||
builder.RegisterType(typeToRegister) | ||
.As(defaults.ToArray()) | ||
.AsSelf() | ||
.WithPropertyInjection() | ||
.ApplyLifeStyle(typeof(TLifetime)); | ||
} | ||
} | ||
|
||
private static void AddStartableIfPossible(Type typeToRegister, ICollection<Type> defaultInterfaces) | ||
{ | ||
if (typeToRegister.IsAssignableTo<IStartable>()) | ||
{ | ||
defaultInterfaces.Add(typeof(IStartable)); | ||
} | ||
} | ||
} | ||
public static class AutofacExtensions | ||
{ | ||
/// <summary> | ||
/// Helper for anonymouse resolvings <see cref="IocManager.Resolve{T}(object)" /> | ||
/// </summary> | ||
/// <param name="this">The this.</param> | ||
/// <returns></returns> | ||
internal static IEnumerable<TypedParameter> GetTypedResolvingParameters(this object @this) | ||
{ | ||
foreach (PropertyInfo propertyInfo in @this.GetType().GetTypeInfo().GetProperties()) | ||
{ | ||
yield return new TypedParameter(propertyInfo.PropertyType, propertyInfo.GetValue(@this, null)); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.