Beispiel #1
0
        public MantisClient(string baseUri, string apiKey)
            : this()
        {
            _lookupCache = new LookupCache();

            _baseUri = this.NormalizeUri(baseUri);
            _apiKey  = apiKey;
            _restClient.Authorization = _apiKey;
        }
        public static T Create <T>(this Dictionary <string, object> props, LookupCache cache)
            where T : MantisEntity, new()
        {
            T result;

            result = new T();

            props.CopyTo(result, cache);

            return(result);
        }
        public static void CopyTo(this Dictionary <string, object> props, object item, LookupCache cache)
        {
            foreach (PropertyInfo property in item.GetType().GetProperties())
            {
                JsonAttribute attribute;

                attribute = property.GetCustomAttribute <JsonAttribute>();

                if (attribute != null)
                {
                    string key;
                    object value;

                    key = attribute.Key;

                    if (props.TryGetValue(key, out value))
                    {
                        Type type;

                        type = property.PropertyType;

                        if (Type.GetTypeCode(type) == TypeCode.Object && cache != null)
                        {
                            // HACk: This is pretty awful

                            if (typeof(NamedMantisEntity).IsAssignableFrom(type))
                            {
                                Dictionary <string, object> childProps;

                                childProps = (Dictionary <string, object>)value;

                                if (type == typeof(User))
                                {
                                    value = cache.User.Get(childProps);
                                }
                                else if (type == typeof(ViewState))
                                {
                                    value = cache.ViewState.Get(childProps);
                                }
                                else if (type == typeof(AccessLevel))
                                {
                                    value = cache.AccessLevel.Get(childProps);
                                }
                                else if (type == typeof(RelationshipType))
                                {
                                    value = cache.RelationshipType.Get(childProps);
                                }
                                else if (key == "project")
                                {
                                    // HACK: Using common "Reference" type so can't tell the different
                                    value = cache.Project.Get(childProps);
                                }
                                else
                                {
                                    throw new NotImplementedException("Property '" + key + "' not supported.");
                                }
                            }
                            else
                            {
                                throw new NotImplementedException("Type '" + type.FullName + "' not supported.");
                            }
                        }

#if NET20 || NET35 || NET40
                        property.SetValue(item, Convert.ChangeType(value, type), null);
#else
                        property.SetValue(item, Convert.ChangeType(value, type));
#endif
                    }
                }
            }
        }
        public static TCollection GetReferences <TCollection, TChild>(this Dictionary <string, object> props, string key, LookupCache <TChild> cache)
            where TCollection : class, ICollection <TChild>, new()
            where TChild : MantisEntity, new()
        {
            TCollection result;
            object      list;

            if (props.TryGetValue(key, out list))
            {
                List <object> childProps;

                result     = new TCollection();
                childProps = (List <object>)list;

                for (int i = 0; i < childProps.Count; i++)
                {
                    TChild item;

                    item = cache.Get((Dictionary <string, object>)childProps[i]);

                    result.Add(item);
                }
            }
            else
            {
                result = null;
            }

            return(result);
        }
        public static T GetReference <T>(this Dictionary <string, object> props, string key, LookupCache <T> cache)
            where T : MantisEntity, new()
        {
            T      result;
            object childProps;

            result = props.TryGetValue(key, out childProps) ? cache.Get((Dictionary <string, object>)childProps) : null;

            return(result);
        }
        public static TCollection GetChildren <TCollection, TChild>(this Dictionary <string, object> props, string key, LookupCache cache)
            where TCollection : class, ICollection <TChild>, new()
            where TChild : class, new()
        {
            TCollection result;
            object      list;

            if (props.TryGetValue(key, out list))
            {
                List <object> childProps;

                result     = new TCollection();
                childProps = (List <object>)list;

                for (int i = 0; i < childProps.Count; i++)
                {
                    TChild item;
                    Dictionary <string, object> values;

                    item = new TChild();

                    values = (Dictionary <string, object>)childProps[i];
                    values.CopyTo(item, cache);

                    result.Add(item);
                }
            }
            else
            {
                result = null;
            }

            return(result);
        }
Beispiel #7
0
 public LookupCache(LookupCache owner)
 {
     _cache = new Dictionary <int, T>();
     _lock  = new object();
     _owner = owner;
 }