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 (_lazyLoadingHelper.IsEnabled(args.Options)) { var resolved = GetConstructorParameters(configuration); var proxy = _generator.CreateClassProxy(configuration.Type, resolved, new LazyObjectInterceptor(mappingAction, args)); args.Result = proxy; ModelCounter.Instance.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; ModelCounter.Instance.ConcreteModelCreated++; ModelCounter.Instance.ModelsMapped++; } } } base.Execute(args); }
public LazyObjectInterceptor(ObjectConstructionArgs args, LazyLoadingHelper lazyLoadingHelper) { _args = args; Values = new ConcurrentDictionary <string, object>(); _mappingContext = _args.AbstractTypeCreationContext.CreateDataMappingContext(null); //if lazy loading diabled load all values now if (!lazyLoadingHelper.IsEnabled(args.Options)) { LoadAllValues(); } }
/// <summary> /// Initializes a new instance of the <see cref="LazyItemEnumerable{T}"/> class. /// </summary> /// <param name="getItems">The get items.</param> /// <param name="isLazy">if set to <c>true</c> [is lazy].</param> /// <param name="inferType">if set to <c>true</c> [infer type].</param> /// <param name="service">The service.</param> /// <param name="versionHandler"></param> public LazyItemEnumerable( GetItemsOptions options, ISitecoreService service, LazyLoadingHelper lazyLoadingHelper ) { _options = options; _service = service; _lazyItemList = new Lazy <IList <T> >(() => ProcessItems()); if (!lazyLoadingHelper.IsEnabled(options)) { // Force the loading of the items into the list so this occurs in the current security scope. var dummy = _lazyItemList.Value; } }
/// <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 (_lazyLoadingHelper.IsEnabled(args.Options)) { //here we create a lazy loaded version of the class args.Result = CreateLazyObject(args); ModelCounter.Instance.ProxyModelsCreated++; } else { //here we create a concrete version of the class args.Result = CreateObjectAndMapProperties(args); ModelCounter.Instance.ModelsMapped++; ModelCounter.Instance.ConcreteModelCreated++; } } base.Execute(args); }