Ejemplo n.º 1
0
        public void OptionalBasicServiceWithKeyAndScopedService()
        {
            var container = new DependencyInjectionContainer();

            container.Configure(c =>
            {
                c.Export <BasicService>().AsKeyed <IBasicService>("basic").Lifestyle.SingletonPerScope();
                var optionalStrategy = new OptionalWrapperStrategy(c.OwningScope);
                c.AddActivationStrategy(optionalStrategy);
                c.AddMissingDependencyExpressionProvider(optionalStrategy);
            });

            var scope = container.CreateChildScope();

            var instance1 = scope.Locate <Optional <IBasicService> >(withKey: "basic");
            var instance2 = scope.Locate <Optional <IBasicService> >(withKey: "basic");

            Assert.True(instance1.IsSatisfied());
            Assert.True(instance2.IsSatisfied());

            var value1 = instance1.Value;

            Assert.NotNull(value1);
            var value2 = instance2.Value;

            Assert.NotNull(value2);

            Assert.Same(value1, value2);
        }
Ejemplo n.º 2
0
        public void OptionalBasicServiceWithKeyAndNotIsSatisfied()
        {
            var container = new DependencyInjectionContainer();

            container.Configure(c =>
            {
                var optionalStrategy = new OptionalWrapperStrategy(c.OwningScope);
                c.AddActivationStrategy(optionalStrategy);
                c.AddMissingDependencyExpressionProvider(optionalStrategy);
            });

            var instance = container.Locate <Optional <IBasicService> >(withKey: "basic");

            Assert.False(instance.IsSatisfied());

            var value = instance.Value;

            Assert.Null(value);
        }