prepare for #33: create interfaces for OnCreate an WithParameters

pull/37/head
Simon Gockner 6 years ago
parent 33d857826f
commit 165cfd16f2
  1. 29
      LightweightIocContainer/Interfaces/Registrations/FluentProviders/IOnCreate.cs
  2. 41
      LightweightIocContainer/Interfaces/Registrations/FluentProviders/IWithParameters.cs
  3. 44
      LightweightIocContainer/Interfaces/Registrations/IRegistrationBase.cs
  4. 5
      LightweightIocContainer/IocContainer.cs

@ -0,0 +1,29 @@
// Author: Simon Gockner
// Created: 2019-12-07
// Copyright(c) 2019 SimonG. All Rights Reserved.
using System;
using LightweightIocContainer.Interfaces.Installers;
namespace LightweightIocContainer.Interfaces.Registrations.FluentProviders
{
/// <summary>
/// Provides an <see cref="OnCreate"/> method to an <see cref="IRegistrationBase{TInterface}"/>
/// </summary>
/// <typeparam name="TInterface">The registered interface</typeparam>
public interface IOnCreate<TInterface>
{
/// <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="OnCreate"/></para>
/// </summary>
Action<TInterface> OnCreateAction { get; }
/// <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="IRegistrationBase{TInterface}"/></returns>
IRegistrationBase<TInterface> OnCreate(Action<TInterface> action);
}
}

@ -0,0 +1,41 @@
// Author: Simon Gockner
// Created: 2019-12-07
// Copyright(c) 2019 SimonG. All Rights Reserved.
using System;
using LightweightIocContainer.Exceptions;
using LightweightIocContainer.Interfaces.Installers;
namespace LightweightIocContainer.Interfaces.Registrations.FluentProviders
{
/// <summary>
/// Provides a <see cref="WithParameters(object[])"/> method to an <see cref="IRegistrationBase{TInterface}"/>
/// </summary>
/// <typeparam name="TInterface">The registered interface</typeparam>
public interface IWithParameters<TInterface>
{
/// <summary>
/// An <see cref="Array"/> of parameters that are used to <see cref="IIocContainer.Resolve{T}()"/> an instance of this <see cref="IRegistration.InterfaceType"/>
/// <para>Can be set in the <see cref="IIocInstaller"/> by calling <see cref="WithParameters(object[])"/></para>
/// </summary>
object[] Parameters { get; }
/// <summary>
/// Pass parameters that will be used to<see cref="IIocContainer.Resolve{T}()"/> an instance of this <see cref="IRegistration.InterfaceType"/>
/// <para>Parameters set with this method are always inserted at the beginning of the argument list if more parameters are given when resolving</para>
/// </summary>
/// <param name="parameters">The parameters</param>
/// <returns>The current instance of this <see cref="IRegistrationBase{TInterface}"/></returns>
/// <exception cref="InvalidRegistrationException"><see cref="Parameters"/> are already set or no parameters given</exception>
IRegistrationBase<TInterface> WithParameters(params object[] parameters);
/// <summary>
/// Pass parameters that will be used to<see cref="IIocContainer.Resolve{T}()"/> an instance of this <see cref="IRegistration.InterfaceType"/>
/// <para>Parameters set with this method are inserted at the position in the argument list that is passed with the parameter if more parameters are given when resolving</para>
/// </summary>
/// <param name="parameters">The parameters with their position</param>
/// <returns>The current instance of this <see cref="IRegistrationBase{TInterface}"/></returns>
/// <exception cref="InvalidRegistrationException"><see cref="Parameters"/> are already set or no parameters given</exception>
IRegistrationBase<TInterface> WithParameters(params (int index, object parameter)[] parameters);
}
}

