コード例 #1
0
 public  void ReadFromItem(ISitecoreService service, object target, Item item, SitecoreClassConfig config)
 {
     foreach (var handler in config.DataHandlers)
     {
         handler.SetProperty(target, item, service);
     }
 }
コード例 #2
0
        public static object CreateProxy(SitecoreClassConfig config,  ISitecoreService service, Item item, bool inferType){
            object proxy = null;

            Type type = config.Type;

            if (type.IsInterface)
            {
                proxy = _generator.CreateInterfaceProxyWithoutTarget(type, new InterfaceMethodInterceptor(config, item, service));
            }
            else
            {
                proxy = _generator.CreateClassProxy(type, _options, new ProxyClassInterceptor(type,
                   service,
                   item, inferType));
            }

            return proxy;

        }
コード例 #3
0
        /// <summary>
        /// Returns a delegate method that will load a class based on its constuctor
        /// </summary>
        /// <param name="classConfig">The SitecoreClassConfig to store the delegate method in</param>
        /// <param name="constructorParameters">The list of contructor parameters</param>
        /// <param name="delegateType">The type of the delegate function to create</param>
        /// <returns></returns>
        internal static void CreateConstructorDelegates(SitecoreClassConfig classConfig)
        {
            Type type = classConfig.Type;

            var constructors = type.GetConstructors();

            foreach (var constructor in constructors)
            {
                var parameters = constructor.GetParameters();

                var dynMethod = new DynamicMethod("DM$OBJ_FACTORY_" + type.Name, type, parameters.Select(x => x.ParameterType).ToArray(), type);

                ILGenerator ilGen = dynMethod.GetILGenerator();
                for (int i = 0; i < parameters.Count(); i++)
                {
                    ilGen.Emit(OpCodes.Ldarg, i);
                }

                ilGen.Emit(OpCodes.Newobj, constructor);

                ilGen.Emit(OpCodes.Ret);

                Type genericType = null;
                switch (parameters.Count())
                {
                    case 0:
                        genericType = typeof(Func<>);
                        break;
                    case 1:
                         genericType = typeof(Func<,>);
                         break;
                    case 2:
                         genericType = typeof(Func<,,>);
                         break;
                    case 3:
                         genericType = typeof(Func<,,,>);
                        break;
                    case 4:
                         genericType = typeof(Func<,,,,>);
                         break;
                    default:
                         throw new MapperException("Only supports constructors with  a maximum of 4 parameters");
                }

                var delegateType = genericType.MakeGenericType(parameters.Select(x=>x.ParameterType).Concat(new []{ type}).ToArray());


                classConfig.CreateObjectMethods[constructor] = dynMethod.CreateDelegate(delegateType);
            }
        }
コード例 #4
0
 public InterfaceMethodInterceptor(SitecoreClassConfig config, Item item, ISitecoreService service){
     _config = config;
     _item = item;
     _service = service;
     _values = new Dictionary<string, object>();
 }
コード例 #5
0
         private void BaseTemplateChecks(ItemDefinition template, DataProvider provider, CallContext context, SitecoreClassConfig config)
         {
             //check base templates



             var templateItem = Factory.GetDatabase("master").GetItem(template.ID);


             var baseTemplatesField = templateItem[BaseTemplatesFieldId];
             StringBuilder sb = new StringBuilder(baseTemplatesField);

             global::Sitecore.Diagnostics.Log.Info("Type {0}".Formatted(config.Type.FullName), this);


             Action<Type> idCheck = (type) =>
             {
                global::Sitecore.Diagnostics.Log.Info("ID Check {0}".Formatted(type.FullName), this);

                 if (!Classes.ContainsKey(type)) return;

                 var baseConfig = Classes[type];
                 //TODO: check if inherited templates inherit this template, maybe fix this in de SitecoreClass attribute (option: inherit all)
                 if (baseConfig != null && !string.IsNullOrEmpty(baseConfig.ClassAttribute.TemplateId))
                 {
                     if (!baseTemplatesField.Contains(baseConfig.ClassAttribute.TemplateId))
                     {
                         sb.Append("|{0}".Formatted(baseConfig.ClassAttribute.TemplateId));
                     }
                 }
             };

             Type baseType = config.Type.BaseType;


             while (baseType != null)
             {
                 idCheck(baseType);
                 baseType = baseType.BaseType;
             }

             

             config.Type.GetInterfaces().ForEach(x => idCheck(x));

             //if you inherit from a sitecoreclass model it collects it as a 'new' model and results in inheriting itself
             if (sb.ToString().IndexOf(template.ID.ToString(), StringComparison.InvariantCultureIgnoreCase) > -1)
                 sb = new StringBuilder(Regex.Replace(sb.ToString(), Regex.Escape(string.Format("|{0}", template.ID)), string.Empty, RegexOptions.IgnoreCase));

             if (baseTemplatesField != sb.ToString())
             {
                 templateItem.Editing.BeginEdit();
                 templateItem[BaseTemplatesFieldId] = sb.ToString();
                 templateItem.Editing.EndEdit();
             }


         }
