protected void ResolveManager()
 {
     SPModelDescriptor.RegisterReferencedAssemblies(this.Web.Read().Site);
     this.Descriptor = SPModelDescriptor.Resolve(this.TypeName);
     this.Manager    = this.Descriptor.CreateManager(this.Web.Read());
     OnManagerResolved();
 }
 public SPModelFieldAssociation(SPModelDescriptor descriptor, SPFieldAttribute attribute, PropertyInfo queryProperty)
 {
     CommonHelper.ConfirmNotNull(descriptor, "descriptor");
     CommonHelper.ConfirmNotNull(attribute, "attribute");
     this.descriptor    = descriptor;
     this.attribute     = attribute;
     this.queryProperty = queryProperty;
 }
        protected override Expression VisitTypeIs(TypeBinaryExpression expression)
        {
            SPModelQueryExpressionScope currentScope = stack.Peek();

            try {
                SPModelDescriptor descriptor = SPModelDescriptor.Resolve(expression.TypeOperand);
                currentScope.Expression = descriptor.GetContentTypeExpression(manager.Descriptor);
            } catch (ArgumentException) {
                currentScope.Expression = Caml.False;
            }
            return(expression);
        }
Beispiel #4
0
 protected override Expression VisitTypeIs(TypeBinaryExpression expression)
 {
     if (invariantExpression || builder.SelectExpression != null)
     {
         return(base.VisitTypeIs(expression));
     }
     try {
         SPModelDescriptor descriptor = SPModelDescriptor.Resolve(expression.TypeOperand);
         currentScope.Expression = descriptor.GetContentTypeExpression(manager.Descriptor);
     } catch (ArgumentException) {
         currentScope.Expression = Caml.False;
     }
     return(expression);
 }
Beispiel #5
0
        private SPModelManager <SiteConfigEntry> CreateManager(SPSite site)
        {
            SPListAttribute listAttribute = new SPListAttribute();

            listAttribute.Url              = "Lists/SiteConfig";
            listAttribute.Title            = "Site Config";
            listAttribute.EnableVersioning = SPOption.True;
            listAttribute.OnQuickLaunch    = true;
            listAttribute.DefaultViewQuery = "<OrderBy><FieldRef Name=\"SiteConfigCategory\" Ascending=\"TRUE\"/><FieldRef Name=\"Title\" Ascending=\"TRUE\"/></OrderBy>";

            listAttribute = InitializeListSettings(listAttribute);
            SPModelDescriptor descriptor = SPModelDescriptor.Resolve(typeof(SiteConfigEntry));

            descriptor.Provision(site.RootWeb, SPModelProvisionOptions.Asynchronous, new SPModelListProvisionOptions(listAttribute)).GetListCollection();
            return(new SPModelManager <SiteConfigEntry>(site.RootWeb));
        }
Beispiel #6
0
 public static IEnumerable <SPFieldAttribute> EnumerateFieldAttributes(SPModelDescriptor descriptor, Type sourceType)
 {
     CommonHelper.ConfirmNotNull(descriptor, "descriptor");
     CommonHelper.ConfirmNotNull(sourceType, "sourceType");
     lock (syncLock) {
         foreach (SPFieldAttribute v in EnumerateFieldAttributes(descriptor, sourceType, sourceType))
         {
             yield return(v);
         }
         foreach (Type interfaceType in sourceType.GetInterfaces())
         {
             foreach (SPFieldAttribute v in EnumerateFieldAttributes(descriptor, interfaceType, sourceType))
             {
                 yield return(v);
             }
         }
     }
 }
        object IDynamicParameters.GetDynamicParameters()
        {
            ResolveManager();
            SPModelDescriptor descriptor = SPModelDescriptor.Resolve(this.TypeName);
            RuntimeDefinedParameterDictionary parameters = new RuntimeDefinedParameterDictionary();

            foreach (PropertyInfo property in descriptor.ModelType.GetProperties())
            {
                if (property.CanWrite || property.PropertyType.IsOf(typeof(IList <>)))
                {
                    ParameterAttribute attribute = new ParameterAttribute();
                    attribute.ParameterSetName = "__AllParameterSets";
                    parameters.Add(property.Name, new RuntimeDefinedParameter(property.Name, property.PropertyType, new Collection <Attribute> {
                        attribute
                    }));
                }
            }
            modelParameters = parameters;
            return(parameters);
        }
