コード例 #1
0
        public void SingletonPolicyBasedOnConcreteTypeRatherThanRequestedType()
        {
            WCSFBuilder       builder           = new WCSFBuilder();
            LifetimeContainer lifetimeContainer = new LifetimeContainer();
            Locator           locator           = CreateLocator(lifetimeContainer);

            builder.Policies.Set <ITypeMappingPolicy>(new TypeMappingPolicy(typeof(Foo), null), typeof(IFoo), null);
            builder.Policies.SetDefault <ISingletonPolicy>(new SingletonPolicy(true));
            GC.Collect();
            object obj1 = builder.BuildUp(locator, typeof(IFoo), null, null);
            object obj2 = builder.BuildUp(locator, typeof(IFoo), null, null);

            Assert.AreSame(obj1, obj2);
            GC.KeepAlive(lifetimeContainer);
        }
コード例 #2
0
        public void CanCreateSingletonObjectWithDefaultObjectBuilder()
        {
            WCSFBuilder       builder  = new WCSFBuilder();
            Locator           locator  = new Locator();
            LifetimeContainer lifetime = new LifetimeContainer();

            locator.Add(typeof(ILifetimeContainer), lifetime);

            builder.Policies.Set <ISingletonPolicy>(new SingletonPolicy(true), typeof(MockObject), "foo");

            MockObject obj1 = builder.BuildUp <MockObject>(locator, "foo", null);
            MockObject obj2 = builder.BuildUp <MockObject>(locator, "foo", null);

            Assert.AreSame(obj1, obj2);
        }
コード例 #3
0
        public void EmptyBuilderWillCreateAnyValueTypeWithDefaultValue()
        {
            WCSFBuilder builder = new WCSFBuilder();
            Locator     locator = CreateLocator();

            int actual = builder.BuildUp <int>(locator, null, null);

            Assert.AreEqual(default(int), actual);
        }
コード例 #4
0
        public void EmptyBuilderWillCreateSimpleInstances()
        {
            WCSFBuilder builder = new WCSFBuilder();
            Locator     locator = CreateLocator();

            SimpleObject o = builder.BuildUp <SimpleObject>(locator, null, null);

            Assert.IsNotNull(o);
            Assert.AreEqual(0, o.IntParam);
        }
コード例 #5
0
        public void EmptyBuilderWillCreateComplexInstances()
        {
            WCSFBuilder builder = new WCSFBuilder();
            Locator     locator = CreateLocator();

            ComplexObject o = builder.BuildUp <ComplexObject>(locator, null, null);

            Assert.IsNotNull(o);
            Assert.IsNotNull(o.SimpleObject);
            Assert.AreEqual(default(int), o.SimpleObject.IntParam);
        }
コード例 #6
0
        public void CanMapTypesWithDefaultObjectBuilder()
        {
            WCSFBuilder builder = new WCSFBuilder();
            Locator     locator = CreateLocator();

            TypeMappingPolicy policy = new TypeMappingPolicy(typeof(MockObject), null);

            builder.Policies.Set <ITypeMappingPolicy>(policy, typeof(IMockObject), null);

            IMockObject obj = builder.BuildUp <IMockObject>(locator, null, null);

            Assert.IsTrue(obj is MockObject);
        }
コード例 #7
0
        public void BuildUpThrowsWhenTryingToInjectNonIStateValueParameters()
        {
            Locator           locator   = new Locator();
            LifetimeContainer container = new LifetimeContainer();

            locator.Add(typeof(ILifetimeContainer), container);
            WCSFBuilder builder = new WCSFBuilder();

            builder.Policies.SetDefault <ISingletonPolicy>(new SingletonPolicy(true));
            locator.Add(new DependencyResolutionLocatorKey(typeof(ISessionStateLocatorService), null),
                        new MockSessionStateLocatorService());

            builder.BuildUp(locator, typeof(InvalidClass), Guid.NewGuid().ToString(), null);
        }
コード例 #8
0
        public void TestSimpleDependency()
        {
            Locator           locator   = new Locator();
            LifetimeContainer container = new LifetimeContainer();

            locator.Add(typeof(ILifetimeContainer), container);
            WCSFBuilder builder = new WCSFBuilder();

            TestProviderHost.Provider = new MockProvider();

            TestSimpleDependency obj = builder.BuildUp <TestSimpleDependency>(locator, "foo", null);

            Assert.IsNotNull(obj.Provider);
            Assert.AreSame(TestProviderHost.Provider, obj.Provider);
        }
コード例 #9
0
        public void TestPareameteDependencyWithNamedGetter()
        {
            Locator           locator   = new Locator();
            LifetimeContainer container = new LifetimeContainer();

            locator.Add(typeof(ILifetimeContainer), container);
            WCSFBuilder builder = new WCSFBuilder();

            TestProviderHost.AnotherProvider = new MockProvider();

            TestNamedParameterDependency obj = builder.BuildUp <TestNamedParameterDependency>(locator, "foo", null);

            Assert.IsNotNull(obj.Provider);
            Assert.AreSame(TestProviderHost.AnotherProvider, obj.Provider);
        }
