Ejemplo n.º 1
0
 public SubResource(IParentableResource parent, IResourceModel parentInstance, String name = null) : base(parent.Context, name: name)
 {
     if (!parent.GetModelType().IsAssignableFrom(parentInstance.GetType()))
     {
         throw new ShopifyConfigurationException("Parent model instance provided to SubResource must be appropriate to the provided Parent Resource.");
     }
     if (parentInstance.Id == null)
     {
         throw new ShopifyConfigurationException("Parent model instance provided for subresource has null id, which would lead to untenable subresource URIs.");
     }
     ParentResource = parent;
     ParentInstance = parentInstance;
 }
Ejemplo n.º 2
0
        private static void RecurseThroughAllHasOneProperties(IShopifyAPIContext shopify, IResourceModel model)
        {
            // change all placeholders (which tell us the ID that was on the _id) into single
            // instance subresources (lives ones that are Get()able)
            var hasOneProperties = from p in model.GetType().GetProperties() where p.PropertyType.IsGenericType && p.PropertyType.GetGenericTypeDefinition() == (typeof(IHasOne <>)) select p;

            foreach (var placeholderPropCandidate in hasOneProperties)
            {
                // naturally, any null or inline ones (HasOneInline<>) are skipped
                var hasOnePlaceholder = placeholderPropCandidate.GetValue(model) as IHasOnePlaceholderUntyped;
                if (hasOnePlaceholder == null)
                {
                    continue;
                }

                // get resource model type from the has_a property
                var subResourceModelType = placeholderPropCandidate.PropertyType.GetGenericArguments()[0];
                var baseSubresourceType  = typeof(SingleInstanceSubResource <>);
                var resourceType         = baseSubresourceType.MakeGenericType(new Type[] { subResourceModelType });

                // I was hoping to avoid using Activator in this project, but no such luck
                var subResourceInstance = Activator.CreateInstance(resourceType, shopify, hasOnePlaceholder.Id);

                placeholderPropCandidate.SetValue(model, subResourceInstance);
            }

            // recurse into and do the same to any inlined hasones on the model.
            foreach (var inlineHasOnePropCandidate in hasOneProperties)
            {
                var inlineHasOne = inlineHasOnePropCandidate.GetValue(model) as IHasOneInlineUntyped;
                if (inlineHasOne == null)
                {
                    continue;
                }

                var inlineModel = inlineHasOne.GetUntypedInlineModel();
                if (inlineModel == null)
                {
                    continue;
                }

                RecurseThroughAllHasOneProperties(shopify, inlineModel);
            }
        }