Example #1
0
        /// <inheritdoc />
        protected override IXmlMappingEngine CreateEngine(IUnityContainer container)
        {
            // Get the service locator.
            var locator = container.Resolve <IServiceLocator>();

            IXmlMapperFactory mapperFactory = new LocatorXmlMapperFactory(locator);

            if (CacheMappers)
            {
                // Wrap in the cache provider.
                var lf = mapperFactory;
                mapperFactory = new CachingXmlMapperFactory(lf);
            }
            var engine = new XmlMappingEngine(mapperFactory);

            return(engine);
        }
        public virtual void Initialize()
        {
            this.ExpectedXml = this.CreateExpectedXml();
            this.ExpectedDto = this.CreateExpectedDto();

            var locator = new Mock <IServiceLocator>();
            var factory = new LocatorXmlMapperFactory(locator.Object);
            var cache   = new CachingXmlMapperFactory(factory);

            this.MappingEngine = new XmlMappingEngine(cache);
            this.RegisterChildMappers(this.MappingEngine);

            this.XPathProcessor = new XPathProcessor();
            this.XPathProcessor.Initialize(this.ExpectedXml);

            this.Mapper = this.CreateMapper(this.MappingEngine);
            this.MappingEngine.RegisterMapper(this.Mapper);
        }
        public void FindMapperViaTypes()
        {
            var locator = new Mock<IServiceLocator>();
            var innerFactory = new Mock<IXmlMapperFactory>();
            var factory = new CachingXmlMapperFactory(innerFactory.Object);
            var mapperFactory = new LocatorXmlMapperFactory(locator.Object);
            var engine = new XmlMappingEngine(mapperFactory);

            var type = typeof(IXmlMapper<Parent, XElement>);
            innerFactory.Setup(x => x.Mapper(typeof(Parent), typeof(XElement), null)).Returns(new ParentXmlMapper(engine));
            locator.Setup(x => x.GetInstance(type)).Returns(new ParentXmlMapper(engine));

            var candidate = factory.Mapper(typeof(Parent), typeof(XElement));
            Assert.IsNotNull(candidate);
            Assert.IsTrue(candidate is ParentXmlMapper);

            factory.Mapper(typeof(Parent), typeof(XElement));
            innerFactory.Verify(x => x.Mapper(typeof(Parent), typeof(XElement), null), Times.Once());
        }
        public void FindMapperViaTypes()
        {
            var locator       = new Mock <IServiceLocator>();
            var innerFactory  = new Mock <IXmlMapperFactory>();
            var factory       = new CachingXmlMapperFactory(innerFactory.Object);
            var mapperFactory = new LocatorXmlMapperFactory(locator.Object);
            var engine        = new XmlMappingEngine(mapperFactory);

            var type = typeof(IXmlMapper <Parent, XElement>);

            innerFactory.Setup(x => x.Mapper(typeof(Parent), typeof(XElement), null)).Returns(new ParentXmlMapper(engine));
            locator.Setup(x => x.GetInstance(type)).Returns(new ParentXmlMapper(engine));

            var candidate = factory.Mapper(typeof(Parent), typeof(XElement));

            Assert.IsNotNull(candidate);
            Assert.IsTrue(candidate is ParentXmlMapper);

            factory.Mapper(typeof(Parent), typeof(XElement));
            innerFactory.Verify(x => x.Mapper(typeof(Parent), typeof(XElement), null), Times.Once());
        }