- instance passed to onCreate can't be null

master
Simon G. 1 year ago
parent c5b8ffd885
commit acbce3b412
Signed by: SimonG
GPG Key ID: 7CD6DBFB1B2786ED
  1. 13
      LightweightIocContainer/Interfaces/Registrations/Fluent/IOnCreate.cs
  2. 4
      LightweightIocContainer/IocContainer.cs
  3. 7
      LightweightIocContainer/LightweightIocContainer.xml
  4. 2
      LightweightIocContainer/Registrations/MultipleMultitonRegistration.cs
  5. 8
      LightweightIocContainer/Registrations/MultipleRegistration.cs
  6. 16
      LightweightIocContainer/Registrations/TypedRegistration.cs

@ -15,8 +15,8 @@ public interface IOnCreate
/// This <see cref="Action"/> is invoked when an instance of this type is created.
/// <para>Can be set in the <see cref="IIocInstaller"/> by calling <see cref="IOnCreate{TInterface, TImplementation}.OnCreate"/></para>
/// </summary>
internal Action<object?>? OnCreateAction { get; }
internal Func<object?, Task>? OnCreateActionAsync { get; }
internal Action<object>? OnCreateAction { get; }
internal Func<object, Task>? OnCreateActionAsync { get; }
}
/// <summary>
@ -31,7 +31,12 @@ public interface IOnCreate<TInterface, TImplementation> : IOnCreate where TImple
/// </summary>
/// <param name="action">The <see cref="Action{T}"/></param>
/// <returns>The current instance of this <see cref="ITypedRegistration{TInterface,TImplementation}"/></returns>
ITypedRegistration<TInterface, TImplementation> OnCreate(Action<TImplementation?> action);
ITypedRegistration<TInterface, TImplementation> OnCreate(Action<TImplementation> action);
ITypedRegistration<TInterface, TImplementation> OnCreateAsync(Func<TImplementation?, Task> action);
/// <summary>
/// Pass an <see cref="Action{T}"/> that will be invoked when an instance of this type is created
/// </summary>
/// <param name="action">The <see cref="Action{T}"/></param>
/// <returns>The current instance of this <see cref="ITypedRegistration{TInterface,TImplementation}"/></returns>
ITypedRegistration<TInterface, TImplementation> OnCreateAsync(Func<TImplementation, Task> action);
}

