/// <summary>
        /// Initializes a new instance of the <see cref="MultiInterfacePropertyInterceptor"/> class.
        /// </summary>
        /// <param name="args">The args.</param>
        public MultiInterfacePropertyInterceptor(ObjectConstructionArgs args)
        {
            _args = args;
              _configs =
                  new[] {args.Configuration}.Union(args.Parameters[CreateMultiInferaceTask.MultiInterfaceConfigsKey] as IEnumerable<AbstractTypeConfiguration>);
			  _lazyValues = new Lazy<IDictionary<string, object>>(LoadValues);
        }
Пример #2
0
 public override void Execute(ObjectConstructionArgs args)
 {
     using (new DepthCheck(args.Configuration.Type))
     {
         base.Execute(args);
     }
 }
Пример #3
0
        /// <summary>
        /// Executes the specified args.
        /// </summary>
        /// <param name="args">The args.</param>
        public void Execute(ObjectConstructionArgs args)
        {
            if (args.Result != null)
                return;

            var type = args.Configuration.Type;

            if(type.IsInterface)
            {
                return;
            }

            if(args.AbstractTypeCreationContext.IsLazy)
            {
                //here we create a lazy loaded version of the class
                args.Result = CreateLazyObject(args);
                args.AbortPipeline();

            }
            else
            {
                //here we create a concrete version of the class
                args.Result = CreateObject(args);
                args.AbortPipeline();

                
            }
        }
        /// <summary>
        /// Executes the specified args.
        /// </summary>
        /// <param name="args">The args.</param>
        public override void Execute(ObjectConstructionArgs args)
        {
            if (args.Result == null
                && args.Configuration != null
                && !args.Configuration.Type.IsInterface
                && !args.Configuration.Type.IsSealed)
            {
                if(args.AbstractTypeCreationContext.IsLazy && DisableLazyLoading.Current == LazyLoadSetting.Enabled)
                {
                    //here we create a lazy loaded version of the class
                    args.Result = CreateLazyObject(args);
                    args.Counters.ProxyModelsCreated++;

                }
                else
                {
                    //here we create a concrete version of the class
                    args.Result = CreateObject(args);
                    args.Counters.ModelsMapped++;
                    args.Counters.ConcreteModelCreated++;
                }
            }

            base.Execute(args);
        }
 /// <summary>
 /// Executes the specified args.
 /// </summary>
 /// <param name="args">The args.</param>
 public void Execute(ObjectConstructionArgs args)
 {
     if (args.Result== null 
         && args.Configuration.Type.IsInterface) 
     {
         args.Result = _generator.CreateInterfaceProxyWithoutTarget(args.Configuration.Type, new InterfacePropertyInterceptor(args));
     }
 }
 public LazyObjectInterceptor(Action<object> mappingAction, ObjectConstructionArgs args)
 {
     MappingAction = (obj) =>
     {
         args.Counters.ProxyModelsCreated++;
         mappingAction(obj);
     };
 }
        public override void Execute(ObjectConstructionArgs args)
        {
            
                var counter = GetCounter();
                counter++;
            ThreadData.SetValue(CalledKey,counter);

            base.Execute(args);
        }
        /// <summary>
        /// Executes the specified args.
        /// </summary>
        /// <param name="args">The args.</param>
        public override void Execute(ObjectConstructionArgs args)
        {
            if (args.Result== null 
                && args.Configuration.Type.IsInterface) 
            {
                args.Result = _generator.CreateInterfaceProxyWithoutTarget(args.Configuration.Type, new InterfacePropertyInterceptor(args));
                args.Counters.ProxyModelsCreated++;
            }

            base.Execute(args);
        }
Пример #9
0
        public void Execute(ObjectConstructionArgs args)
        {
            if (args.Result != null && args.Configuration.Cachable && args.AbstractTypeCreationContext.CacheEnabled && DisableCache.Current == CacheSetting.Enabled)
            {
                var key = args.Context.Name + args.AbstractTypeCreationContext.GetUniqueKey();

                // This will also OVERRIDE any existing item that may already be cached (to be consistent across different cache impls)
                // Will allow for multiple threads to update the cached object on first load, when they are all racing to cache the item for the first time
                _cacheManager.AddOrUpdate(key, args.Result);
            }
        }
