Andres B
asked on
Using a container class in a restful architecture
I'm trying to implement a restful architecture using .NET Core based on HATEOAS.
For that, I would be using an include parameter with a list in the GET requests so as to include the entities related to the parent.
For example, if I want to get a master (master/details) with the related client I would perform the following GET request:
The will happen with every related entity.
What I'm trying to find out is the best way to implement this in my base resource's class.
The solution I applied is to use a base class using generics:
The object property of InstanceResource<Client> is the actual Client object. If Client is not included in the Include clause of the GET request, the object will be null and it will only show Href and ClassName.
GET http://api.com/master/id=1?include=Client
GET http://api.com/master/id=1
I saw other solutions where all the properties of the Object are at the same level that Href. Something like this:
That's why I thought about my solution, because it let me fill the entire object only if its needed, and if its filled, I would know that the property values are all filled up correctly.
So, is there any drawbacks that I could find using a container class as a solution? I would be glad to here some recommendations or opinions.
For that, I would be using an include parameter with a list in the GET requests so as to include the entities related to the parent.
For example, if I want to get a master (master/details) with the related client I would perform the following GET request:
GET http://api.com/master/id=1?include=Client
If the include clause is not there, the related client in the master will only show the link to get it.The will happen with every related entity.
What I'm trying to find out is the best way to implement this in my base resource's class.
The solution I applied is to use a base class using generics:
public class InstanceResource<T> where T : BaseResource
{
public string Href { get; set; }
public string ClassName { get; set; }
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public T Object { get; set; }
}
Using this, I will have a Master with a InstanceResource related to it.The object property of InstanceResource<Client> is the actual Client object. If Client is not included in the Include clause of the GET request, the object will be null and it will only show Href and ClassName.
GET http://api.com/master/id=1?include=Client
{
"object": {
"client": {
"object": {
"name": "JOHN DOE"
},
"href": "http://api.com/client/1",
"className": "Client"
},
"details": {
"href": "http://api.com/details/masterId=1",
"className": "Collection<Detail>"
}
},
"href": "http://api.com/master/1",
"className": "Master"
}
Not included Client:GET http://api.com/master/id=1
{
"object": {
"client": {
"href": "http://api.com/client/1",
"className": "Client"
},
"details": {
"href": "http://api.com/details/masterId=1",
"className": "Collection<Detail>"
}
},
"href": "http://api.com/master/1",
"className": "Master"
}
As you can see, this approach uses a container class to represent the resource. What I need to know if this solution has any disadvantage.I saw other solutions where all the properties of the Object are at the same level that Href. Something like this:
{
"client": {
"href": "http://api.com/client/1",
"className": "Client",
"name": "JOHN DOE",
},
"details": {
"href": "http://api.com/details/masterId=1",
"className": "Collection<Detail>"
},
"href": "http://api.com/master/1",
"className": "Master"
}
But I'm not convinced with it. What if the actual client name is null or a default value? The client application wouldn't know if the value came that way because if was not filled or because its the actual value.That's why I thought about my solution, because it let me fill the entire object only if its needed, and if its filled, I would know that the property values are all filled up correctly.
So, is there any drawbacks that I could find using a container class as a solution? I would be glad to here some recommendations or opinions.
This question needs an answer!
Become an EE member today
7 DAY FREE TRIALMembers can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
My opinion anyway!
-J