Beispiel #8
0
        private SPModelParameterizedQuery(ParameterizedExpression expression, ISPModelManagerInternal manager)
        {
            SPModelQueryBuilder builder = SPModelQueryExpressionVisitor.Translate(expression, manager);

            this.Descriptor = builder.ModelType != null?SPModelDescriptor.Resolve(builder.ModelType) : manager.Descriptor;

            this.Expression     = builder.Expression;
            this.TaxonomyFields = new ReadOnlyCollection <string>(builder.TaxonomyFields.ToArray());

            this.expression  = expression;
            this.executeMode = builder.ExecuteMode;
            this.emptyArray  = Array.CreateInstance(this.Descriptor.ModelType, 0);
            if (builder.SelectExpression != null)
            {
                this.projector = ((Expression <ResultEvaluator>)builder.SelectExpression).Compile();
            }
            else
            {
                this.projector = (ResultEvaluator)Delegate.CreateDelegate(typeof(ResultEvaluator), mOfType.MakeGenericMethod(this.Descriptor.ModelType));
            }
            this.evaluators = new ReadOnlyDictionary <string, ParameterEvaluator>(builder.ParameterEvaluators);
            for (int i = 0; i < PIndexMax; i++)
            {
                parameterNames[i] = (string)builder.Parameters[i];
            }

            if (builder.ModelType != null)
            {
                this.ContentTypeFilterExpression = builder.ContentTypeIds.Aggregate(Caml.False, (v, a) => v | Caml.OfContentType(a));
            }
            if (!builder.SelectAllProperties)
            {
                List <string> properties = new List <string>(builder.SelectProperties);
                if (!properties.Contains(SPBuiltInFieldName.ContentTypeId))
                {
                    properties.Add(SPBuiltInFieldName.ContentTypeId);
                }
                this.SelectProperties = new ReadOnlyCollection <string>(properties);
            }
        }
Beispiel #9
0
        private static IEnumerable <SPFieldAttribute> EnumerateFieldAttributes(SPModelDescriptor descriptor, Type sourceType, Type implementedType)
        {
            InterfaceMapping mapping = default(InterfaceMapping);

            if (sourceType.IsInterface)
            {
                mapping = implementedType.GetInterfaceMap(sourceType);
            }
            foreach (MemberInfo member in sourceType.GetMembers(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance))
            {
                SPModelFieldAssociationCollection collection      = QueryableFields.EnsureKeyValue(member);
                SPModelFieldAssociationCollection otherCollection = null;
                if (sourceType.IsInterface)
                {
                    MethodInfo lookupMethod = null;
                    if (member.MemberType == MemberTypes.Property)
                    {
                        lookupMethod = ((PropertyInfo)member).GetGetMethod() ?? ((PropertyInfo)member).GetSetMethod();
                    }
                    else if (member.MemberType == MemberTypes.Method)
                    {
                        lookupMethod = (MethodInfo)member;
                    }
                    if (lookupMethod != null)
                    {
                        int pos = Array.IndexOf(mapping.InterfaceMethods, lookupMethod);
                        if (pos >= 0)
                        {
                            MemberInfo mappedMember = mapping.TargetMethods[pos];
                            if (member.MemberType == MemberTypes.Property)
                            {
                                mappedMember = implementedType.GetProperty(mappedMember.Name.Substring(4));
                            }
                            if (mappedMember != null)
                            {
                                otherCollection = QueryableFields.EnsureKeyValue(mappedMember);
                                if (otherCollection.Count > 0)
                                {
                                    foreach (SPModelFieldAssociation value in otherCollection)
                                    {
                                        collection.Add(value);
                                    }
                                }
                            }
                        }
                    }
                }

                SPFieldProvisionMode provisionMode = SPFieldProvisionMode.Default;
                SPModelFieldAssociationCollection basePropertyCollection = null;
                IEnumerable <SPFieldAttribute>    attributes             = member.GetCustomAttributes <SPFieldAttribute>(true);

                if (member.MemberType == MemberTypes.Property)
                {
                    PropertyInfo baseProperty = ((PropertyInfo)member).GetBaseDefinition();
                    if (baseProperty != member)
                    {
                        if (!attributes.Any())
                        {
                            attributes    = baseProperty.GetCustomAttributes <SPFieldAttribute>(false);
                            provisionMode = SPFieldProvisionMode.None;
                        }
                        else
                        {
                            basePropertyCollection = QueryableFields.EnsureKeyValue(baseProperty);
                            provisionMode          = SPFieldProvisionMode.FieldLink;
                        }
                    }
                }
                if (member.DeclaringType != sourceType)
                {
                    provisionMode = SPFieldProvisionMode.FieldLink;
                }
                foreach (SPFieldAttribute attribute in attributes)
                {
                    if (attribute.IncludeInQuery)
                    {
                        PropertyInfo property = null;
                        SPModelQueryPropertyAttribute queryPropertyAttribute = member.GetCustomAttribute <SPModelQueryPropertyAttribute>(false);
                        if (queryPropertyAttribute != null)
                        {
                            property = queryPropertyAttribute.QueryProperty;
                        }
                        SPModelFieldAssociation value = new SPModelFieldAssociation(descriptor, attribute, property);
                        collection.Add(value);
                        if (otherCollection != null)
                        {
                            otherCollection.Add(value);
                        }
                        if (basePropertyCollection != null)
                        {
                            basePropertyCollection.Add(value);
                        }
                        if (property != null)
                        {
                            SPModelFieldAssociationCollection foreignPropertyCollection = QueryableFields.EnsureKeyValue(property);
                            foreignPropertyCollection.Add(new SPModelFieldAssociation(descriptor, attribute, null));
                        }
                    }
                    yield return(attribute.Clone(provisionMode));
                }
            }
        }
