/// <summary> /// Registers that a single instance of <typeparamref name="TConcrete"/> will be returned for /// each lifetime scope that has been started using /// <see cref="BeginLifetimeScope">BeginLifetimeScope</see>. When the /// lifetime scope is disposed and <typeparamref name="TConcrete"/> implements <see cref="IDisposable"/>, /// the cached instance will be disposed as well. /// Scopes can be nested, and each scope gets its own instance. /// </summary> /// <typeparam name="TConcrete">The concrete type that will be registered.</typeparam> /// <param name="container">The container to make the registrations in.</param> /// <param name="disposeWhenLifetimeScopeEnds">If set to <c>true</c> the cached instance will be /// disposed at the end of its lifetime.</param> /// <exception cref="ArgumentNullException"> /// Thrown when the <paramref name="container"/> is a null reference.</exception> /// <exception cref="InvalidOperationException"> /// Thrown when this container instance is locked and can not be altered, or when an /// the <typeparamref name="TConcrete"/> has already been registered. /// </exception> /// <exception cref="ArgumentException">Thrown when the <typeparamref name="TConcrete"/> is a type /// that can not be created by the container.</exception> public static void RegisterLifetimeScope <TConcrete>(this Container container, bool disposeWhenLifetimeScopeEnds) where TConcrete : class, IDisposable { Requires.IsNotNull(container, "container"); container.Register <TConcrete, TConcrete>(LifetimeScopeLifestyle.Get(disposeWhenLifetimeScopeEnds)); }
/// <summary> /// Registers the specified delegate that allows returning instances of <typeparamref name="TService"/>, /// and returned instances are cached during the lifetime of a given scope that has been started using /// <see cref="BeginLifetimeScope">BeginLifetimeScope</see>. When the lifetime scope is disposed, /// <paramref name="disposeWhenLifetimeScopeEnds"/> is set to <b>true</b>, and the cached instance /// implements <see cref="IDisposable"/>, that cached instance will be disposed as well. /// Scopes can be nested, and each scope gets its own instance. /// </summary> /// <typeparam name="TService">The interface or base type that can be used to retrieve instances.</typeparam> /// <param name="container">The container to make the registrations in.</param> /// <param name="instanceCreator">The delegate that allows building or creating new instances.</param> /// <param name="disposeWhenLifetimeScopeEnds">If set to <c>true</c> the cached instance will be /// disposed at the end of its lifetime.</param> /// <exception cref="ArgumentNullException"> /// Thrown when either the <paramref name="container"/>, or <paramref name="instanceCreator"/> are /// null references.</exception> /// <exception cref="InvalidOperationException"> /// Thrown when this container instance is locked and can not be altered, or when the /// <typeparamref name="TService"/> has already been registered.</exception> public static void RegisterLifetimeScope <TService>(this Container container, Func <TService> instanceCreator, bool disposeWhenLifetimeScopeEnds) where TService : class { Requires.IsNotNull(container, "container"); Requires.IsNotNull(instanceCreator, "instanceCreator"); container.Register <TService>(instanceCreator, LifetimeScopeLifestyle.Get(disposeWhenLifetimeScopeEnds)); }
/// <summary> /// Registers that a single instance of <typeparamref name="TImplementation"/> will be returned for /// each lifetime scope that has been started using /// <see cref="BeginLifetimeScope">BeginLifetimeScope</see>. When the lifetime scope is disposed, /// <paramref name="disposeWhenLifetimeScopeEnds"/> is set to <b>true</b>, and the cached instance /// implements <see cref="IDisposable"/>, that cached instance will be disposed as well. /// Scopes can be nested, and each scope gets its own instance. /// </summary> /// <typeparam name="TService">The interface or base type that can be used to retrieve the instances.</typeparam> /// <typeparam name="TImplementation">The concrete type that will be registered.</typeparam> /// <param name="container">The container to make the registrations in.</param> /// <param name="disposeWhenLifetimeScopeEnds">If set to <c>true</c> the cached instance will be /// disposed at the end of its lifetime.</param> /// <exception cref="ArgumentNullException"> /// Thrown when the <paramref name="container"/> is a null reference.</exception> /// <exception cref="InvalidOperationException"> /// Thrown when this container instance is locked and can not be altered, or when an /// the <typeparamref name="TService"/> has already been registered.</exception> /// <exception cref="ArgumentException">Thrown when the given <typeparamref name="TImplementation"/> /// type is not a type that can be created by the container. /// </exception> public static void RegisterLifetimeScope <TService, TImplementation>( this Container container, bool disposeWhenLifetimeScopeEnds) where TImplementation : class, TService, IDisposable where TService : class { Requires.IsNotNull(container, "container"); container.Register <TService, TImplementation>(LifetimeScopeLifestyle.Get(disposeWhenLifetimeScopeEnds)); }