コード例 #10
0
        public void BuildUpReturnsNewInstanceOfStateValue()
        {
            Locator           locator   = new Locator();
            LifetimeContainer container = new LifetimeContainer();

            locator.Add(typeof(ILifetimeContainer), container);
            WCSFBuilder builder = new WCSFBuilder();

            builder.Policies.SetDefault <ISingletonPolicy>(new SingletonPolicy(true));
            MockSessionStateLocatorService stateLocator = new MockSessionStateLocatorService();

            locator.Add(new DependencyResolutionLocatorKey(typeof(ISessionStateLocatorService), null), stateLocator);

            SampleClass builtObject =
                builder.BuildUp(locator, typeof(SampleClass), Guid.NewGuid().ToString(), null) as SampleClass;

            Assert.IsNotNull(builtObject.MyStringValue);
        }
コード例 #11
0
        public void BuildUpReturnsInstanceOfStateValueWithCorrespondantValueFromSessionWhenInjectingConstructor()
        {
            Locator locator = new Locator();
            LifetimeContainer container = new LifetimeContainer();
            locator.Add(typeof (ILifetimeContainer), container);
            WCSFBuilder builder = new WCSFBuilder();
            builder.Policies.SetDefault<ISingletonPolicy>(new SingletonPolicy(true));
            MockSessionStateLocatorService stateLocator = new MockSessionStateLocatorService();
            locator.Add(new DependencyResolutionLocatorKey(typeof (ISessionStateLocatorService), null), stateLocator);

            stateLocator.SessionState["stringKey"] = "value";
            SampleClass builtObject =
                builder.BuildUp(locator, typeof (SampleClass), Guid.NewGuid().ToString(), null) as SampleClass;

            Assert.IsNotNull(builtObject.MyStringValue);
            Assert.IsNotNull(builtObject.MyStringValue.Value);
            Assert.AreEqual("value", builtObject.MyStringValue.Value);
        }
コード例 #12
0
        public void TestBuildWithSimpleDependency()
        {
            Locator     locator = new Locator();
            WCSFBuilder builder = new WCSFBuilder();

            builder.Policies.SetDefault <ISingletonPolicy>(new SingletonPolicy(true));

            TestableRootCompositionContainer compositionContainer = new TestableRootCompositionContainer(builder);

            locator.Add(new DependencyResolutionLocatorKey(typeof(CompositionContainer), null), compositionContainer);
            ITestService service = compositionContainer.Services.AddNew <TestService, ITestService>();

            TestSimpleDependency builtObject =
                builder.BuildUp(locator, typeof(TestSimpleDependency), Guid.NewGuid().ToString(), null) as TestSimpleDependency;

            Assert.IsNotNull(builtObject);
            Assert.IsNotNull(builtObject.TestService);
            Assert.AreEqual(service, builtObject.TestService);
        }
コード例 #13
0
        public void BuildUpReturnsInstanceOfStateValueWithCorrespondantValueFromSessionWhenInjectingProperties()
        {
            Locator           locator   = new Locator();
            LifetimeContainer container = new LifetimeContainer();

            locator.Add(typeof(ILifetimeContainer), container);
            WCSFBuilder builder = new WCSFBuilder();

            builder.Policies.SetDefault <ISingletonPolicy>(new SingletonPolicy(true));
            MockSessionStateLocatorService stateLocator = new MockSessionStateLocatorService();

            locator.Add(new DependencyResolutionLocatorKey(typeof(ISessionStateLocatorService), null), stateLocator);

            stateLocator.SessionState["intKey"] = 5;
            SampleClass builtObject =
                builder.BuildUp(locator, typeof(SampleClass), Guid.NewGuid().ToString(), null) as SampleClass;

            Assert.IsNotNull(builtObject.MyIntValue);
            Assert.AreEqual(5, builtObject.MyIntValue.Value);
        }
コード例 #14
0
        public void TestDependantProviderDependency()
        {
            Locator           locator   = new Locator();
            LifetimeContainer container = new LifetimeContainer();

            locator.Add(typeof(ILifetimeContainer), container);
            TestableRootCompositionContainer compositionContainer = new TestableRootCompositionContainer();

            locator.Add(new DependencyResolutionLocatorKey(typeof(CompositionContainer), null), compositionContainer);
            MockDependencyObject mockDependency =
                compositionContainer.Services.AddNew <MockDependencyObject, MockDependencyObject>();
            WCSFBuilder builder = new WCSFBuilder();

            TestDependentProviderHost.Provider = new MockDependentProvider();

            TestDependentParameterDependency obj = builder.BuildUp <TestDependentParameterDependency>(locator, "foo", null);

            Assert.IsNotNull(obj.Provider);
            Assert.AreSame(TestDependentProviderHost.Provider, obj.Provider);
            Assert.IsNotNull(TestDependentProviderHost.Provider.DependencyObject);
            Assert.AreSame(mockDependency, TestDependentProviderHost.Provider.DependencyObject);
        }
