コード例 #1
0
 public TypeAdapterFactory(
     bool singletonAdapters = false,
     TypeAdapterRegistry <IAdapter> registry = null,
     Func <Type, IAdapter> creator           = null
     )
 {
     _typeAdapterRegistry = registry ?? new TypeAdapterRegistry <IAdapter>();
     _adapterCreator      = creator ?? DefaultCreator;
     _singletonCache      = singletonAdapters ? new Dictionary <Type, IAdapter>() : null;
 }
コード例 #2
0
        public void FindReturnsEveryTypeOnlyOnce2()
        {
            // arrange
            var registry = new TypeAdapterRegistry <IApple>();

            // act
            registry.Register <DisguiseSpecialPearAndPearAsApple>();
            var pearAdapters = registry.FindAdapterTypesFor <SpecialPear>();

            // assert
            Assert.AreEqual(typeof(DisguiseSpecialPearAndPearAsApple), pearAdapters.Single());
        }
コード例 #3
0
        public void SimpleRegisterAndFind()
        {
            // arrange
            var registry = new TypeAdapterRegistry <IApple>();

            // act
            registry.Register <DisguisePearAsApple>();
            var adapter = registry.FindAdapterTypesFor <Pear>().Single();

            // assert
            Assert.AreEqual(typeof(DisguisePearAsApple), adapter);
        }
コード例 #4
0
        public void RegisterAndFindMultiPurposeAdapter()
        {
            // arrange
            var registry = new TypeAdapterRegistry <IApple>();

            // act
            registry.Register <DisguisePearOrOrangeAsApple>();
            var pearAdapter   = registry.FindAdapterTypesFor <Pear>().Single();
            var orangeAdapter = registry.FindAdapterTypesFor <Orange>().Single();

            // assert
            Assert.AreEqual(typeof(DisguisePearOrOrangeAsApple), pearAdapter);
            Assert.AreEqual(typeof(DisguisePearOrOrangeAsApple), orangeAdapter);
        }
コード例 #5
0
        public void RegisterAndFindSpecializedAdaptee2()
        {
            // arrange
            var registry = new TypeAdapterRegistry <IApple>();

            // act
            registry.Register <DisguisePearAsApple>();
            var pearAdapter        = registry.FindAdapterTypesFor <Pear>().Single();
            var specialPearAdapter = registry.FindAdapterTypesFor <SpecialPear>().Single();

            // assert
            Assert.AreEqual(typeof(DisguisePearAsApple), pearAdapter);
            Assert.AreEqual(typeof(DisguisePearAsApple), specialPearAdapter);
        }
コード例 #6
0
        public void ExpectArgumentExceptionIfAdapterWrongType()
        {
            // arrange
            var registry = new TypeAdapterRegistry <IPear>();

            try
            {
                // act
                registry.Register(typeof(DisguisePearAsApple));

                // assert
                Assert.Fail("Expecting ArgumentException");
            }
            catch (ArgumentException)
            {
                // success :-)
            }
        }
コード例 #7
0
        public void ExpectArgumentExceptionIfAdapteeCouldNotBeResolved()
        {
            // arrange
            var registry = new TypeAdapterRegistry <IApple>();

            try
            {
                // act
                registry.Register <DisguisePearAsBadApple>();

                // assert
                Assert.Fail("Expecting ArgumentException");
            }
            catch (ArgumentException)
            {
                // success :-)
            }
        }
コード例 #8
0
        public void FindReturnsInReverseRegistrationOrder2()
        {
            // arrange
            var registry = new TypeAdapterRegistry <IApple>();

            // act
            registry.Register <DisguiseSpecialPearAndPearAsApple>();
            registry.Register <DisguiseSpecialPearAsApple>();
            registry.Register <DisguisePearAsApple>();

            var pearAdapters        = registry.FindAdapterTypesFor <Pear>().ToArray();
            var specialPearAdapters = registry.FindAdapterTypesFor <SpecialPear>().ToArray();

            // assert
            Assert.AreEqual(2, pearAdapters.Length);
            Assert.AreEqual(typeof(DisguisePearAsApple), pearAdapters[0]);
            Assert.AreEqual(typeof(DisguiseSpecialPearAndPearAsApple), pearAdapters[1]);

            Assert.AreEqual(3, specialPearAdapters.Length);
            Assert.AreEqual(typeof(DisguiseSpecialPearAsApple), specialPearAdapters[0]);
            Assert.AreEqual(typeof(DisguiseSpecialPearAndPearAsApple), specialPearAdapters[1]);
            Assert.AreEqual(typeof(DisguisePearAsApple), specialPearAdapters[2]);
        }