Пример #1
0
        public override void Execute(Glass.Mapper.Pipelines.ObjectConstruction.ObjectConstructionArgs args)
        {
            //check that no other task has created an object
            //also check that this is a dynamic object


            if (args.Result == null)
            {
                var configuration = args.Configuration;
                if (!configuration.Type.IsAssignableFrom(typeof(IDynamicMetaObjectProvider)))
                {
                    //check to see if the type is registered with the SimpleInjector container
                    //if it isn't added it
                    if (!IsRegistered(configuration.Type))
                    {
                        lock (_lock)
                        {
                            if (!IsRegistered(configuration.Type))
                            {
                                Register(configuration.Type);
                            }
                        }
                    }

                    Action <object> mappingAction = (target) =>
                                                    configuration.MapPropertiesToObject(target, args.Service,
                                                                                        args
                                                                                        .AbstractTypeCreationContext);


                    if (args.AbstractTypeCreationContext.IsLazy)
                    {
                        var resolved = GetConstructorParameters(configuration);

                        var proxy = _generator.CreateClassProxy(configuration.Type, resolved,
                                                                new LazyObjectInterceptor(mappingAction, args));
                        args.Result = proxy;
                        args.Counters.ProxyModelsCreated++;
                    }
                    else
                    {
                        //create instance using SimpleInjector
                        var obj = CreateConcreteInstance(configuration);
                        //map properties from item to model
                        mappingAction(obj);

                        //set the new object as the returned result
                        args.Result = obj;
                        args.Counters.ConcreteModelCreated++;
                        args.Counters.ModelsMapped++;
                    }
                }
            }

            base.Execute(args);
        }
        public void Execute(GlassMapper.Pipelines.ObjectConstruction.ObjectConstructionArgs args)
        {
            //check that no other task has created an object
            //also check that this is a dynamic object
            if (args.Result == null && !args.Configuration.Type.IsAssignableFrom(typeof(IDynamicMetaObjectProvider)))
            {
                //Get IOC container
                var container = AutofacConfig.ServiceLocator;
                //check to see if the type is registered with the Autofac container
                //if it isn't, return
                if (container == null || !container.IsRegistered(args.Configuration.Type))
                {
                    return;
                }

                Action <object> mappingAction = target => args.Configuration.MapPropertiesToObject(target, args.Service, args.AbstractTypeCreationContext);
                object          result;
                if (args.AbstractTypeCreationContext.IsLazy)
                {
                    result = container.ResolveNamed(args.Configuration.Type.FullName + ":lazy", args.Configuration.Type);
                    var proxy       = result as IProxyTargetAccessor;
                    var interceptor = proxy.GetInterceptors().First(x => x is LazyObjectInterceptor) as LazyObjectInterceptor;
                    interceptor.MappingAction = mappingAction;
                    interceptor.Actual        = result;
                }
                else
                {
                    result = container.Resolve(args.Configuration.Type);
                    if (result != null)
                    {
                        mappingAction(result);
                    }
                }

                //set the new object as the returned result
                args.Result = result;
            }
        }