2using System.Collections.Generic;
3using System.Runtime.Serialization;
21 #region Private Properties
23 private int? _hashCode;
30 private static readonly string TypeCollectionName;
34 private readonly string _localCollectionName;
35 #endregion Private Properties
37 #region Public Properties
54 [
Obsolete(
"This property has the potentially unexpected side-effect of resolving the " +
55 "reference (an expensive network operation) if it is not already resolved. " +
56 "Use the more explicit GetValue() or Resolve() methods instead.")]
66 #endregion Public Properties
79 TypeCollectionName =
null;
103 if (
href ==
null)
return;
111 "discernible collection name. Expecting something like " +
112 "https://apiurl.net/collection_name/guid",
e);
141 TryResolveSelf(value);
147 private bool TryResolveSelf(
T value)
150 if (value ==
null)
return false;
157 #endregion Constructors
159 #region Static Methods
165 if (TypeCollectionName ==
null && value ==
null)
166 throw new Exception(
"Cannot resolve the reference because the collection for " +
167 "resources of type " +
typeof(
T).NiceTypeName() +
" could not be determined " +
168 "and no explicit reference value was provided.");
170 return String.IsNullOrWhiteSpace(
ref_id) ?
null : API.ServerURL +
171 (value?.collection_name ?? TypeCollectionName) +
"/" +
ref_id;
173 #endregion Static Methods
175 #region Public Methods
230 throw new Exception(
"Cannot resolve the reference because " +
231 "the href property has not been set and the collection for " +
232 $"resources of type {typeof(T).NiceTypeName()} could not be determined.");
268 #endregion Public Methods
270 #region Equality and Object overrides
295 if (!_hashCode.HasValue)
305 return _hashCode.Value;
312 return ref_id ?? (
resolved ?
"new " + _value.GetType().NiceTypeName() :
"(empty)");
314 #endregion Equality and Object overrides
Describes a collection of resources which can be listed.
API methods / requests made available to the user.
static object GetUri(Type deserializeType, Uri href, IEnumerable< Parameter > requestParameters=null, int? timeout=null)
Perform a GET on an arbitrary resource by extracting the path and query parameters from a manually sp...
static object RequestAndParse(Type deserializeType, string resource, Method method, IEnumerable< Parameter > requestParameters=null, int? timeout=null)
Perform a REST request on the server and serializes the response to the desired run-time type.
static string GetCollectionName(Type requestType)
Gets the collection name for the given APIResource type, whether it's instantiable or not.
static string GetCollectionNameFromResourceHref(string href)
Return the collection name implied by the href for a given resource assuming the URL is in the format...
Implements the reference entity interface, with support for resolving references using lazy loading.
Reference(string ref_id, string href)
Creates a new reference that refers to an object of the current type, with the specified id but with ...
override string ToString()
Returns this reference's id, or value type if it has no id.
bool resolved
Indicates whether this reference has been resolved into an object.
void Resolve(IEnumerable< Parameter > requestParameters=null, int? timeout=null, bool updateCache=false)
Resolve this reference by retrieving the referenced object. If this reference is already resolved,...
override bool Equals(object obj)
Determines whether the two reference objects refer to the same object.
Reference(string ref_id)
Creates a new instance of this reference type that refers to an object with the specified id.
static string GuessHref(string ref_id, T value=default(T))
Attempts to guess the URL at which this resource can be resolved based on the resource collection nam...
T GetValue(IEnumerable< Parameter > requestParameters=null, int? timeout=null, bool updateCache=false)
Gets the resource that this reference refers to. If the reference has not previously been resolved,...
Type ResourceType
Returns the runtime type of resource this reference refers to, or the generic type parameter T if un...
override int GetHashCode()
Serves as a hash function for reference objects.
Reference(T value)
Creates a new reference to the specified resource.
Reference(IReference other)
Creates a new reference class that refers to the same object referred to by the other reference class...
string CollectionName
The name of the collection at which this resource resides.
T Value
Returns the object this reference refers to by resolving it with a request to the server.
Utility for resolving types given only the types name (Useful for parsing ambiguous JSON objects).
static Type ResolveBaseTypeByCollectionName(string collection)
Get the base type of object referenced by the supplied server URL.
static Type ResolveBaseTypeByURL(string href)
Get the base type of object referenced by the supplied server URL.
static Type GetGenericCovariantBase(Type T)
Returns the most strongly typed covariant base type which is assignable from the specified type T.
Interface for Base class used by all resources.
Base interface for all reference entities.
string ref_id
The id of the object being referred to.
string reference_type
In cases where a model can reference resources belonging to one of a number of collections,...
string href
The HREF that can be used to retrieve the object being referred to.