Doing preliminary research on using long id’s with a couple tables…
I’ve come across the following SO posts on the topic.
It would seem the issue would be JavaScript interpreting long ints past a certain value. Is the following still the best way and the recommended way to handle it to work on the JavaScript client?
// https://stackoverflow.com/questions/32695359/servicestack-serialize-long-number-to-string
JsConfig<long>.RawSerializeFn = (long num) =>
{
if (num > 9007199254740991L || num < -9007199254740991L)
return string.Format("\"{0}\"", num);
return num.ToString();
};
Maybe in the case of id’s it should always be a string. No math will be used on the ids.
JsConfig<long>.RawSerializeFn = (long num) =>
{
// changed to single quotes instead of double quotes
return string.Format("'{0}'", num);
};
Or would it be better to create each DTO with a long int id as string and leave the data model as long and do the parsing between the DTO and the data model? I think this would probably be the better option, unless there is something missing.
public class HelloService : Service
{
public object Post(Hello request)
{
if (!request.Name.IsNullOrEmpty())
{
Db.Insert(new HelloModel
{
Id = request.Id.ToInt64(),
Name = request.Name
});
}
return new HelloResponse { Result = $ "Hello, {request.Name}!" };
}
[Route("/hello")]
[Route("/hello/{Name}")]
public class Hello : IReturn<HelloResponse>
{
public string Id { get; set; }
public string Name { get; set; }
}
public class HelloResponse
{
public string Result { get; set; }
}
}
// Data Model
[Alias("hello")]
public class HelloModel
{
[AutoIncrement]
[PrimaryKey]
[ReturnOnInsert]
[Alias("hello_id")]
public long Id { get; set; }
public string Name { get; set; }
}
Note: The space after the “$” (string interpolation) is needed for the SS forum as it seems the markdown converts it to a guid without hyphens when it is next to a double quote in a code section.