Skip to content

doc(Identifiable): add xml docs #364

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 1, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 25 additions & 5 deletions src/JsonApiDotNetCore/Models/Identifiable.cs
Original file line number Diff line number Diff line change
@@ -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));