/// <summary> /// Initializes a new instance of the <see /// cref="ShopifyAPIAdapterLibrary.RestResource`1"/> class. /// </summary> /// <param name='name'> /// The lowercase resource name, as it would appear in URIs. /// Default is underscorized from the model type. /// </param> // TODO: put name back as an optional public RestResource(IShopifyAPIContext context, string name = null) { Context = context; if (name == null) { Name = ShopifyAPIContext.Underscoreify(typeof(T).Name); } else { Name = name; } }
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); } }
public SingleInstanceSubResource(IShopifyAPIContext context, T model) { Context = context; Set(model); }
public SingleInstanceSubResource(IShopifyAPIContext context, int id) { Context = context; Id = id; }