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

Commit

Permalink
BeforeRegistrationCompletedEvent implemented #29
Browse files Browse the repository at this point in the history
  • Loading branch information
osoykan committed Jul 14, 2017
1 parent 9c36762 commit a19c9d1
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 25 deletions.
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
@@ -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 a19c9d1

Please sign in to comment.