Beispiel #10
0
        private CamlExpression GetExpressionFromQueryableMethod(MethodCallExpression expression)
        {
            int argCount = expression.Arguments.Count;

            if (argCount > 1)
            {
                LambdaExpression lamdba = StripQuotes(expression.Arguments[1]) as LambdaExpression;
                if (lamdba != null)
                {
                    lambdaParam = lamdba.Parameters[0];
                }
            }

            switch (expression.Method.Name)
            {
            case "Count":
            case "Any":
            case "FirstOrDefault":
            case "First":
            case "SingleOrDefault":
            case "Single":
                if (argCount == 2)
                {
                    Visit(expression.Arguments[1]);
                }
                builder.ExecuteMode = Enum <SPModelQueryExecuteMode> .Parse(expression.Method.Name);

                AppendSelectExpression(expression, builder.ExecuteMode.ToString(), true);
                return(currentScope.Expression);

            case "All":
                Visit(expression.Arguments[1]);
                builder.ExecuteMode = SPModelQueryExecuteMode.Any;
                AppendSelectExpression(expression, builder.ExecuteMode.ToString(), true);
                return(~currentScope.Expression);

            case "ElementAtOrDefault":
            case "ElementAt":
                Visit(expression.Arguments[1]);
                builder.Parameters[SPModelParameterizedQuery.PIndexOffset] = currentScope.ParameterName;
                builder.ExecuteMode = expression.Method.Name == "ElementAt" ? SPModelQueryExecuteMode.First : SPModelQueryExecuteMode.FirstOrDefault;
                AppendSelectExpression(expression, builder.ExecuteMode.ToString(), true, Expression.Constant(0));
                return(Caml.Empty);

            case "Aggregate":
            case "Average":
            case "Max":
            case "Min":
            case "Sum":
            case "Select":
            case "SelectMany":
                AppendSelectExpression(expression);
                return(Caml.Empty);

            case "Take":
                Visit(expression.Arguments[1]);
                builder.Parameters[SPModelParameterizedQuery.PIndexLimit] = currentScope.ParameterName;
                return(Caml.Empty);

            case "Skip":
                Visit(expression.Arguments[1]);
                builder.Parameters[SPModelParameterizedQuery.PIndexOffset] = currentScope.ParameterName;
                return(Caml.Empty);

            case "OrderBy":
            case "ThenBy":
            case "OrderByDescending":
            case "ThenByDescending":
                CamlOrder dir = expression.Method.Name.Contains("Descending") ? CamlOrder.Descending : CamlOrder.Ascending;
                Visit(expression.Arguments[1]);
                return(this.currentScope.GetExpression(s => Caml.OrderBy(s.FieldRef, dir), true));

            case "Where":
                Visit(expression.Arguments[1]);
                return(currentScope.Expression);

            case "OfType":
                if (builder.SelectExpression != null)
                {
                    AppendSelectExpression(expression);
                }
                else
                {
                    if (builder.ModelType == null)
                    {
                        builder.ContentTypeIds.AddRange(manager.Descriptor.ContentTypeIds);
                    }
                    builder.ModelType = expression.Method.GetGenericArguments()[0];
                    SPModelDescriptor descriptor;
                    try {
                        descriptor = SPModelDescriptor.Resolve(builder.ModelType);
                    } catch (ArgumentException) {
                        throw new NotSupportedException("'OfType' constraint must be used with valid model type or interface type");
                    }
                    SPContentTypeId[] result = SPModelDescriptor.IntersectContentTypeIds(builder.ContentTypeIds, descriptor.ContentTypeIds.ToArray());
                    builder.ContentTypeIds.Clear();
                    builder.ContentTypeIds.AddRange(result);
                }
                return(Caml.Empty);
            }
            throw ThrowMethodNotSupported(expression.Method);
        }