@ -2,9 +2,7 @@
// Created: 2019-05-20
// Copyright(c) 2019 SimonG. All Rights Reserved.
using System;
using LightweightIocContainer.Exceptions;
using LightweightIocContainer.Interfaces.Installers;
using LightweightIocContainer.Interfaces.Registrations.FluentProviders;
namespace LightweightIocContainer.Interfaces.Registrations
{
@ -12,49 +10,11 @@ namespace LightweightIocContainer.Interfaces.Registrations
/// The <see cref="IRegistrationBase{TInterface}"/> that is used to register an Interface
/// </summary>
/// <typeparam name="TInterface">The registered Interface</typeparam>
public interface IRegistrationBase<TInterface> : IRegistration
public interface IRegistrationBase<TInterface> : IRegistration, IOnCreate<TInterface>, IWithParameters<TInterface>
{
/// <summary>
/// The Lifestyle of Instances that are created with this <see cref="IRegistrationBase{TInterface}"/>
/// </summary>
Lifestyle Lifestyle { get; }
/// <summary>
/// This <see cref="Action{T}"/> is invoked when an instance of this type is created.
/// <para>Can be set in the <see cref="IIocInstaller"/> by calling <see cref="OnCreate"/></para>
/// </summary>
Action<TInterface> OnCreateAction { get; }
/// <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="IRegistrationBase{TInterface}"/></returns>
IRegistrationBase<TInterface> OnCreate(Action<TInterface> action);
/// <summary>
/// An <see cref="Array"/> of parameters that are used to <see cref="IIocContainer.Resolve{T}()"/> an instance of this <see cref="IRegistration.InterfaceType"/>
/// <para>Can be set in the <see cref="IIocInstaller"/> by calling <see cref="WithParameters(object[])"/></para>
/// </summary>
object[] Parameters { get; }
/// <summary>
/// Pass parameters that will be used to<see cref="IIocContainer.Resolve{T}()"/> an instance of this <see cref="IRegistration.InterfaceType"/>
/// <para>Parameters set with this method are always inserted at the beginning of the argument list if more parameters are given when resolving</para>
/// </summary>
/// <param name="parameters">The parameters</param>
/// <returns>The current instance of this <see cref="IRegistrationBase{TInterface}"/></returns>
/// <exception cref="InvalidRegistrationException"><see cref="Parameters"/> are already set or no parameters given</exception>
IRegistrationBase<TInterface> WithParameters(params object[] parameters);
/// <summary>
/// Pass parameters that will be used to<see cref="IIocContainer.Resolve{T}()"/> an instance of this <see cref="IRegistration.InterfaceType"/>
/// <para>Parameters set with this method are inserted at the position in the argument list that is passed with the parameter if more parameters are given when resolving</para>
/// </summary>
/// <param name="parameters">The parameters with their position</param>
/// <returns>The current instance of this <see cref="IRegistrationBase{TInterface}"/></returns>
/// <exception cref="InvalidRegistrationException"><see cref="Parameters"/> are already set or no parameters given</exception>
IRegistrationBase<TInterface> WithParameters(params (int index, object parameter)[] parameters);
}
}

@ -12,6 +12,7 @@ using LightweightIocContainer.Exceptions;
using LightweightIocContainer.Interfaces;
using LightweightIocContainer.Interfaces.Installers;
using LightweightIocContainer.Interfaces.Registrations;
using LightweightIocContainer.Interfaces.Registrations.FluentProviders;
using LightweightIocContainer.Registrations;
namespace LightweightIocContainer
@ -343,12 +344,12 @@ namespace LightweightIocContainer
}
/// <summary>
/// Update the given arguments with the <see cref="IRegistrationBase{TInterface}.Parameters"/> of the given <see cref="IRegistrationBase{TInterface}"/>
/// Update the given arguments with the <see cref="IWithParameters{TInterface}.Parameters"/> of the given <see cref="IRegistrationBase{TInterface}"/>
/// </summary>
/// <typeparam name="T">The given <see cref="Type"/></typeparam>
/// <param name="registration">The <see cref="IRegistrationBase{TInterface}"/> of the given <see cref="Type"/></param>
/// <param name="arguments">The constructor arguments</param>
/// <returns>The argument list updated with the <see cref="IRegistrationBase{TInterface}.Parameters"/></returns>
/// <returns>The argument list updated with the <see cref="IWithParameters{TInterface}.Parameters"/></returns>
private object[] UpdateArgumentsWithRegistrationParameters<T>(IRegistrationBase<T> registration, object[] arguments)
{
if (arguments != null && arguments.Any()) //if more arguments were passed to resolve

Loading…
Cancel
Save