Wiki

Clone wiki

HaveBox / Custom lifetime setups

Home - News - Documentation

Given:

#!CSharpLexer

namespace HaveBoxExample
{
    public interface IHelloWorldProvider
    {
        string GetHelloWorld();
    }

    public class HelloWorldProvider : IHelloWorldProvider
    {
        public string GetHelloWorld()
        {
            return "Hello World";
        }
    }
}
The child container is a copy of its ancestor, with its own singleton instances :
#!CSharpLexer

using HaveBox;
using HaveBoxExample.HaveBoxExample;
using System;

namespace HaveBoxExample
{
    class Helloworld
    {
        static void Main(string[] args)
        {
            var container = new Container();
            container.Configure(config => config.For<IHelloWorldProvider>().Use<HelloWorldProvider>().AsSingleton().WithPerContainerLifeTime());

            using (var childContainer = container.CreateChildContainer())
            {
                var helloWorldProvider = childContainer.GetInstance<IHelloWorldProvider>();

                Console.Out.WriteLine(helloWorldProvider.GetHelloWorld());
            }
        }
    }
}
The child container is a clone of its ancestor, with shared singleton instances :
#!CSharpLexer

using HaveBox;
using HaveBoxExample.HaveBoxExample;
using System;

namespace HaveBoxExample
{
    class Helloworld
    {
        static void Main(string[] args)
        {
            var container = new Container();
            container.Configure(config => config.For<IHelloWorldProvider>().Use<HelloWorldProvider>().AsSingleton());

            using (var childContainer = container.CreateChildContainer())
            {
                var helloWorldProvider = childContainer.GetInstance<IHelloWorldProvider>();

                Console.Out.WriteLine(helloWorldProvider.GetHelloWorld());
            }
        }
    }
}

Updated