Is Autofac magical – part II


Now that you've read part I perhaps you can answer this.

What does this code do?

    class FO : IEnumerable
    {

        public IEnumerator GetEnumerator()
        {
            throw new NotImplementedException();
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var cb = new ContainerBuilder();
            cb.Register(ctx => new FO()).As<IEnumerable>().InstancePerLifetimeScope();
            cb.Register<Func<FO>>(ctx => { return () => new FO(); }).As<Func<IEnumerable>>();
            var x = cb.Build();
            using (var s = x.BeginLifetimeScope())
            {
                var f = s.Resolve<Func<IEnumerable>>();
                var i1 = f();
                var i2 = f();
                var ie = x.Resolve<IEnumerable>();
            }
        }
    }

In particular

Are i1, i2, and ie distinct?

What if you comment out the line cb.Register<Func<FO>>(ctx => { return () => new FO(); }).As<Func<IEnumerable>>();?

Comments (0)

Skip to main content