Esempio n. 1
0
        public void SingletonPerScope_Keyed()
        {
            var container = new DependencyInjectionContainer(GraceDynamicMethod.Configuration());

            container.Configure(c =>
            {
                c.Export <AlphaNameService>().AsKeyed <INameService>("Alpha").Lifestyle.SingletonPerRequest();
                c.Export <BethaNameService>().AsKeyed <INameService>("Betha");

                c.Export <MultiNameService>()
                .ImportProperty(s => s.Alpha).LocateWithKey("Alpha").IsRequired(false)
                .ImportProperty(s => s.Betha).LocateWithKey("Betha").IsRequired(false)
                .ImportProperty(s => s.Gamma).LocateWithKey("Gamma").IsRequired(false);
            });


            var multiNameService = container.Locate <MultiNameService>();

            Assert.NotNull(multiNameService);

            Assert.NotNull(multiNameService.Alpha);
            Assert.Equal("Alpha", multiNameService.Alpha.Name);

            Assert.NotNull(multiNameService.Betha);
            Assert.Equal("Betha", multiNameService.Betha.Name);

            Assert.Null(multiNameService.Gamma);
        }
Esempio n. 2
0
        public void DynamicMethod_9_Constant()
        {
            var container = new DependencyInjectionContainer(GraceDynamicMethod.Configuration(c =>
            {
                c.Trace = s => Assert.DoesNotContain("falling back", s);
            }));

            container.Configure(c =>
            {
                c.Export <Dependent9>().
                WithCtorParam <IBasicService>(() => new BasicService()).Named("basicService1").
                WithCtorParam <IBasicService>(() => new BasicService()).Named("basicService2").
                WithCtorParam <IBasicService>(() => new BasicService()).Named("basicService3").
                WithCtorParam <IBasicService>(() => new BasicService()).Named("basicService4").
                WithCtorParam <IBasicService>(() => new BasicService()).Named("basicService5").
                WithCtorParam <IBasicService>(() => new BasicService()).Named("basicService6").
                WithCtorParam <IBasicService>(() => new BasicService()).Named("basicService7").
                WithCtorParam <IBasicService>(() => new BasicService()).Named("basicService8").
                WithCtorParam <IBasicService>(() => new BasicService()).Named("basicService9");
            });

            var instance = container.Locate <Dependent9>();

            Assert.NotNull(instance);;
        }
Esempio n. 3
0
        public void Grace()
        {
            var container = new DependencyInjectionContainer(GraceDynamicMethod.Configuration());

            container.Configure(c =>
            {
                foreach (var type in _types)
                {
                    c.Export(type).As(type);
                }
            });

            int length = 0;

            if (Scenario == ResolveScenario.ResolveOne)
            {
                length = 1;
            }
            else if (Scenario == ResolveScenario.ResolveHalf)
            {
                length = _types.Length / 2;
            }
            else if (Scenario == ResolveScenario.ResolveAll)
            {
                length = _types.Length;
            }

            for (var i = 0; i < length; i++)
            {
                container.Locate(_types[i]);
            }

            container.Dispose();
        }
        public void DynamicCompile_Fallback()
        {
            var fallback = false;

            var container = new DependencyInjectionContainer(GraceDynamicMethod.Configuration(c =>
            {
                c.Trace = s =>
                {
                    if (s.Contains("falling back"))
                    {
                        fallback = true;
                    }
                };
            }));

            container.Configure(c => c.ExportFactory <IInjectionContext, IDependentService <IBasicService> >(context =>
            {
                Assert.NotNull(context);

                return(new DependentService <IBasicService>(new BasicService()));
            }));

            var service = container.Locate <IDependentService <IBasicService> >();

            Assert.NotNull(service);

            Assert.True(fallback);
        }
Esempio n. 5
0
        public void Prepare()
        {
            container = new DependencyInjectionContainer(GraceDynamicMethod.Configuration());

            RegisterBasic();
            RegisterPropertyInjection();
            RegisterOpenGeneric();
            RegisterMultiple();
        }
Esempio n. 6
0
        public static void Main(string[] args)
        {
            var container = new DependencyInjectionContainer(GraceDynamicMethod.Configuration());

            container.Configure(c =>
            {
                c.ExportFactory(() => new BasicService()).As <IBasicService>().Lifestyle.Singleton();
            });

            var instance = container.Locate <DependentService <IEnumerable <IBasicService> > >();
        }
