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);
        }
        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);
        }
        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());
        }
        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);
        }
        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]);
        }
 public IEnumerable <IAdapter> GetAdaptersFor(Type adapteeType)
 {
     foreach (var adapterType in _typeAdapterRegistry.FindAdapterTypesFor(adapteeType))
     {
         IAdapter result = null;
         if (_singletonCache == null || !_singletonCache.TryGetValue(adapterType, out result))
         {
             result = _adapterCreator(adapterType);
             if (_singletonCache != null)
             {
                 _singletonCache[adapterType] = result;
             }
         }
         yield return(result);
     }
 }