Example #1
0
 /// <summary>
 /// Constructs an ItemId from an unspecified object which can be a container, a content class or a summary
 /// </summary>
 /// <param name="coll">The collator for the system in which to create this itemid</param>
 /// <param name="o">The unspecified object</param>
 public ItemId(Collator coll, object o)
 {
     if (o is IContentContainer)
     {
         this.Type = Collator.GetContentType(o);
         this.Id   = coll.GetIdProperty(o.GetType().UnproxiedType()).GetValue(o);
     }
     else if (o is Summary)
     {
         this.Type = ((Summary)o).Type;
         this.Id   = ((Summary)o).Id;
     }
     else
     {
         var container = coll.GetContainer(o);
         this.Type = Collator.GetContentType(container);
         this.Id   = coll.GetIdProperty(container.GetType().UnproxiedType()).GetValue(container);
     }
 }
Example #2
0
 /// <summary>
 /// Construct an ItemId by extracting it from a container
 /// </summary>
 /// <param name="coll">The collator from the system for which we are determining the itemid</param>
 /// <param name="container">The container</param>
 public ItemId(Collator coll, IContentContainer container)
     : this(Collator.GetContentType(container), coll.GetIdProperty(container.GetType().UnproxiedType()).GetValue(container))
 {
 }
Example #3
0
        /// <summary>
        /// Get a query body which converts the address into a query which should return the item from that address
        /// when used as an argument to a Repository method.
        /// </summary>
        /// <typeparam name="T">Container type query is applied to, which the address addresses</typeparam>
        /// <param name="coll">The collator of the data system in which the query body will run</param>
        /// <returns>The query body</returns>
        public Func <IQueryable <T>, IQueryable <T> > GetAsQueryBody <T>(Collator coll)
        {
            Func <IQueryable <T>, IQueryable <T> > queryBody = null;

            if (this.ContainsKey("_id"))
            {
                var idProp = coll.GetIdProperty(typeof(T));
                return(iq => iq.Where(LinqX.GetPropertyTest <T>(idProp.Name, this["_id"])));
            }

            Dictionary <string, string> keyProps = typeof(T).GetProperties()
                                                   .Select(pi => new { pi, attr = pi.GetCustomAttribute <AddressComponentAttribute>() })
                                                   .Where(pii => pii.attr != null)
                                                   .ToDictionary(pii => pii.attr.UsePath ? "{Path}" : (pii.attr.RouteKey ?? ("_" + pii.pi.Name)),
                                                                 pii => pii.pi.Name);

            if (keyProps.ContainsKey("{Path}"))
            {
                queryBody = iq => iq.Where(LinqX.GetPropertyTest <T>(keyProps["{Path}"], GetAsContentPath()));
            }
            else
            {
                foreach (string key in keyProps.Keys)
                {
                    string propName = keyProps[key];
                    object matchVal;
                    if (this.ContainsKey(key))
                    {
                        matchVal = this[key];
                    }
                    else
                    {
                        // this address doesn't have a value for this address component property so use a default value
                        Type keyType = typeof(T).GetProperty(propName).PropertyType;
                        if (keyType.IsValueType())
                        {
                            matchVal = Activator.CreateInstance(keyType);
                        }
                        else
                        {
                            matchVal = null;
                        }
                    }
                    if (queryBody == null)
                    {
                        queryBody = iq => iq.Where(LinqX.GetPropertyTest <T>(propName, matchVal));
                    }
                    else
                    {
                        var innerQueryBody = queryBody;
                        queryBody = iq => innerQueryBody(iq).Where(LinqX.GetPropertyTest <T>(propName, matchVal));
                    }
                }
                if (queryBody == null)
                {
                    queryBody = iq => iq;
                }
            }

            return(queryBody);
        }