示例#1
0
        /// <inheritdoc/>
        object IUmbMapperConfig.Map(IPublishedContent content)
        {
            object result;

            if (this.createProxy)
            {
                // Create a proxy instance to replace our object.
                result = this.hasIPublishedContructor ? this.proxyType.GetInstance(content) : this.proxyType.GetInstance();

                // First add any lazy mappings, use count to prevent allocations
                var lazyProperties = new Dictionary <string, Lazy <object> >(this.lazyNames.Count);
                foreach (PropertyMap <T> map in this.lazyMaps)
                {
                    lazyProperties[map.Info.Property.Name] = new Lazy <object>(() => MapProperty(map, content, result));
                }

                // Then lazy predicate mappings
                foreach (PropertyMap <T> map in this.lazyPredicateMaps)
                {
                    lazyProperties[map.Info.Property.Name] = new Lazy <object>(() => MapProperty(map, content, result));
                }

                // Set the interceptor and replace our result with the proxy
                var interceptor = new LazyInterceptor(lazyProperties);
                ((IProxy)result).Interceptor = interceptor;
            }
            else
            {
                result = this.hasIPublishedContructor ? this.MappedType.GetInstance(content) : this.MappedType.GetInstance();
            }

            // Now map the non-lazy properties
            foreach (PropertyMap <T> map in this.nonLazyMaps)
            {
                object value = MapProperty(map, content, result);
                if (value != null)
                {
                    this.propertyAccessor.SetValue(map.Info.Property.Name, result, value);
                }
            }

            // Then non-lazy predicate mappings
            foreach (PropertyMap <T> map in this.nonLazyPredicateMaps)
            {
                object value = MapProperty(map, content, result);
                if (value != null)
                {
                    this.propertyAccessor.SetValue(map.Info.Property.Name, result, value);
                }
            }

            return(result);
        }
        public void LazyObjectShouldNeverBeInitialized()
        {
            var container = new ServiceContainer();
            container.AddService<IProxyFactory>(new ProxyFactory());
            container.AddService<IMethodBuilder<MethodInfo>>(new MethodBuilder());

            Assert.IsTrue(container.Contains(typeof (IProxyFactory)));

            var proxyFactory = container.GetService<IProxyFactory>();
            var interceptor = new LazyInterceptor<ISampleService>(() => new SampleLazyService());

            SampleLazyService.Reset();
            // The instance should be uninitialized at this point
            var proxy = proxyFactory.CreateProxy<ISampleService>(interceptor);
            Assert.IsFalse(SampleLazyService.IsInitialized);

            // The instance should be initialized once the method is called
            proxy.DoSomething();
            Assert.IsTrue(SampleLazyService.IsInitialized);
        }
示例#3
0
        public void LazyObjectShouldNeverBeInitialized()
        {
            var container = new ServiceContainer();

            container.AddService <IProxyFactory>(new ProxyFactory());
            container.AddService <IMethodBuilder <MethodInfo> >(new MethodBuilder());

            Assert.IsTrue(container.Contains(typeof(IProxyFactory)));

            var proxyFactory = container.GetService <IProxyFactory>();
            var interceptor  = new LazyInterceptor <ISampleService>(() => new SampleLazyService());

            SampleLazyService.Reset();
            // The instance should be uninitialized at this point
            var proxy = proxyFactory.CreateProxy <ISampleService>(interceptor);

            Assert.IsFalse(SampleLazyService.IsInitialized);

            // The instance should be initialized once the method is called
            proxy.DoSomething();
            Assert.IsTrue(SampleLazyService.IsInitialized);
        }