Пример #10
0
        /// <summary>
        /// Executes the specified args.
        /// </summary>
        /// <param name="args">The args.</param>
        public void Execute(ObjectConstructionArgs args)
        {
            if (args.Result != null)
                return;

            if (args.Configuration.Type.IsInterface) 
            {
                
                args.Result = _generator.CreateInterfaceProxyWithoutTarget(args.Configuration.Type, new InterfacePropertyInterceptor(args));
                // args.AbortPipeline();
            }
        }
        public override void Execute(ObjectConstructionArgs args)
        {
            var innerCallback = args.CreatedCallback;
            //args.CreatedCallback = () =>
            //{
                var counter = GetCounter();
                counter++;
                ThreadData.SetValue(CacheMissKey, counter);
            //    innerCallback();
            //};

            base.Execute(args);
        }
Пример #12
0
        public void Execute(ObjectConstructionArgs args)
        {
            if (args.Result == null && args.Configuration.Cachable && args.AbstractTypeCreationContext.CacheEnabled && DisableCache.Current == CacheSetting.Enabled)
            {
                var key = args.Context.Name + args.AbstractTypeCreationContext.GetUniqueKey();

                var cacheItem = _cacheManager.Get<object>(key);
                if (cacheItem != null)
                {
                    args.Result = cacheItem;
                    args.AbortPipeline();
                }
            }
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="InterfacePropertyInterceptor"/> class.
        /// </summary>
        /// <param name="args">The args.</param>
        public InterfacePropertyInterceptor(ObjectConstructionArgs args)
        {
            _args = args;
            _fullName = _args.Configuration.Type.FullName;
            Values = new ConcurrentDictionary<string, object>();

            _mappingContext = _args.Service.CreateDataMappingContext(_args.AbstractTypeCreationContext, null);

            //if lazy loading diabled load all values now
            if (!args.AbstractTypeCreationContext.IsLazy)
            {
                LoadAllValues();
            }
        }
        public void Execute(ObjectConstructionArgs args)
        {
            if (args.Result == null 
                && args.Configuration.Type.IsInterface 
                && args.Parameters.ContainsKey(MultiInterfaceConfigsKey))
            {
                var configs = args.Parameters[MultiInterfaceConfigsKey] as IEnumerable<AbstractTypeConfiguration>;

                if (configs != null)
                {
                    args.Result = _generator.CreateInterfaceProxyWithoutTarget(
                        args.Configuration.Type,
                        configs.Select(x => x.Type).ToArray(),
                        new MultiInterfacePropertyInterceptor(args));
                }
            }
        }
Пример #15
0
        public override void Execute(ObjectConstructionArgs args)
        {
            if (args.Result == null 
                && args.Configuration.Cachable 
                && DisableCache.Current == CacheSetting.Enabled
                && args.AbstractTypeCreationContext.CacheEnabled
                )
            {
                var key = CacheKeyGenerator.Generate(args);

                var cacheItem = CacheManager.Get<object>(key);
                if (cacheItem != null)
                {
                    args.Result = cacheItem;
                    args.Counters.CachedModels++;
                }

                DisableLazyLoading disableLazyLoading = null;

                if (args.Service.GlassContext.Config.EnableLazyLoadingForCachableModels == false)
                {
                    disableLazyLoading = new DisableLazyLoading();
                }

                try
                {
                    base.Execute(args);
                    CacheManager.AddOrUpdate(key, args.Result);
                }
                finally
                {
                    if (disableLazyLoading != null)
                    {
                        disableLazyLoading.Dispose();
                    }
                }
            }
            else 
            {
                base.Execute(args);
            }


        }
        public override void Execute(ObjectConstructionArgs args)
        {
            var stopwatch = new Stopwatch();

            stopwatch.Start();

            try
            {
                base.Execute(args);
            }
            finally
            {
                stopwatch.Stop();
                if (stopwatch.ElapsedMilliseconds > _debugSettings.SlowModelThreshold)
                {

                    var key = _cacheKeyGenerator.Generate(args) + "stopwatch";
                    var finaltType = args.Result.GetType();

                    _log.Warn("Slow Glass Model - Time: {0} Cachable: {1} Type: {2} Key: {3}".Formatted(stopwatch.ElapsedMilliseconds, args.Configuration.Cachable, finaltType.FullName, key));
                }
            }
        }
Пример #17
0
        /// <summary>
        /// Creates the object.
        /// </summary>
        /// <param name="args">The args.</param>
        /// <returns>System.Object.</returns>
        protected virtual object CreateObject(ObjectConstructionArgs args)
        {

            var constructorParameters = args.AbstractTypeCreationContext.ConstructorParameters;

            var parameters =
                constructorParameters == null || !constructorParameters.Any()
                    ? Type.EmptyTypes
                    : constructorParameters.Select(x => x.GetType()).ToArray();

            var constructorInfo = args.Configuration.Type.GetConstructor(parameters);

            Delegate conMethod = args.Configuration.ConstructorMethods[constructorInfo];

            var obj = conMethod.DynamicInvoke(constructorParameters);

            args.Configuration.MapPropertiesToObject(obj, args.Service, args.AbstractTypeCreationContext);

            return obj;
        }
Пример #18
0
 /// <summary>
 /// Creates the lazy object.
 /// </summary>
 /// <param name="args">The args.</param>
 /// <returns>System.Object.</returns>
 protected virtual object CreateLazyObject(ObjectConstructionArgs args)
 {
     return  _generator.CreateClassProxy(args.Configuration.Type, new LazyObjectInterceptor(args));
 }
		/// <summary>
		/// Initializes a new instance of the <see cref="InterfacePropertyInterceptor"/> class.
		/// </summary>
		/// <param name="args">The args.</param>
		public InterfacePropertyInterceptor(ObjectConstructionArgs args)
		{
			_args = args;
		    _fullName = _args.Configuration.Type.FullName;
			_lazyValues = new Lazy<IDictionary<string, object>>(LoadValues);
		}
		private IDictionary<string, object> LoadValues()
		{
			var config = _args.Configuration;

			var values = new Dictionary<string, object>();
			var mappingContext = _args.Service.CreateDataMappingContext(_args.AbstractTypeCreationContext, null);

			foreach (var property in config.Properties)
			{
				var result = property.Mapper.MapToProperty(mappingContext);
				values[property.PropertyInfo.Name] = result;
			}

            //release the context
		    _args = null;

			return values;
		}
Пример #21
0
        /// <summary>
        /// Creates the object.
        /// </summary>
        /// <param name="args">The args.</param>
        /// <returns>System.Object.</returns>
        protected virtual object CreateObject(ObjectConstructionArgs args)
        {

            var constructorParameters = args.AbstractTypeCreationContext.ConstructorParameters;

            Delegate conMethod = null;
            object obj;

            try
            {
                if (constructorParameters == null || constructorParameters.Length == 0)
                {
                    //conMethod = args.Configuration.DefaultConstructor;
                    obj = Activator.CreateInstance(args.Configuration.Type);
                }
                else
                {
                    var parameters = constructorParameters.Select(x => x.GetType()).ToArray();
                    var constructorInfo = args.Configuration.Type.GetConstructor(parameters);
                    conMethod = args.Configuration.ConstructorMethods[constructorInfo];
                    obj = conMethod.DynamicInvoke(constructorParameters);
                }

                args.Configuration.MapPropertiesToObject(obj, args.Service, args.AbstractTypeCreationContext);
            }
            catch (Exception ex)
            {
                throw new MapperException("Failed to create type {0}".Formatted(args.Configuration.Type), ex);
            }
            return obj;
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="InterfacePropertyInterceptor"/> class.
 /// </summary>
 /// <param name="args">The args.</param>
 public InterfacePropertyInterceptor(ObjectConstructionArgs args)
 {
     _args = args;
 }
Пример #23
0
 /// <summary>
 /// Initializes a new instance of the <see cref="LazyObjectInterceptor"/> class.
 /// </summary>
 /// <param name="args">The args.</param>
 public LazyObjectInterceptor(ObjectConstructionArgs args)
 {
     _args = args;
 }
   /// <summary>
 /// Initializes a new instance of the <see cref="MultiInterfacePropertyInterceptor"/> class.
 /// </summary>
 /// <param name="args">The args.</param>
 public MultiInterfacePropertyInterceptor(ObjectConstructionArgs args)
 {
     _args = args;
       _configs =
           new[] {args.Configuration}.Union(args.Parameters[CreateMultiInferaceTask.MultiInterfaceConfigsKey] as IEnumerable<AbstractTypeConfiguration>);
 }
		/// <summary>
		/// Initializes a new instance of the <see cref="InterfacePropertyInterceptor"/> class.
		/// </summary>
		/// <param name="args">The args.</param>
		public InterfacePropertyInterceptor(ObjectConstructionArgs args)
		{
			_args = args;
			_lazyValues = new Lazy<IDictionary<string, object>>(LoadValues);
		}