Esempio n. 7
0
        public void DynamicMethod_Per_Scope()
        {
            var container = new DependencyInjectionContainer(GraceDynamicMethod.Configuration(c =>
            {
                c.Trace = s => Assert.DoesNotContain("falling back", s);
            }));

            container.Configure(c => c.Export <BasicService>().As <IBasicService>().Lifestyle.SingletonPerScope());

            var value = container.Locate <DisposableDependent>();

            using (var scope = container.BeginLifetimeScope())
            {
                value = scope.Locate <DisposableDependent>();
            }
        }
        public void DynamicMethod_MemberInit_Test()
        {
            var container = new DependencyInjectionContainer(GraceDynamicMethod.Configuration(c =>
            {
                c.Trace = s => Assert.DoesNotContain("falling back", s);
            }));

            container.Configure(c =>
            {
                c.Export <BasicService>().As <IBasicService>();
                c.Export <PropertyInjectionService>().As <IPropertyInjectionService>().ImportProperty(i => i.BasicService);
            });

            var instance = container.Locate <IPropertyInjectionService>();

            Assert.NotNull(instance);
            Assert.NotNull(instance.BasicService);
        }
        public void DynamicMethod_Array_Test()
        {
            var container = new DependencyInjectionContainer(GraceDynamicMethod.Configuration(c =>
            {
                c.Trace = s => Assert.DoesNotContain("falling back", s);
            }));

            container.Configure(c => c.Export <BasicService>().As <IBasicService>());

            var instance = container.Locate <DependentService <IBasicService[]> >();

            Assert.NotNull(instance);
            Assert.NotNull(instance.Value);

            var array = instance.Value;

            Assert.NotNull(array);
            Assert.Equal(1, array.Length);
            Assert.IsType <BasicService>(array[0]);
        }
Esempio n. 10
0
        public void DynamicMethod_Primary_Constant_Test()
        {
            var container = new DependencyInjectionContainer(GraceDynamicMethod.Configuration(c =>
            {
                c.Trace = s => Assert.DoesNotContain("falling back", s);
            }));

            container.Configure(c => c.Export <ConstantValues>().
                                WithCtorParam(() => "StringValue").
                                WithCtorParam(() => 5).
                                WithCtorParam(() => 10.0).
                                WithCtorParam(() => true));

            var instance = container.Locate <ConstantValues>();

            Assert.NotNull(instance);
            Assert.Equal("StringValue", instance.StringValue);
            Assert.Equal(5, instance.IntValue);
            Assert.Equal(10.0, instance.DoubleValue);
            Assert.True(instance.BoolValue);
        }
Esempio n. 11
0
        public void DynamicMethod_AspNet()
        {
            var container = new DependencyInjectionContainer(GraceDynamicMethod.Configuration(c =>
            {
                c.Trace = s => Assert.DoesNotContain("falling back", s);
            }));

            container.Configure(c =>
            {
                c.Export <TestController1>();
                c.Export <RepositoryTransient1>().As <IRepositoryTransient1>();
                c.Export <RepositoryTransient2>().As <IRepositoryTransient2>();
                c.Export <RepositoryTransient3>().As <IRepositoryTransient3>();
                c.Export <RepositoryTransient4>().As <IRepositoryTransient4>();
                c.Export <RepositoryTransient5>().As <IRepositoryTransient5>();
                c.Export <ScopedService1>().As <IScopedService1>().Lifestyle.SingletonPerScope();
                c.Export <ScopedService2>().As <IScopedService2>().Lifestyle.SingletonPerScope();
                c.Export <ScopedService3>().As <IScopedService3>().Lifestyle.SingletonPerScope();
                c.Export <ScopedService4>().As <IScopedService4>().Lifestyle.SingletonPerScope();
                c.Export <ScopedService5>().As <IScopedService5>().Lifestyle.SingletonPerScope();
            });

            var controller = container.Locate <TestController1>();
        }
 public override void PrepareBasic()
 {
     this.container = new DependencyInjectionContainer(GraceDynamicMethod.Configuration());
     this.RegisterBasic();
 }
Esempio n. 13
0
        public void PrepareBasic()
        {
            container = new DependencyInjectionContainer(GraceDynamicMethod.Configuration());

            RegisterBasic();
        }