Skip to content

Commit

Permalink
Merge pull request #364 from json-api-dotnet/chore/docs
Browse files Browse the repository at this point in the history
doc(Identifiable): add xml docs
  • Loading branch information
jaredcnance authored Aug 1, 2018
2 parents 74667eb + 5f4a91d commit 9c54535
Showing 1 changed file with 25 additions and 5 deletions.
30 changes: 25 additions & 5 deletions src/JsonApiDotNetCore/Models/Identifiable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,55 @@
namespace JsonApiDotNetCore.Models
{
public class Identifiable : Identifiable<int>
{}
{ }

public class Identifiable<T> : IIdentifiable<T>
{
/// <summary>
/// The resource identifier
/// </summary>
public virtual T Id { get; set; }

/// <summary>
/// The string representation of the `Id`.
///
/// This is used in serialization and deserialization.
/// The getters should handle the conversion
/// from `typeof(T)` to a string and the setter vice versa.
///
/// To override this behavior, you can either implement the
/// <see cref="IIdentifiable{T}"> interface directly or override
/// `GetStringId` and `GetTypedId` methods.
/// </summary>
[NotMapped]
public string StringId
{
get => GetStringId(Id);
set => Id = GetTypedId(value);
}

/// <summary>
/// Convert the provided resource identifier to a string.
/// </summary>
protected virtual string GetStringId(object value)
{
var type = typeof(T);
var stringValue = value.ToString();

if(type == typeof(Guid))
if (type == typeof(Guid))
{
var guid = Guid.Parse(stringValue);
return guid == Guid.Empty ? string.Empty : stringValue;
}

return stringValue == "0"
? string.Empty
return stringValue == "0"
? string.Empty
: stringValue;
}

/// <summary>
/// Convert a string to a typed resource identifier.
/// </summary>
protected virtual T GetTypedId(string value)
{
var convertedValue = TypeHelper.ConvertType(value, typeof(T));
Expand Down

0 comments on commit 9c54535

Please sign in to comment.