Exemplo n.º 1
0
        static void Main(string[] args)
        {
            //Console.WriteLine(typeof(int).Assembly.FullName);
            //Console.WriteLine(typeof(string).Assembly.FullName);
            //Console.WriteLine(typeof(bool).Assembly.FullName);
            //Console.WriteLine("Hello World!");
            //var root = new Cat()
            //    .Register<IFoo, Foo>(Lifetime.Transisent)
            //    .Register<IBar>(_ => new Bar(), Lifetime.Self)
            //    .Register<IBaz, Baz>(Lifetime.Root)
            //    .Register(Assembly.GetEntryAssembly());
            //var cat1 = root.CreateChild();
            //var cat2 = root.CreateChild();
            //void GetServices<TService>(Cat cat)
            //{
            //    cat.GetService<TService>();
            //    cat.GetService<TService>();
            //}
            //GetServices<IFoo>(cat1);
            //GetServices<IBar>(cat1);
            //GetServices<IBaz>(cat1);
            //GetServices<IQux>(cat1);
            //Console.WriteLine();
            //GetServices<IFoo>(cat2);
            //GetServices<IBar>(cat2);
            //GetServices<IBaz>(cat2);
            //GetServices<IQux>(cat2);

            //var cat = new Cat()
            //    .Register<IFoo, Foo>(Lifetime.Transisent)
            //    .Register<IBar, Bar>(Lifetime.Transisent)
            //    .Register(typeof(IFoobar<,>), typeof(Foobar<,>), Lifetime.Transisent);
            //var foobar = (Foobar<IFoo, IBar>)cat.GetService<IFoobar<IFoo, IBar>>();
            //Debug.Assert(foobar.Foo is Foo);
            //Debug.Assert(foobar.Bar is Bar);

            //var services = new Cat()
            //    .Register<Base, Foo>(Lifetime.Transisent)
            //    .Register<Base, Bar>(Lifetime.Transisent)
            //    .Register<Base, Baz>(Lifetime.Transisent)
            //    .GetServices<Base>();
            //Debug.Assert(services.OfType<Foo>().Any());
            //Debug.Assert(services.OfType<Bar>().Any());
            //Debug.Assert(services.OfType<Baz>().Any());

            using (
                var root = new Cat()
                           .Register <IFoo, Foo>(Lifetime.Transisent)
                           .Register <IBar>(_ => new Bar(), Lifetime.Self)
                           .Register <IBaz, Baz>(Lifetime.Root)
                           .Register(Assembly.GetEntryAssembly())
                )
            {
                using (
                    var cat = root.CreateChild()
                    )
                {
                    cat.GetService <IFoo>();
                    cat.GetService <IBar>();
                    cat.GetService <IBaz>();
                    cat.GetService <IQux>();
                    Console.WriteLine($"Child cat is disposed.");
                }
                Console.WriteLine($"Root cat is disposed.");
            }
        }