示例#1
0
 public ContextScope(AmbientContext context)
 {
     if (AmbientContext.Current != null)
     {
         throw new InvalidOperationException("An ambient context already exists");
     }
     AmbientContext.Current = context;
 }
        public void a_dependency_registered_in_one_context_is_not_registered_in_another()
        {
            var objectForScope1 = new TheClass();

            var scope1 = new AmbientContext();
            var scope2 = new AmbientContext();

            Resolver.AddDependency<IContextStore, AmbientContextStore>();

            using (new ContextScope(scope1))
                Resolver.AddDependencyInstance<TheClass>(objectForScope1, DependencyLifetime.PerRequest);

            using (new ContextScope(scope2))
            {
                Resolver.HasDependency(typeof (TheClass)).ShouldBeFalse();

                Executing(() => Resolver.Resolve<TheClass>()).ShouldThrow<DependencyResolutionException>();
            }
        }
        public void registering_instances_in_different_scopes_results_in_only_the_context_specific_registrations_to_be_resolved_in_a_context()
        {
            var objectForScope1 = new TheClass();
            var objectForScope2 = new TheClass();
            var scope1 = new AmbientContext();
            var scope2 = new AmbientContext();

            Resolver.AddDependency<IContextStore, AmbientContextStore>();

            using (new ContextScope(scope1))
                Resolver.AddDependencyInstance<TheClass>(objectForScope1, DependencyLifetime.PerRequest);

            using (new ContextScope(scope2))
                Resolver.AddDependencyInstance<TheClass>(objectForScope2, DependencyLifetime.PerRequest);

            using (new ContextScope(scope1))
            {
                Resolver.ResolveAll<TheClass>()
                    .ShouldContain(objectForScope1)
                    .Count().ShouldBe(1);
            }
        }
        public void registering_instances_in_different_scopes_results_in_each_consumer_getting_the_correct_registration()
        {
            var objectForScope1 = new TheClass();
            var objectForScope2 = new TheClass();
            var scope1 = new AmbientContext();
            var scope2 = new AmbientContext();

            Resolver.AddDependency<IContextStore, AmbientContextStore>();

            using (new ContextScope(scope1))
                Resolver.AddDependencyInstance<TheClass>(objectForScope1, DependencyLifetime.PerRequest);

            using (new ContextScope(scope2))
                Resolver.AddDependencyInstance<TheClass>(objectForScope2, DependencyLifetime.PerRequest);

            using (new ContextScope(scope1))
            {
                Resolver.Resolve<TheClass>().ShouldBeTheSameInstanceAs(objectForScope1);
            }
        }
        public virtual void a_type_registered_as_transient_gets_an_instance_which_is_created_with_another_instance_and_is_registered_as_perwebrequest()
        {
            Resolver.AddDependency<IContextStore,AmbientContextStore>();

            Resolver.AddDependency<TheDependentClassThatNeedsYou>(DependencyLifetime.Transient);

            var contextStore = new AmbientContext();
            using (new ContextScope(contextStore))
            {
                var objectForScope = new TheClassThatNeedsYou(new NeedYou());

                Resolver.AddDependencyInstance(typeof(TheClassThatNeedsYou), objectForScope, DependencyLifetime.PerRequest);

               var dependentClass =  Resolver.Resolve<TheDependentClassThatNeedsYou>();
               dependentClass.ShouldNotBeNull();
            }
        }
        public void a_type_registered_as_transient_gets_an_instance_stored_in_context_injected()
        {
            Resolver.AddDependency<IContextStore, AmbientContextStore>();
            var objectForScope = new TheClass();
            var scope = new AmbientContext();
            Resolver.AddDependency<TheDependentClass>(DependencyLifetime.Transient);

            using(new ContextScope(scope))
            {
                Resolver.AddDependencyInstance(typeof(TheClass),objectForScope,DependencyLifetime.PerRequest);

                var dependentClass = Resolver.Resolve<TheDependentClass>();
                dependentClass.ShouldNotBeNull();
                dependentClass.Dependent.ShouldBeTheSameInstanceAs(objectForScope);
            }
        }
 public ContextScope(AmbientContext context)
 {
     _previous = AmbientContext.Current;
     AmbientContext.Current = context;
 }