@ -398,7 +398,7 @@ public class IocContainer : IIocContainer, IIocResolver
if (registration is ILifestyleProvider { Lifestyle: Lifestyle.Singleton })
_singletons.Add((GetType<T>(registration), instance));
if (registration is IOnCreate onCreateRegistration)
if (registration is IOnCreate onCreateRegistration && instance is not null)
onCreateRegistration.OnCreateAction?.Invoke(instance);
return instance;
@ -407,7 +407,7 @@ public class IocContainer : IIocContainer, IIocResolver
private async Task<T> CreateInstanceAsync<T>(IRegistration registration, object?[]? arguments)
{
T instance = CreateInstance<T>(registration, arguments);
if (registration is IOnCreate { OnCreateActionAsync: not null } onCreateRegistration)
if (registration is IOnCreate { OnCreateActionAsync: not null } onCreateRegistration && instance is not null)
await onCreateRegistration.OnCreateActionAsync.Invoke(instance);
return instance;

@ -620,6 +620,13 @@
<param name="action">The <see cref="T:System.Action`1"/></param>
<returns>The current instance of this <see cref="T:LightweightIocContainer.Interfaces.Registrations.ITypedRegistration`2"/></returns>
</member>
<member name="M:LightweightIocContainer.Interfaces.Registrations.Fluent.IOnCreate`2.OnCreateAsync(System.Func{`1,System.Threading.Tasks.Task})">
<summary>
Pass an <see cref="T:System.Action`1"/> that will be invoked when an instance of this type is created
</summary>
<param name="action">The <see cref="T:System.Action`1"/></param>
<returns>The current instance of this <see cref="T:LightweightIocContainer.Interfaces.Registrations.ITypedRegistration`2"/></returns>
</member>
<member name="T:LightweightIocContainer.Interfaces.Registrations.Fluent.IWithDisposeStrategy">
<summary>
Provides a <see cref="T:LightweightIocContainer.DisposeStrategy"/> to an <see cref="T:LightweightIocContainer.Interfaces.Registrations.IRegistration"/>

@ -43,7 +43,7 @@ internal class MultipleMultitonRegistration<TInterface1, TInterface2, TImplement
/// </summary>
/// <param name="action">The <see cref="Action{T}"/></param>
/// <returns>The current instance of this <see cref="ITypedRegistration{TInterface,TImplementation}"/></returns>
public override ITypedRegistration<TInterface1, TImplementation> OnCreate(Action<TImplementation?> action)
public override ITypedRegistration<TInterface1, TImplementation> OnCreate(Action<TImplementation> action)
{
foreach (IRegistration registration in Registrations)
{

@ -62,7 +62,7 @@ internal class MultipleRegistration<TInterface1, TInterface2, TImplementation> :
/// </summary>
/// <param name="action">The <see cref="Action{T}"/></param>
/// <returns>The current instance of this <see cref="ITypedRegistration{TInterface,TImplementation}"/></returns>
public override ITypedRegistration<TInterface1, TImplementation> OnCreate(Action<TImplementation?> action)
public override ITypedRegistration<TInterface1, TImplementation> OnCreate(Action<TImplementation> action)
{
foreach (IRegistration registration in Registrations)
{
@ -110,7 +110,7 @@ internal class MultipleRegistration<TInterface1, TInterface2, TInterface3, TImpl
/// </summary>
/// <param name="action">The <see cref="Action{T}"/></param>
/// <returns>The current instance of this <see cref="ITypedRegistration{TInterface,TImplementation}"/></returns>
public override ITypedRegistration<TInterface1, TImplementation> OnCreate(Action<TImplementation?> action)
public override ITypedRegistration<TInterface1, TImplementation> OnCreate(Action<TImplementation> action)
{
foreach (IRegistration registration in Registrations)
{
@ -163,7 +163,7 @@ internal class MultipleRegistration<TInterface1, TInterface2, TInterface3, TInte
/// </summary>
/// <param name="action">The <see cref="Action{T}"/></param>
/// <returns>The current instance of this <see cref="ITypedRegistration{TInterface,TImplementation}"/></returns>
public override ITypedRegistration<TInterface1, TImplementation> OnCreate(Action<TImplementation?> action)
public override ITypedRegistration<TInterface1, TImplementation> OnCreate(Action<TImplementation> action)
{
foreach (IRegistration registration in Registrations)
{
@ -221,7 +221,7 @@ internal class MultipleRegistration<TInterface1, TInterface2, TInterface3, TInte
/// </summary>
/// <param name="action">The <see cref="Action{T}"/></param>
/// <returns>The current instance of this <see cref="ITypedRegistration{TInterface,TImplementation}"/></returns>
public override ITypedRegistration<TInterface1, TImplementation> OnCreate(Action<TImplementation?> action)
public override ITypedRegistration<TInterface1, TImplementation> OnCreate(Action<TImplementation> action)
{
foreach (IRegistration registration in Registrations)
{

@ -34,40 +34,40 @@ internal class TypedRegistration<TInterface, TImplementation> : RegistrationBase
/// This <see cref="Action"/> is invoked when an instance of this type is created.
/// <para>Can be set in the <see cref="IIocInstaller"/> by calling <see cref="IOnCreate{TInterface,TImplementation}.OnCreate"/></para>
/// </summary>
private Action<object?>? OnCreateAction { get; set; }
private Action<object>? OnCreateAction { get; set; }
/// <summary>
/// This <see cref="Action"/> is invoked when an instance of this type is created.
/// <para>Can be set in the <see cref="IIocInstaller"/> by calling <see cref="IOnCreate{TInterface,TImplementation}.OnCreate"/></para>
/// </summary>
private Func<object?, Task>? OnCreateActionAsync { get; set; }
private Func<object, Task>? OnCreateActionAsync { get; set; }
/// <summary>
/// This <see cref="Action"/> is invoked when an instance of this type is created.
/// <para>Can be set in the <see cref="IIocInstaller"/> by calling <see cref="IOnCreate{TInterface,TImplementation}.OnCreate"/></para>
/// </summary>
Action<object?>? IOnCreate.OnCreateAction => OnCreateAction;
Action<object>? IOnCreate.OnCreateAction => OnCreateAction;
/// <summary>
/// This <see cref="Action"/> is invoked when an instance of this type is created.
/// <para>Can be set in the <see cref="IIocInstaller"/> by calling <see cref="IOnCreate{TInterface,TImplementation}.OnCreate"/></para>
/// </summary>
Func<object?, Task>? IOnCreate.OnCreateActionAsync => OnCreateActionAsync;
Func<object, Task>? IOnCreate.OnCreateActionAsync => OnCreateActionAsync;
/// <summary>
/// Pass an <see cref="Action{T}"/> that will be invoked when an instance of this type is created
/// </summary>
/// <param name="action">The <see cref="Action{T}"/></param>
/// <returns>The current instance of this <see cref="ITypedRegistration{TInterface,TImplementation}"/></returns>
public virtual ITypedRegistration<TInterface, TImplementation> OnCreate(Action<TImplementation?> action)
public virtual ITypedRegistration<TInterface, TImplementation> OnCreate(Action<TImplementation> action)
{
OnCreateAction = a => action((TImplementation?) a);
OnCreateAction = a => action((TImplementation) a);
return this;
}
public ITypedRegistration<TInterface, TImplementation> OnCreateAsync(Func<TImplementation?, Task> action)
public ITypedRegistration<TInterface, TImplementation> OnCreateAsync(Func<TImplementation, Task> action)
{
OnCreateActionAsync = a => action((TImplementation?) a);
OnCreateActionAsync = a => action((TImplementation) a);
return this;
}

Loading…
Cancel
Save