コード例 #6
0
        private IDList GetChildIDsTemplate(SitecoreClassConfig template, ItemDefinition itemDefinition, CallContext context)
        {
            IDList fields = new IDList();

            List<string> processed = new List<string>();
            var sections = template.Properties
                .Where(x=>x.Property.DeclaringType == template.Type)
                .Select(x=>x.Attribute).OfType<SitecoreFieldAttribute>()
                .Select(x => new { x.SectionName, x.SectionSortOrder });

            var providers = Database.GetDataProviders();
            var otherProvider = providers.FirstOrDefault(x => !(x is GlassDataProvider));
            //If sitecore contains a section with the same name in the database, use that one instead of creating a new one
            var otherChildIds = otherProvider.GetChildIDs(itemDefinition, context);
            var existing = (otherChildIds ?? new IDList()).OfType<ID>().Select(id => otherProvider.GetItemDefinition(id, context)).ToList();

            foreach (var section in sections)
            {
                if (processed.Contains(section.SectionName) || section.SectionName.IsNullOrEmpty())
                    continue;

                var record = SectionTable.FirstOrDefault(x => x.TemplateId == itemDefinition.ID && x.Name == section.SectionName);

                if (record == null)
                {
                    var exists = existing.FirstOrDefault(def => def.Name.Equals(section));
                    if (exists != null)
                    {
                        record = new SectionInfo(section.SectionName, exists.ID, itemDefinition.ID, section.SectionSortOrder) { Existing = true };
                    }
                    else
                    {
                        record = new SectionInfo(section.SectionName, new ID(Guid.NewGuid()), itemDefinition.ID, section.SectionSortOrder);
                    }
                    SectionTable.Add(record);
                }
                processed.Add(section.SectionName);
                if (!record.Existing)
                    fields.Add(record.SectionId);
            }
            return fields;
        }
コード例 #7
0
         private void BaseTemplateChecks(ItemDefinition template, DataProvider provider, CallContext context, SitecoreClassConfig config)
         {
             //check base templates



             var templateItem = Factory.GetDatabase("master").GetItem(template.ID);


             var baseTemplatesField = templateItem[BaseTemplatesFieldId];
             StringBuilder sb = new StringBuilder(baseTemplatesField);

             global::Sitecore.Diagnostics.Log.Info("Type {0}".Formatted(config.Type.FullName), this);


             Action<Type> idCheck = (type) =>
             {
                global::Sitecore.Diagnostics.Log.Info("ID Check {0}".Formatted(type.FullName), this);

                 if (!Classes.ContainsKey(type)) return;

                 var baseConfig = Classes[type];
                 if (baseConfig != null && baseConfig.ClassAttribute.CodeFirst)
                 {
                     if (!baseTemplatesField.Contains(baseConfig.ClassAttribute.TemplateId))
                     {
                         sb.Append("|{0}".Formatted(baseConfig.ClassAttribute.TemplateId));
                     }
                 }
             };

             Type baseType = config.Type.BaseType;


             while (baseType != null)
             {
                 idCheck(baseType);
                 baseType = baseType.BaseType;
             }

             

             config.Type.GetInterfaces().ForEach(x => idCheck(x));

             if (baseTemplatesField != sb.ToString())
             {
                 templateItem.Editing.BeginEdit();
                 templateItem[BaseTemplatesFieldId] = sb.ToString();
                 templateItem.Editing.EndEdit();
             }


         }