Example #1
0
        public static SPModelUsage Create(SPList list, SPContentTypeId id)
        {
            CommonHelper.ConfirmNotNull(list, "list");
            SPModelUsage returnValue = new SPModelUsage(list, id, true);

            dictionary.EnsureKeyValue(id, () => returnValue.GetWithoutList());
            foreach (SPContentType contentType in list.ContentTypes)
            {
                dictionary.EnsureKeyValue(contentType.Id, () => new SPModelUsage(list, contentType.Id, false));
            }
            return(returnValue);
        }
        public static TValue EnsureKeyValue <TKey, TValue>(this IDictionary <TKey, TValue> dictionary, TKey key, Func <TKey, TValue> valueFactory)
        {
            CommonHelper.ConfirmNotNull(dictionary, "dictionary");
            CommonHelper.ConfirmNotNull(key, "key");
            CommonHelper.ConfirmNotNull(valueFactory, "valueFactory");
            ConcurrentDictionary <TKey, TValue> concurrent = dictionary as ConcurrentDictionary <TKey, TValue>;

            if (concurrent != null)
            {
                return(concurrent.EnsureKeyValue(key, valueFactory));
            }
            TValue value;

            if (dictionary.TryGetValue(key, out value))
            {
                return(value);
            }
            TValue newValue = valueFactory(key);

            if (Object.ReferenceEquals(newValue, null))
            {
                throw new InvalidOperationException("valueFactory returned null");
            }
            dictionary.Add(key, newValue);
            return(newValue);
        }
Example #3
0
        private static LoggerOptionsAttribute GetCallingMethodOptions(string customCategory)
        {
            StackFrame[] frames   = new StackTrace(2).GetFrames();
            int          hashCode = frames.Aggregate(0, (v, a) => v ^ a.GetMethod().GetHashCode() + 13);
            ConcurrentDictionary <MethodBase, LoggerOptionsAttribute> dictionary = attributeCaches.EnsureKeyValue(hashCode);
            LoggerOptionsAttribute attribute = dictionary.EnsureKeyValue(frames[0].GetMethod(), () => GetCallingMethodOptions(dictionary, frames));

            attribute          = attribute.Clone();
            attribute.Category = customCategory ?? attribute.Category ?? "General";
            return(attribute);
        }
        public static TValue EnsureKeyValue <TKey, TValue>(this IDictionary <TKey, TValue> dictionary, TKey key) where TValue : new()
        {
            CommonHelper.ConfirmNotNull(dictionary, "dictionary");
            CommonHelper.ConfirmNotNull(key, "key");
            ConcurrentDictionary <TKey, TValue> concurrent = dictionary as ConcurrentDictionary <TKey, TValue>;

            if (concurrent != null)
            {
                return(concurrent.EnsureKeyValue(key));
            }
            TValue value;

            if (dictionary.TryGetValue(key, out value))
            {
                return(value);
            }
            TValue newValue = ReflectionHelper.CreateInstance <TValue>();

            dictionary.Add(key, newValue);
            return(newValue);
        }
Example #5
0
 public static SPModelFieldAssociationCollection GetByMember(MemberInfo member)
 {
     return(QueryableFields.EnsureKeyValue(member));
 }
