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 #30 from osoykan/dev
Browse files Browse the repository at this point in the history
dev to master
  • Loading branch information
osoykan authored Jul 14, 2017
2 parents a378b75 + a19c9d1 commit f983efd
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 29 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.1.0</VersionPrefix>
<VersionPrefix>3.1.1</VersionPrefix>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<NoWarn>$(NoWarn);CS1591</NoWarn>
<PackageIconUrl>https://raw.githubusercontent.com/osoykan/Stove/master/stove.png</PackageIconUrl>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
<ItemGroup>
<PackageReference Include="Autofac" Version="4.6.0" />
<PackageReference Include="Autofac.Extras.DynamicProxy" Version="4.2.1" />
<PackageReference Include="Castle.Core" Version="4.1.0" />
<PackageReference Include="Castle.Core" Version="4.1.1" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net452' ">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;

namespace Autofac.Extras.IocManager
{
public class BeforeRegistrationCompletedEventArgs : EventArgs
{
public BeforeRegistrationCompletedEventArgs(ContainerBuilder containerBuilder)
{
this.ContainerBuilder = containerBuilder;
}

public ContainerBuilder ContainerBuilder { get; }
}
}
5 changes: 5 additions & 0 deletions src/Autofac.Extras.IocManager/IServiceRegistration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ public interface IServiceRegistration
/// </summary>
event EventHandler<OnRegisteringEventArgs> OnRegistering;

/// <summary>
/// Occurs when [before registration completed].
/// </summary>
event EventHandler<BeforeRegistrationCompletedEventArgs> BeforeRegistrationCompleted;

/// <summary>
/// Registers the specified lifetime.
/// </summary>
Expand Down
40 changes: 23 additions & 17 deletions src/Autofac.Extras.IocManager/ServiceRegistration.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;

Expand Down Expand Up @@ -66,6 +65,11 @@ public ServiceRegistration(ContainerBuilder containerBuilder)
/// </summary>
public event EventHandler<OnRegisteringEventArgs> OnRegistering;

/// <summary>
/// Occurs when [before registration completed].
/// </summary>
public event EventHandler<BeforeRegistrationCompletedEventArgs> BeforeRegistrationCompleted;

/// <summary>
/// Registers the specified lifetime.
/// </summary>
Expand Down Expand Up @@ -354,14 +358,14 @@ public void RegisterIfAbsent(Type serviceType, Type implementationType, Lifetime
.IfNotRegistered(serviceType);

_containerBuilder.Register(c => c.Resolve(implementationType))
.As(serviceType)
.WithPropertyInjection()
.OnActivating(args =>
{
object instance = _decoratorService.Decorate(args.Instance, new ResolverContext(new Resolver(args.Context)));
args.ReplaceInstance(instance);
})
.IfNotRegistered(serviceType);
.As(serviceType)
.WithPropertyInjection()
.OnActivating(args =>
{
object instance = _decoratorService.Decorate(args.Instance, new ResolverContext(new Resolver(args.Context)));
args.ReplaceInstance(instance);
})
.IfNotRegistered(serviceType);

registration.ApplyLifeStyle(lifetime);
}
Expand Down Expand Up @@ -596,6 +600,8 @@ public void RegisterAssemblyAsClosedTypesOf(
/// <returns></returns>
public IRootResolver CreateResolver(bool ignoreStartableComponents = false)
{
BeforeRegistrationCompleted?.Invoke(this, new BeforeRegistrationCompletedEventArgs(_containerBuilder));

IContainer container = _containerBuilder.Build(ignoreStartableComponents ? ContainerBuildOptions.IgnoreStartableComponents : ContainerBuildOptions.None);
_rootResolver = new RootResolver(container);

Expand Down Expand Up @@ -654,21 +660,21 @@ internal void RegisterApplyingLifetime<TLifetime>(ContainerBuilder builder, Type
AddStartableIfPossible(typeToRegister, defaultGenerics);
OnRegistering?.Invoke(this, new OnRegisteringEventArgs(_containerBuilder, typeToRegister, defaultGenerics.ToArray(), FindLifetime(typeof(TLifetime))));
builder.RegisterGeneric(typeToRegister)
.As(defaultGenerics.ToArray())
.AsSelf()
.WithPropertyInjection()
.ApplyLifeStyle(typeof(TLifetime));
.As(defaultGenerics.ToArray())
.AsSelf()
.WithPropertyInjection()
.ApplyLifeStyle(typeof(TLifetime));
}
else
{
List<Type> defaults = defaultInterfaces.Where(t => !t.GetTypeInfo().IsGenericType).ToList();
AddStartableIfPossible(typeToRegister, defaults);
OnRegistering?.Invoke(this, new OnRegisteringEventArgs(_containerBuilder, typeToRegister, defaults.ToArray(), FindLifetime(typeof(TLifetime))));
builder.RegisterType(typeToRegister)
.As(defaults.ToArray())
.AsSelf()
.WithPropertyInjection()
.ApplyLifeStyle(typeof(TLifetime));
.As(defaults.ToArray())
.AsSelf()
.WithPropertyInjection()
.ApplyLifeStyle(typeof(TLifetime));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<ProjectReference Include="..\..\src\Autofac.Extras.IocManager\Autofac.Extras.IocManager.csproj" />
<PackageReference Include="Autofac" Version="4.6.0" />
<PackageReference Include="Autofac.Extras.DynamicProxy" Version="4.2.1" />
<PackageReference Include="FakeItEasy" Version="3.4.1" />
<PackageReference Include="FakeItEasy" Version="3.4.2" />
<PackageReference Include="NSubstitute" Version="2.0.3" />
<PackageReference Include="Shouldly" Version="2.8.3" />
<PackageReference Include="xunit" Version="2.2.0" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FakeItEasy" Version="3.4.1" />
<PackageReference Include="FakeItEasy" Version="3.4.2" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Reflection;

using Autofac.Extras.IocManager.TestBase;

Expand All @@ -15,20 +13,32 @@ public class LastChanceOfRegistrationEvent_Tests : TestBaseWithIocBuilder
[Fact]
public void test()
{
var dbContexts = new List<Type>();

Building(builder =>
{
builder.RegisterServices(r =>
{
r.OnConventionalRegistering += (sender, args) => { };

r.OnRegistering += (sender, args) =>
r.OnRegistering += (sender, args) => { args.ContainerBuilder.RegisterType<CStoveDbContext>().As<IStoveDbContext>().AsSelf(); };

r.RegisterAssemblyByConvention(typeof(LastChanceOfRegistrationEvent_Tests).GetTypeInfo().Assembly);
});
});

LocalIocManager.Resolve<CStoveDbContext>().ShouldNotBeNull();
}

[Fact]
public void before_registration_completed_should_register_last_chance()
{
Building(builder =>
{
builder.RegisterServices(r =>
{
r.BeforeRegistrationCompleted += (sender, args) =>
{
args.ContainerBuilder.RegisterType<CStoveDbContext>().As<IStoveDbContext>().AsSelf();
};

r.RegisterAssemblyByConvention(typeof(LastChanceOfRegistrationEvent_Tests).GetTypeInfo().Assembly);
});
});

Expand Down

0 comments on commit f983efd

Please sign in to comment.