Ejemplo n.º 1
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))
 {
 }
Ejemplo n.º 2
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);
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Construct an ItemId by extracting it from a container, using the primary Lynicon system for this
 /// </summary>
 /// <param name="container">The container</param>
 public ItemId(IContentContainer container)
     : this(Collator.GetContentType(container), Collator.Instance.GetIdProperty(container.GetType().UnproxiedType()).GetValue(container))
 {
 }
Ejemplo n.º 4
0
 /// <summary>
 /// Constructs an address by extracting it from a container or a content item where AddressComponent attributes were
 /// used to map content fields to the address
 /// </summary>
 /// <param name="cont">The container or content item</param>
 public Address(object cont)
     : base(FromContainer(cont))
 {
     this.Type = Collator.GetContentType(cont);
 }