Example #6
0
        private SPModelDescriptor(Type targetType, SPModelDefaultsAttribute defaultsAttribute)
        {
            this.ModelType = targetType;
            TargetTypeDictionary.TryAdd(targetType, this);
            TargetTypeDictionary.TryGetValue(targetType.BaseType, out this.Parent);
            if (this.Parent is SPModelInterfaceTypeDescriptor)
            {
                this.Parent = null;
            }

            this.contentTypeAttribute = targetType.GetCustomAttribute <SPContentTypeAttribute>(false);
            ResolveContentTypeId(contentTypeAttribute, targetType);
            ContentTypeDictionary.Add(contentTypeAttribute.ContentTypeId, this);

            this.defaultManagerType         = GetDefaultManagerType(targetType);
            this.provisionEventReceiverType = contentTypeAttribute.ProvisionEventReceiverType;
            this.hasExplicitListAttribute   = targetType.GetCustomAttribute <SPListAttribute>(false) != null;
            this.listAttribute   = targetType.GetCustomAttribute <SPListAttribute>(true) ?? new SPListAttribute();
            this.fieldAttributes = SPModelFieldAssociationCollection.EnumerateFieldAttributes(this, targetType).ToArray();

            if (contentTypeAttribute.Group == null && defaultsAttribute != null)
            {
                contentTypeAttribute.Group = defaultsAttribute.DefaultContentTypeGroup;
            }
            foreach (SPFieldAttribute attribute in fieldAttributes)
            {
                if (attribute.Group == null)
                {
                    if (this.Parent != null)
                    {
                        SPFieldAttribute baseAttribute = this.Parent.fieldAttributes.FirstOrDefault(v => v.InternalName == attribute.InternalName);
                        if (baseAttribute != null)
                        {
                            attribute.Group = baseAttribute.Group;
                            continue;
                        }
                    }
                    if (defaultsAttribute != null)
                    {
                        attribute.Group = defaultsAttribute.DefaultFieldGroup;
                    }
                }
            }

            if (contentTypeAttribute.ContentTypeId.IsChildOf(ContentTypeId.Page))
            {
                this.ItemType = SPModelItemType.PublishingPage;
            }
            else if (contentTypeAttribute.ContentTypeId.IsChildOf(SPBuiltInContentTypeId.DocumentSet))
            {
                this.ItemType = SPModelItemType.DocumentSet;
            }
            else if (contentTypeAttribute.ContentTypeId.IsChildOf(SPBuiltInContentTypeId.Folder))
            {
                this.ItemType = SPModelItemType.Folder;
            }
            else if (contentTypeAttribute.ContentTypeId.IsChildOf(SPBuiltInContentTypeId.Document))
            {
                this.ItemType = SPModelItemType.File;
            }

            if (this.ItemType == SPModelItemType.GenericItem)
            {
                this.baseType = SPBaseType.GenericList;
            }
            else if (contentTypeAttribute.ContentTypeId.IsChildOf(SPBuiltInContentTypeId.Issue))
            {
                this.baseType = SPBaseType.Issue;
            }
            else
            {
                this.baseType = SPBaseType.DocumentLibrary;
            }

            if (this.Parent != null)
            {
                this.Parent.Children.Add(this);
                this.fieldAttributes = fieldAttributes.Concat(this.Parent.fieldAttributes).Distinct().ToArray();
                if (provisionEventReceiverType == null)
                {
                    this.provisionEventReceiverType = this.Parent.provisionEventReceiverType;
                }
            }

            foreach (SPFieldAttribute v in fieldAttributes)
            {
                AddRequiredViewField(v);
            }
            foreach (Type interfaceType in targetType.GetInterfaces())
            {
                if (!interfaceType.IsDefined(typeof(SPModelIgnoreAttribute), true))
                {
                    SPModelInterfaceTypeDescriptor interfaceDescriptor = (SPModelInterfaceTypeDescriptor)TargetTypeDictionary.EnsureKeyValue(interfaceType, SPModelInterfaceTypeDescriptor.Create);
                    interfaceDescriptor.AddImplementedType(this);
                    this.Interfaces.Add(interfaceDescriptor);
                }
            }
            if (targetType.BaseType != typeof(SPModel) && targetType.BaseType.GetCustomAttribute <SPContentTypeAttribute>(false) == null)
            {
                SPModelInterfaceTypeDescriptor interfaceDescriptor = (SPModelInterfaceTypeDescriptor)TargetTypeDictionary.EnsureKeyValue(targetType.BaseType, SPModelInterfaceTypeDescriptor.Create);
                interfaceDescriptor.AddImplementedType(this);
                this.Interfaces.Add(interfaceDescriptor);
            }
            if (!targetType.IsAbstract)
            {
                instanceType = new Lazy <Type>(() => targetType);
            }
            else
            {
                instanceType = new Lazy <Type>(() => SPModel.BuildTypeFromAbstractBaseType(targetType), LazyThreadSafetyMode.ExecutionAndPublication);
            }
        }