コード例 #15
0
        public void EmptyBuilderWillCreateComplexInstances()
        {
            WCSFBuilder builder = new WCSFBuilder();
            Locator locator = CreateLocator();

            ComplexObject o = builder.BuildUp<ComplexObject>(locator, null, null);

            Assert.IsNotNull(o);
            Assert.IsNotNull(o.SimpleObject);
            Assert.AreEqual(default(int), o.SimpleObject.IntParam);
        }
コード例 #16
0
        public void CanMapTypesWithDefaultObjectBuilder()
        {
            WCSFBuilder builder = new WCSFBuilder();
            Locator locator = CreateLocator();

            TypeMappingPolicy policy = new TypeMappingPolicy(typeof (MockObject), null);
            builder.Policies.Set<ITypeMappingPolicy>(policy, typeof (IMockObject), null);

            IMockObject obj = builder.BuildUp<IMockObject>(locator, null, null);

            Assert.IsTrue(obj is MockObject);
        }
コード例 #17
0
        public void CanCreateSingletonObjectWithDefaultObjectBuilder()
        {
            WCSFBuilder builder = new WCSFBuilder();
            Locator locator = new Locator();
            LifetimeContainer lifetime = new LifetimeContainer();
            locator.Add(typeof (ILifetimeContainer), lifetime);

            builder.Policies.Set<ISingletonPolicy>(new SingletonPolicy(true), typeof (MockObject), "foo");

            MockObject obj1 = builder.BuildUp<MockObject>(locator, "foo", null);
            MockObject obj2 = builder.BuildUp<MockObject>(locator, "foo", null);

            Assert.AreSame(obj1, obj2);
        }
コード例 #18
0
        public void SingletonPolicyBasedOnConcreteTypeRatherThanRequestedType()
        {
            WCSFBuilder builder = new WCSFBuilder();
            LifetimeContainer lifetimeContainer = new LifetimeContainer();
            Locator locator = CreateLocator(lifetimeContainer);
            builder.Policies.Set<ITypeMappingPolicy>(new TypeMappingPolicy(typeof (Foo), null), typeof (IFoo), null);
            builder.Policies.SetDefault<ISingletonPolicy>(new SingletonPolicy(true));
            GC.Collect();
            object obj1 = builder.BuildUp(locator, typeof (IFoo), null, null);
            object obj2 = builder.BuildUp(locator, typeof (IFoo), null, null);

            Assert.AreSame(obj1, obj2);
            GC.KeepAlive(lifetimeContainer);
        }
コード例 #19
0
        public void EmptyBuilderWillCreateSimpleInstances()
        {
            WCSFBuilder builder = new WCSFBuilder();
            Locator locator = CreateLocator();

            SimpleObject o = builder.BuildUp<SimpleObject>(locator, null, null);

            Assert.IsNotNull(o);
            Assert.AreEqual(0, o.IntParam);
        }
コード例 #20
0
        public void EmptyBuilderWillCreateAnyValueTypeWithDefaultValue()
        {
            WCSFBuilder builder = new WCSFBuilder();
            Locator locator = CreateLocator();

            int actual = builder.BuildUp<int>(locator, null, null);
            Assert.AreEqual(default(int), actual);
        }
コード例 #21
0
        public void BuildUpReturnsNewInstanceOfStateValue()
        {
            Locator locator = new Locator();
            LifetimeContainer container = new LifetimeContainer();
            locator.Add(typeof (ILifetimeContainer), container);
            WCSFBuilder builder = new WCSFBuilder();
            builder.Policies.SetDefault<ISingletonPolicy>(new SingletonPolicy(true));
            MockSessionStateLocatorService stateLocator = new MockSessionStateLocatorService();
            locator.Add(new DependencyResolutionLocatorKey(typeof (ISessionStateLocatorService), null), stateLocator);

            SampleClass builtObject =
                builder.BuildUp(locator, typeof (SampleClass), Guid.NewGuid().ToString(), null) as SampleClass;

            Assert.IsNotNull(builtObject.MyStringValue);
        }
コード例 #22
0
        public void BuildUpThrowsWhenTryingToInjectNonIStateValueParameters()
        {
            Locator locator = new Locator();
            LifetimeContainer container = new LifetimeContainer();
            locator.Add(typeof (ILifetimeContainer), container);
            WCSFBuilder builder = new WCSFBuilder();
            builder.Policies.SetDefault<ISingletonPolicy>(new SingletonPolicy(true));
            locator.Add(new DependencyResolutionLocatorKey(typeof (ISessionStateLocatorService), null),
                        new MockSessionStateLocatorService());

            builder.BuildUp(locator, typeof (InvalidClass), Guid.NewGuid().ToString(), null);
        }